Requirements: 1, write a commodity category, there are commodity number, commodity name, commodity classification, commodity price properties. 2, write a commodity item information class, there are goods and quantity of two attributes, the total price method of goods.
3, write a shopping cart class, there are add commodity method, view order information, delete merchandise, modify merchandise, empty shopping cart, the total amount of goods in the cart to find the method. 4, write a test class, test the above method.
Product Category:
public class Product {private int productid;//commodity number private string productname;//commodity name private string category;//commodity classification
Private double price;//Unit price public Product () {//non-parametric 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 + ", Catego
Ry= "+ category +", price= "+ price +"];
public int GetProductID () {return productId;
The 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; }
}Item Information class:
public class Productitem {
private product product;//purchase
private int count;//Commodity quantity public
double Totalmoney () {//Subtotal
double Price=product.getprice ()//Get commodity price 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 class:
Import java.util.Collection;
Import Java.util.Iterator;
Import Java.util.LinkedHashMap;
Import Java.util.Map; public class ShoppingCart {//Shopping cart//key: Item Number value: Item Private map<integer,productitem> map=new linkedhashmap<
Integer,productitem> ();
public void Addproduct (product p) {//Add commodity 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 ("Item Number:" +product.getproductid () + ", Product Name:" +product.getproductname () + ", Unit Price:" +product.getprice () +
", Quantity:" +productitem.getcount () + ", Subtotal:" +productitem.totalmoney ());
}public boolean deleteproduct (int productId) {//delete commodity 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) {Pr
Oductitem Productitem = Map.get (productId);
Productitem.setcount (count);
return true;
}else if (count==0) {//delete the commodity deleteproduct (PRODUCTID);
return true;
return false;
public void Clearcart () {//Empty cart map.clear ();
Public double Totalallmoney () {//Total amount of merchandise 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 to 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 Appliance", 2799);
Product P4=new Product (104, "Autumn Pants", "costume", 128);
Product P5=new Product (105, "Huawei Mobile", "mobile", 2998);
Product P6=new Product (101, "Asus Notebook", "Notebook", 4599);//test to buy 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 ("Item number is: +p2.getproductid () +") The item was deleted successfully.
");
}else{System.out.println ("delete failed");
} cart.showall ();
System.out.println ("############");
Boolean flag2=cart.modifyproduct (P3.getproductid (), 2); if (Flag2) {System.out.println ("Item number is: +p3.getproductid () +") The product was successfully modified.
"); }else{System.out.println ("XiuChange Failure ");
} cart.showall ();
Cart.clearcart ();
Cart.showall ();
System.out.println ("The total price of goods is:" +cart.totalallmoney ());
}
}Run Effect chart: