使用面向对象编程思想,模拟购物车模块的功能

介于学习的内容暂时有限,
此代码只是在编程软件的输出结果中实现了购物车功能,并不是生成了相关界面,
功能并不是足够完善,由于时间有限,定义功能时未考虑完全,如有错误请指点,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
使用面向对象编程思想,
模拟购物车模块的功能:
①需要实现添加商品到购物车中去,
②同时需要提供修改商品的
③购买数量
④结算商品价格功能

package ShopCar;

import java.util.Scanner;

public class ShopCarTest {
    public static void main(String[] args) {
        //定义一个商品类 Goods存放在另外class文件中
        //定义购物车对象,暂时用数组代替(还没学到集合)
        Goods[] shopCar = new Goods[1000];
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("添加商品:add" + "\t\t");
            System.out.print("查看商品:query" + "\t\t");
            System.out.print("修改数量:update" + "\t\t");
            System.out.print("结算价格:pay" + "\t\t");
            System.out.println("退出购物车");
            System.out.println("请您选择要操作的功能:");
            String putin = sc.next();
            switch (putin) {
                //根据用户选择的功能不同调用不同的方法
                case "add":
                    addGoods(shopCar, sc);
                    break;

                case "query":
                    queryGoods(shopCar);
                    break;

                case "update":
                    updataGoods(shopCar, sc);
                    break;

                case "pay":
                    payaGoods(shopCar);
                    break;
                case "退出购物车":
                    return;
                default:
                    System.out.println("没有该功能~");
            }
        }
    }

    //结算价格方法数,为了将案例简单化,结算商品为结算全部商品
    public static void payaGoods(Goods[] shopCar) {
        System.out.println("购物车所有商品信息为:");
        queryGoods(shopCar);
        double sumPrice = 0;
        for (int i = 0; i < shopCar.length - 1; i++) {
            Goods g = shopCar[i];
            if (g != null) {
                sumPrice += g.price * g.num;
            } else
                break;
        }
        System.out.println("购物车的商品总共需要花费" + sumPrice + "元购买");
    }

    //修改购买数量方法:让用户输入商品id,找出对应的商品修改其数量
    public static void updataGoods(Goods[] shopCar, Scanner sc) {
        while (true) {
            System.out.println("请你输入需要修改数量的商品id");
            int GoodId = sc.nextInt();
            Goods g = judge(shopCar, GoodId);
            if (g == null) {
                System.out.println("没有该商品信息~~~");
            } else {
                //说明存在该上面对象,可以进行修改
                System.out.println("请您输入" + g.name + "最新的数量");
                int newNum = sc.nextInt();
                g.num = newNum;
                System.out.println(g.name + "的数量修改成功!!!");
                //重新查询购物车信息,已观看是否商品数量是否修改成功;
                queryGoods(shopCar);
                break;
            }
        }
    }

    //查看商品方法
    public static void queryGoods(Goods[] shopCar) {
        System.out.println("~~~~~~~~~~购物车中商品信息如下~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("编号" + "\t\t" + "商品名称" + "\t\t" + "商品价格" + "\t\t" + "商品数量");
        for (int i = 0; i < shopCar.length - 1; i++) {
            Goods g = shopCar[i];//定义一个Goods变量来接ShopCar中i的地址
            if (g != null) {
                System.out.println(g.id + "\t\t" + g.name + "\t\t" + g.price + "\t\t" + g.num);
            } else
                break;
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    //添加商品方法
    public static void addGoods(Goods[] shopCar, Scanner sc) {
        //录入用户输入的商品信息
        System.out.println("请您输入商品的编号(不重复):");
        int id = sc.nextInt();
        System.out.println("请您输入商品的名称:");
        String name = sc.next();
        System.out.println("请您输入商品的价格:");
        double price = sc.nextDouble();
        System.out.println("请您输入添加商品的数量:");
        int num = sc.nextInt();

        //将商品信息封装成一个对象;利用Goods依次将信息输入到对象中去
        Goods g = new Goods();
        g.id = id;
        g.name = name;
        g.price = price;
        g.num = num;

        //将这个对象存入到购物车数组中去
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                //说明购物车该位置没有元素,可以存入;
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("~~添加成功,您的商品:" + g.name + "已经添加到购物车里面去啦~~");

    }

    //定义一个方法用来查看用户输入的商品id在购物车中是否存在;
    //方法类型定义为Goods,Goods是一开始定义好了的类
    public static Goods judge(Goods[] ShopCar, int GoodId) {
        for (int i = 0; i < ShopCar.length - 1; i++) {
            Goods g = ShopCar[i];
            if (g != null) {
                if (g.id == GoodId) {
                    return g;
                }
            } else
                return null;//代表找完了前面存在的商品都没有找到,返回null

        }
        return null;//这个return表示找完了购物车所有的商品都没有找到对应得到id
    }
}

 class Goods {
    int id;//编号
    String name;//名称
    double price;//价格
    int num;//购买的数量
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值