Java面向对象实战——购物车功能的简单实现

2.17更新:用javabean进行了封装

完成功能:

  • 向购物车中添加商品
  • 查询购物车中商品信息
  • 更新购物车中某商品数量
  • 结算
  • 退出程序

大致思路是,这些功能都是基于一件件商品对象来完成的,因而首要的事情是建立一个商品类用来存储商品信息。至于购物车,可以看成是很多件商品构成的数组,因而定义一个商品数组来实现即可。

对于该项目我建立了一个名叫demo1的软件包并存放了两个文件,用来记录商品类和实现购物车。

由于本人懒惰,一些地方并未处理异常情况......

具体见代码及其注释:

商品类:

package demo1;

public class Goods { //定义商品类以存储信息
    int id;
    String name;
    double price;
    int buyNumber;
}
//Goods.java

实现购物车:

package demo1;

import java.util.Scanner;

public class ShopCarTest {
    public static void main(String[] args) {
        Goods[] shopping_cart = new Goods[100];
        //定义一个购物车对象,该对象可以用商品类型的数组对象来表示
        main_loop: //放一个标签在这,以方便杀死循环
        while (true) {
            System.out.println("please choose the orders below: ");
            System.out.println("Add goods into your shopping cart: add");
            System.out.println("Query goods in your shopping cart: query");
            System.out.println("Update the number of goods in your shopping cart: update");
            System.out.println("Pay your bill: pay");
            System.out.println("Kill the program: exit");
            System.out.println("input your order: ");
            //漫长的指令提示...
            Scanner sc = new Scanner(System.in);
            //new一个扫描器出来进行用户输入
            String order = sc.next();
            //读入用户指令
            switch (order) {
                case "add" -> addGoods(shopping_cart, sc);
                case "query" -> queryGoods(shopping_cart);
                case "update" -> updateGoods(shopping_cart, sc);
                case "pay" -> payGoods(shopping_cart);
                //为了架构合理美观,我们将指令处理抽象为方法
                case "exit" -> {
                    break main_loop; //杀死程序
                }
                default -> System.out.println("wrong order!"); //处理异常情况
            }
        }
    }
    //下面是对应的方法
    private static void payGoods(Goods[] shopping_cart) { //结算方法
        queryGoods(shopping_cart); //先把账目给用户观看
        double money = 0;
        for (Goods g : shopping_cart) {
            if (g != null) {
                money += g.price * g.buyNumber;
            } else {
                //遍历结束
                break;
            }
        }
        System.out.println("You need to pay: " + money + " dollars.");
    }

    private static void updateGoods(Goods[] shopping_cart, Scanner sc) { //更新商品信息
        //输入商品id,找出对应商品并修改购买数量
        while (true) {
            System.out.println("input the id of commodity you want to update: ");
            int id = sc.nextInt();
            Goods g = getGoodById(shopping_cart, id); //我们把按id找商品这件事交给一个新方法去做
            if(g == null) {
                //没有该商品
                System.out.println("you have not added it into your shopping cart!");
            }else {
                System.out.println("please input the number of " + g.name + " you want to buy: ");
                g.buyNumber = sc.nextInt();
                System.out.println("successfully update!");
                queryGoods(shopping_cart); //更新完成后给用户看账目
                break;
            }
        }
    }

    public static Goods getGoodById(Goods[] shopping_cart, int id) { //服务于上一个方法
        for (Goods g : shopping_cart) {
            if (g != null) {
                if (g.id == id)
                    //是我们要找的id
                    return g;
            } else {
                return null;
            }
        }

        return null;
    }

    private static void queryGoods(Goods[] shopping_cart) { //查询购物车中商品信息
        System.out.println("================查询购物车信息如下================");
        System.out.println("id\t\tname\t\t\tprice\t\t\tnumber");
        for (Goods g : shopping_cart) {
            if (g != null) {
                System.out.println(g.id + "\t\t" + g.name + "\t\t\t" + g.price + "\t\t\t" + g.buyNumber);
            } else {
                //遍历结束
                break;
            }
        }
    }

    private static void addGoods(Goods[] shopping_cart, Scanner sc) { //向购物车中添加商品
        // 录入商品信息
        System.out.println("input the id: ");
        int id = sc.nextInt();
        System.out.println("input the name: ");
        String name = sc.next();
        System.out.println("input the number: ");
        int buyNumber = sc.nextInt();
        System.out.println("input the price: ");
        double price = sc.nextDouble();

        // 把这些信息封装成一个商品对象
        Goods g = new Goods();
        g.id = id;
        g.name = name;
        g.buyNumber = buyNumber;
        g.price = price;

        // 把商品对象添加到购物车数组中去
        for (int i = 0; i < shopping_cart.length; i++) {
            if (shopping_cart[i] == null) {
                //说明此位置没有存入商品,可以使用
                shopping_cart[i] = g;
                break;
            }
        }
        System.out.println("Successfully add " + g.name + "into your shopping cart!");
    }
}
// ShopCarTest.java

更新版本

Goods.java

package javabean_demo0;

public class Goods {
    private int id;
    private String name;

    public Goods() {
    }

    private double price;

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

    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 getBuyNumber() {
        return buyNumber;
    }

    public void setBuyNumber(int buyNumber) {
        this.buyNumber = buyNumber;
    }

    private int buyNumber;
}
//Goods.java

ShopCarTest.java

package javabean_demo0;

import java.util.Scanner;

public class ShopCarTest {
    public static void main(String[] args) {
        Goods[] shopping_cart = new Goods[100];
        //定义一个购物车对象,该对象可以用商品类型的数组对象来表示
        main_loop: //放一个标签在这,以方便杀死循环
        while (true) {
            System.out.println("please choose the orders below: ");
            System.out.println("Add goods into your shopping cart: add");
            System.out.println("Query goods in your shopping cart: query");
            System.out.println("Update the number of goods in your shopping cart: update");
            System.out.println("Pay your bill: pay");
            System.out.println("Kill the program: exit");
            System.out.println("input your order: ");
            //漫长的指令提示...
            Scanner sc = new Scanner(System.in);
            //new一个扫描器出来进行用户输入
            String order = sc.next();
            //读入用户指令
            switch (order) {
                case "add" -> addGoods(shopping_cart, sc);
                case "query" -> queryGoods(shopping_cart);
                case "update" -> updateGoods(shopping_cart, sc);
                case "pay" -> payGoods(shopping_cart);
                //为了架构合理美观,我们将指令处理抽象为方法
                case "exit" -> {
                    break main_loop; //杀死程序
                }
                default -> System.out.println("wrong order!"); //处理异常情况
            }
        }
    }

    //下面是对应的方法
    private static void payGoods(Goods[] shopping_cart) { //结算方法
        queryGoods(shopping_cart); //先把账目给用户观看
        double money = 0;
        for (Goods g : shopping_cart) {
            if (g != null) {
                money += g.getPrice() * g.getBuyNumber();
            } else {
                //遍历结束
                break;
            }
        }
        System.out.println("You need to pay: " + money + " dollars.");
    }

    private static void updateGoods(Goods[] shopping_cart, Scanner sc) { //更新商品信息
        //输入商品id,找出对应商品并修改购买数量
        while (true) {
            System.out.println("input the id of commodity you want to update: ");
            int id = sc.nextInt();
            Goods g = getGoodById(shopping_cart, id); //我们把按id找商品这件事交给一个新方法去做
            if(g == null) {
                //没有该商品
                System.out.println("you have not added it into your shopping cart!");
            }else {
                System.out.println("please input the number of " + g.getName() + " you want to buy: ");
                int num = sc.nextInt();
                g.setBuyNumber(num);
                System.out.println("successfully update!");
                queryGoods(shopping_cart); //更新完成后给用户看账目
                break;
            }
        }
    }

    public static Goods getGoodById(Goods[] shopping_cart, int id) { //服务于上一个方法
        for (Goods g : shopping_cart) {
            if (g != null) {
                if (g.getId() == id)
                    //是我们要找的id
                    return g;
            } else {
                return null;
            }
        }

        return null;
    }

    private static void queryGoods(Goods[] shopping_cart) { //查询购物车中商品信息
        System.out.println("================查询购物车信息如下================");
        System.out.println("id\t\tname\t\t\tprice\t\t\tnumber");
        for (Goods g : shopping_cart) {
            if (g != null) {
                System.out.println(g.getId() + "\t\t" + g.getName() + "\t\t\t" + g.getPrice() + "\t\t\t" + g.getBuyNumber());
            } else {
                //遍历结束
                break;
            }
        }
    }

    private static void addGoods(Goods[] shopping_cart, Scanner sc) { //向购物车中添加商品
        // 录入商品信息
        System.out.println("input the id: ");
        int id = sc.nextInt();
        System.out.println("input the name: ");
        String name = sc.next();
        System.out.println("input the number: ");
        int buyNumber = sc.nextInt();
        System.out.println("input the price: ");
        double price = sc.nextDouble();

        // 把这些信息封装成一个商品对象
        Goods g = new Goods();
        g.setId(id);
        g.setName(name);
        g.setBuyNumber(buyNumber);
        g.setPrice(price);

        // 把商品对象添加到购物车数组中去
        for (int i = 0; i < shopping_cart.length; i++) {
            if (shopping_cart[i] == null) {
                //说明此位置没有存入商品,可以使用
                shopping_cart[i] = g;
                break;
            }
        }
        System.out.println("Successfully add " + g.getName() + "into your shopping cart!");
    }
}
// ShopCarTest.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AryCra_07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值