实体类1(商品):
package mall.model;
public class goods {
 private int shoppingID; // 商品编号
    private String shoppingName;// 商品名
    private int price; // 商品价格    public goods(int shoppingID, String shoppingName, int price) {
        this.shoppingID = shoppingID;
        this.shoppingName = shoppingName;
        this.price = price;
    }    public goods() {
        super();
    }    public int getShoppingID() {
        return shoppingID;
    }    public void setShoppingID(int shoppingID) {
        this.shoppingID = shoppingID;
    }    public String getShoppingName() {
        return shoppingName;
    }    public void setShoppingName(String shoppingName) {
        this.shoppingName = shoppingName;
    }    public int getPrice() {
        return price;
    }    public void setPrice(int price) {
        this.price = price;
    }}
实体类2(购物车)
package mall.model;
public class shoppingCart {
    private int shoppingID; // 商品编号
    private String shoppingName; // 商品名
    private int price; // 商品价格    public shoppingCart() {
        super();
    }    public shoppingCart(int shoppingID, String shoppingName, int price) {
        super();
        this.shoppingID = shoppingID;
        this.shoppingName = shoppingName;
        this.price = price;
    }    public int getShoppingID() {
        return shoppingID;
    }    public void setShoppingID(int shoppingID) {
        this.shoppingID = shoppingID;
    }    public String getShoppingName() {
        return shoppingName;
    }    public void setShoppingName(String shoppingName) {
        this.shoppingName = shoppingName;
    }    public int getPrice() {
        return price;
    }    public void setPrice(int price) {
        this.price = price;
    }}
实体类3(用户)
package mall.model;
/**
 * @author shixi1809 用户类
 */
public class user {
    private String userName; // 用户名
    private String passWord; // 用户密码
    private int type; // 用户类型,1位管理员,2为会员    public user(String userName, String passWord, int type) {
        super();
        this.userName = userName;
        this.passWord = passWord;
        this.type = type;
    }    public user() {
        super();
    }    public String getUserName() {
        return userName;
    }    public void setUserName(String userName) {
        this.userName = userName;
    }    public String getPassWord() {
        return passWord;
    }    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }    public int getType() {
        return type;
    }    public void setType(int type) {
        this.type = type;
    }    public void show1() {
        String ttt = null;
        if (getType() == 1) {
            ttt = "管理员";
        } else if (getType() == 2) {
            ttt = "会员";
        }
        System.out.println("用户名:" + getUserName() + ",密码:" + getPassWord()
                + ",用户类型:" + ttt);
    }}
 
测试类
package mall.controller;
import mall.function.service;
public class test {
    public static void main(String[] args) {
        System.out.println("请登录:");
        service service = new service();
        service.login();
    }
}功能实现
package mall.function;
import java.util.Scanner;
import mall.model.goods;
import mall.model.shoppingCart;
import mall.model.user;public class service {
    private static user user[] = new user[5];
    private static goods good[] = new goods[5];
    private static shoppingCart cart[] = new shoppingCart[5];
    private static Integer num; // 登录的用户id
    static {
        for (int i = 0; i < good.length; i++) {
            good[i] = new goods();
        }
        for (int i = 0; i < good.length; i++) {
            cart[i] = new shoppingCart();
        }
    }    // 登录
    @SuppressWarnings("resource")
    public void login() {
        // 给用户添加两个值,1为管理员,2位用户
        user[0] = new user("admin", "admin", 1);
        user[1] = new user("zhangsan", "zhangsan", 2);
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String str1 = scanner1.next();
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("请输入密码:");
        String str2 = scanner2.next();        for (int i = 0; i < user.length; i++) {
            if (user[i] != null) {
                if (str1.equals(user[i].getUserName())
                        && str2.equals(user[i].getPassWord())) {
                    if (user[i].getType() == 1) {
                        System.out.println("欢迎进入管理员界面!!");
                        while (true) {
                            System.out
                                    .println("1.增加商品\t 2.删除商品\t 3.修改商品\t 4.商品显示\t 5.切换用户\t 0.退出系统");
                            Scanner scanner = new Scanner(System.in);
                            System.out.println("请选择功能:");
                            int sel = scanner.nextInt();
                            switch (sel) {
                            case 1:
                                add();
                                break;
                            case 2:
                                Scanner scan1 = new Scanner(System.in);
                                System.out.println("请输入要删除的商品编号:");
                                int sttr1 = scan1.nextInt();
                                System.out.println("是否确定要删除该商品?");
                                Scanner scan2 = new Scanner(System.in);
                                System.out
                                        .println("请输入商品名(确认请输入'y',取消请输入'n'):");
                                String sttr2 = scan2.next();
                                if (sttr2.equals("y")) {
                                    remove(sttr1);
                                } else if (sttr2.equals("n")) {
                                    break;
                                } else {
                                    System.out.println("输入有误!");
                                }
                                break;
                            case 3:
                                Scanner scan3 = new Scanner(System.in);
                                System.out.println("请输入商品编号:");
                                int strr = scan3.nextInt();
                                modify(strr);
                                break;
                            case 4:
                                show();
                                break;
                            case 5:
                                login();
                                break;
                            case 0:
                                System.exit(0);
                                break;
                            default:
                                System.out.println("输入有误!");
                                break;
                            }
                        }
                    } else {
                        System.out.println("欢迎进入用户界面!!");
                        num = i; // 登录的用户id
                        System.out.println(num);
                        while (true) {
                            System.out
                                    .println("1.购买商品\t 2.商品搜索\t 3.商品展示\t 4.切换用户\t 0.退出系统");
                            Scanner scanner = new Scanner(System.in);
                            System.out.println("请选择功能:");
                            int sel = scanner.nextInt();
                            switch (sel) {
                            case 1:
                                Scanner scannerBuy = new Scanner(System.in);
                                System.out.println("请输入商品编号:");
                                int buyNo = scannerBuy.nextInt();
                                buy(buyNo);
                                break;
                            case 2:
                                Scanner sele = new Scanner(System.in);
                                System.out.println("请输入商品名:");
                                String selName = sele.next();
                                select(selName);
                                break;
                            case 3:
                                show();
                                break;
                            case 4:
                                login();
                                break;
                            case 0:
                                System.exit(0);
                                break;
                            default:
                                System.out.println("输入有误!");
                                break;
                            }
                        }
                    }
                }
            }
        }
        System.out.println("用户名与密码不符,请重新输入!");
    }    // 加入购物车
    @SuppressWarnings("resource")
    public void cartAdd(int no) {
        for (int i = 0; i < cart.length; i++) {
            if (cart[i].getShoppingID() == 0) {
                cart[i] = new shoppingCart(good[no].getShoppingID(),
                        good[no].getShoppingName(), good[no].getPrice());
                System.out.println("购物车添加成功!");
                System.out.println("请选择继续购物或进入购物车:1.继续购物\t 2.进入购物车 ");
                Scanner scanner1 = new Scanner(System.in);
                System.out.println("请选择:");
                int str1 = scanner1.nextInt();
                switch (str1) {
                case 1:
                    show();
                    break;
                case 2:
                    cartShow();
                    break;
                default:
                    System.out.println("输入有误!!");
                    break;
                }
                break;
            }
        }
    }    // 搜索商品
    @SuppressWarnings("resource")
    public void select(String name) {
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingName() != null
                    && good[i].getShoppingName() != "") {
                if (good[i].getShoppingName().indexOf(name) != -1) {
                    for (int j = 0; j < good.length; j++) {
                        if (good[i].getShoppingName().equals(
                                good[j].getShoppingName())) {
                            System.out.println("商品编号:"
                                    + good[i].getShoppingID() + ",商品名:"
                                    + good[i].getShoppingName() + ",价格:"
                                    + good[i].getPrice());
                            System.out
                                    .println("请选择购买商品或加入购物车:1.购买该商品\t 2.加入购物车\t 3.继续购物\t 4.进入购物车 ");
                            Scanner scanner1 = new Scanner(System.in);
                            System.out.println("请选择:");
                            int str1 = scanner1.nextInt();
                            switch (str1) {
                            case 1:
                                buy(j);
                                break;
                            case 2:
                                cartAdd(j);
                                break;
                            case 3:
                                show();
                                break;
                            case 4:
                                cartShow();
                                break;
                            default:
                                System.out.println("输入有误!!");
                                break;
                            }
                        }
                    }
                }
            }
        }
    }    // 购买商品
    @SuppressWarnings("resource")
    public void buy(int no) {
        for (int i = 0; i < good.length; i++) {
            if (no == good[i].getShoppingID()) {
                System.out.println("商品编号:" + good[i].getShoppingID() + ",商品名:"
                        + good[i].getShoppingName() + ",价格:"
                        + good[i].getPrice());
                System.out.println("是否确认购买(请回复y/n)?");
                Scanner scan2 = new Scanner(System.in);
                String sttr2 = scan2.next();
                if (sttr2.equals("y")) {
                    System.out.println("商品购买成功!");
                } else if (sttr2.equals("n")) {
                    System.out.println("商品购买失败!");
                } else {
                    System.out.println("输入有误!");
                }
            }
        }
    }    // 添加商品
    @SuppressWarnings("resource")
    public void add() {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入商品编号:");
        int str1 = scanner1.nextInt();
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("请输入商品名:");
        String str2 = scanner2.next();
        Scanner scanner3 = new Scanner(System.in);
        System.out.println("请输入商品价格:");
        int str3 = scanner3.nextInt();
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingID() != 0) {
                if (good[i].getShoppingID() == str1) {
                    System.out.println("该商品已存在!!!");
                    return;
                }
            }
        }
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingID() == 0) {
                good[i] = new goods(str1, str2, str3);
                System.out.println("商品添加成功!");
                break;
            }
        }
    }    // 移除商品
    public void remove(int no) {
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingID() != 0) {
                if (no == good[i].getShoppingID()) {
                    good[i] = new goods();
                    System.out.println("商品已删除!");
                } else {
                    System.out.println("不存在该商品!");
                }
            }
        }
    }    // 商品修改
    @SuppressWarnings("resource")
    public void modify(int no) {
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingID() != 0) {
                if (no == good[i].getShoppingID()) {
                    Scanner scanner2 = new Scanner(System.in);
                    System.out.println("请输入商品名:");
                    String str2 = scanner2.next();
                    Scanner scanner3 = new Scanner(System.in);
                    System.out.println("请输入商品价格:");
                    int str3 = scanner3.nextInt();
                    good[i] = new goods(no, str2, str3);
                    System.out.println("商品已修改。。。");
                    return;
                }
            }
        }
        System.out.println("商品未找到。。。");
    }    // 显示商品
    public void show() {
        for (int i = 0; i < good.length; i++) {
            if (good[i].getShoppingID() != 0) {
                System.out.println("商品编号:" + good[i].getShoppingID() + ",商品名:"
                        + good[i].getShoppingName() + ",价格:"
                        + good[i].getPrice());
            }
        }
    }    // 进入购物车
    public void cartShow() {
        System.out.println(user[num].getUserName() + "的购物车:");
        for (int i = 0; i < cart.length; i++) {
            if (cart[i].getShoppingID() != 0) {
                System.out.println("商品编号:" + cart[i].getShoppingID() + ",商品名:"
                        + cart[i].getShoppingName() + ",价格:"
                        + cart[i].getPrice());
            }
        }
    }}