1. What is object-oriented?
stay Java In order to obtain objects, you must first design classes. Class (design drawing): Description of common characteristics of objects; Object: A concrete instance of what really exists The basic format of a class: public class Class name{ 1.Member variables (representing attributes) 2.Member method (representative behavior) }
Be careful:
1. The fully defined format of member variables is: modifier data type variable name = initialization value; Generally, no initialization value needs to be specified, and there is a default value.
2. The first letter of the class name is capitalized and meaningful to meet the "hump pattern"
3. Multiple class classes can be defined in a Java file, and only one class can be a public modifier, and the public modifier class name must be a code file name.
It is also recommended that a file define a class class class in actual development.
4. Class names cannot be keywords, satisfying flag rules
5. There is no need to specify an initialization value, there is a default value.
2. Object-oriented programming training: analog Shopping Cart module
(1) Needs analysis and architecture
Requirements: Simulate the function of shopping cart module, need to add goods to the shopping cart, need to provide modify the quantity of purchase of goods, settle the price of goods function (use object-oriented programming to solve). Analysis: 1.Each item in the shopping cart is an object, and a commodity class needs to be defined. 2.The shopping cart itself is an object: you can use an array object to represent it. 3.Complete the interface architecture, allowing users to select the functionality of the operation.
Define a commodity class and call it whenever you want public class Goods { int id; //number String name; //Name double price; // Price int buyNumber; //Purchase Quantity }
Then it's the idea of our main framework: public class ShopCarTest { public static void main(String[] args) { // 1. Define commodity classes for later creation of commodity objects // 2. Define the shopping cart object: use an array object to represent it. Goods[] shopCar = new Goods[100]; // 3. Set up operation structure while(true){ System.out.println("Please select the following command to operate:"); System.out.println("Add items to cart: add"); System.out.println("Query goods to cart: query"); System.out.println("Modify the quantity of goods purchased: update"); System.out.println("Settles the amount of goods purchased: pay"); Scanner sc = new Scanner(System.in); System.out.println("Please enter a command:"); 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("There was an error in the command you entered"); } } }
1. What can each item in the shopping cart look like and what do you need to prepare first?
#You can see objects one by one
#Define commodity class: Goods, later created object represents a commodity information
2. What does the shopping cart object represent and what can it be used for?
#Shopping carts can be represented by an array object of the commodity type and can be used to store the commodity object.
#Goods[ ] shorCar = new Goods[ 100];
(2) Add goods to shopping carts, view shopping cart information
Requirements: Let users enter commodity information and join the shopping cart, and view the shopping cart information immediately. Analysis: 1.Users need to enter commodity information and create commodity objects to encapsulate commodity information. 2.And add the object to the shopping cart array. 3.Querying the shopping cart information is to iterate through each object in the shopping cart array.
public static void addGoods(Goods[] shopCar , Scanner sc){ // 1. Enter the purchase information entered by the user. System.out.println("Please enter the number of your purchase(Do not repeat): "); int id = sc.nextInt(); System.out.println("Please enter the name of your purchase:"); String name = sc.next(); System.out.println("Please enter the quantity you have purchased:"); int buyNumber = sc.nextInt(); System.out.println("Please enter the purchase price:"); double price = sc.nextDouble(); // 2. Encapsulate the purchase information into a single object Goods g = new Goods(); g.id = id; g.name = name; g.buyNumber = buyNumber; g.price = price; //3. Add this object to the shopping cart array. for (int i = 0; i < shopCar.length ; i++) { if (shopCar[i] == null){ // Explain that there are no elements in this location. Add our new purchase here. shopCar[i] = g; break; } } System.out.println("Your goods:"+ g.name +"Add to cart complete."); } }
public static void queryGoods(Goods[] shopCar){ System.out.println("================Query shopping cart information as follows==================="); System.out.println("number\t\t Name\t\t Price\t\t\t Number of purchases"); 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 { //End of Traverse break; } }
1. How to complete the function of adding goods?
#Objects that create Goods classes represent commodity objects and encapsulate commodity information entered by users.
#Save the item object in an array that represents the shopping cart.
2. How to view the shopping cart information?
#Traversal represents an array of shopping carts, each traversing to a commodity object outputs its information display
(3) Modify purchase quantity
Requirements: Let users enter goods id,Find the corresponding item and modify its purchase quantity Analysis: Definition methods can be based on user input id Go to the shopping cart array to see if the item object exists.
public static void updateGoods(Goods[] shopCar , Scanner sc) { // Ask the user to enter the id of the item to be modified and query the object of the item to be modified based on the id while (true) { System.out.println("Please enter the item you want to modify id: "); int id = sc.nextInt(); Goods g = getGoods(shopCar, id); if (g == null) { System.out.println("Sorry, we didn't buy anything!"); } else { System.out.println("Please enter:" + g.name + "Latest purchases:"); int buyNumber = sc.nextInt(); g.buyNumber = buyNumber; System.out.println("Modification complete!"); 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){ //Determine if the id of this commodity object is what we are looking for if (g.id == id){ return g; } }else { return null; } } // return null; }
- How to modify the quantity purchased?
#Query out the object of the goods to be modified based on the commodity id entered by the user.
(4) Settlement amount
Requirements: When the user enters pay After the order, you need to show the information and total amount of all purchases Analysis: Define the sum variable, iterate through all the items in the shopping cart array, and add up the unit price*Buy quantity.
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("Total Order Amount:" + money); }
- How to calculate the total order amount for the goods?
#Defines the sum variable, iterates through all the items in the shopping cart array, and adds up the unit price * the quantity purchased
3. Simulated Shopping Cart Test
####(1.) Test to add modules
####(2). Query Commodity Module
###(3) Modify commodity module
###(4) Settlement module
4. Concluding remarks
This is just a simulation of the shopping cart, so the process is simple, and there are many bug s not considered: such as input the same number of goods to deal with, and so on. Bye today, see you tomorrow.