Product.java
/** * * 商品类 * 1.商品属性 * 2.get-set * 3.重写toString、hashCode和equals方法 * 4.构造方法 */ import java.math.BigDecimal; public class Product { // 商品编号 private int id; // 商品名称 private String name; // 商品价格 跟金融有关的数据我们一般用BigDecimal这个数据类型 private BigDecimal price; public Product() { } public Product(int id, String name, BigDecimal price) { super(); this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } @Override public String toString() { return "商品 [id=" + id + ", name=" + name + ", 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; } }
ShopCar.java
import java.math.BigDecimal; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * * 购物车类 1.创建Map集合 2.在构造方法中初始化map; 3.add方法,向购物车中添加商品 4.从购物车中删除商品 5.清空购物车 6.打印 */ 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.containsKey(product)) { productMap.put(product, num); } else { // 若包含 int before = productMap.get(product); // 取得之前购物车中的商品数量 int after = before + num; productMap.put(product, after); } // 总价 totalPrice = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(num))); } public void remove(Product product, int num) { // 获取购物车中的商品数量 int before = productMap.get(product); if (num >= before) { productMap.remove(product); // 将该商品删除 totalPrice= totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(before))); } else { int after = before - 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 print() { System.out.println("商品清单"); Set<Product> key = productMap.keySet(); Iterator<Product> iter = key.iterator(); while(iter.hasNext()) { Product product = iter.next(); Integer i = productMap.get(product); System.out.println(i+ " " + product+ product.getPrice().multiply(BigDecimal.valueOf(i))); } System.out.println("总价:" + totalPrice); } }
Test,java
import java.math.BigDecimal; /** * * 实现购物车的功能 * * 分析: Product商品类 ShopCar购物车类 测试类Test */ public class Test { public static void main(String[] args) { Product apple = new Product(1001, "苹果", BigDecimal.valueOf(10)); Product bananer = new Product(1002, "香蕉", BigDecimal.valueOf(5)); Product orange = new Product(1003, "橘子", BigDecimal.valueOf(15)); Product longyan = new Product(1004, "龙眼", BigDecimal.valueOf(4.5)); ShopCar sc = new ShopCar(); sc.add(apple, 10); sc.add(longyan, 2); sc.add(orange, 20); sc.add(bananer, 3); // sc.remove(longyan, 2); // sc.add(apple, 10); sc.clear(); sc.print(); } }