改造后的购物车案例

利用JavaBean改造的购物车案例

package zhtPractice.JavaBean改造购物车;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zht
 * @Date: 2022年01月03日 21:43
 * @Description:
 */
public class Goods {
    private int id;
    private String name;
    private double price;
    private int count;

    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 getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
package zhtPractice.JavaBean改造购物车;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zht
 * @Date: 2022年01月03日 21:45
 * @Description:
 */
public class shopCarTest {
    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("结算购买商品的价格:count");
            Scanner sc = new Scanner(System.in);
            String choose = sc.nextLine();

            switch (choose){
                case "add":
                    addGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar);
                    break;
                case "count":
                    countGoods(shopCar);
                    break;
            }
        }
    }

    private static void countGoods(Goods[] shopCar) {
        double sum=0.00;
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if(g != null){
                sum += g.getCount() * g.getPrice();
            }else{
                break;
            }
        }
        System.out.println("商品的总价格是:" + sum);
    }

    private static void updateGoods(Goods[] shopCar) {
        while (true) {
            System.out.println("请选择需要修改数量的商品id:");
            Scanner sc = new Scanner(System.in);
            int id = sc.nextInt();
            Goods g = getGoodsById(shopCar,id);
            if(g == null){
                System.out.println("抱歉,没有该商品!");
            }else{
                System.out.println("请您输入新的商品数量");
                 int num = sc.nextInt();
                 g.setCount(num);
                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.getId() == id) {
                    return g;
                }
            }else {
                return null;
            }
        }
        return null;//已经遍历了所有的id都没有找到
    }

    private static void queryGoods(Goods[] shopCar) {
        System.out.println("=========查询商品信息如下=======");
        System.out.println("编号\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.getId() + "\t\t" + g.getName() + "\t\t" + g.getPrice() + "\t\t" + g.getCount());
            }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("请输入商品数量:");
        int count = sc.nextInt();
        System.out.println("请输入商品价格:");
        double price = sc.nextDouble();

        //把这个商品封装成一个对象
        Goods g = new Goods();
        g.setId(id);
        g.setName(name);
        g.setPrice(price);
        g.setCount(count);

        //遍历购物车,看哪边有空的地方,加入商品到购物车
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i] == null){
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("商品已经添加完成!");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值