Write a simple online shopping cart program using java code for your reference. The specific content is as follows
need:
1. Write a product category, including product number, product name, product classification, and product unit price attributes.
2. Write a product entry information category, which has two attributes: product and quantity, and has a total price method for the product.
3. Write a shopping cart class, which includes methods to add products, view order information, delete products, modify products, clear shopping cart, and find the total amount of all products in the shopping cart. 4. Write a test class to test the above methods.
Product category:
public class Product { private int productId;// Product number private String productName;// Product name private String category;// Product category private double price;// Unit price public Product() {// No parameter construction super(); } public Product(int productId, String productName, String category, double price) { super(); this.productId = productId; this.productName = productName; this.category = category; this.price = price; } public String toString() { return "Product [productId=" + productId + ", productName=" + productName + ", category=" + category + ", price=" + price + "]"; } public int getProductId() { return productId; } public void setProductId(int productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } Product entry information category:
public class ProductItem { private Product product;//Purchased private int count;//Product quantity public double totalMoney(){//Subto double price=product.getPrice();//Get the unit price of the product return price*count; } public ProductItem() { super(); } public ProductItem(Product product, int count) { super(); this.product = product; this.count = count; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } Shopping cart category:
import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class ShoppingCart {//Shopping cart//key: Product number value: Product entry private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>(); public void addProduct(Product p){//Add product int productId=p.getProductId(); if(map.containsKey(productId)){ ProductItem productItem=map.get(productId); productItem.setCount(productItem.getCount()+1); }else{ map.put(productId, new ProductItem(p,1)); } } public void showAll(){//View order information Collection<ProductItem> productItems = map.values(); Iterator<ProductItem> iterator = productItems.iterator(); while(iterator.hasNext()){ ProductItem productItem = iterator.next(); Product product = productItem.getProduct(); System.out.println("Product number: "+product.getProductId()+", Product name: "+product.getProductName()+", Unit price: "+product.getPrice()+", Quantity: "+productItem.getCount() +", Subtotal: "+productItem.totalMoney()); } } public boolean deleteProduct(int productId){//Delete product if(map.containsKey(productId)){ map.remove(productId); return true; } return false; } public boolean modifyProduct(int productId,int count){//Modify if(map.containsKey(productId)){ if(count>=1){ ProductItem productItem = map.get(productId); productItem.setCount(count); return true; }else if(count==0){//Delete the product deleteProduct(productId); return true; } } return false; } public void clearCart(){//Clear the shopping cart map.clear(); } public double totalAllMoney(){//Total product money double total=0; Collection<ProductItem> productItems = map.values(); Iterator<ProductItem> iterator = productItems.iterator(); while(iterator.hasNext()){ ProductItem productItem = iterator.next(); double money=productItem.totalMoney(); total+=money; } return total; } } Test class:
public class ShoppingCartTest { public static void main(String[] args) { ShoppingCart cart=new ShoppingCart(); Product p1=new Product(101,"Asus Notebook","Notebook",4599); Product p2=new Product(102,"Apple","Fruit",5.9); Product p3=new Product(103,"Color TV","Home Appliances",2799); Product p4=new Product(104,"Autumn Pants","Clothing",128); Product p5=new Product(105,"Huawei Mobile","Mobile",2998); Product p6=new Product(101,"ASUS Notebook","Notebook",4599);//Test the situation of buying two items cart.addProduct(p1); cart.addProduct(p2); cart.addProduct(p3); cart.addProduct(p4); cart.addProduct(p5); cart.addProduct(p6); cart.showAll(); System.out.println("############"); boolean flag=cart.deleteProduct(p2.getProductId()); if(flag){ System.out.println("Product number: "+p2.getProductId()+" was deleted successfully!"); }else{ System.out.println("Deletion failed"); } cart.showAll(); System.out.println("##############"); boolean flag2=cart.modifyProduct(p3.getProductId(), 2); if(flag2){ System.out.println("The product with the product number: "+p3.getProductId()+" was modified successfully!"); }else{ System.out.println("Modification failed"); } cart.showAll(); //cart.clearCart(); //cart.showAll(); System.out.println("The total price of the product is: "+cart.totalAllMoney()); } }Running renderings:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.