购物车模拟

示例如下:

public class Goods {
    int id;
    String name;
    double Price;
    int buyNumber;
}
public static void main(String[] args) {
        Goods [] shopCar=new Goods[100];

        while (true) {
            System.out.println("请您选择如下命令进行操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询购物车商品展示:query");
            System.out.println("修改商品购买数量:update");
            System.out.println("结算商品购买金额:pay");
            Scanner sc=new Scanner(System.in);
            System.out.println("请您输入命令:");
            String command=sc.next();
            switch(command){
                case"add":
                    addGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case"update":
                    updateGoods(shopCar,sc);
                    break;
                case"pay":
                    pay(shopCar);
                    break;
                default:
                    System.out.println("没有该功能!");
            }
        }
    }

调用方法:

 private static void pay(Goods[] shopCar) {
        queryGoods(shopCar);
        double money=0;
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if (g!=null){
                money+=(g.buyNumber*g.Price);
            }else {
                break;
            }
        }

        System.out.println(money);
    }

    private static void updateGoods(Goods[] shopCar,Scanner sc) {
        while (true) {
            System.out.println("请您输入商品id");
            int id=sc.nextInt();
            Goods g=getGoodsbyId(shopCar,id);
            if (g==null) {
                System.out.println("对不起,您没有购买该商品");
            }else {
                System.out.println("请您输入"+g.name+"商品的购买数量:");
                g.buyNumber=sc.nextInt();
                System.out.println("修改完成!");
                queryGoods(shopCar);
                break;
            }
        }
    }

    private static Goods getGoodsbyId(Goods[] shopCar, int id) {
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if (g!=null) {
                if (g.id==id){
                    return g;
                }
            }else {
                return null;
            }
        }
        return null;//找完了100个商品都没有找到id一样的商品
    }

    private static void queryGoods(Goods[] shopCar) {
        System.out.println("========查询购物车信息如下========");
        System.out.println("编号\t\t名称\t\t\t价格\t\t\t购买数量");

        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if (g !=null){
                System.out.println(g.id + "\t\t" + g.name + "\t\t\t" + g.Price + "\t\t\t" + g.buyNumber);
            }else {
                break;
            }
        }
    }

    private static void addGoods(Goods[] shopCar,Scanner sc) {

        System.out.println("请您输入购买商品的编号:");
        int id=sc.nextInt();
        System.out.println("请您输入购买商品的名称:");
        String name=sc.next();
        System.out.println("请你输入购买商品的价格:");
        double Price=sc.nextDouble();
        System.out.println("请您输入购买商品的数量:");
        int buyNumber=sc.nextInt();

       Goods g=new Goods();
       g.id=id;
       g.name=name;
       g.Price=Price;
       g.buyNumber=buyNumber;


        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i]==null){
                shopCar[i]=g;
                break;
            }
        }
        System.out.println("您的商品:"+g.name+"添加购物车成功。");
    }

}

出现过的问题:

        由于在编写"while"的死循环时将第一行的数组定义的代码也包括进去了,导致在执行时每次数组的地址都相当于重置了。后经检查修复原了这个bug,结果如上述所示。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 购物车类 */ public class Cart { //创建一个map对象,用来保存商品,key为商品,value为商品的数量 private Map<Goods, Integer> map = new HashMap<Goods, Integer>(); /** * 添加商品到购物车方法 * @param id 商品编号 * @param quantity 商品数量 */ public void addGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ Integer oQuantity = map.get(goods);//获取商品在购物车中原本的数量 if(oQuantity!=null){ oQuantity += quantity; }else{ oQuantity = quantity; } map.put(goods, oQuantity);//添加商品到map中 System.out.println("添加商品"+goods.getName()+"成功!"); }else{ System.out.println("添加失败!商品编号不存在!"); } } /** * 按指定的编号删除商品 * @param id 商品编号 */ public void delGoods(int id){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.remove(goods);//从map删除商品 System.out.println("删除商品"+goods.getName()+"成功!"); }else{ System.out.println("删除失败!商品编号不存在!"); } } /** * 修改商品数量方法 * @param id 商品编号 * @param quantity 要修改的商品数量 */ public void updateGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.put(goods, quantity);//从map删除商品 }else{ System.out.println("修改失败!商品编号不存在!"); } } /** * 打印购物车商品列表 */ public void show(){ Set<Entry<Goods, Integer>> entrySet = map.entrySet(); System.out.println("编号\t单价\t数量\t名称\t总价"); for(Entry<Goods, Integer> entry:entrySet){ Goods goods = entry.getKey(); Integer quantity = entry.getValue(); System.out.println(goods.getId()+"\t"+goods.getPrice()+"\t"+quantity+"\t"+goods.getName()+"\t"+goods.getPrice()*quantity); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值