KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > business > cart > CartImpl


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.business.cart;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import golfShop.spec.cart.*;
26 import golfShop.business.item.*;
27
28 public class CartImpl implements Cart,java.io.Serializable JavaDoc {
29
30     private static final int maxQuantity = 9999; // set qty limit
31

32     private CartItemQuery itemQuery; // Use this to find items.
33
private Hashtable JavaDoc itemPairs; // The actual contents.
34
private UndoRedoStack undoRedo; // Used for undo/redo.
35

36     /**
37      * Constructor. Must pass in the query object so that later it
38      * can convert ObjectIds to CartItems. Start off with an empty cart.
39      *
40      * @param itemQuery A CartItemQuery object for finding CartItems.
41      */

42     public CartImpl (CartItemQuery itemQuery) {
43         this.itemQuery = itemQuery;
44         itemPairs = new Hashtable JavaDoc();
45
46         /*
47          * Initialize the undo / redo stack. It will keep copies of
48          * the hashtable, and manage the different versions.
49          * This call creates it and sets the current version to the
50          * initially empty shopping cart.
51          * WARNING: This implementation keeps coppies of the cart, and
52          * it's objects. So it's O(N^2). In the future, just
53          * store the diffs.
54          */

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             // throw exception
65
}
66         if (containsItem(ObjectId)) {
67             // If the item is already in cart, just change the quantity.
68
int newQty = quantity +
69                         ((CartItemPairImpl)(itemPairs.get(new Long JavaDoc(ObjectId)))).getQuantity();
70             setQuantity(ObjectId, newQty);
71         } else if (quantity != 0) {
72             // Otherwise, if quantity is non-zero, add the item to the cart.
73
CartItem item = itemQuery.getItem(ObjectId);
74             CartItemPairImpl itemPair = new CartItemPairImpl(item, quantity);
75             itemPairs.put(new Long JavaDoc(ObjectId), itemPair);
76         }
77     }
78
79     public void removeItem(long ObjectId) {
80         itemPairs.remove(new Long JavaDoc(ObjectId));
81     }
82
83     public Enumeration JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc 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 JavaDoc(ObjectId)));
118     }
119
120     public boolean isEmpty() {
121         return (itemPairs.isEmpty());
122     }
123
124     public void reset() {
125         /*
126          * Reset the cart to an empy, fresh state.
127          * Just forget about the old contents.
128         */

129         this.itemPairs = new Hashtable JavaDoc();
130         this.undoRedo = new UndoRedoStack(itemPairs);
131     }
132
133     
134     //------------------------------------------------------------
135
// Support for undo/redo.
136
//------------------------------------------------------------
137

138     public boolean canUndo () {
139         return undoRedo.canUndo();
140     }
141
142     public boolean canRedo () {
143         return undoRedo.canRedo();
144     }
145
146     /*
147      * Call addItem and/or setQuantity and/or removeItem as much as you
148      * want. When you are done, call this function. It will save a copy of
149      * the hash table.
150      * This function should be called after every user interaction with
151      * the cart. For example, adding an item to the cart is one user
152      * interaction. Changing the quantity of every item in the cart, and then
153      * clicking on the "update" button is also one user interaction, although
154      * it results in many calls to setQuantity. After all the calls to
155      * setQuantity, call this function. That way all the modifications will
156      * be undone and redone in one chunk, which is what the user expects.
157      */

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     /**
173      * Generate information about the cart for debugging.
174      */

175     public String JavaDoc toString() {
176         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177         Enumeration JavaDoc 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