Shopping cart group homework

Posted by yoda699 on Thu, 21 Oct 2021 17:19:00 +0200

1. Team members and task assignment

classfull nametask
Network 2014LAN suyuResponsible for coding related work: coding specification, object-oriented design, function design
Network 2014Liu YuhengResponsible for writing blog related work: preliminary investigation, blog production

2. Preliminary investigation (taking Jingdong Mall interface as an example)

2.1 main interface

2.2 product interface

2.3 shopping cart interface


It can be seen from the observation of Jingdong Mall that the main interface should be classified to make the buyer more purposeful. When displaying goods, the main information includes the name, description and unit price of the goods. The information displayed in the shopping cart includes: commodity name and unit price, subtotal of pre order quantity and amount of each commodity, and total amount of all commodities. The function operation keys of the shopping cart include: increase or decrease the pre order quantity of a certain commodity, delete a certain commodity, and empty the shopping cart.

3. System description

After entering the main interface of the mall, users can see the box of commodity search function and commodity classification options. Then click the commodity, you can see the name and unit price of the commodity, and you can choose to add the commodity to the shopping cart. After entering the shopping cart, you can see the name, quantity and subtotal amount of a commodity. You can increase or decrease the quantity of a commodity, delete a commodity, or empty the shopping cart directly.

4. System function structure diagram and main function flow chart

4.1 functional structure diagram

4.2 structure logic diagram

5. Class design and UML class diagram

Class 5.1 design

5.1.1Commodity (commodity)

5.1.2Menu (menu interaction class)

5.1.3 shopingmall (mall)

5.1.4Cart (shopping cart)

5.2 UML class diagram

6. Key code

6.1 mall

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ShoppingMall {
	// Define the List set, which is used to store all product information
		public static List<Commodity> goods = new ArrayList<>();

		static{//Commodity id// Trade name// Unit price// category// description;
			//Food,Electronic,Book,Clothe
			goods.add(new Commodity(1,"Apple",3.50,"Food","Crisp and sweet"));
			goods.add(new Commodity(2,"Banana",2.50,"Food","Sweet soft waxy"));
			goods.add(new Commodity(3,"Net red cake",3.00,"Food","Sugar free"));
			goods.add(new Commodity(4,"Huawei mobile phone",3999.00,"Electronic","Popular pop"));
			goods.add(new Commodity(5,"Millet Watch",788.0,"Electronic","Price reduction at the beginning of school"));
			goods.add(new Commodity(6,"<Principles of computer composition",46.0,"Book","Science Press, the fifth edition in 2013, printed on the 87th word in January 2018"));
			goods.add(new Commodity(7,"<Introduction to Algorithms",89.0,"Book","quality goods"));
			goods.add(new Commodity(8,"ins Wind jacket",3.0,"Clothe","Seasonal clearance"));
			goods.add(new Commodity(9,"Korean version BFT Shirt",3.0,"Clothe","Seasonal clearance"));
		}
		
		public static void displayAll() {
			if(goods.size() == 0) {
				System.out.println("Sorry, pro, the goods are still on the shelves");
			}else {
				for (Commodity goods : goods) {
					System.out.println(goods);
				}			
			}					
		}
		
		public static Commodity searchBy(int id) {
			if (id <= 0) {
				System.out.println("Input error, no product found, return to the mall");
				
			} else {
				for (int i = 0; i < goods.size(); i++) {
					if (goods.get(i).getId().equals(id)) {
					System.out.println(goods.get(i));				 
					return goods.get(i);
					}
				}
				
			}
			return null;
			
		}
//		public static int searchBy(String str) {
//			if (str =="Food") {
//				System.out.println("input error, return to mall");
//				return 0;
//			} else {
//				for (int i = 0; i < goods.size(); i++) {
//					if (goods.get(i).getId().equals(str))
//						System.out.println(goods.get(i));
//					return i;
//
//				}
//
//			}
//		}
		 
		public static void placeOrder() {
			
			System.out.println("Please enter the item number to purchase:");
			Scanner scan = new Scanner(System.in);
			int  i = scan.nextInt();
			ShoppingMall.searchBy(i);
			System.out.println("Enter item quantity");
			double  qty = scan.nextInt();
			
			double money = goods.get(i-1).price * qty;
			ShoppingMall.searchBy(i);
			System.out.println("Subtotal:"+money+"¥");
			System.out.println("Confirm the order, enter 1; Return to the mall and enter 0");
			

			int choice = scan.nextInt();
			switch (choice) {
			
			case 0: {
			
				Menu.shoppingMallMenu();
				Menu.shoppingMallKeyDown(scan);
			}
				break;

			case 1: {
				System.out.println("Order successfully!");
			}
				break;
			default: {
				System.out.println("Input error! Abnormal exit");
				Menu.shoppingMallMenu();
				Menu.shoppingMallKeyDown(scan);
			}
				break;
			}
		}
		
		public static void addToCart() {
			ShoppingMall.displayAll();
			System.out.println("Please enter the item number to add to the shopping cart:");
			Scanner scan = new Scanner(System.in);
			int  i = scan.nextInt();
			
			
			System.out.println("Set the number of items to add to the shopping cart"); 
			Integer  qty = scan.nextInt();
						
			
			System.out.println("Add shopping cart, enter 1; Return to the mall and enter 0");
			int choice = scan.nextInt();
			switch (choice) {
			
			case 0: {
			
				Menu.shoppingMallMenu();
				Menu.shoppingMallKeyDown(scan);
			}
				break;

			case 1: {
				
				Cart.cart.add( ShoppingMall.searchBy(i),qty);
				System.out.println("Successfully added to shopping cart!");
			}
				break;
			default: {
				System.out.println("Input error! Abnormal exit");
				Menu.shoppingMallMenu();
				Menu.shoppingMallKeyDown(scan);
			}
				break;
			}
			//scan.close();
		}
		
		
}

6.2 shopping carts

 class Cart {
	private static List<ItemEntry> itemList;
	public static Cart cart = new Cart();
	public Cart() {
		itemList = new ArrayList<>();

	}

	// static these methods are attributes of the cart class and do not belong to a cart object.
	public static boolean remove(Integer id) {
		if (id == null)
			return false;
		int index = findById(id);
		if (index == -1) {
			return false;
		} else {
			ItemEntry entry = itemList.get(index);
			if (entry.getQty() <= 1) {
				itemList.remove(index);// list.remove
			} else {
				entry.decrease();
			}
		}
		return true;
	}

	public static void add(Commodity e) {
		if (e == null)
			return;
		int index = findById(e.getId());
		if (index == -1) {
			itemList.add(new ItemEntry(e));
		} else {
			System.out.println("This item exists in the shopping cart. Now increase the quantity");
			itemList.get(index).increase();
		}
	}

	public static void add(Commodity e, Integer qty) {
		if (e == null)
			return;
		int index = findById(e.getId());
		if (index == -1) {
			itemList.add(new ItemEntry(e, qty));
		} else {
			System.out.println("This item exists in the shopping cart. Now increase the quantity");
			itemList.get(index).increase();
		}
	}

	public static void displayAll() {
		if (itemList.size() == 0) {
			System.out.println("Your shopping cart is empty. Go to the mall");
		} else {
			for (ItemEntry itemEntry : itemList) {// for each
				System.out.println(itemEntry);
			}
		}
	}

	public static void modifyQuantityInCart(Scanner scan) {
		Cart.displayAll();
		if (itemList.size() == 0)
			return;
		System.out.println("Please enter the item number to be modified:");
	
		Integer i = scan.nextInt();
		int index = Cart.findById(i);
		if (index == -1) {
			System.out.println("Input error, the item is not in the shopping cart.");
			return;
		
		}
		System.out.println("Enter item quantity");
		Integer qty = scan.nextInt();
		itemList.get(index).setQty(qty);
		itemList.get(index).totalPrice=itemList.get(index).item.price*qty;
		System.out.println("Modified successfully");
		Cart.displayAll();
	}

	public static void removeCommodity(Scanner scan) {
		Cart.displayAll();
		System.out.println("Please enter the item number to be removed: (integer greater than 0)");

		Integer id = scan.nextInt();

		int index = findById(id);
		if (index == -1) {
			System.out.println("The item is not in the shopping cart");
			return;
		} else {
			itemList.remove(index);
			System.out.println("Removed successfully!");
			System.out.println("Display shopping cart:");
			Cart.displayAll();
		}

	}

	public static void settleAccounts() {
		Cart.displayAll();
		Double total = 0.0;//total
//		System.out.println("please enter the commodity number to be modified: (integer greater than 0)");
//		Scanner scan = new Scanner(System.in);
//		Integer id = scan.nextInt();
		for (ItemEntry itemEntry : itemList) {
			total += itemEntry.getTotalPrice();
		}
		
		System.out.println("total:"+total);	

	}

	private static int findById(Integer id) {
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getItem().getId().equals(id))
				return i;

		}
		return -1;
	}

	private static class ItemEntry {
		Commodity item;
		Integer qty;
		Double totalPrice;

		public Double getTotalPrice() {
			return totalPrice;
		}

		public ItemEntry(Commodity item) {// The initial quantity of added goods is 1;
			this.item = item;
			qty = 1;
		}

		public ItemEntry(Commodity item, Integer qty) {// The initial quantity of added goods is 1;
			this.item = item;
			this.qty = qty;
			this.totalPrice = item.getPrice()*qty;
		}

		public void increase() {
			qty++;
		}

		public void decrease() {
			qty--;
		}

		public Commodity getItem() {
			return item;
		}

		public Integer getQty() {
			return qty;
		}

		public void setQty(Integer qty) {
			this.qty = qty;
		}

		@Override
		public String toString() {
			return "  item:" + item + ",quantity qty=" + qty +"Subtotal:"+ totalPrice+"]";
		}
	}
}

6.3 goods

public class Commodity {
	
	public Integer id;//Because you can't use privata to inherit
	public String name;
	public Double price;
	public Category category;
	public String description;
	private enum Category{//Commodity category enumeration class
		Food,Electronic,Book,Clothe
	};
	//Construct goods
	public  Commodity() {
		
	}
	public  Commodity(Integer id,String name) {
		this.id = id;
		this.name = name;
	}	
	public Commodity(Integer id, String name, Double price, String category, String description) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.category = Category.valueOf(category);
		this.description = description;
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime *result +((id == null)? 0:id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {//Only the id is compared. If the id is the same, it means that the two goods are the same
		if(this == obj)
			return true;
		if(obj == null)
			return false;
		Commodity other = (Commodity)obj;
		if (id == null) {
			if(other.id !=null)
				return false;
		}else if (!id.equals(other.id))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Commodity [Item number( id)=" + id 
				+ " Trade name( name):" +name 
				+" Commodity unit price( price): "+price
				+" Commodity category( category):"+category
				+" Product description( description):"+description;
	}
}

7. Summary

  • This is the first big job of Java object-oriented design, shopping cart. Our team has carried out preliminary investigation, class and object design, code structure design, etc. due to being unfamiliar, after carefully studying the job requirements,
  • When writing code, we will pay attention to the definition of variable name, function name, class name and method name, that is, we will pay attention to the standardization of the code. Do a good job in the code naming specification to make the code more readable and easy for readers to read.
  • In the process of writing code, we inevitably encounter various problems, such as unexpected errors in the written code, unclear attributes and methods contained in the class, etc. we solve the problems by consulting experts, baidu learning from similar projects, trying new ideas step by step, etc.
  • There are still many problems in this assignment after many modifications. We will continue to update it in the future.

Topics: Eclipse