JAVA object oriented review

Posted by shainh on Tue, 11 Jan 2022 13:38:31 +0100

Object oriented and object-oriented

Today, it has basically entered the core content of Java, object-oriented
Object oriented is divided into three modules
Today, let's talk about classes and objects

Class is the description of a class of things, which is an abstract and conceptual definition; An object is each individual of such things that actually exist, so it is also called an instance. If the object is compared to a car, the class is like the design drawing of a car. Therefore, the focus of object-oriented programming is class design, not object design. The attribute of class to object, that is, the method of member variable and operation attribute, that is, the encapsulation of member function. As long as the object is created through the new keyword, the attribute of the object can be assigned, then the method of the object is used to perform various operations to achieve various functions. This is probably one of the three major features of java object-oriented, namely encapsulation.

Object oriented instance
Realize the basic functions of shopping cart

Insert picture description here

Code example
Establishment of Goods class

package ObjectOriented;

public class Goods {
    int id; //Item number
    String name; //Trade name
    double price; //commodity price
    int buynumber; //Purchase quantity of goods
}

Test main function

package ObjectOriented;

import java.util.Scanner;

public class ShopCar {
    public static void main(String[] args) {
        //Define a commodity class for later creation of commodity objects
        //Define a shopping cart object, represented by an array
        Goods[] shopCar = new Goods[100]; //Goods is the new class name and the type name of the shopping cart array created
        //Build operation architecture
        while (true) {
            System.out.println("Please select the following items to operate:");
            System.out.println("Add item to Cart: add");
            System.out.println("Query items in shopping cart: query");
            System.out.println("Modify the quantity of goods purchased: update");
            System.out.println("Settlement of purchase amount: pay");
            Scanner sc = new Scanner(System.in);
            System.out.println("Please enter your choice");
            String command = sc.next();
            switch(command){
                case "add":
                    addGGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar,sc);
                    break;
                case "pay":
                    payGoods(shopCar);
                    break;
                default:
                    System.out.println("No such function!");
            }
        }
    }

    private static void payGoods(Goods[] shopCar) {
        //Display the product information in the shopping cart
        queryGoods(shopCar);
        //Define a variable to accept the total accumulated amount of the shopping cart
        double money = 0;
        //Traverse the shopping cart array and sum the quantity * unit price of each commodity
        for (int i = 0; i < shopCar.length ;i++) {
            Goods g = shopCar[i];
            if(g != null){
                money += (g.buynumber * g.price);
            }
            else{
                break;
            }
        }
        System.out.println("The total amount of orders in the shopping cart is" + money);
    }

    private static void updateGoods(Goods[] shopCar,Scanner sc) {
        //Let the user enter the commodity id to be modified, and query the commodity object to be modified according to the id
        //Write a separate method for implementation
        while (true) {
            System.out.println("Please enter the product information to be modified id");
            int id = sc.nextInt();
            Goods g = getGoodsbyid(shopCar,id);
            if(g == null){
                System.out.println("Sorry, we can't find the item with the corresponding number");
            }
            else{
                //Indicates that the product exists
                //Make changes
                System.out.println("Please enter"+g.name+"Latest purchase quantity of");
                int buyNumber = sc.nextInt();
                g.buynumber = buyNumber;
                System.out.println("Modification completed");
                queryGoods(shopCar);
                break;
            }
        }

    }
    //A separate method looks up the product information according to the id
    public static Goods getGoodsbyid(Goods[] shopCar,int id){
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i]; //Using variables to undertake, the later expression is simple and convenient
            if(g!=null){
                //Judge whether this product is what we are looking for
                if(g.id == id){
                    return g;
                }
                else{
                    return null;
                }
            }
        }
        return null; //It means that you have found 100 commodities without finding commodities with the same id
    }

    private static void queryGoods(Goods[] shopCar) {
        //Query shopping cart information and display
        System.out.println("=======The query information is as follows==========");
        System.out.println("number\t\t name\t\t\t Price\t\t\t Purchase quantity");
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if(g != null){
                //Display product object
                System.out.println(g.id+"\t\t"+g.name+"\t\t"+g.buynumber+"\t\t"+g.price);
            }
            else{
                break;
            }
        }
    }

    private static void addGGoods(Goods[] shopCar,Scanner sc) {//There is already an input scanner above. If you use parameter acceptance, you do not need to create a scanner again in the method
        //Enter commodity information
        System.out.println("Please enter the purchase information:");
        System.out.println("Please enter the item number:");
        int id = sc.nextInt();
        System.out.println("Please enter the name of the purchased commodity:");
        String name = sc.next();
        System.out.println("Please enter the quantity of required goods:");
        int buynumber = sc.nextInt();
        System.out.println("Please enter the price of the commodity to be purchased");
        double price = sc.nextDouble();
        //Encapsulate the purchased commodity information into a commodity object
        Goods g = new Goods();
        g.id = id;
        g.name = name;
        g.buynumber = buynumber;
        g.price = price;
        //Add the item object to the shopping cart array
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i] == null){
                //Indicates that there are no elements stored in this location
                shopCar[i] = g;
                break;//Product added successfully
            }
        }
        System.out.println("Your product" + g.name + "Successfully added to shopping cart");

    }
}

Today's study is like this
Generally speaking
A class is a large container that contains the contents of different attributes of the same kind. For example, you can define a student class in which you can create different variables, such as student number, student name, and student age and age. After defined in the class, you can call the class in the test function. Then the class name becomes the type when the object is defined. The variable defined later is based on the types defined in the class. It is like an all inclusive container. A variable can contain multiple attributes of the same type.
Class is a very important point in later engineering development
It will also be used with encapsulation and so on later

So that's all for today
See you tomorrow.

Topics: Java Back-end