KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > ShoppingCartPanel


1 /**
2  * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Gregory Lapouchnian
22  * Patrick Smith
23  * --------------------------------------------------------------------------
24  * $Id: ShoppingCartPanel.java,v 1.2 2005/07/08 14:00:45 glapouch Exp $
25  * --------------------------------------------------------------------------
26  */

27 package olstore.client;
28
29 import java.awt.Component;
30 import java.math.BigDecimal;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.swing.JPanel;
35
36 /**
37  * A panel to display the contents of the shopping cart.
38  */

39 public class ShoppingCartPanel extends JPanel {
40
41     /** The list of items in the shopping cart. */
42     private ArrayList items;
43
44     /** A reference to the main client. */
45     private OlstoreSwingClient client;
46
47     /**
48      * A panel to store and display all the items in the shopping cart.
49      *
50      * @param client the client application to which this cart belongs
51      */

52     public ShoppingCartPanel(OlstoreSwingClient client) {
53         items = new ArrayList();
54         this.client = client;
55     }
56
57     /**
58      * Add a new item to the shopping cart.
59      * @param item the item to be added
60      */

61     public void addNewItemToCart(OrderItem item) {
62         items.add(item);
63         add(new OrderPanel(item));
64         updatePrice(Double.valueOf(item.getPrice()).doubleValue());
65     }
66
67     /**
68      * Increase the quantity of the given item in the shopping cart by one.
69      * @param item the item whose quantity is to be increased
70      */

71     public void increaseQuantity(OrderItem item) {
72         // remove the corresponding OrderPanel from this component
73
Component[] orderPanels = getComponents();
74
75         for (int i = 0; i < orderPanels.length; i++) {
76             OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();
77
78             if (o.equals(item)) {
79                 o.setQuantity(o.getQuantity() + 1);
80
81                 // repaint this panel
82
((OrderPanel) orderPanels[i]).updateQuantity();
83
84                 updatePrice(Double.valueOf(o.getPrice()).doubleValue());
85
86                 return;
87             }
88         }
89     }
90
91     /**
92      * Add change to the total price displayed in the client.
93      * @param change the amount to add to the total (negative number to decrease
94      * the total)
95      */

96     public void updatePrice(double change) {
97         client.updatePrice(new BigDecimal(change));
98     }
99
100     /**
101      * Check whether there are any items in the shopping cart.
102      * @return true is there are no items in the shopping cart, false otherwise
103      */

104     public boolean isEmpty() {
105         return items.isEmpty();
106     }
107
108     /**
109      * Remove all items from the shopping cart.
110      */

111     public void removeAllItems() {
112         items = new ArrayList();
113         removeAll();
114
115         client.setPrice(new BigDecimal(0));
116
117         validate();
118         repaint();
119     }
120
121     /**
122      * Remove the given item from the shopping cart.
123      * @param item the item to be removed from the shopping cart.
124      */

125     public void removeFromCart(OrderItem item) {
126         // remove the corresponding OrderPanel from this component
127
Component[] orderPanels = getComponents();
128
129         for (int i = 0; i < orderPanels.length; i++) {
130             OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();
131
132             if (o.equals(item)) {
133                 remove(orderPanels[i]);
134                 updatePrice(-o.getTotal());
135                 break;
136             }
137         }
138
139         // remove the item from the list
140
items.remove(item);
141
142         // repaint this panel
143
validate();
144         repaint();
145     }
146
147     /**
148      * Get the contents fo the shopping cart.
149      * @return the array of all the OrderItems as an array of Objects
150      */

151     public Object[] getCartContents() {
152         return items.toArray();
153     }
154
155     /**
156      * Get the items in the cart.
157      * @return a list of all the items in the cart
158      */

159     public List getItemsInCart() {
160         return items;
161     }
162
163     /**
164      * Check whether the given order item is already in the cart.
165      * @param item the item that we need to check for
166      * @return true if the item is already in the shopping cart, false otherwise
167      */

168     public boolean isItemInCart(OrderItem item) {
169         return items.contains(item);
170     }
171 }
172
Popular Tags