001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.business.cart;
022:
023: import java.util.Hashtable;
024: import java.util.Enumeration;
025: import golfShop.spec.cart.*;
026: import golfShop.business.item.*;
027:
028: public class CartImpl implements Cart, java.io.Serializable {
029:
030: private static final int maxQuantity = 9999; // set qty limit
031:
032: private CartItemQuery itemQuery; // Use this to find items.
033: private Hashtable itemPairs; // The actual contents.
034: private UndoRedoStack undoRedo; // Used for undo/redo.
035:
036: /**
037: * Constructor. Must pass in the query object so that later it
038: * can convert ObjectIds to CartItems. Start off with an empty cart.
039: *
040: * @param itemQuery A CartItemQuery object for finding CartItems.
041: */
042: public CartImpl(CartItemQuery itemQuery) {
043: this .itemQuery = itemQuery;
044: itemPairs = new Hashtable();
045:
046: /*
047: * Initialize the undo / redo stack. It will keep copies of
048: * the hashtable, and manage the different versions.
049: * This call creates it and sets the current version to the
050: * initially empty shopping cart.
051: * WARNING: This implementation keeps coppies of the cart, and
052: * it's objects. So it's O(N^2). In the future, just
053: * store the diffs.
054: */
055: undoRedo = new UndoRedoStack(itemPairs);
056: }
057:
058: public void addItem(long ObjectId) {
059: addItem(ObjectId, 1);
060: }
061:
062: public void addItem(long ObjectId, int quantity) {
063: if (quantity < 0 || quantity > maxQuantity) {
064: // throw exception
065: }
066: if (containsItem(ObjectId)) {
067: // If the item is already in cart, just change the quantity.
068: int newQty = quantity
069: + ((CartItemPairImpl) (itemPairs.get(new Long(
070: ObjectId)))).getQuantity();
071: setQuantity(ObjectId, newQty);
072: } else if (quantity != 0) {
073: // Otherwise, if quantity is non-zero, add the item to the cart.
074: CartItem item = itemQuery.getItem(ObjectId);
075: CartItemPairImpl itemPair = new CartItemPairImpl(item,
076: quantity);
077: itemPairs.put(new Long(ObjectId), itemPair);
078: }
079: }
080:
081: public void removeItem(long ObjectId) {
082: itemPairs.remove(new Long(ObjectId));
083: }
084:
085: public Enumeration getContents() {
086: return itemPairs.elements();
087: }
088:
089: public void setQuantity(long ObjectId, int quantity) {
090: if (quantity != 0) {
091: CartItemPairImpl itemPair = (CartItemPairImpl) itemPairs
092: .get(new Long(ObjectId));
093: if (itemPair != null)
094: itemPair.setQuantity(quantity);
095: } else {
096: removeItem(ObjectId);
097: }
098: }
099:
100: public int getQuantity(long ObjectId) {
101: CartItemPairImpl itemPair = (CartItemPairImpl) itemPairs
102: .get(new Long(ObjectId));
103: if (itemPair != null)
104: return itemPair.getQuantity();
105: return -1;
106: }
107:
108: public double getTotal() {
109: double total = 0.0;
110: CartItemPairImpl itemPair;
111: Enumeration enumeration = getContents();
112: while (enumeration.hasMoreElements()) {
113: itemPair = (CartItemPairImpl) enumeration.nextElement();
114: int quantity = itemPair.getQuantity();
115: double price = itemPair.getItem().getPrice();
116: total += quantity * price;
117: }
118: return total;
119: }
120:
121: public boolean containsItem(long ObjectId) {
122: return (itemPairs.containsKey(new Long(ObjectId)));
123: }
124:
125: public boolean isEmpty() {
126: return (itemPairs.isEmpty());
127: }
128:
129: public void reset() {
130: /*
131: * Reset the cart to an empy, fresh state.
132: * Just forget about the old contents.
133: */
134: this .itemPairs = new Hashtable();
135: this .undoRedo = new UndoRedoStack(itemPairs);
136: }
137:
138: //------------------------------------------------------------
139: // Support for undo/redo.
140: //------------------------------------------------------------
141:
142: public boolean canUndo() {
143: return undoRedo.canUndo();
144: }
145:
146: public boolean canRedo() {
147: return undoRedo.canRedo();
148: }
149:
150: /*
151: * Call addItem and/or setQuantity and/or removeItem as much as you
152: * want. When you are done, call this function. It will save a copy of
153: * the hash table.
154: * This function should be called after every user interaction with
155: * the cart. For example, adding an item to the cart is one user
156: * interaction. Changing the quantity of every item in the cart, and then
157: * clicking on the "update" button is also one user interaction, although
158: * it results in many calls to setQuantity. After all the calls to
159: * setQuantity, call this function. That way all the modifications will
160: * be undone and redone in one chunk, which is what the user expects.
161: */
162: public void doneModifying() {
163: undoRedo.storeNewVersion(itemPairs);
164: }
165:
166: public void undo() {
167: if (canUndo())
168: itemPairs = undoRedo.undoToPreviousVersion();
169: }
170:
171: public void redo() {
172: if (canRedo())
173: itemPairs = undoRedo.redoToNewerVersion();
174: }
175:
176: /**
177: * Generate information about the cart for debugging.
178: */
179: public String toString() {
180: StringBuffer buf = new StringBuffer();
181: Enumeration enumeration = itemPairs.elements();
182: while (enumeration.hasMoreElements()) {
183: CartItemPairImpl itemPair = (CartItemPairImpl) enumeration
184: .nextElement();
185: buf.append(itemPair.toString());
186: buf.append("\n");
187: }
188: return buf.toString();
189: }
190: }
|