Java模拟购物车系统

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

Java模拟购物车系统


1. 功能介绍

初学javase,暂时使用数组来模拟购物车系统中的添加商品功能,查询商品功能,修改购买商品数量以及计算商品总金额功能


2. 代码展示

import java.util.Scanner;

public class ShopCarTest {
    public static void main(String[] args) {
        // 定义一个存储 100 个商品的数组
        Goods[] shopcar = new Goods[100];
        OUT:
        while (true) {
            System.out.println("操作信息如下: ");
            System.out.println("添加商品信息: add");
            System.out.println("查询商品信息: query");
            System.out.println("修改商品信息: update");
            System.out.println("计算商品总价: pay");
            System.out.println("退出系统操作: quit");
            System.out.println("---------------------");
            System.out.println("请选择您的操作");
            Scanner sc = new Scanner(System.in);
            String op = sc.next();
            switch (op) {
                case "add":
                    // 添加商品
                    add(shopcar, sc);
                    break;
                case "query":
                    query(shopcar);
                    // 查询商品
                    break;
                case "update":
                    // 修改商品
                    update(shopcar, sc);
                    break;
                case "pay":
                    pay(shopcar);
                    // 计算商品金额
                    break;
                case "quit":
                    // 退出系统
                    System.out.println("您已成功退出系统!");
                    break OUT;
                default:
                    System.out.println("您选择的操作不符合要求!");
                    break;
            }
        }
    }

    public static void add(Goods[] shopcar, Scanner sc) {
        System.out.println("=======================");
        System.out.println("请添加商品信息: ");
        System.out.println("=======================");
        Goods g = new Goods();
        System.out.println("请输入商品编号: ");
        int id = sc.nextInt();
        System.out.println("请输入商品品牌: ");
        String brand = sc.next();
        System.out.println("请输入商品价格: ");
        double price = sc.nextDouble();
        System.out.println("请输入购买数量: ");
        int buyNum = sc.nextInt();
        g.id = id;
        g.brand = brand;
        g.price = price;
        g.buyNum = buyNum;
        for (int i = 0; i < shopcar.length; i++) {
            if (shopcar[i] == null) {
                shopcar[i] = g;
                System.out.println(g.brand + "已经成功添加");
                break;
            }
            if (shopcar[shopcar.length - 1] != null) {
                System.out.println("该商品数组无空闲位置!");
            }
        }
    }

    public static void query(Goods[] shopcar) {
        System.out.println("=======================");
        System.out.println("查询商品信息如下");
        System.out.println("=======================");
        System.out.println("商品编号\t\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\t" + g.brand + "\t\t\t" + g.price + "\t\t\t" + g.buyNum);
            } else {
                break;
            }
        }
    }

    public static void update(Goods[] shopcar, Scanner sc) {
        System.out.println("=======================");
        System.out.println("请修改商品购买数量");
        System.out.println("=======================");
        while (true) {
            System.out.println("请输入所要修改商品的编号: ");
            int id = sc.nextInt();
            Goods g = getGoodsId(shopcar, id);
            // 注意判断 g 的情况
            if (g == null) {
                System.out.println("该商品不存在!");
            } else {
                System.out.println("请输入" + g.brand + "最新的购买数量: ");
                int buyNum = sc.nextInt();
                g.buyNum = buyNum;
                System.out.println("修改成功! ");
                query(shopcar);
                break;
            }
        }


    }

    public static Goods getGoodsId(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;
                }
                break;
            } else {
                return null;
            }
        }
        return null; // 遍历完整个数组未发现该商品
    }

    public static void pay(Goods[] shopcar) {
        double money = 0;
        for (int i = 0; i < shopcar.length; i++) {
            Goods g = shopcar[i];
            if (g != null) {
                money += (g.price * g.buyNum);
            } else {
                break;
            }
        }
        System.out.println("商品总价格为: " + money);
    }
}


3. 总结

  1. 尽量使用方法来提高代码复用性
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值