javase实现购物车功能

一、主类


package com.tlstudio.shopping.map.price; import java.util.*; /** * @类名 购物系统(主类) * @功能 程序的入口 */ public class ShopingSystem { public static void main(String[] args) { if (login()) {// 登陆 shopping();// 购物 } else {// 登陆失败,退出系统 System.exit(0); } } /** * @function 购物 */ public static void shopping() { Storage storage = new Storage();// 实例化仓库对象 storage.showGoods();// 首次 显示商品信息 ShoppingCart shoppingCart = new ShoppingCart(storage);// 实例化购物车对象,将购物车与仓库对接 while (true) {// 除非选择了退出系统,否则可重复操作里面的功能 shoppingCart.showFunction();// 显示可选功能界面 Scanner scn = new Scanner(System.in); System.out.print("请输入:"); int function = 0; try { function = scn.nextInt();// 获取用户输入的功能(序号) } catch (InputMismatchException e) { System.out.println("您的输入不合法,必须是1-7的数字哦"); continue; } switch (function) { case 1: if (shoppingCart.addGoods()) {// 调用购物车的添加商品到购物车的方法,返回是否成功 System.out.println("商品添加成功"); } else { System.out.println("商品添加失败!"); } break; case 2: if (shoppingCart.alterGoodsNum()) {// 调用购物车的修改购物车中商品数量的方法,返回是否成功 System.out.println("修改商品数量成功"); } else { System.out.println("修改商品数量失败"); } break; case 3: if (shoppingCart.removeGoods()) {// 调用购物车的删除购物车中商品的方法,返回是否成功 System.out.println("删除购物车中的商品成功"); } else { System.out.println("删除购物车中的商品失败"); } break; case 4: shoppingCart.showGoods();// 调用购物车的显示购物车中商品的方法 break; case 5: storage.showGoods();// 调用仓库中显示仓库中商品信息的方法 break; case 6: shoppingCart.accountAll();// 结账 break; case 7: System.out.println("您选择了退出系统,欢迎您下次光临"); System.exit(0); break; default: System.out.println("您的输入有误,没有该功能,请重新选择【序号只有 1-7】"); break; } } } /** * @function 用户登录——系统拥有的判断登陆功能 * * @return 是否登陆成功(true,false) */ public static boolean login() { System.out.println("*****************************************"); System.out.println("*\t\t用户登录\t\t\t*"); System.out.println("*****************************************"); int count = 3;// 有三次输错的机会 String input_userName = "";// 用户输入的用户名 String input_pwd = "";// 用户输入的密码 Scanner scn = new Scanner(System.in); while (true) { System.out.print("请输入用户名:"); input_userName = scn.next();// 获取用户输入的用户名 System.out.print("请输入密 码:"); input_pwd = scn.next();// 获取用户输入的密码 Users users = new Users(input_userName, input_pwd);// 声明一个用户类 if (users.checkedUser()) {// 判断用户名和密码是否存在 System.out.println("登陆成功!欢迎您 " + users.getUserName()); return true; } else { count--;// 输错一次,可输入次数减一 if (count > 0) { System.out.println("用户名或密码错误,请重来(您还有次 " + count + " 机会)"); continue; } else {// 次数用完,冻结账户 System.out.println("三次机会已用完,您的账户已冻结,请与管理员联系!"); return false; } } } } }

  

二、用户类

    package com.tlstudio.shopping.map.price;  
    /** 
     *  
     * @类名 用户类 
     * @功能 用于登陆到系统的凭证,可以检查用户名和密码是否合法 
     * @属性 userName(用户名),pwd(密码) 
     * @方法 checkedUser()用于检查用户名密码是否合法 
     * @author HDL 
     */  
    class Users {  
        private String userName;// 用户名  
        private String pwd;// 密码  
        // 封装属性——属性的get set方法  
        public String getUserName() {  
            return userName;  
        }  
        public void setUserName(String userName) {  
            this.userName = userName;  
        }  
        public String getPwd() {  
            return pwd;  
        }  
        public void setPwd(String pwd) {  
            this.pwd = pwd;  
        }  
        // 构造方法  
        public Users() {  
        }  
        public Users(String userName, String pwd) {  
            this.userName = userName;  
            this.pwd = pwd;  
        }  
        /** 
         * @function 判断用户是否合法 
         * @return 是否存在该用户(true,false) 
         */  
        public boolean checkedUser() {  
            if ("123".equals(userName) && "132".equals(pwd)) {  
                return true;  
            }  
            return false;  
        }  
    }  

三、仓库类

package com.tlstudio.shopping.map.price;  
import java.util.*;  
/** 
 *  
 * @类名 仓库类 
 * @function 用于存放未出售的商品的地方 
 * @属性 goods_map 
 * @方法 checkedGIDNum() alertGoodsNum() getGoods() showGoods() 
 * @author HDL 
 */  
public class Storage {  
    private static Map<String, Goods> goods_map = new HashMap<String, Goods>();// 仓库中的商品列表(用于存放商品)  
    static {  
        addGoods(new Goods("WD8720D", "美特斯邦威T恤", 100, 110));  
        addGoods(new Goods("MK8287V", "太阳雨男士T恤", 100, 200));  
        addGoods(new Goods("TC9927K", "纯棉高领潮男衣", 100, 50));  
        addGoods(new Goods("BMG228S", "蝙蝠侠情侣套装", 100, 80));  
        addGoods(new Goods("JKW832A", "胖子男孩休闲衣", 100, 88));  
        addGoods(new Goods("RH2987V", "胖人服饰显瘦衣", 100, 400));  
        addGoods(new Goods("OSP387L", "紫烟格圆领短袖", 100, 298));  
        addGoods(new Goods("PL2988S", "喇叭袖藏青色衣", 100, 300));  
        addGoods(new Goods("BM2987U", "半袖纯棉休闲衣", 100, 20));  
        addGoods(new Goods("KOG887W", "街舞鬼步舞T恤", 100, 70));  
    }  
    /** 
     * @function 获取商品列表 
     * @return 商品列表 
     */  
    public Map<String, Goods> getGoods_map() {  
        return goods_map;  
    }  
    /** 
     * @function 设置商品列表 
     * @param goods_map 
     */  
    @SuppressWarnings("static-access")  
    public void setGoods_map(Map<String, Goods> goods_map) {  
        this.goods_map = goods_map;  
    }  
    public Storage() {  
    }  
    /** 
     * @function 添加商品——入库的时候使用 
     *  
     * @param goods 
     *            需要添加的商品 
     */  
    public static void addGoods(Goods goods) {  
        goods_map.put(goods.getgId(), goods);  
    }  
    /** 
     * @function 显示仓库中的商品 
     */  
    public void showGoods() {  
        int count = 0;  
        int sum = 0;  
        System.out.println("***************显示货存商品信息****************");  
        System.out  
                .println("-----------------------------------------------------------------");  
        System.out.println("商品编号\t|\t商品名\t\t|\t库存\t|\t价格\t|");  
        System.out  
                .println("-----------------------------------------------------------------");  
        Set<Map.Entry<String, Goods>> goods_sets = goods_map.entrySet();  
        for (Map.Entry<String, Goods> goods_entry : goods_sets) {  
            Goods goods = goods_entry.getValue();  
            System.out.println(goods_entry.getKey() + "\t|\t"  
                    + goods.getgName() + "\t|\t" + goods.getNum() + "\t|\t"  
                    + goods.getPrice() + "\t|");  
            count += goods.getNum();  
            sum += goods.getPrice() * goods.getNum();  
        }  
        System.out  
                .println("-----------------------------------------------------------------");  
        System.out  
                .println("\t\t\t\t\t还有 " + count + " 件商品,总价值 " + sum + "元\t|");  
        System.out  
                .println("-----------------------------------------------------------------");  
    }  
    /** 
     * @function 判断是否存在商品编号和数量 
     * @param input_gId 
     *            用户输入的商品编号 
     * @param input_num 
     *            用户输入的数量 
     * @return 是否添加成功(true,false) 
     */  
    public boolean checkedGIDNum(String input_gId, int input_num) {  
        Goods goods = goods_map.get(input_gId);  
        if (goods != null && input_num <= goods.getNum() && input_num > 0) {  
            return true;  
        } else {  
            System.out.println("找不到商品编号或数量不够");  
        }  
        return false;  
    }  
    /** 
     * @function 获取商品——出库的时候使用 
     * @param input_gId 
     *            用户输入的商品编号 
     * @param input_num 
     *            用户输入的商品数量 
     * @return 商品对象 
     */  
    public Goods getGoods(String input_gId, int input_num) {  
        Goods goods = goods_map.get(input_gId);  
        if (goods != null && goods.getNum() >= input_num && input_num > 0) {  
            goods.setNum(goods.getNum() - input_num);  
            return new Goods(input_gId, goods.getgName(), input_num,  
                    goods.getPrice());  
        }  
        return null;  
    }  
}

四、购物车类

package com.tlstudio.shopping.map.price;  
import java.util.*;  
/** 
 *  
 * @类名 购物车类 
 * @功能 用户存放用户购买的商品的“地方”,可以增删查改商品的功能 
 * @属性 goods_map,storage,input_gId,input_num 
 * @方法 alterGoodsNum() addGoods() removeGoods() checkedGIDNum() showGoods() 
 *     showFunction() 
 * @author HDL 
 */  
public class ShoppingCart {  
    private Map<String, Goods> goods_map;// 购物车中商品列表对象  
    private Storage storage;// 对接仓库对象  
    private String input_gId = "";// 声明一个用户输入的商品编号  
    private int input_num = 0;// 声明一个用户输入的商品数量  
    public ShoppingCart() {// 初始化商品列表对象和键盘录入对象  
        goods_map = new HashMap<String, Goods>();  
    }  
    public ShoppingCart(Storage storage) {  
        this();// 调用无参构造方法  
        this.storage = storage;// 关联仓库对象  
    }  
    /** 
     * @function 显示仓库中的商品 
     */  
    public void showGoods() {  
        if (goods_map.size() != 0) {  
            int count = 0;  
            int sum = 0;  
            System.out.println("***************查询购物车中的商品***************");  
            System.out  
                    .println("-------------------------------------------------------------------------");  
            System.out.println("商品编号\t|\t商品名\t\t|\t数量\t|\t价格\t\t|");  
            System.out  
                    .println("-------------------------------------------------------------------------");  
            Set<Map.Entry<String, Goods>> goods_set = goods_map.entrySet();  
            for (Map.Entry<String, Goods> goods_entry : goods_set) {  
                Goods goods = goods_entry.getValue();  
                System.out.println(goods_entry.getKey() + "\t|\t"  
                        + goods.getgName() + "\t|\t" + goods.getNum() + "\t|"  
                        + goods.getNum() + " * " + goods.getPrice() + " = "  
                        + goods.getNum() * goods.getPrice() + "(元)\t|");  
                count += goods.getNum();// 计件  
                sum += goods.getNum() * goods.getPrice();// 计价  
            }  
            System.out  
                    .println("-------------------------------------------------------------------------");  
            System.out.println("\t\t\t\t\t\t有 " + count + " 件商品,总价值 " + sum  
                    + " 元\t|");  
            System.out  
                    .println("-------------------------------------------------------------------------");  
        } else {  
            System.out.println("购物车中还没有任何商品,请先添加");  
        }  
    }  
    /** 
     * @function 添加商品到购物车中 
     *  
     * @return 是否添加成功(true,false) 
     */  
    public boolean addGoods() {  
        System.out.println("***************添加商品到购物车中***************");  
        System.out.print("要添加商品的编号:");  
        Scanner scn = new Scanner(System.in);  
        input_gId = scn.next();  
        // 用户输入有误就叫用户重新输入  
        System.out.print("要添加商品的数量:");  
        try {  
            input_num = scn.nextInt();  
        } catch (InputMismatchException e) {  
            System.out.println("您输入的商品数量不合法,必须是数字1-100哦");  
            return false;  
        }  
        if (storage.checkedGIDNum(input_gId, input_num)) {// 检查用户输入的商品编号和数量的合法性  
            Goods goods = goods_map.get(input_gId);  
            // 判断是否存在该商品  
            if (goods != null) {  
                // 存在 就将购物车中相应的商品数量增加input_num,仓库中的数量也相应的减少  
                goods.setNum(goods.getNum() + input_num);// 增加购物车中的商品数量  
                Goods goods_storage = storage.getGoods_map().get(input_gId);// 获取仓库中的商品对象  
                goods_storage.setNum(goods_storage.getNum() - input_num);// 将仓库中的商品数量减少input_num  
            } else {  
                goods_map  
                        .put(input_gId, storage.getGoods(input_gId, input_num));// 没有添加过——直接添加  
            }  
            return true;  
        }  
        return false;  
    }  
    /** 
     * @function 显示功能界面 
     */  
    public void showFunction() {  
        System.out.println("*****************************************");  
        System.out.println("*  请选择操作(输入序号即可)!\t\t\t*");  
        System.out.println("*\t\t\t\t\t*");  
        System.out.println("*  1、添加商品到购物车\t\t2、修改购物车中的商品数量\t*");  
        System.out.println("*  3、从购物车中删除商品  \t4、查询购物车中的商品\t*");  
        System.out.println("*  5、显示货存商品信息\t\t6、结账\t\t*");  
        System.out.println("*  7、退出系统\t\t\t\t*");  
        System.out.println("*****************************************");  
    }  
    /** 
     * @function 从购物车中删除商品 
     *  
     * @return 是否删除成功(true,false) 
     */  
    public boolean removeGoods() {  
        if (goods_map.size() != 0) {// 必须要有商品存在才能继续一下面的操作  
            System.out.println("***************从购物车中删除商品 ***************");  
            System.out.print("要删除商品的编号:");  
            Scanner scn = new Scanner(System.in);  
            input_gId = scn.next();  
            Goods goods = goods_map.get(input_gId);  
            if (goods != null) {// 商品存在才能删除  
                goods_map.remove(input_gId);  
                storage.getGoods_map().get(input_gId).setNum(100);// 恢复仓库中的商品数量  
                return true;  
            } else {  
                System.out.println("没有编号为: " + input_gId + " 的商品,请检查您的输入!");  
            }  
        } else {  
            System.out.println("购物车中还没有任何的商品,不能删除,请先添加");  
        }  
        return false;  
    }  
    /** 
     * @function 修改购物车中的商品数量 
     *  
     * @return 是否修改成功(true,false) 
     */  
    public boolean alterGoodsNum() {  
        if (goods_map.size() != 0) {// 必须要购物车中有商品的时候才能修改  
            System.out.println("**************修改购物车中的商品数量**************");  
            System.out.print("要修改商品的编号:");  
            Scanner scn = new Scanner(System.in);// 必须要定义为局部变量,否则有异常抛出的时候,下一次输入就会出错  
            input_gId = scn.next();  
            System.out.print("将商品数量修改为:");  
            try {  
                input_num = scn.nextInt();  
            } catch (InputMismatchException e) {  
                System.out.println("您输入的商品数量不合法,必须是数字1-100哦");  
                return false;  
            }  
            Goods goods = goods_map.get(input_gId);// 获取用户输入的商品  
            Goods goods_storage = storage.getGoods_map().get(input_gId);// 获取用户输入的对应仓库的商品  
            if (goods != null && input_num <= 100 && input_num > 0) {// 商品要存在,且输入的数量必须满足一定的条件  
                goods.setNum(input_num);// 修改购物车中商品中的数量  
                goods_storage.setNum(100 - input_num);// 修改仓库中商品的数量  
            } else {  
                System.out.println("您输入的商品编号不存在,或商品数量不够哦!");  
                return false;  
            }  
        } else {  
            System.out.println("购物车中还没有任何商品,不能修改,请先添加");  
            return false;  
        }  
        return true;  
    }  
    /** 
     * @function 结账 
     */  
    public void accountAll() {  
        if (goods_map.size() != 0) {  
            System.out.println("***************结  账***************");  
            System.out.println("*\t\t\t\t*");  
            int sum = 0;// 统计总价  
            int count = 0;// 统计商品数量  
            Set<Map.Entry<String, Goods>> goods_set = goods_map.entrySet();// 变为Set集合  
            for (Map.Entry<String, Goods> goods_entry : goods_set) {  
                Goods goods = goods_entry.getValue();  
                sum += (goods.getNum()) * (goods.getPrice());  
                count += goods.getNum();  
            }  
            System.out.println("*\t有 " + count + " 件商品,共花费 " + sum + " 元\t*");  
            System.out.println("*\t\t\t\t*");  
            System.out.println("*********************************\n");  
        } else {  
            System.out.println("您的购物车中还没有任何商品,还不能结账哦");  
        }  
    }  
}

五、商品类

package com.tlstudio.shopping.map.price;  
/** 
 *  
 * @类名 商品类 
 * @功能 用于设定商品的信息 
 * @属性 gId gName num price 
 * @author HDL 
 */  
public class Goods {  
    private String gId;// 商品编号  
    private String gName;// 商品名  
    private int num;// 商品的数量  
    private int price;//商品价格  
    // 封装属性——属性的get set方法  
    public int getNum() {  
        return num;  
    }  
    public void setNum(int num) {  
        this.num = num;  
    }  
    public int getPrice() {  
        return price;  
    }  
    public void setPrice(int price) {  
        this.price = price;  
    }  
    public String getgId() {  
        return gId;  
    }  
    public void setgId(String gId) {  
        this.gId = gId;  
    }  
    public String getgName() {  
        return gName;  
    }  
    public void setgName(String gName) {  
        this.gName = gName;  
    }  
    // 构造方法  
    public Goods() {  
    }  
    public Goods(String gId, String gName, int num, int price) {  
        this.price = price;  
        this.gId = gId;  
        this.gName = gName;  
        this.num = num;  
    }  
}

 

posted @ 2017-01-09 15:04  烟雨观春柳  阅读(175)  评论(0)    收藏  举报