KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > dragDrop > CartItem


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.dragDrop;
35
36 import com.icesoft.faces.context.effects.Effect;
37 import com.icesoft.faces.context.effects.Highlight;
38
39 import javax.faces.event.ActionEvent;
40
41 /**
42  * <p>The CartItem class represents a single type of item for sale in the Drag
43  * and Drop store demo. The item object maintains available and purchased
44  * quantities; these are increased and decreased as they are added or removed
45  * from the store.</p>
46  */

47 public class CartItem {
48     // item attributes
49
private String JavaDoc key = null;
50     private String JavaDoc name = "Unknown";
51     private double price = 0.0;
52     private boolean isPurchased = false;
53     private int quantity = 0;
54     private int purchasedQuantity = 0;
55
56     // images for drag and drop
57
private String JavaDoc image;
58     private String JavaDoc imageSmall;
59
60     // types of items
61
public static final String JavaDoc ICE_SAILER = "Ice Sailer";
62     public static final String JavaDoc ICE_CASTLE = "Ice Castle";
63     public static final String JavaDoc ICE_BERG = "Ice Berg";
64     public static final String JavaDoc ICE_BREAKER = "Ice Breaker";
65
66     // store reference
67
private CartBean cartBean;
68
69     // effect used when an item is added to the cart
70
private Effect effect = new Highlight();
71
72     public CartItem() {
73     }
74
75     /**
76      * Instatiates a new store item.
77      *
78      * @param cartBean reference to the store object
79      * @param name the name of the item
80      * @param price the price of the item
81      * @param quantity the number of items available for purchase
82      */

83     public CartItem(CartBean cartBean, String JavaDoc name, double price,
84                     int quantity) {
85         this.cartBean = cartBean;
86         this.name = name;
87         this.price = price;
88         this.quantity = quantity;
89
90         // large image for the store, small image for the cart
91
image = "./images/dragDrop/" + name.toLowerCase().replace(' ', '_');
92         imageSmall = image + "_small.jpg";
93         image += ".jpg";
94     }
95
96     /* Getters */
97     public String JavaDoc getKey() {
98         return (key);
99     }
100
101     public String JavaDoc getName() {
102         return (name);
103     }
104
105     public String JavaDoc getImage() {
106         return (image);
107     }
108
109     public String JavaDoc getImageSmall() {
110         return (imageSmall);
111     }
112
113     public double getPrice() {
114         return (price);
115     }
116
117     public boolean isPurchased() {
118         return (isPurchased);
119     }
120
121     public int getQuantity() {
122         return (quantity);
123     }
124
125     public int getPurchasedQuantity() {
126         return (purchasedQuantity);
127     }
128
129     public double getProductTotalDouble() {
130         return purchasedQuantity * price;
131     }
132
133     public String JavaDoc getProductTotal() {
134         return StoreTable.CURRENCY_FORMAT.format(getProductTotalDouble());
135     }
136
137     public Effect getEffect() {
138         return effect;
139     }
140
141     /* Setters */
142     public void setKey(String JavaDoc key) {
143         this.key = key;
144     }
145
146     public void setName(String JavaDoc name) {
147         this.name = name;
148     }
149
150     public void setImage(String JavaDoc image) {
151         this.image = image;
152     }
153
154     public void setPrice(double price) {
155         this.price = price;
156     }
157
158     public void setPurchased(boolean isPurchased) {
159         this.isPurchased = isPurchased;
160     }
161
162     public void setQuantity(int quantity) {
163         this.quantity = quantity;
164     }
165
166     public void setPurchasedQuantity(int purchasedQuantity) {
167         this.purchasedQuantity = purchasedQuantity;
168     }
169
170     public void setEffect(Effect effect) {
171         this.effect = effect;
172     }
173
174     /**
175      * 'Moves' one of the items from the cart to the store. This is achieved by
176      * incrementing and decrementing the quantity and purchasedQuantity values,
177      * respectively. Used for "Return" button on cart list.
178      *
179      * @param e the event that fired
180      */

181     public void returnOne(ActionEvent e) {
182         cartBean.removeFromPurchases(this);
183     }
184
185     /**
186      * Convenience method to decrease the quantity of this item by 1.
187      */

188     public void decreaseQuantity() {
189         if (quantity >= 1) {
190             quantity--;
191             purchasedQuantity++;
192         }
193     }
194
195     /**
196      * Convenience method to increase the quantity of this item by 1.
197      */

198     public void increaseQuantity() {
199         quantity++;
200         purchasedQuantity--;
201     }
202
203     /**
204      * Method to return this item price in a currency formatted manner: Example:
205      * "$4.25".
206      *
207      * @return String formatted price
208      */

209     public String JavaDoc getPriceFormatted() {
210         return (StoreTable.CURRENCY_FORMAT.format(price));
211     }
212 }
213
Popular Tags