rendered paste bodypackage 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());
}
}
}