Schreiben Sie ein einfaches Online -Einkaufswagenprogramm mit Java -Code als Referenz. Der spezifische Inhalt ist wie folgt
brauchen:
1. Schreiben Sie eine Produktkategorie, einschließlich Produktnummer, Produktname, Produktklassifizierung und Produkteinheitspreisattribute.
2. Schreiben Sie eine Kategorie zur Produkteintragsinformation mit zwei Attributen: Produkt und Menge und über eine Gesamtpreismethode für das Produkt.
3. Schreiben Sie eine Einkaufswagenklasse, die Methoden zum Hinzufügen von Produkten, zum Hinzufügen von Bestellinformationen, zum Löschen von Produkten, zum Ändern von Produkten, zum klaren Einkaufswagen und den Gesamtmengen aller Produkte im Einkaufswagen enthält. 4. Schreiben Sie eine Testklasse, um die oben genannten Methoden zu testen.
Produktkategorie:
PUBLIC CLASSE PRODUKT {private int productId; // Produktnummer privater String -Produktname; // Produktname private String -Kategorie; // Produktkategorie privater Doppelpreis; // Einheit Preis öffentliches Produkt () {// No Parameter Construction Super (); } public product (int productId, String productName, String -Kategorie, Doppelpreis) {Super (); this.Productid = productId; this.ProductName = ProductName; this.category = Kategorie; this.price = Preis; } 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 () {Rückgabekategorie; } public void setCategory (String -Kategorie) {this.category = category; } public double getPrice () {Rückgabepreis; } public void setPrice (Doppelpreis) {this.price = price; }} Produkteintragsinformationskategorie:
public class ProductItem {privates Produkt; // gekaufte private int count; // Produktmenge öffentliche doppelte totalmoney () {// subto double price = product.getPrice (); // Erhalten Sie den Einheitspreis des Produktrenditepreises*count; } public productItem () {Super (); } public productItem (Produktprodukt, int count) {super (); this.Product = Produkt; this.count = count; } öffentliches Produkt getProduct () {Rückgabeprodukt; } public void setProduct (Produktprodukt) {this.Product = Produkt; } public int getCount () {return count; } public void setCount (int count) {this.count = count; }} Einkaufswagenkategorie:
Import Java.util.Collection; Import Java.util.iterator; import Java.util.linkedhashMap; import Java.util.map; öffentliche Klasse ShoppingCart {// Einkaufswagen // Schlüssel: Produktnummer Wert: Produkteintrag Private Karte <Integer, ProductItem> map = new LinkedHasMap <Integer, ProductItem> (); public void addProduct (Produkt p) {// Produkt 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 () {// Auftragsinformation ansehen <produktItem> productItems = map.Values (); Iterator <produktItem> iterator = productItems.Iterator (); while (iterator.hasnext ()) {productItem productItem = iterator.next (); Product product = productItem.getProduct (); System.out.println ("Produktnummer:"+product.getProductid ()+", Produktname:"+product.getProductName ()+", Einheit Preis:"+product.getPrice ()+", Menge:"+productCountem.getCount ()+", subtotal:"+productIntem.TotalMoney ()); }} public Boolean DeleteProdu (int productId) {// Produkt löschen if (map.containsKey (productId)) {map.remove (productId); zurückkehren; } 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); zurückkehren; } else if (count == 0) {// das Produkt löschen (productId); zurückkehren; }} return false; } public void clearCart () {// Die Einkaufswagenkarte löschen.Clear (); } public Double TotalAllMoney () {// Gesamtproduktgeld doppelte Gesamt = 0; Sammlung <produktItem> productItems = map.Values (); Iterator <produktItem> iterator = productItems.Iterator (); while (iterator.hasnext ()) {productItem productItem = iterator.next (); Double Money = ProductItem.TotalMoney (); Gesamt+= Geld; } Return Total; }} Testklasse:
öffentliche Klasse coppingCartTest {public static void main (String [] args) {coppingCart cart = new ShoppingCart (); Produkt P1 = neues Produkt (101, "Asus Notebook", "Notebook", 4599); Produkt P2 = neues Produkt (102, "Apfel", "Frucht", 5,9); Produkt P3 = neues Produkt (103, "Color TV", "Home Appliances", 2799); Produkt P4 = neues Produkt (104, "Herbsthose", "Kleidung", 128); Produkt P5 = neues Produkt (105, "Huawei Mobile", "Mobile", 2998); Produkt P6 = neues Produkt (101, "ASUS -Notebook", "Notebook", 4599); // Testen Sie die Situation des Kaufs von zwei Artikeln. cart.addproduct (p2); cart.addproduct (p3); cart.addproduct (p4); cart.addproduct (p5); cart.addproduct (p6); cart.showall (); System.out.println ("#################################################; booleschen Flaggen = cart.DeletEProduct (p2.getProductId ()); if (flag) {System.out.println ("Produktnummer:"+p2.getProductId ()+"wurde erfolgreich gelöscht!"); } else {System.out.println ("Deletion fehlgeschlagen"); } cart.showall (); System.out.println("##############"); boolean flag2 = cart.modifyProduct (p3.getProductId (), 2); if (flag2) {System.out.println ("Das Produkt mit der Produktnummer:"+p3.getProductId ()+"wurde erfolgreich geändert!"); } else {System.out.println ("Modifikation fehlgeschlagen"); } cart.showall (); //cart.clearcart (); //cart.showall (); System.out.println ("Der Gesamtpreis des Produkts lautet:"+cart.totalallmoney ()); }}Rennrenderungen:
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.