0. Division of labor
task | Full name |
---|---|
Coding Specification, Pre-investigation and Functional Design | Tang Yuyue |
Object-oriented design, PPT production, or blogging | Lv Yiqing |
1. Pre-investigation
Shopping cart function:
- 1. Show all items in the library
- 2. Search function of goods
- 3. Add selected items to shopping cart
- 4. View all items in the shopping cart or delete a selected item from the shopping cart.
- 5. Empty the shopping cart
- 6. Procedures for displaying the total settlement price in the shopping cart
First enter the Jingdong Shopping Center, you can see a lot of commodity information, we want to search on the top of the goods
Click on one of the items you find to see the details of the item and add it to the shopping cart at the bottom
Searching for cola is the same process as searching for biscuits, but you still get information about your goods and can join the shopping cart
When the user enters the shopping cart, he can see the name and price of the goods in the shopping cart and the number of goods he has joined. He can also click on actions such as deleting the goods. At the bottom right corner, he can see the total price of all the selected goods. Click on the settlement to purchase the selected goods in the shopping cart.
2. System Functional Structure Diagram
3. System description
Red characters represent possible objects (nouns) or attributes, and blue characters represent possible methods (verbs)
Show all the items in the library, search for specific items through the user's choice, add or delete items to the shopping cart, even empty the shopping cart, and finally calculate the total amount of goods in the shopping cart.
4.UML Class Diagram
The key properties of a class and its relationship to methods, classes and classes.
5. Packagability
Variables in the List class are set to private, which can only be called by programs within the class and operated on externally by getter and setter methods.
6. Introduction of Program Functions
6.1 Start and End Menus
6.2 Function Selection Interface
1. Display commodities
Choose option 1 to show all the information about the goods in the library
If you choose to add items to the shopping cart, the user is prompted to enter the item id and quantity
After successfully joining the shopping cart, you can choose whether to continue adding or not, if not, return to the selection interface
Shopping cart with merchandise added
(2) Commodity inquiry
Choosing option 1 prompts the user to enter the name of the product they want to find and outputs the appropriate information [yes/no]
If the item exists, the user is asked if he wants to add it to the shopping cart, and if so, he continues to ask how many items he wants to add to the shopping cart.
Joined Shopping Cart
(3) View shopping carts
When items are added to the cart, their information is displayed and users are asked if they want to delete the items in the cart.
If yes, the id and quantity of the item you want to delete will be asked
Deleted shopping cart
(4) Empty the shopping cart
Empty shopping cart
Shopping cart pre-settlement
Choose option 5 to display all the items in the shopping cart and calculate the total price automatically
7. Main Code
Main Class
package Shopping; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); Scanner pause = new Scanner(System.in);// Pause parameters List.list();// Add merchandise to Library while (true) { Menu.beginMenu();// // Menu.endMenu(); System.out.println("Please enter your choice:"); int choose = sc.nextInt(); switch (choose) { case 1:// Show Goods Show.showGoods(); System.out.println("<Enter any number and press Enter to return to the main interface>"); String Pause = pause.next(); break; case 2:// Commodity Query System.out.println("Please enter a commodity name:"); String name = sc.next(); if (List.searchGoods(name) == 0) System.out.println("The item was not found!"); System.out.println("<Enter any number and press Enter to return to the main interface>"); Pause = pause.next(); break; case 3:// View shopping cart Show.showCart(); System.out.println("<Enter any number and press Enter to return to the main interface>"); Pause = pause.next(); break; case 4:// empty cart ShoppingCart.cleanGoods(); System.out.println("<Shopping cart emptied>"); System.out.println("<Enter any number and press Enter to return to the main interface>"); Pause = pause.next(); break; case 5:// Shopping cart pre-settlement Show.showPay(); System.out.println("<Enter any number and press Enter to return to the main interface>"); Pause = pause.next(); break; case 0:// Sign out break; } for (int i = 0; i < 25; i++) System.out.println(); if (choose == 0) break; } Menu.endMenu(); } }
Menu class
package Shopping; public class Menu { public static void beginMenu() { System.out.println(" =================================="); System.out.println(" ====== Welcome! ======"); System.out.println(" =================================="); System.out.println("What do you need?"); System.out.println("1)Commodity display"); System.out.println("2)Commodity Query ( Name)"); System.out.println("3)View shopping cart"); System.out.println("4)empty cart"); System.out.println("5)Shopping cart pre-settlement"); System.out.println("0)Sign out"); } public static void endMenu() { System.out.println(" ======================================"); System.out.println(" ==== Thank you for your patronage,Looking forward to your next visit! ===="); System.out.println(" ======================================"); } }
List Class
package Shopping; import java.util.Scanner; public class List { private static int goodsNumber = 12;// Total number of goods private static int[] goodsId = new int[100];// Commodity id private static String[] goodsName = new String[100];// Commodity Name private static double[] goodsPrice = new double[100];// commodity price public static int getGoodsNumber() { return goodsNumber; } public static void setGoodsNumber(int goodsNumber) { List.goodsNumber = goodsNumber; } public static int[] getGoodsId() { return goodsId; } public static void setGoodsId(int[] goodsId) { List.goodsId = goodsId; } public static String[] getGoodsName() { return goodsName; } public static void setGoodsName(String[] goodsName) { List.goodsName = goodsName; } public static double[] getGoodsPrice() { return goodsPrice; } public static void setGoodsPrice(double[] goodsPrice) { List.goodsPrice = goodsPrice; } // Add merchandise to Library public static void list() { for (int i = 0; i < 12; i++) { goodsId[i] = i; switch (i) { case 0: goodsName[i] = "Biscuits"; goodsPrice[i] = 58.0; break; case 1: goodsName[i] = "Coca Cola"; goodsPrice[i] = 38.9; break; case 2: goodsName[i] = "Orange Juice"; goodsPrice[i] = 20.3; break; case 3: goodsName[i] = "Notebook"; goodsPrice[i] = 5.5; break; case 4: goodsName[i] = "Pen"; goodsPrice[i] = 103.6; break; case 5: goodsName[i] = "Mineral water"; goodsPrice[i] = 2.0; break; case 6: goodsName[i] = "Washbasin"; goodsPrice[i] = 23.7; break; case 7: goodsName[i] = "Potato chips"; goodsPrice[i] = 5.0; break; case 8: goodsName[i] = "Soak noodles"; goodsPrice[i] = 12.3; break; case 9: goodsName[i] = "Cap"; goodsPrice[i] = 51.5; break; case 10: goodsName[i] = "scarf"; goodsPrice[i] = 78.4; break; case 11: goodsName[i] = "Building blocks"; goodsPrice[i] = 133.8; break; } } } Show Goods // public static void showGoods() { // System.out.println("ID commodity name price"); // for (int i = 0; i < goodsNumber; i++) { // System.out.println(goodsId[i] + " " + goodsName[i] + " " + goodsPrice[i]); // } // } // Search for Goods public static int searchGoods(String name) { Scanner sc = new Scanner(System.in); // By looping, you compare the names you need to find with those in the repository one by one for (int i = 0; i < goodsNumber; i++) { if (name.equals(goodsName[i])) { System.out.println("Here are the results of the search : "); System.out.println(); System.out.println("ID Commodity Name Price"); System.out.println(goodsId[i] + " " + goodsName[i] + " " + goodsPrice[i]); System.out.print("Do you want to add this item to the shopping cart?[Yes (1)/No (0)] : "); int chioce = sc.nextInt(); if (chioce == 1) { System.out.print("Add quantity?:"); int number = sc.nextInt(); if (true) { if (ShoppingCart.addGoods(goodsId[i], number) == 1) System.out.println("Joined the shopping cart!");// Add items to shopping cart list else System.out.println("Add failed!"); } } return 1;// Goods Found } } return 0;// No merchandise found } }
ShoppingCart class
package Shopping; public class ShoppingCart { private static int[] number = new int[100];// Record the number of each item, and the number of the array corresponds exactly to the item id // If number[i]=3, there are three items with id=i in the shopping cart public static int[] getNumber() { return number; } public static void setNumber(int[] number) { ShoppingCart.number = number; } // Add shopping cart items public static int addGoods(int goodsId, int goodsAddNumber) { for (int i = 0; i < List.getGoodsNumber(); i++) { if (i == goodsId) {// Find the id of the item number[i] += goodsAddNumber;// Increase the number of corresponding goods return 1;// Added Successfully } } return 0;// Failed to add } // Delete shopping cart items public static int subGoods(int goodsId, int goodsSubNumber) { for (int i = 0; i < List.getGoodsNumber(); i++) { if (i == goodsId) {// Find the corresponding item number[i] = number[i] - goodsSubNumber;// Delete the corresponding quantity of goods if (number[i] < 0) number[i] = 0;// The quantity of goods must not be less than 0 return 1;// Delete succeeded } } return 0;// Delete failed } // Empty shopping cart public static void cleanGoods() { // Make the corresponding number for each item 0 for (int i = 0; i < List.getGoodsNumber(); i++) { number[i] = 0;// The quantity of goods must not be less than 0 } } }
Show Class
package Shopping; import java.util.Scanner; public class Show { // Show list of items public static void showGoods() { System.out.println("ID Commodity Name Price"); for (int i = 0; i < List.getGoodsNumber(); i++) { System.out.println(List.getGoodsId()[i] + " " + List.getGoodsName()[i] + " " + List.getGoodsPrice()[i]); } while (true) { System.out.print("Do you want to add items to the shopping cart?[Yes (1)/No (0)] : "); Scanner sc = new Scanner(System.in); int chioce = sc.nextInt(); if (chioce == 1) { System.out.println("Please enter the corresponding item you need to add ID:"); int addId = sc.nextInt(); System.out.println("Please enter the number of items you need to add:"); int addNumber = sc.nextInt(); if (true) { if (ShoppingCart.addGoods(addId, addNumber) == 1) System.out.println("Joined the shopping cart!");// Add items to shopping cart list else System.out.println("Add failed!"); } } else break; } } public static void showCart() {// Show the goods in the shopping cart double countCartPay = 0; System.out.println("------ Your shopping cart --------"); System.out.println("commodity ID" + " " + "Commodity Name" + " " + "item pricing" + " " + "Purchase Quantity"); for (int i = 0; i < List.getGoodsNumber(); i++) { if (ShoppingCart.getNumber()[i] != 0) { System.out.println(List.getGoodsId()[i] + " " + List.getGoodsName()[i] + " " + List.getGoodsPrice()[i] + " * " + ShoppingCart.getNumber()[i]); } countCartPay = countCartPay + (ShoppingCart.getNumber()[i] * List.getGoodsPrice()[i]); } System.out.println(); System.out.println(" Your Bill Estimate : " + String.format("%.2f", countCartPay)); // Let users choose whether to delete items in shopping carts System.out.println(); System.out.println("------------------"); System.out.print("Do you want to delete the items in the shopping cart?[Yes (1)/No (0)] : "); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice == 1) { System.out.println("Please enter the corresponding item you want to delete ID:"); int subId = sc.nextInt(); System.out.println("Please enter the quantity you need to reduce:"); int subNumber = sc.nextInt(); if (ShoppingCart.subGoods(subId, subNumber) == 1) System.out.println("Removed from shopping cart!");// Add items to shopping cart list else System.out.println("Delete goods failed!");// Determine whether the input parameters are valid } } public static void showPay() { double Pay = 0; System.out.println(); System.out.println("==================================="); System.out.println("===============Shopping Cart==============="); System.out.println("commodity ID" + " " + "Commodity Name" + " " + "item pricing" + " " + "Purchase Quantity"); System.out.println(); for (int i = 0; i < List.getGoodsNumber(); i++) { if (ShoppingCart.getNumber()[i] != 0) {// This item is in the shopping cart System.out.println(List.getGoodsId()[i] + " " + List.getGoodsName()[i] + " " + List.getGoodsPrice()[i] + " " + ShoppingCart.getNumber()[i]); // Total Price = Unit Price * Quantity Pay = Pay + (ShoppingCart.getNumber()[i] * List.getGoodsPrice()[i]); } } System.out.println(); System.out.println("==================================="); // String.format() is used for general type formatting of strings System.out.println("============Total price:" + String.format("%.2f", Pay) + "============"); System.out.println(""); } }