java 购物车简单练习

商品,购物车,测试类三个类,商品有价格,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();

	}
}

原文:http://www.monkey1024.com/javase/596

猜你喜欢

转载自blog.csdn.net/sinat_41132860/article/details/84315905
今日推荐