JAVA homework -- shopping cart

catalogue

0. Division of labor

1. Preliminary investigation

2. System function structure diagram

3. System description

4. UML diagram

5. Object oriented encapsulation

6. Package structure and key codes

0. Division of labor

taskfull name
Coding specification, functional design, object-oriented designLuo mengvanadium
Blog production, preliminary investigationJiang Fanghua

1. Preliminary investigation

(1) Display product information and recommend it to users

 

(2) . enter JD home page and enter the product name to query the product related information

(3) . enter the specific commodity page to display commodity attributes and price information, and have the function of adding to the shopping cart  

(4) The shopping cart has the function of calculating the price and quantity of all goods.

2. System function structure diagram

Main menu functions:

  Shopping cart function

menu

 

 

3. System description

Mall category:

Display the commodities owned by the mall to users; Enter the product number to get the product information

Commodity category:

Store product related information

Shopping carts:

Add goods to shopping cart; Query shopping cart list; Empty the shopping cart; No. item information found

4. UML diagram

 

5. Object oriented encapsulation

JAVA: object oriented programming language. In this shopping cart case, different execution objects have different operations to implement. Therefore, first find out the objects and methods one by one, that is, find out the nouns and verbs, and then design the code by corresponding the nouns and verbs to the corresponding objects and methods one by one. In this way, different methods are designed for different objects, which is more logical and clear,

For the shopping mall object, you can set the commodity information of a private attribute to sub package this method. In this way, this commodity information can only be called in this class and cannot be used by other classes; The methods of displaying commodity information and searching commodity information can be set to the public attribute for other packages to call these methods to improve their functions.

For the shopping cart object, the methods of adding goods to the shopping cart, obtaining the shopping cart list, emptying the shopping cart, obtaining the commodity number, and generating an order are set to the public attribute

6. Package structure and key codes

package shopping;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		goods.list();//Load item list
		Scanner num = new Scanner(System.in);
		Scanner goodsId = new Scanner(System.in);
		goods.showGoods();//Display item
		while(true) {
			
			Menu.main();
			System.out.print("Please enter an option:");
			int Num = num.nextInt();
			
			switch (Num) {

			case 1://Commodity inquiry
				if(goods.searchGoods()==0)
					System.out.println("The item was not found");
				break;
			case 2://Commodity purchase
				System.out.println();
				System.out.println("Please enter the item serial number you need:");
				int needGoods = goodsId.nextInt();
				System.out.println("Please enter the quantity of goods you need:");
				int needAddNum = goodsId.nextInt();
				if(ShoppingCart.addGoods(needGoods, needAddNum)==1) 
					System.out.println("Added successfully");
				
				else System.out.println("Add failed");//Judge whether the input parameters are valid
				
				break;
			
			case 3://View shopping cart
				ShoppingCart.showCart();
				
				break;
			case 4://Shopping cart settlement
				Order.showPay();
				
				break;
			case 0:
				break;
			
		}
			for(int i=0;i<3;i++)
				System.out.println();
			if(Num == 0)
				break;
		}
		
		Menu.end();
	}

}
package shopping;
import java.util.*;

public class goods {

	public static int[] goodsID = new int[100];
	public static String[] goodsName =new String[100];
	public static double[] goodsPrice = new double[100];
	
	public static void showGoods() {
		System.out.println("Item number	Trade name	commodity price");
		for(int i = 0;i < 10;i++) {
			System.out.println(goodsID[i]+"	"+goodsName[i]+"	"+goodsPrice[i]);
		}
	}
	
	public static int searchGoods() {
		Scanner search = new Scanner(System.in);
		System.out.println("Please enter the item number:");
		int Search = search.nextInt();
		for(int i = 0;i < 10;i++) {
			if(Search == goodsID[i]) {
				System.out.print("Item found : \r\n");
				System.out.println("Item number	Trade name	commodity price");
				System.out.println(goodsID[i]+"	"+goodsName[i]+"	"+goodsPrice[i]);
				return 1;
			}
		}
		return 0;//Goods not found
	}
	
	public static void list() {
		for(int i = 0;i<10;i++) {
			goodsID[i] = i;
			switch (i) {
				case 0:
					goodsName[i] = "Sugar free dark chocolate" ;
					goodsPrice[i] = 58.60 ;
					break;
				case 1:
					goodsName[i] = "Imported milk from New Zealand" ;
					goodsPrice[i] = 89.00 ;
					break;
				case 2:
					goodsName[i] = "Cosmetic mirror" ;
					goodsPrice[i] = 49.00 ;
					break;
				case 3:
					goodsName[i] = "Golden September cake" ;
					goodsPrice[i] = 228.00 ;
					break;
				case 4:
					goodsName[i] = "Northeast rice";
					goodsPrice[i] = 12.00;
					break;
				case 5:
					goodsName[i] = "Xinjiang flour";
					goodsPrice[i] = 42.90;
					break;
				case 6:
					goodsName[i] = "Tie Guanyin";
					goodsPrice[i] =  119.00 ;
					break;
				case 7:
					goodsName[i] = "Big knife meat" ;
					goodsPrice[i] = 15.60 ;
					break;
				case 8:
					goodsName[i] = "Macadamia nut";
					goodsPrice[i] = 41.90 ;
					break;
				case 9:
					goodsName[i] = "Spiced melon seeds";
					goodsPrice[i] = 23.80 ;
					break;
			}
		}		
	}

}
package shopping;

public class Menu {
	public static void main() {
		System.out.println("Please select the operation number you want to perform");

		System.out.println("(1)Commodity inquiry");
		System.out.println("(2)Commodity purchase");
		System.out.println("(3)View shopping cart");
		System.out.println("(4)Shopping cart pre settlement");
		System.out.println("(0)sign out");
	}
	
	public static void end() {
		System.out.println(" -----------------------------------");
		System.out.println("          Thank you for your use and patronage!        ");
		System.out.println(" -----------------------------------");
	}

	
}
package shopping;

public class Order {
	public static void showPay() {//Show items in shopping cart
		double willPay = 0;
		System.out.println("");
		System.out.println("----------------------------------");
		System.out.println("Item number"+"	"+"Trade name"+"	"+"item pricing "+"	* "+"Purchase quantity");
		System.out.println("----------------------------------");
		
		for(int i = 0;i < 10;i++) {
			if( ShoppingCart.payNum[i] != 0) {
				System.out.println(goods.goodsID[i]+"	"+goods.goodsName[i]+"	"+goods.goodsPrice[i]+"	4 "+ShoppingCart.payNum[i]);
			}
			willPay = willPay + (ShoppingCart.payNum[i] * goods.goodsPrice[i]);
		}
		System.out.println("----------------------------------");
		System.out.println("	Actual payable :  "+String.format("%.2f", willPay));
		System.out.println("----------------------------------");
	}
}
package shopping;

public class ShoppingCart {
public static int[] payNum = new int[100];
	public static int addGoods(int goodsId,int goodsAddNum) {//Add goods
		
		for(int i = 0;i < 10;i++) {
			if(i == goodsId) {
				payNum[i] = goodsAddNum;
				System.out.println("Item number"+"	"+"Trade name"+"	"+"item pricing "+"	  "+"Purchase quantity");
				System.out.println(goods.goodsID[i]+"	"+goods.goodsName[i]+"	"+goods.goodsPrice[i]+"	* "+ShoppingCart.payNum[i]);
				return 1;
			}
		}
		return 0;
	}
	
	public static void showCart() {//Show items in shopping cart
		double cartPay=0;
		System.out.println("------ Your shopping cart————————————");
		System.out.print("Item number"+"	"+"Trade name"+"	"+"item pricing "+"	  "+"Purchase quantity\r\n");
		
		
		for(int i = 0;i < 10;i++) {
			if( ShoppingCart.payNum[i] != 0) {
				System.out.println(goods.goodsID[i]+"	"+goods.goodsName[i]+"	"+goods.goodsPrice[i]+"	* "+ShoppingCart.payNum[i]);
			}
			cartPay = cartPay + (ShoppingCart.payNum[i] * goods.goodsPrice[i]);
		}
		System.out.println();
		System.out.println("Your bill estimate :  "+String.format("%.2f", cartPay));
	}

}

Tags: Java

Posted on Mon, 04 Oct 2021 13:02:29 -0400 by Hagbard