All pastes #809623 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-12-09 01:48 UTC
rendered paste body
package trabalhoPDS.shoppingCart;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;

public class ShoppingCart {
 
	private HashMap<ShoppingCartProduct, Item>items; 
	
	 
	public ShoppingCart() {
		super();
		this.items = new HashMap<ShoppingCartProduct, Item>();
	}

	public void addItem(ShoppingCartProduct s, int qtd) {
		if( items.containsKey(s)){			
			Item i = this.items.remove(s);
			this.items.put(s, new Item(qtd + i.getQtd(), s));
		}
		else			
			items.put(s, new Item(qtd,s));
	}
	 
	public Item removeItem(ShoppingCartProduct s) {
		return items.remove(s);
	}
	
	public void limparCarrinho(){
		items.clear();
	}
	 
	public BigDecimal getTotal() {
		
		Iterator<Item> it = this.items.values().iterator();
		BigDecimal total = new BigDecimal(0.0);		
		while (it.hasNext()){
			
			total = total.add((it.next()).getPreco());
			
		}		
		return total;
	
	}
	 
	public Iterator<ShoppingCartProduct> iterator() {
		return items.keySet().iterator();
	}
	
	public void construct(Builder builder){
		Iterator<Item> it = this.items.values().iterator();
		while(it.hasNext()){
			builder.buildObject(it.next());
		}
	}
	 
}