KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > pets > domain > model > pojo > ShoppingCart


1 package org.apache.tapestry.pets.domain.model.pojo;
2
3 import java.io.Serializable JavaDoc;
4 import java.math.BigDecimal JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import johnmammen.betterpetshop.service.PetshopManager;
10 import johnmammen.betterpetshop.service.spring.PetshopManagerImpl;
11
12 import org.apache.tapestry.pets.domain.model.IBasketItem;
13 import org.apache.tapestry.pets.domain.model.IInventory;
14 import org.apache.tapestry.pets.domain.model.IShoppingCart;
15
16 /**
17  * @author Luis Neves
18  *
19  * Shopping cart functions.
20  */

21 public class ShoppingCart implements IShoppingCart, Serializable JavaDoc {
22
23     private ArrayList JavaDoc alShoppingCart = new ArrayList JavaDoc();
24
25     /**
26      * Total number of items in the cart.
27      */

28     public int getCount() {
29         return alShoppingCart.size();
30     }
31
32     /**
33      * Total price of all items.
34      */

35     public double getTotal() {
36         if (alShoppingCart.size() == 0) { return 0.0; }
37
38         double total = 0.0;
39
40         for (Iterator JavaDoc it = alShoppingCart.iterator(); it.hasNext();) {
41             BasketItem bitem = (BasketItem) it.next();
42             total += (bitem.getPrice().multiply(BigDecimal.valueOf(bitem
43                     .getQty()))).doubleValue();
44         }
45
46         return total;
47     }
48
49     /**
50      * Return collection of items
51      *
52      *@returns Collection of items in cart.
53      */

54     public Collection JavaDoc getItems() {
55         return alShoppingCart;
56     }
57
58     /**
59      * Return item at specified index.
60      */

61     public IBasketItem getBasketItem(int index) {
62         return (IBasketItem) alShoppingCart.get(index);
63     }
64
65     /**
66      * Remove all items from cart.
67      */

68     public void clearCart() {
69         alShoppingCart.clear();
70     }
71
72     /**
73      * Add item to cart.
74      *
75      * @param value
76      * New item to add.
77      */

78     public void add(IBasketItem value) {
79         // see if this product is already in the cart
80
for (Iterator JavaDoc it = alShoppingCart.iterator(); it.hasNext();) {
81             IBasketItem bitem = (IBasketItem) it.next();
82
83             if (bitem.getItemID().equals(value.getItemID())) {
84                 // item is already in cart, bump qty up by one
85
// and recalculate in stock value
86
bitem.setQty(bitem.getQty() + 1);
87
88                 return;
89             }
90         }
91
92         // if we've fallen through to here, add to cart
93
alShoppingCart.add(value);
94     }
95
96     /**
97      * Remove item from cart.
98      *
99      * @param index
100      * Index of item to remove.
101      */

102     public void removeAt(int index) {
103         alShoppingCart.remove(index);
104     }
105
106     /**
107      * Remove item from cart.
108      *
109      * @param itemID
110      * Item to remove.
111      */

112     public void remove(String JavaDoc itemID) {
113         // loop through list and look for item
114
for (Iterator JavaDoc it = alShoppingCart.iterator(); it.hasNext();) {
115             IBasketItem bitem = (IBasketItem) it.next();
116
117             if (bitem.getItemID().equals(itemID)) {
118                 // found item, remove it from list
119
alShoppingCart.remove(bitem);
120
121                 return;
122             }
123         }
124     }
125
126     /**
127      * Updates the InStock property for each item in the basket. This requires a
128      * call to the database to get the inventory.
129      */

130     public void updateInStock() {
131         // go through and update the InStock prop of each item
132
IBasketItem item;
133         IInventory inventory;
134
135         PetshopManager petManager = new PetshopManagerImpl();
136
137         IInventory[] arrInventory = petManager.getInventory("csv");
138         for (int i = 0; i < arrInventory.length; i++) {
139             inventory = (IInventory) arrInventory[i];
140
141             for (Iterator JavaDoc it = alShoppingCart.iterator(); it.hasNext();) {
142                 item = (IBasketItem) it.next();
143
144                 if (item.getItemID().equals(inventory.getItemID())) {
145                     item
146                             .setInStock(((inventory.getQty() - item.getQty()) >= 0) ? true
147                                     : false);
148                 }
149             }
150         }
151     }
152
153     private String JavaDoc getBasketItemsCSVList() {
154         int k = 0;
155         StringBuffer JavaDoc csv = new StringBuffer JavaDoc();
156
157         for (Iterator JavaDoc it = alShoppingCart.iterator(); it.hasNext();) {
158             IBasketItem bitem = (IBasketItem) it.next();
159
160             if (k == 0) {
161                 csv.append("\'" + bitem.getItemID().trim() + "\'");
162             } else {
163                 csv.append(", \'" + bitem.getItemID().trim() + "\'");
164             }
165
166             k++;
167         }
168
169         return csv.toString();
170     }
171 }
Popular Tags