01: /*
02: * JFox - The most lightweight Java EE Application Server!
03: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
04: *
05: * JFox is licenced and re-distributable under GNU LGPL.
06: */
07: package org.jfox.petstore.domain;
08:
09: import java.io.Serializable;
10: import java.util.Collection;
11: import java.util.Collections;
12: import java.util.Map;
13: import java.util.TreeMap;
14:
15: import org.jfox.petstore.entity.Item;
16:
17: /**
18: * Cart is not a Entity Object, just a logic domain Object contain CartItem
19: */
20: public class Cart implements Serializable {
21:
22: // itemId => CartItem
23: private final Map<String, CartItem> itemMap = Collections
24: .synchronizedMap(new TreeMap<String, CartItem>());
25:
26: public Collection<CartItem> getCartItemList() {
27: return itemMap.values();
28: }
29:
30: public CartItem getCartItem(String itemId) {
31: return itemMap.get(itemId);
32: }
33:
34: public int getNumberOfItems() {
35: return itemMap.size();
36: }
37:
38: public boolean containsItemId(String itemId) {
39: return itemMap.containsKey(itemId);
40: }
41:
42: public void addItem(Item item, boolean isInStock) {
43: CartItem cartItem = itemMap.get(item.getItemId());
44: if (cartItem == null) {
45: cartItem = new CartItem();
46: cartItem.setItem(item);
47: cartItem.setQuantity(0);
48: cartItem.setInStock(isInStock);
49: itemMap.put(item.getItemId(), cartItem);
50: }
51: cartItem.incrementQuantity();
52: }
53:
54: public Item removeItemById(String itemId) {
55: CartItem cartItem = itemMap.remove(itemId);
56: if (cartItem == null) {
57: return null;
58: } else {
59: return cartItem.getItem();
60: }
61: }
62:
63: public void incrementQuantityByItemId(String itemId) {
64: CartItem cartItem = itemMap.get(itemId);
65: cartItem.incrementQuantity();
66: }
67:
68: public void setQuantityByItemId(String itemId, int quantity) {
69: CartItem cartItem = itemMap.get(itemId);
70: cartItem.setQuantity(quantity);
71: }
72:
73: public double getSubTotal() {
74: double subTotal = 0;
75: for (CartItem cartItem : itemMap.values()) {
76: Item item = cartItem.getItem();
77: double listPrice = item.getListPrice();
78: int quantity = cartItem.getQuantity();
79: subTotal += listPrice * quantity;
80: }
81: return subTotal;
82: }
83: }
|