java 購物車簡單練習
阿新 • • 發佈:2018-11-21
商品,購物車,測試類三個類,商品有價格,id,名稱,三個屬性,購物車有總價和車兩個屬性,有新增,刪除,清空,列印四個方法
商品類:
//定義商品,三個屬性,名稱,價格(BigDecimal型別),id編號,重寫toString,hashCode,equals方法,構造方法 import java.math.BigDecimal; public class Product { private String name; private int id; private BigDecimal price; public Product() { } public Product(String name, int id, BigDecimal price) { super(); this.name = name; this.id = id; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((price == null) ? 0 : price.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Product other = (Product) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (price == null) { if (other.price != null) return false; } else if (!price.equals(other.price)) return false; return true; } @Override public String toString() { return "Product [name=" + name + ", id=" + id + ", price=" + price + "]"; } }
購物車類:
//商品購物車,兩個屬性,商品型別,和總價格(用BigDecimal型別精確) import java.math.BigDecimal; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class ShopCar { Map<Product, Integer> productMap; BigDecimal totalPrice = BigDecimal.valueOf(0.0); // 新增商品 public ShopCar() { productMap = new HashMap<>(); } public void add(Product product, int num) { //檢查購物車是否包含這件商品,(不包含直接新增,否則取出該商品數量,計算加入後的總數,再重新放回購物車),最後計算總價 if (productMap.get(product) != null) { int before = productMap.get(product); int after = before + num; productMap.put(product, after); } else { productMap.put(product, num); } totalPrice = totalPrice.add(BigDecimal.valueOf(num).multiply(product.getPrice())); } public void remove(Product product, int num) { int removeBefore = productMap.get(product); if (num >= removeBefore) { productMap.remove(product); totalPrice = totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(removeBefore))); } else { int after = removeBefore - num; productMap.put(product, after); totalPrice = totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(num))); } } public void clear() { productMap.clear(); totalPrice = BigDecimal.valueOf(0.0); } public void check() { System.out.println("productList:"); System.out.println("id " + "\t" + "name" + "\t" + "number" + "\t " + "price"); Set<Map.Entry<Product, Integer>> set = productMap.entrySet(); Iterator<Map.Entry<Product, Integer>> it = set.iterator(); while (it.hasNext()) { Entry<Product, Integer> entry = it.next(); Product p = entry.getKey(); Integer i = entry.getValue(); System.out.println( p.getId() + "\t" + p.getName() + "\t" + i + "\t" + p.getPrice().multiply(BigDecimal.valueOf(i))); } System.out.print("\t\ttotalPrice :" + totalPrice); } }
測試類:
import java.math.BigDecimal; public class ShopCarTest { public static void main(String[] args) { ShopCar sCar = new ShopCar(); Product mobile = new Product("mobile", 1001, BigDecimal.valueOf(1000)); Product banana = new Product("banana", 1002, BigDecimal.valueOf(1)); Product apple = new Product("apple", 1003, BigDecimal.valueOf(5)); Product pad = new Product("pad", 1004, BigDecimal.valueOf(2000)); Product tv = new Product("tv", 1005, BigDecimal.valueOf(1300)); sCar.add(mobile, 3); sCar.add(tv, 1); sCar.add(pad, 2); sCar.check(); sCar.check(); sCar.check(); sCar.add(banana, 10); sCar.add(apple, 20); sCar.remove(mobile, 3); sCar.remove(apple, 10); sCar.check(); } }