购物车案例

 刚开始的错误写法:

package zhtPractice.购物车案例;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zht
 * @Date: 2022年01月02日 19:43
 * @Description:
 */
public class Goods {
    int id;//编号
    String name;//名称
    double price;//价格
    int count;//数量

}
package zhtPractice.购物车案例;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zht
 * @Date: 2022年01月02日 19:42
 * @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];
            sum += g.count * g.price;
        }
        System.out.println("商品的总价格是:" + sum);
    }

    private static void updateGoods(Goods[] shopCar) {
        System.out.println("请选择需要修改数量的商品id:");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if(g.id == id){
                System.out.println("请输入修改后的商品数量:");
                Scanner sc1 = new Scanner(System.in);
                g.count = sc1.nextInt();
                break;
            }else{
                System.out.println("该商品不存在!");
            }

        }
        System.out.println("请重新选择购物商品数量");

    }

   

    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.id + "\t\t" + g.name + "\t\t" + g.price + "\t\t" + g.count);
            }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.id = id;
        g.name = name;
        g.price = price;
        g.count = count;
        
        //遍历购物车,看哪边有空的地方,加入商品到购物车
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i] == null){
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("商品已经添加完成!");
    }
}

修改后的正确写法:

package zhtPractice.购物车案例;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zht
 * @Date: 2022年01月02日 19:42
 * @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.count * g.price;
            }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("请您输入新的商品数量");
                g.count = 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;//已经遍历了所有的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.id + "\t\t" + g.name + "\t\t" + g.price + "\t\t" + g.count);
            }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.id = id;
        g.name = name;
        g.price = price;
        g.count = count;
        
        //遍历购物车,看哪边有空的地方,加入商品到购物车
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i] == null){
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("商品已经添加完成!");
    }
}

正确运行结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值