1 20 21 package golfShop.business.cart; 22 23 import java.util.Hashtable ; 24 import java.util.Enumeration ; 25 import golfShop.spec.cart.*; 26 import golfShop.business.item.*; 27 28 public class CartImpl implements Cart,java.io.Serializable { 29 30 private static final int maxQuantity = 9999; 32 private CartItemQuery itemQuery; private Hashtable itemPairs; private UndoRedoStack undoRedo; 36 42 public CartImpl (CartItemQuery itemQuery) { 43 this.itemQuery = itemQuery; 44 itemPairs = new Hashtable (); 45 46 55 undoRedo = new UndoRedoStack(itemPairs); 56 } 57 58 public void addItem(long ObjectId) { 59 addItem(ObjectId, 1); 60 } 61 62 public void addItem(long ObjectId, int quantity) { 63 if (quantity < 0 || quantity > maxQuantity) { 64 } 66 if (containsItem(ObjectId)) { 67 int newQty = quantity + 69 ((CartItemPairImpl)(itemPairs.get(new Long (ObjectId)))).getQuantity(); 70 setQuantity(ObjectId, newQty); 71 } else if (quantity != 0) { 72 CartItem item = itemQuery.getItem(ObjectId); 74 CartItemPairImpl itemPair = new CartItemPairImpl(item, quantity); 75 itemPairs.put(new Long (ObjectId), itemPair); 76 } 77 } 78 79 public void removeItem(long ObjectId) { 80 itemPairs.remove(new Long (ObjectId)); 81 } 82 83 public Enumeration getContents() { 84 return itemPairs.elements(); 85 } 86 87 public void setQuantity(long ObjectId, int quantity) { 88 if (quantity != 0) { 89 CartItemPairImpl itemPair = (CartItemPairImpl) itemPairs.get(new Long (ObjectId)); 90 if (itemPair != null) 91 itemPair.setQuantity(quantity); 92 } else { 93 removeItem(ObjectId); 94 } 95 } 96 97 public int getQuantity(long ObjectId) { 98 CartItemPairImpl itemPair = (CartItemPairImpl) itemPairs.get(new Long (ObjectId)); 99 if (itemPair != null) return itemPair.getQuantity(); 100 return -1; 101 } 102 103 public double getTotal() { 104 double total = 0.0; 105 CartItemPairImpl itemPair; 106 Enumeration enumeration = getContents(); 107 while (enumeration.hasMoreElements()) { 108 itemPair = (CartItemPairImpl) enumeration.nextElement(); 109 int quantity = itemPair.getQuantity(); 110 double price = itemPair.getItem().getPrice(); 111 total += quantity * price; 112 } 113 return total; 114 } 115 116 public boolean containsItem(long ObjectId) { 117 return (itemPairs.containsKey(new Long (ObjectId))); 118 } 119 120 public boolean isEmpty() { 121 return (itemPairs.isEmpty()); 122 } 123 124 public void reset() { 125 129 this.itemPairs = new Hashtable (); 130 this.undoRedo = new UndoRedoStack(itemPairs); 131 } 132 133 134 138 public boolean canUndo () { 139 return undoRedo.canUndo(); 140 } 141 142 public boolean canRedo () { 143 return undoRedo.canRedo(); 144 } 145 146 158 public void doneModifying () { 159 undoRedo.storeNewVersion(itemPairs); 160 } 161 162 public void undo () { 163 if (canUndo()) 164 itemPairs = undoRedo.undoToPreviousVersion(); 165 } 166 167 public void redo () { 168 if (canRedo()) 169 itemPairs = undoRedo.redoToNewerVersion(); 170 } 171 172 175 public String toString() { 176 StringBuffer buf = new StringBuffer (); 177 Enumeration enumeration = itemPairs.elements(); 178 while (enumeration.hasMoreElements()) { 179 CartItemPairImpl itemPair = (CartItemPairImpl)enumeration.nextElement(); 180 buf.append(itemPair.toString()); 181 buf.append("\n"); 182 } 183 return buf.toString(); 184 } 185 } 186 | Popular Tags |