www.pudn.com > restrant.zip > ShoppingCartUtil.java, change:2014-01-29,size:2097b


package com.alex.restrantol.util; 
 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
 
import com.alex.restrantol.entity.MealEntity; 
 
public class ShoppingCartUtil { 
 
	private int total; 
 
	private Map<Integer,MealItem> map = new HashMap<Integer,MealItem>(); 
 
	public void addMeal(MealEntity m) { 
		MealItem item = (MealItem) this.getMap().get( 
				Integer.valueOf(m.getMealId())); 
		if (item == null) { 
			this.getMap().put(Integer.valueOf(m.getMealId()), new MealItem(m)); 
		} else { 
			item.addQuantity(); 
		} 
		this.total++; 
	} 
 
	public float totalPrice() { 
		float float1 = 0.0f; 
		Iterator<MealItem> it = this.getMap().values().iterator(); 
		MealItem mealItem = null; 
		while (it.hasNext()) { 
			mealItem = (MealItem) it.next(); 
			float1 += mealItem.getQuantity() 
					* mealItem.getMeal().getMealPrice().floatValue(); 
		} 
		return float1; 
	} 
 
	public void remove(int mid) { 
		MealItem item = (MealItem) this.getMap().get(Integer.valueOf(mid)); 
		if (item != null) { 
			this.getMap().remove(Integer.valueOf(mid)); 
			this.total -= item.getQuantity(); 
		} 
	} 
 
	public void add(int mid) { 
		MealItem item = (MealItem) this.getMap().get(Integer.valueOf(mid)); 
		if (item != null) { 
			item.addQuantity(); 
			this.total++; 
		} 
	} 
 
	public void sub(int mid) { 
		MealItem item = (MealItem) this.getMap().get(Integer.valueOf(mid)); 
		if (item != null) { 
			item.subQuantity(); 
			this.total--; 
		} 
	} 
 
	public void upd(int mid, int quan) { 
		MealItem item = (MealItem) this.getMap().get(Integer.valueOf(mid)); 
		if (item != null) { 
			int temp = quan - item.getQuantity(); 
			item.setQuantity(temp); 
			this.total += temp; 
		} 
	} 
 
	public void clearShopCart() { 
		if (!this.getMap().isEmpty()) { 
			this.map.clear(); 
			this.total = 0; 
		} 
	} 
 
	public int getTotal() { 
		return total; 
	} 
 
	public void setTotal(int total) { 
		this.total = total; 
	} 
 
	public Map<Integer,MealItem> getMap() { 
		return map; 
	} 
 
	public void setMap(Map<Integer,MealItem> map) { 
		this.map = map; 
	} 
}