package trabalhoPDS.shoppingCart; import java.math.BigDecimal; import java.util.HashMap; import java.util.Iterator; public class ShoppingCart { private HashMapitems; public ShoppingCart() { super(); this.items = new HashMap(); } 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 it = this.items.values().iterator(); BigDecimal total = new BigDecimal(0.0); while (it.hasNext()){ total = total.add((it.next()).getPreco()); } return total; } public Iterator iterator() { return items.keySet().iterator(); } public void construct(Builder builder){ Iterator it = this.items.values().iterator(); while(it.hasNext()){ builder.buildObject(it.next()); } } }