1 27 package olstore.client; 28 29 import java.awt.Component; 30 import java.math.BigDecimal; 31 import java.util.ArrayList; 32 import java.util.List; 33 34 import javax.swing.JPanel; 35 36 39 public class ShoppingCartPanel extends JPanel { 40 41 42 private ArrayList items; 43 44 45 private OlstoreSwingClient client; 46 47 52 public ShoppingCartPanel(OlstoreSwingClient client) { 53 items = new ArrayList(); 54 this.client = client; 55 } 56 57 61 public void addNewItemToCart(OrderItem item) { 62 items.add(item); 63 add(new OrderPanel(item)); 64 updatePrice(Double.valueOf(item.getPrice()).doubleValue()); 65 } 66 67 71 public void increaseQuantity(OrderItem item) { 72 Component[] orderPanels = getComponents(); 74 75 for (int i = 0; i < orderPanels.length; i++) { 76 OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem(); 77 78 if (o.equals(item)) { 79 o.setQuantity(o.getQuantity() + 1); 80 81 ((OrderPanel) orderPanels[i]).updateQuantity(); 83 84 updatePrice(Double.valueOf(o.getPrice()).doubleValue()); 85 86 return; 87 } 88 } 89 } 90 91 96 public void updatePrice(double change) { 97 client.updatePrice(new BigDecimal(change)); 98 } 99 100 104 public boolean isEmpty() { 105 return items.isEmpty(); 106 } 107 108 111 public void removeAllItems() { 112 items = new ArrayList(); 113 removeAll(); 114 115 client.setPrice(new BigDecimal(0)); 116 117 validate(); 118 repaint(); 119 } 120 121 125 public void removeFromCart(OrderItem item) { 126 Component[] orderPanels = getComponents(); 128 129 for (int i = 0; i < orderPanels.length; i++) { 130 OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem(); 131 132 if (o.equals(item)) { 133 remove(orderPanels[i]); 134 updatePrice(-o.getTotal()); 135 break; 136 } 137 } 138 139 items.remove(item); 141 142 validate(); 144 repaint(); 145 } 146 147 151 public Object[] getCartContents() { 152 return items.toArray(); 153 } 154 155 159 public List getItemsInCart() { 160 return items; 161 } 162 163 168 public boolean isItemInCart(OrderItem item) { 169 return items.contains(item); 170 } 171 } 172 | Popular Tags |