现在的位置: 首页 > 综合 > 正文

购物车模块

2017年03月29日 ⁄ 综合 ⁄ 共 3671字 ⁄ 字号 评论关闭

运用集合实现简易的购物车模块:

可以生成订单号,日期。

效果:

目录结构:

源代码如下:

package entity;

public class Product {

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

	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	 
	
}

package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import util.DateHelper;
import util.GenerateID;
import entity.Product;

public class ShoppingCar {
	private Map<Product,Integer> m=new HashMap<Product,Integer>();
	//private Map<Product,Integer> m=new LinkedHashMap<Product,Integer>();
    private float total;
    public void addProduct(Product p){
    	//计算该购物车中是否存在该产品
    	if(m.containsKey(p)){
    		int oldValue=m.get(p);
    		m.put(p,oldValue+1);
    	}else{
    		m.put(p,1);
    	}
    }
    public static void main(String[] args) {
		Product p1=new Product(111,"牙膏",14);
		Product p2=new Product(123,"篮球",68);
		Product p3=new Product(123,"篮球",68);
		Product p4=new Product(888,"黄焖鸡",17);
		ShoppingCar mycar=new ShoppingCar();
		mycar.addProduct(p4);
		mycar.addProduct(p1);
		mycar.addProduct(p2);
		mycar.addProduct(p3);
	
		
		mycar.print();
	}
    
    public float getTotal(){
    	total=0;
    	Iterator<Product> it=m.keySet().iterator();
    	
    	while(it.hasNext()){
    		Product p=it.next();
    		float price=p.getPrice();
    		int num=m.get(p);
    		total+=price*num;
    	}
    	return total;
    }
    
    
    public void removeProduct(Product p){
    	
    }

    public void clearCar(Product p){
    	
    }

    public void print(){
    	System.out.println("\t\t\t\t\t\t  订单号:"+GenerateID.id());
    	System.out.println("————————————————————————————————————————————————————————————————————————————————");
    	System.out.println("序号\t\t产品名\t\t商品单价(¥)\t\t数量\t\t小计(¥)");
    	System.out.println("————————————————————————————————————————————————————————————————————————————————");
    
    	Iterator<Product> pit=m.keySet().iterator();
    	 int no=0;
    	 while(pit.hasNext()){
    	  no++;
          Product p=pit.next();
          int val=m.get(p);
    	  System.out.println(no+"\t\t"+p.getName()+"\t\t"+p.getPrice()+"\t\t\t"+val+"\t\t"+(p.getPrice()*val));
      	  System.out.println("——————————————————————————————————————————————————————————————————————————————");
    	 }
    	 System.out.println("\r\n\r\n\t\t\t\t\t\t总计:" +getTotal());
    	 
    	 System.out.println("\t\t\t\t\t\t日期:"+DateHelper.format("yyyy年MM月dd日hh时mm分ss秒"));
    }
   }


package util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateHelper {

	public static String format(String pattern){
		SimpleDateFormat  sf=new SimpleDateFormat(pattern);
		return sf.format(new Date(System.currentTimeMillis()));
	}
}

package util;
import java.util.Random;
public class GenerateID {
	//public synchronized static String id(){
		/*String first=DateHelper.format("yyyyMMddhhmmss");
			Random r1=new Random(System.currentTimeMillis());
			String n1=r1.nextInt(10)+"";
			Random r2=new Random(System.currentTimeMillis());
			String n2=r2.nextInt(10)+"";
			Random r3=new Random(System.currentTimeMillis());
			String n3=r3.nextInt(10)+"";
			Random r4=new Random(System.currentTimeMillis());
			String n4=r4.nextInt(10)+"";
			Random r5=new Random(System.currentTimeMillis());
			String n5=r5.nextInt(10)+"";*/
		   public synchronized static String id(){
			   String first=DateHelper.format("yyyyMMdd");
			   String second="";
			   Random random=new Random();
			   for(int i=0;i<5;i++){
				   second+=String.valueOf(random.nextInt(10));
			   }
		  // }
			return first+second;
		//return first+n1+n2+n3+n4+n5;
	}
}
【上篇】
【下篇】

抱歉!评论已关闭.