用Java Map相关知识实现简易购物车功能

实现购物车功能

问题:实现一个购物车功能
分析:要实现这个功能,可以创建三个类,分别是Product商品类,ShopCart购物车类,Exercise测试类。Product类,里面描述商品的一些基本属性,编号,名称,单价。

下面演示如何用代码实现需求。先定义一个商品类。

package com.mycode.set.exercise;

import java.math.BigDecimal;

/**
 *商品类 
 *
 */
public class Product {
	//商品编号
   private int id;
   //商品名称
   private String name;
   //单价
   private BigDecimal 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 + "]";
}
public Product() {
	super();
}

public Product(int id, String name, BigDecimal price) {
	super();
	this.id = id;
	this.name = name;
	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;
}


}

接下来是Shopcar类。

package com.mycode.set.exercise;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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){
		  //判断map中是否包含当前商品,如果不包含,则直接将商品和数量添加到Map里面
		  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(num)));
			  
		  }
		  else{
			  //剩余的商品数量
			    int after=before-num;
			    productMap.put(product, after);
			  
		  }
		  totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(num)));
	  }
	  //清空购物车
	  public void clear(){
		  
		  productMap.clear();
		  totalPrice=totalPrice=BigDecimal.valueOf(0.0);
	  }
	  //打印详单
	  public void print(){
		   System.out.println("商品清单:"); 
		   //获取map中所有的键
		    Set<Product> key=productMap.keySet();
		    Iterator<Product> iter=key.iterator();
		    while(iter.hasNext()){
		    	
		    	Product p=iter.next();
		    	Integer i=productMap.get(p);
		    	System.out.println(i+ "件    "+p+"\t\t\t"+p.getPrice().multiply(BigDecimal.valueOf(i)));
		    	
		    }
		    System.out.println("\t\t\t\t\t总价:"+totalPrice);
	  }
	  
}

最后是测试类,测试相关功能

package com.mycode.set.exercise;

import java.math.BigDecimal;

/**
 * 
 *分析:要实现这个功能,可以创建三个类,分别是Product商品类,ShopCart购物车类,Exercise测试类。
        Product类,里面描述商品的一些基本属性,编号,名称,单价
 */
public class Exercise06 {
    public static void main(String[] args) {
    	  Product orange=new Product(1001,"橘子",BigDecimal.valueOf(10));
          Product apple=new Product(1002,"苹果",BigDecimal.valueOf(15));
          Product grape=new Product(1003,"葡萄",BigDecimal.valueOf(20));
          
          ShopCar sc=new ShopCar();
          sc.add(apple, 5);
          sc.add(orange, 10);
          sc.add(grape, 20);
          sc.add(apple, 5);
          //sc.remove(apple, 3);
          //sc.clear();
          sc.print();
	}
}

实现结果如下,这里的删除和清楚功能我就不一一测试了,有兴趣大家可以试一下。

入门练手小例子,写的不好,还望大家多多指教。

猜你喜欢

转载自blog.csdn.net/qq_41378597/article/details/82665389