java實現網上購物車程式,java網上購物車

來源:互聯網
上載者:User

java實現網上購物車程式,java網上購物車

用java代碼寫一個簡單的網上購物車程式,供大家參考,具體內容如下

需求:

1、寫一個商品類,有商品編號、商品名稱、商品分類、商品單價屬性。

2、寫一個商品條目資訊類,有商品和數量兩個屬性,有商品總價格方法。

3、寫一個購物車類,有添加商品方法、查看訂單資訊,刪除商品,修改商品,清空購物車,求購物車中所有商品總金額方法。4、寫一個測試類別,測試上述方法。

商品類:

public class Product {   private int productId;// 商品編號   private String productName;// 商品名稱   private String category;// 商品分類   private double price;// 單價    public Product() {// 無參構造     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;   }  } 

商品條目資訊類:

public class ProductItem {   private Product product;//購買的商品   private int count;//商品數量   public double totalMoney(){//小計     double price=product.getPrice();//擷取商品單價     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;   }    } 

購物車類:

import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class ShoppingCart {//購物車   //key:商品編號 value:商品條目   private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>();      public void addProduct(Product p){//添加商品     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(){//查看訂單資訊     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.getProductId()+",商品名稱:"       +product.getProductName()+",單價:"+product.getPrice()+",數量:"+productItem.getCount()       +",小計:"+productItem.totalMoney());     }   }   public boolean deleteProduct(int productId){//刪除商品     if(map.containsKey(productId)){       map.remove(productId);       return true;     }     return false;   }   public boolean modifyProduct(int productId,int count){//修改     if(map.containsKey(productId)){       if(count>=1){         ProductItem productItem = map.get(productId);         productItem.setCount(count);         return true;       }else if(count==0){//刪除該商品         deleteProduct(productId);         return true;       }       }     return false;   }      public void clearCart(){//清空購物車     map.clear();   }      public double totalAllMoney(){//商品總錢數     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;   } } 

測試類別:

public class ShoppingCartTest {    public static void main(String[] args) {     ShoppingCart cart=new ShoppingCart();     Product p1=new Product(101,"華碩筆記本","筆記本",4599);     Product p2=new Product(102,"蘋果","水果",5.9);     Product p3=new Product(103,"彩電","家電",2799);     Product p4=new Product(104,"秋褲","服裝",128);     Product p5=new Product(105,"華為手機","手機",2998);     Product p6=new Product(101,"華碩筆記本","筆記本",4599);//測試買兩件商品的情況     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("商品編號為:"+p2.getProductId()+"的商品刪除成功!");     }else{       System.out.println("刪除失敗");     }     cart.showAll();     System.out.println("############");     boolean flag2=cart.modifyProduct(p3.getProductId(), 2);     if(flag2){       System.out.println("商品編號為:"+p3.getProductId()+"的商品修改成功!");     }else{       System.out.println("修改失敗");     }     cart.showAll();          //cart.clearCart();     //cart.showAll();     System.out.println("商品總價錢為:"+cart.totalAllMoney());    }  } 

運行:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.