模拟购物车系统

模拟购物车系统

/*    数组容器 模拟购物车
    功能:
        添加商品
            把商品对象添加到容器中
        查看商品
            把购物车中的商品展示给用户
        修改商品
            把商品的购买数量取出修改
        删除商品
            把商品从购物车中删除
        结算==>生成订单
            想用户展示商品名字 数量 一类商品总价 和订单总价

 */

构造商品

/*  private int id;  //商品的id
    private String name;//商品名字
    private double price;//商品的价格
    private  int num;//商品的购买数量

 */

public class Goods {
    private int id;
    private String name;
    private double price;
    private int num;

    public Goods() {
    }

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

开始界面

 public static void main(String[] args) {
        //定义商品容量为20;
        Goods[] goods = new Goods[20];
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("请输入你要进行的操作:");
            System.out.println("1、添加商品:");
            System.out.println("2、查看商品:");
            System.out.println("3、修改商品:");
            System.out.println("4、删除商品:");
            System.out.println("5、结算商品:");
            System.out.println("按其他按键退出操作:");
            int select = sc.nextInt();
            switch (select) {
                case 1:
                    //System.out.println("1、添加商品:");
                    addGoods(goods);
                    break;
                case 2:
                    //System.out.println("2、查看商品:");
                    checkGoods(goods);
                    break;
                case 3:
                    //System.out.println("3、修改商品:");
                    //把商品的购买数量取出修改
                    reviseGoods(goods);
                    break;
                case 4:
                   // System.out.println("4、删除商品:");
                    deleteGoods(goods);
                    break;
                case 5:
                    //System.out.println("5、结算商品:");
                    checkGoods(goods);
                    accountGoods(goods);

                    break;
                default:
                    flag = false;
                    System.out.println("退出购物车");
                   break;


            }
        }
    }

添加商品

//添加商品
    public static void addGoods(Goods[] goods){
        Goods good = new Goods();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品的名称:");
        String name = sc.nextLine();
        System.out.println("请输入商品ID:");
        int id = sc.nextInt();
        System.out.println("请输入商品的价格:");
        double price = sc.nextDouble();
        System.out.println("请输入商品的数量:");
        int num = sc.nextInt();
        good.setId(id);
        good.setName(name);
        good.setPrice(price);
        good.setNum(num);

        boolean flag = true;
        for (int i = 0; i < goods.length; i++) {
            //如果购物车已经有商品了,不能把原来的商品挤掉,
            if(goods[i] == null){
                goods[i] = good;
                flag = false;
                break;
            }
        }
        if(flag){
            System.out.println("购物车已经满了;");
        }
        System.out.println(Arrays.toString(goods));

    }

判断购物车是否为空

  public static boolean goodsIsNull(Goods[] goods){
        boolean flag = true;
        for (int i = 0; i < goods.length; i++) {
            if(goods[i] != null){
                flag = false;
            }
        }
//        if(flag){
//            System.out.println("购物车是空的,请先添加商品");
//        }
        return flag;
    }

查看商品

    //查看商品
    public static void checkGoods(Goods[] goods) {

        if(goodsIsNull(goods)){
            System.out.println("购物车是空的,请先添加商品");
            return;
        }
        System.out.println("id"+"\t\t\t"+"商品名"+"\t\t\t"+"价格"+"\t\t\t"+"数量"+"\t\t\t"+"总计");
        for (int i = 0; i < goods.length; i++) {
            if(goods[i] != null){
                System.out.print(goods[i].getId()+"\t\t\t");
                System.out.print(goods[i].getName()+"\t\t\t");
                System.out.print(goods[i].getPrice()+"\t\t\t");
                System.out.print(goods[i].getNum()+"\t\t\t");
                System.out.println(goods[i].getNum()*goods[i].getPrice());

            }
        }
    }

修改商品

//修改商品
    private static void reviseGoods(Goods[] goods) {
        //根据商品的ID来查找要修改的商品
        if(goodsIsNull(goods)){
            System.out.println("购物车没有商品");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("你要修改商品的ID:");
        int id = scanner.nextInt();
        boolean flag = true;
        for (int i = 0; i < goods.length; i++) {
            Goods gs = goods[i];
            if(gs != null){
                if(id == gs.getId()){
                    System.out.println("新的数量:");
                    int num = scanner.nextInt();
                    flag = false;
                    gs.setNum(num);
                    System.out.println("id:" + id + "的商品,从购物车中更新了购买数量!");
                    break;
                }
            }
        }
        if(flag){
            System.out.println("商品不存在,");
            return;
        }
    }

删除商品

//删除商品
    //根据商品ID来进行删除
    private static void deleteGoods(Goods[] goods) {
        if(goodsIsNull(goods)){
            System.out.println("购物车没有商品");
            return;
        }
        System.out.println("输入你要删除商品的ID:");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        boolean flag = true;

        for (int i = 0; i < goods.length; i++) {
            Goods gs = goods[i];
            // 我知道为什么不能使用goods[i].getId()
            //如果商品没有满,后面的商品都为null ,不能对null调用方法
           // int goodId = goods[i].getId();

            if(gs != null){
                if(id == gs.getId()){
                    flag = false;
                    goods[i] = null;
                    System.out.println("商品为"+id+"已从购物车移除");
                    break;
                }
            }

        }if(flag){
            System.out.println("你要删除商品不存在");
        }

    }

清空购物车(结算)

   // 把购物车清空
    private static void accountGoods(Goods[] goods) {
        if(!goodsIsNull(goods)){

            double price = 0;
            for (int i = 0; i < goods.length; i++) {
                Goods gs = goods[i];
                if(gs != null){
                    price += gs.getPrice()*gs.getNum();
                    //结算完就把购物车清空
                    goods[i] = null;
                }
            }
            System.out.println("总共花了"+ price+"元");
            return ;
        }
       return ;
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的 Java 模拟购物车系统的实现。 首先,我们需要定义一个商品类 `Product`,它应该包含名称、价格和库存等属性: ```java public class Product { private String name; private double price; private int stock; public Product(String name, double price, int stock) { this.name = name; this.price = price; this.stock = stock; } // getter 和 setter 方法省略 } ``` 然后,我们需要定义一个购物车类 `Cart`,它应该包含商品列表、总价格等属性,以及添加商品、移除商品、计算总价等方法: ```java import java.util.ArrayList; import java.util.List; public class Cart { private List<Product> productList; private double totalPrice; public Cart() { this.productList = new ArrayList<>(); this.totalPrice = 0; } public void addProduct(Product product, int quantity) { if (product.getStock() < quantity) { System.out.println("库存不足!"); return; } for (Product p : productList) { if (p.getName().equals(product.getName())) { p.setStock(p.getStock() - quantity); totalPrice += product.getPrice() * quantity; return; } } Product newProduct = new Product(product.getName(), product.getPrice(), product.getStock() - quantity); productList.add(newProduct); totalPrice += product.getPrice() * quantity; } public void removeProduct(String productName, int quantity) { for (Product p : productList) { if (p.getName().equals(productName)) { if (p.getStock() + quantity > 0) { p.setStock(p.getStock() + quantity); totalPrice -= p.getPrice() * quantity; } else { System.out.println("商品数量不能小于0!"); } return; } } System.out.println("购物车中不存在该商品!"); } public double getTotalPrice() { return totalPrice; } } ``` 最后,我们可以在 `Main` 类中测试购物车系统的功能: ```java public class Main { public static void main(String[] args) { Product p1 = new Product("iPhone 11", 5999, 10); Product p2 = new Product("iPad Pro", 7999, 5); Cart cart = new Cart(); cart.addProduct(p1, 2); cart.addProduct(p2, 1); System.out.println("总价:" + cart.getTotalPrice()); cart.removeProduct("iPhone 11", 1); System.out.println("总价:" + cart.getTotalPrice()); cart.addProduct(p2, 10); System.out.println("总价:" + cart.getTotalPrice()); } } ``` 输出结果如下: ``` 总价:13997.0 总价:9998.0 库存不足! 总价:9998.0 ``` 这就是一个简单的 Java 模拟购物车系统。当然,这只是一个基础版本,您可以根据实际需求进行扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值