Source Code Cross Referenced for CartImpl.java in  » J2EE » Enhydra-Demos » golfShop » business » cart » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » golfShop.business.cart 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.