Learning video:
catalogue
* 1, Demand analysis: simulate the function of shopping cart module
//2. Add items to shopping cart --> Add the commodity object to the array
//3. Query shopping cart information --> Traverse each item object in the shopping cart array
//1. Define a method: you can find the goods in the shopping cart through the user ID
//Traverse shopping cart array
/**
* Shopping Cart module simulation - Architecture Construction
* 1. Each item in the shopping cart can be regarded as an object,
//1. Define commodity category public class Goods { int id; String name; double price; int buyNumber; }
* 2. The shopping cart is represented by an array of commodity types, which is used to store objects
Goods[] shopCar=new Goods[100];
* 1, Demand analysis: simulate the function of shopping cart module
Add goods, modify, view quantity and settle price
framework:
1. Console
while(true){
System.out.println("please select the following command to operate");
System.out.println("add item to shopping cart: add");
System.out.println("query goods to shopping cart: query");
System.out.println("modify shopping cart item quantity: update");
System.out.println("settlement commodity price: pay");
Scanner sc=new Scanner(System.in);
String command=sc.next();
// Implementation console
switch (command) {
case "add":
// add to
System.out.println("============== add goods to shopping cart =============);
addGoods(shopCar,sc);
break;
case "query":
// see
System.out.println("============ view product information =============);
queryGoods(shopCar);
break;
case "update":
// modify
System.out.println("================ modify shopping cart goods ==============);
updateGoods(shopCar,sc);
break;
case "pay":
// settlement
System.out.println("=========== settlement commodity price =============);
pay(shopCar);
break;
default:
System.out.println("no such function");
}
}
}
2. Add product:
//Add goods to the shopping cart, let the user input the goods information, add it to the shopping cart, and view the shopping cart information immediately.
//1. Object of commodity: Commodity id, commodity name, commodity price, and commodity purchase quantity
//2. Add items to shopping cart --> Add the commodity object to the array
//3. Query shopping cart information --> Traverse each item object in the shopping cart array
public static void addGoods(Goods[] shopCar,Scanner sc) {
// 1. User input commodity information
System.out.println("please enter the product number (no repetition):");
int id=sc.nextInt();
System.out.println("please enter the name of the product:");
String name=sc.next();
System.out.println("please enter the quantity of goods:");
int buyNumber=sc.nextInt();
System.out.println("please enter the price of the commodity:");
double price=sc.nextDouble();
// 2. Add the commodity information object to the shopping cart array
Goods g = new Goods();
// ID object = ID
g.id=id;
g.buyNumber=buyNumber;
g.name=name;
g.price=price;
// 3. Add the commodity object to the array
for (int i = 0; i < shopCar.length; i++) {
if(shopCar[i]==null){
// It means that there are no elements stored in this location. Just add the newly purchased goods here
shopCar[i]=g;
break;// Add item complete
}
System.out.println("your product" + g.name + "was successfully added to the shopping cart");
}
}
3. Modification
//Let the user enter the commodity id, find out the corresponding commodity and modify its quantity
//1. Define a method: you can find the goods in the shopping cart through the user ID
//2. If the commodity exists, the address of the commodity object is returned. If it does not exist, null is returned
//3. Judge whether the returned address exists, modify its purchase quantity, and continue if it does not exist
public static void updateGoods(Goods[] shopCar,Scanner sc) {
while (true)
{
System.out.println("please enter the product ID you want to modify");
int id=sc.nextInt();
Goods g=getGoodsbyId(shopCar, id);
if(g==null){
System.out.println("you didn't buy the product, the id is wrong");
// Do not end, continue to enter id
}else{
System.out.println("please enter" + g.name + "latest purchase quantity of goods:");
int buyNumber=sc.nextInt();
g.buyNumber=buyNumber;
System.out.println("modification completed");
queryGoods(shopCar);// Direct view
break;// End the cycle
}
}
}
// method
public static Goods getGoodsbyId(Goods[] shopCar,int id) {
for (int i = 0; i < shopCar.length; i++) {
Goods g=shopCar[i];
if(g != null){
if(g.id == id){
return g;// Determine whether the product id is correct
}else{
return null;// We haven't found any goods yet
}
}
}
return null; // I've found all the goods
}
4. View
//Query the information in the shopping cart and display it
//Traverse shopping cart array
public static void queryGoods(Goods[] shopCar) {
System.out.println("number \ t \ tname \ t\t \ tprice \ t\t \ tpurchase quantity ");
for (int i = 0; i < shopCar.length; i++) {
Goods g=shopCar[i];
if (g !=null) {
// Goods exist, display goods
System.out.println(g.id+"\t\t"+g.name+"\t\t\t"+g.price+"\t\t\t"+g.buyNumber);
}else{
break;
// End of display, end of traversal
}
}
}
5. Settlement price
//After the user enters the Pay command, the information of all purchased goods and the total amount are displayed
//Traverse all goods in the shopping cart array and accumulate the amount (unit price * purchase quantity)
public static void pay(Goods[] 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;
}
}
queryGoods(shopCar); / / display all products
System.out.println("total order amount is" + money);
}
2, All codes
//1. Define commodity category public class Goods { int id; String name; double price; int buyNumber; }
import java.util.Scanner; public class Shoppingcae { public static void main(String[] args) { //2. Define a shopping cart object with a length of 100 Goods[] shopCar=new Goods[100]; //Console while(true){ System.out.println("Please select the following command to operate"); System.out.println("Add item to Cart: add"); System.out.println("Query goods to shopping cart: query"); System.out.println("Modify shopping cart item quantity: update"); System.out.println("Settled commodity price: pay"); Scanner sc=new Scanner(System.in); String command=sc.next(); //Implementation console switch (command) { case "add": //add to System.out.println("==========Add item to shopping cart=========="); addGoods(shopCar,sc); break; case "query": //see System.out.println("==========View product information=========="); queryGoods(shopCar); break; case "update": //modify System.out.println("==========Modify shopping cart items=========="); updateGoods(shopCar,sc); break; case "pay": //settlement System.out.println("==========Settle commodity price=========="); pay(shopCar); break; default: System.out.println("This function is not available"); } } } //After the user enters the Pay command, the information of all purchased goods and the total amount are displayed //Traverse all goods in the shopping cart array and accumulate the amount (unit price * purchase quantity) public static void pay(Goods[] 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; } } queryGoods(shopCar);//Show all products System.out.println("The total order amount is"+money); } //Let the user enter the commodity id, find out the corresponding commodity and modify its quantity //1. Define a method: you can find the goods in the shopping cart through the user ID //2. If the commodity exists, the address of the commodity object is returned. If it does not exist, null is returned //3. Judge whether the returned address exists, modify its purchase quantity, and continue if it does not exist public static void updateGoods(Goods[] shopCar,Scanner sc) { while (true) { System.out.println("Please enter the item you want to modify ID"); int id=sc.nextInt(); Goods g=getGoodsbyId(shopCar, id); if(g==null){ System.out.println("You have not purchased this product, id error"); //Do not end, continue to enter id }else{ System.out.println("Please enter"+g.name+"Latest purchase quantity of goods:"); int buyNumber=sc.nextInt(); g.buyNumber=buyNumber; System.out.println("Modification completed"); queryGoods(shopCar);//Direct view break;//End the cycle } } } //method public static Goods getGoodsbyId(Goods[] shopCar,int id) { for (int i = 0; i < shopCar.length; i++) { Goods g=shopCar[i]; if(g != null){ if(g.id == id){ return g;//Determine whether the product id is correct }else{ return null;//We haven't found any goods yet } } } return null; //I've found all the goods } //Query the information in the shopping cart and display it public static void queryGoods(Goods[] shopCar) { 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) { //Goods exist, display goods System.out.println(g.id+"\t\t"+g.name+"\t\t\t"+g.price+"\t\t\t"+g.buyNumber); }else{ break; //End of display, end of traversal } } } //Add goods to the shopping cart, let the user input the goods information, add it to the shopping cart, and view the shopping cart information immediately. //1. Object of commodity: Commodity id, commodity name, commodity price, and commodity purchase quantity //2. Add the item to the shopping cart -- > add the item object to the array //3. Query shopping cart information -- > traverse each commodity object in the shopping cart array public static void addGoods(Goods[] shopCar,Scanner sc) { //1. User input commodity information System.out.println("Please enter the item number(No repetition): "); int id=sc.nextInt(); System.out.println("Please enter the name of the product:"); String name=sc.next(); System.out.println("Please enter the quantity of goods:"); int buyNumber=sc.nextInt(); System.out.println("Please enter the price of the product:"); double price=sc.nextDouble(); //2. Add the commodity information object to the shopping cart array Goods g = new Goods(); //ID object = ID g.id=id; g.buyNumber=buyNumber; g.name=name; g.price=price; //3. Add the commodity object to the array for (int i = 0; i < shopCar.length; i++) { if(shopCar[i]==null){ //It means that there are no elements stored in this location. Just add the newly purchased goods here shopCar[i]=g; break;//Add item complete } System.out.println("Your product"+g.name+"Successfully added to cart"); } } }