Java学习日记day05:集合总习题(Collection与Map)

集合

问题:购物车商场购物原理实现

购物车需求:

  • 提供添加商品方法(分为添加一个和一类)
  • 提供删除商品方法(分为删除一个和一类)
  • 提供清空购物车方法
  • 提供打印购物清单方法
下面是我所编写的代码
Product.java文件
package com.jvstudy.day05.text1;
/**
 * 商品类
 * id:		商品编号
 * name:	商品名称
 * price:	商品价格
 *  
 */
public class Product {
	private String id;
	private String name;
	private int price;
	
	public Product() {
		super();
	}
	
	public Product(String id,String name,int price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.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 (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}

ShopingCart.java文件
package com.jvstudy.day05.text1;

import java.util.*;
/**
 * 购物车类
 * 需求: 
 * 提供添加商品方法(分为添加一个和一类)
 * 提供删除商品方法(分为删除一个和一类)		
 * 提供清空购物车方法
 * 提供打印购物清单方法
 */
public class ShopingCart {
	Map<Product,Integer> productMaps;
	
	public ShopingCart() {
		productMaps = new HashMap<Product, Integer>();
	}
	//添加一个商品
	void add(Product product) {
		add(product,1);
	}
	//添加一类商品
	void add(Product product,Integer number) {
		productMaps.put(product, number);
	}
	//删除一个商品
	void remove(Product product) {
		int value = productMaps.get(product).intValue();
		value--;
		if(value==0)
			productMaps.remove(product);
		else
			add(product,value);
		System.out.println("\t\t已经从你的购物车删除一个"+product.getName());
	}
	//删除一类商品
	void del(Product product) {
		productMaps.remove(product);
		System.out.println("\t\t"+product.getName()+"已经从你的购物车删除");
	}
	//清空购物车
	void clear() {
		productMaps.clear();
		System.out.println("\t\t购物车已清空");
	}
	//输出购物车详单
	void print() {
		int totalPrice = 0;
		Set<Product> keys = productMaps.keySet();
		Iterator<Product> itr = keys.iterator();
		while(itr.hasNext()) {
			Product product = itr.next();
			int value = productMaps.get(product).intValue();
			totalPrice+=value*product.getPrice();
			System.out.println(product+"总共花费:"+value*product.getPrice());	
		}
		System.out.printf("\t\t你本次总共消费%d\n",totalPrice);
		System.out.println("-----------------------------------------------------");
	}
	
}

测试
package com.jvstudy.day05.text1;

import java.util.ArrayList;

/**
 * 测试
 */
public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Product p1 = new Product("001","java",78);
		Product p2 = new Product("002","python",68);
		Product p3 = new Product("003","php",69);
		Product p4 = new Product("004","c",66);
		Product p5 = new Product("005","go",77);
		ShopingCart spCar = new ShopingCart();	
		spCar.add(p1, 5);
		spCar.add(p2, 2);
		spCar.add(p3, 3);
		spCar.add(p4, 1);
		spCar.add(p5, 2);
		spCar.print();
		spCar.remove(p4);
		spCar.remove(p5);
		spCar.print();
		spCar.del(p1);
		spCar.print();
		spCar.clear();
		spCar.print();
	}

}

你可能感兴趣的:(Java)