Java Object-Oriented Practical Simulation Shopping Cart (Day 4)

1. What is object-oriented?

在Java中,必须先设计类,才能获得对象。
类(设计图):是对象共同特征的描述;对象:是真是存在的具体实例
	一个类的基本格式:
public class 类名{
	1.成员变量(代表属性)
	2.成员方法(代表行为)	
}

Note:
1. The complete definition format of member variables is: modifier data type variable name = initialization value; generally there is no need to specify an initialization value, there is a default value.
2. It is recommended that the first letter of the class name be capitalized, meaningful, and meet the "camel case"
3. Multiple classes can be defined in a Java file, and only one class can be public modified, and the public modified class name must become a code file name.
In actual development, it is recommended to define a class in one file.
Insert image description here
4. Keywords cannot be used in class names, which meet the identifier rules.
5. Generally, there is no need to specify an initialization value, and there is a default value.

2. Object-oriented programming training: simulated shopping cart module

(1) Requirements analysis and architecture construction

需求:
模拟购物车模块的功能,需要实现添加商品到购物车中去,同时需要提供修改商品的购买数量,结算商品价格功能(请使用面向对象编程来解决)。
分析:
1.购物车中的每个商品都是一个对象,需要定义一个商品类。
2.购物车本身也是一个对象:可以使用数组对象代表它。
3.完成界面架构,让用户选择操作的功能。
定义一个商品类,然后可以随时调用它
public class Goods {
    
    
    int id; //编号
    String name; //名称
    double price; // 价格
    int buyNumber; //购买数量
}

然后是我们主要框架的设想:

public class ShopCarTest {
    
    
    public static void main(String[] args) {
    
    
        // 1.定义商品类,用于后期创建商品对象
        // 2.定义购物车对象:使用一个数组对象表示。
        Goods[] shopCar = new Goods[100];
        // 3.搭建操作结构
        while(true){
    
    
        System.out.println("请你选择如下命令进行操作:");
        System.out.println("添加商品到购物车:add");
        System.out.println("查询商品到购物车:query");
        System.out.println("修改商品购买数量:update");
        System.out.println("结算购买商品的金额:pay");
        Scanner sc = new Scanner(System.in);
        System.out.println("请你输入命令:");
        String command = sc.next();
        switch (command){
    
    
            case "add":
                addGoods(shopCar ,sc);
                break;
            case "query":
                queryGoods(shopCar);
                break;
            case "update":
                updateGoods(shopCar ,sc);
                break;
            case "pay":
                pay(shopCar);
                break;
                default:
                    System.out.println("你输入的命令有错误");
        }
    }
   }

1. What can each item in the shopping cart be regarded as, and what preparations need to be made first?
#You can see the objects one by one.
#Define the product category: Goods. The object created later represents a product information
. 2. What does the shopping cart object represent and what can it be used for?
#The shopping cart can be represented by an array object of product type, which can be used to store product objects.
#Goods[ ] shorCar = new Goods[ 100];

(2) Add products to shopping cart and view shopping cart information

需求:
让用户输入商品信息,并加入到购物车中去,且可立即查看购物车信息。
分析:
1.需要让用户录入商品信息,创建商品对象封装商品信息。
2.并把商品对象加入到购物车数组中去。
3.查询购物车信息,就是遍历购物车数组中的每个商品对象。
    public static void addGoods(Goods[] shopCar , Scanner sc){
    
    
        // 1.录入用户输入的购买商品的信息。
        System.out.println("请你输入购买商品的编号(不重复):");
        int id = sc.nextInt();
        System.out.println("请你输入购买商品的名称:");
        String name = sc.next();
        System.out.println("请你输入购买商品的数量:");
        int buyNumber = sc.nextInt();
        System.out.println("请你输入购买商品的价格:");
        double price = sc.nextDouble();

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

        //3.把这个商品对象添加到购物车数组中去。
        for (int i = 0; i < shopCar.length ; i++) {
    
    
            if (shopCar[i] == null){
    
    
                // 说明此位置没有元素存入,把我们新买的商品添加到此处即可
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("你的商品:"+ g.name +"添加到购物车完成。");
    }
}

    public static void queryGoods(Goods[] shopCar){
    
    
        System.out.println("================查询购物车信息如下===================");
        System.out.println("编号\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" + g.name+"\t" + g.price +"\t\t\t" + g.buyNumber);
            }else {
    
    
                //遍历结束
                break;
            }
        }

1. How to complete the adding product function?
#Create an object of the Goods class to represent the product object and encapsulate the product information input by the user.
#Save the product object into the array representing the shopping cart.
2. How to view shopping cart information?
# Traverse the array representing the shopping cart, and output the information display for each product object traversed

(3) Modify the purchase quantity

需求:
让用户输入商品id,找出对应商品修改其购买数量
分析:
定义方法能够根据用户输入的id去购物车数组中查看是否存在该商品对象。
    public static void updateGoods(Goods[] shopCar , Scanner sc) {
    
    
        // 让用户输入要修改商品的id,根据id查询出要修改的商品对象
        while (true) {
    
    
            System.out.println("请你输入要修改的商品id:");
            int id = sc.nextInt();
            Goods g = getGoods(shopCar, id);
            if (g == null) {
    
    
                System.out.println("对不起,没有购买商品!");
            } else {
    
    
                System.out.println("请你输入:" + g.name + "商品最新购买数量:");
                int buyNumber = sc.nextInt();
                g.buyNumber = buyNumber;
                System.out.println("修改完成!");
                queryGoods(shopCar);
                break;
            }
        }
    }

    public static Goods getGoods(Goods[] shopCar , int id){
    
    
        for (int i = 0; i < shopCar.length ; i++) {
    
    
                Goods g = shopCar[i];
                if (g != null){
    
    
                    //判断这个商品对象的id是否是我们要找的
                    if (g.id == id){
    
    
                        return g;
                    }
                }else {
    
    
                    return null;
                }
        }
        //
        return null;
    }
  1. How to modify the quantity of goods purchased?
    #Query the product object to be modified based on the product ID entered by the user.

(4) Settlement amount

需求:
当用户输入了pay命令后,需要展示全部购买的商品信息和总金额
分析:
定义求和变量,遍历购物车数组中的全部商品,累加其单价*购买数量。
    public static void pay(Goods[] shopCar){
    
    
        queryGoods(shopCar);
        double money = 0;
        for (int i = 0; i < shopCar.length ; i++) {
    
    
            Goods g = shopCar[i];
            if (g != null){
    
    
                money += (g.price * g.buyNumber);
            }else {
    
    
                break;
            }
        }
        System.out.println("订单总金额:" + money);
    }
  1. How to calculate the order total of a product?
    #Define the summation variable, traverse all the products in the shopping cart array, and accumulate their unit price * purchase quantity

3. Simulate shopping cart test

### (1.) Add module test
Insert image description here
### (2) Query product module
Insert image description here
### (3) Modify product module
Insert image description here
### (4) Settlement module
Insert image description here

4 Conclusion

This is just a simulated shopping cart, so the process is simple and does not take into account many existing bugs: such as the response to inputting the same product number, etc. Goodbye today, see you tomorrow.

Guess you like

Origin blog.csdn.net/tyloonulinuli/article/details/121584033