Java模拟购物车

构建商品类:

package ShopCar;

public class Goods {
    private int id;
    private String name;//名字
    private double price;//价格
    private int restnum;//剩余数量



    public Goods clone()
    {
        Goods newGood=new Goods();
        newGood.setId(Goods.this.getId());
        newGood.setName(Goods.this.getName());
        newGood.setPrice(Goods.this.getPrice());
        newGood.setRestnum(Goods.this.getRestnum());
        return newGood;

    }

    public Goods(){}
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getRestnum() {
        return restnum;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setRestnum(int restnum) {
        this.restnum = restnum;
    }


    public Goods(int id,String name,double price,int restnum) {
        this.id=id;
        this.name=name;
        this.price=price;
        this.restnum=restnum;
    }


}

购物车实现:

package ShopCar;


import java.util.Scanner;

public class ShopCar {

    public static void main(String[] args) {
        Goods[] ShopCar=new Goods[50];
        Goods[] allGoods=new Goods[10];
        creatGoods(allGoods);
        while (true)
        {
            System.out.println("商品展示:");
            showGoods(allGoods);
            System.out.println("请选择操作:");
            System.out.println("添加商品:add");
            System.out.println("删除商品:delete");
            System.out.println("查询购物车商品:show");
            System.out.println("修改商品数量:update");
            System.out.println("结算金额:pay");
            System.out.println("退出:exit");
            Scanner sc=new Scanner(System.in);
            System.out.println("输入命令:");
            String command=sc.next();
            switch (command)
            {
                case"add"->add(ShopCar,allGoods);
                case"delete"->delete(ShopCar,allGoods);
                case"show"->show(ShopCar);
                case"update"->update(ShopCar,allGoods);
                case"pay"->pay(ShopCar);
                case"exit"->System.exit(0);
                default -> System.out.println("无效命令");

            }
        }


    }



    private static void pay(Goods[] shopCar) {

        double pay=0;
        for (int i = 0; i < shopCar.length; i++)
        {
            if(shopCar[i]==null)
                continue;
            else
            {
                pay+=shopCar[i].getPrice()*shopCar[i].getRestnum();
            }

        }
        System.out.println("共需支付"+pay+"元");
        System.out.println("操作结束");



    }

    private static void update(Goods[] shopCar,Goods[] allGoods) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择商品序号");
        int id=sc.nextInt();
        if(id<0||id>= allGoods.length)
        {
            System.out.println("无此商品!");
            return;
        }
        for (int i=0;i< shopCar.length;i++)
        {
            if(shopCar[i]==null)
                continue;
            if(shopCar[i].getId()==id)
            {
                allGoods[id].setRestnum(allGoods[id].getRestnum()+shopCar[i].getRestnum());
                System.out.println("请重新输入数量:");
                int num=sc.nextInt();
                if(num<=0||num>allGoods[id].getRestnum())
                {
                    System.out.println("数量选择错误!");
                    return;
                }
                shopCar[i].setRestnum(num);
                allGoods[id].setRestnum(allGoods[id].getRestnum()-num);
                System.out.println("操作结束");
                return;
            }

        }
        System.out.println("购物车中无此商品!");

    }

    private static void show(Goods[] shopCar) {
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]==null)
                continue;
            System.out.println(shopCar[i].getId()+".名称:"+shopCar[i].getName()+",价格:"+shopCar[i].getPrice()+",数量:"+shopCar[i].getRestnum());
        }
        System.out.println("操作结束");
    }

    private static void delete(Goods[] shopCar,Goods[] allGoods) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择商品序号");
        int id=sc.nextInt();
        if(id<0||id>= allGoods.length)
        {
            System.out.println("无此商品!");
            return;
        }
        for (int i=0;i< shopCar.length;i++)
        {
            if(shopCar[i]==null)
                continue;
            if(shopCar[i].getId()==id)
            {
                allGoods[id].setRestnum(shopCar[i].getRestnum()+allGoods[id].getRestnum());
                shopCar[i]=null;
                System.out.println("操作结束");
                return;
            }

        }
        System.out.println("购物车中无此商品!");





    }

    private static void add(Goods[] shopCar, Goods[] allGoods) {

        Scanner sc=new Scanner(System.in);
        System.out.println("请选择商品序号:");
        int id=sc.nextInt();
        if(id<0||id>= allGoods.length)
        {
            System.out.println("无此商品!");
            return;
        }

        //查询购物车中是否已经存在该商品
        for(int i=0;i< shopCar.length;i++)
        {
            if(shopCar[i]==null)
                continue;
            if(shopCar[i].getId()==id)
            {
                System.out.println("该商品已经放入购物车,请修改数量");
                return;
            }
        }
        //查找空位并添加
        for (int pos = 0; pos < shopCar.length; pos++)
        {
            if(shopCar[pos]==null)
            {
                shopCar[pos]=allGoods[id].clone();//需要克隆
                System.out.println("请输入购买数量:");
                int num=sc.nextInt();
                if(num<=0||num>allGoods[id].getRestnum())
                {
                    System.out.println("数量选择错误!");
                    return;
                }
                shopCar[pos].setRestnum(num);
                allGoods[id].setRestnum(allGoods[id].getRestnum()-num);
                System.out.println("操作结束");
                return;
            }

        }
        System.out.println("购物车已满!");

    }

    private static void creatGoods(Goods[] allGoods) {
        allGoods[0]=new Goods(0, "笔记本", 5.00, 6);
        allGoods[1]=new Goods(1,"水杯",7.30,9);
        allGoods[2] =new Goods(2,"零食",6.00,4);
        allGoods[3] =new Goods(3,"橘子",5.00,15);
        allGoods[4]=new Goods(4,"鞋",32.50,100);
        allGoods[5]=new Goods(5,"羽绒服",65.80,28);
        allGoods[6]=new Goods(6,"vivo手机",1580.00,1020);
        allGoods[7]=new Goods(7,"电饭煲",388.00,50);
        allGoods[8]=new Goods(8,"床上用品",98.00,540);
        allGoods[9] =new Goods(9,"伞",9.00,2346);
    }

    private static void showGoods(Goods[] allGoods) {
        for(int i=0;i<10;i++)
        {
            System.out.println(allGoods[i].getId()+".名称:"+allGoods[i].getName()+",价格:"+allGoods[i].getPrice()+",库存:"+allGoods[i].getRestnum());
        }

    }




}

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); } } }
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、付费专栏及课程。

余额充值