View Javadoc

1   /*
2    * $Source: /usr/cvsroot/MelatiShopping/src/main/java/org/paneris/melati/shopping/ShoppingTrolleyItem.java,v $
3    * $Revision: 1.12 $
4    *
5    * Copyright (C) 2000 Tim Joyce
6    *
7    * Part of Melati (http://melati.org/ ), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati if you would like 
30   * to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Tim Joyce <timj@paneris.org>
42   *     http://paneris.org/~timj/
43   *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
44   */
45  
46  package org.paneris.melati.shopping;
47  
48  import java.util.Locale;
49  import java.text.NumberFormat;
50  import org.melati.util.InstantiationPropertyException;
51  import org.melati.Melati;
52  
53  /**
54   * Extend this to create your own ShoppingTrolleyItem.
55   */
56  public abstract class ShoppingTrolleyItem  {
57  
58    protected Integer id;
59    protected double quantity;
60    protected double price;
61    protected Locale locale;
62    protected String description;
63    // the shopping trolley to which this item belongs
64    protected ShoppingTrolley trolley;
65    public Melati melati;
66  
67    public static synchronized ShoppingTrolleyItem 
68        newTrolleyItem(MelatiShoppingConfig config)
69        throws InstantiationPropertyException {
70      return config.getShoppingTrolleyItem();
71    }
72  
73   /**
74    * Public Constructor to build a trolley item from some id.
75    **/
76    public void initialise(ShoppingTrolley trolleyIn, Melati melatiIn,
77                           Integer idIn, String descriptionIn, Double priceIn) {
78      this.trolley = trolleyIn;
79      this.id = idIn;
80      this.melati = melatiIn;
81      load(id);
82      if (description != null) this.description = descriptionIn;
83        // set something in the description!
84      if (this.description == null) this.description = id +"";
85      if (priceIn != null) this.price = priceIn.doubleValue();
86    }
87  
88  
89   /**
90    * Load in information about this product given an id.
91    * Perhaps this id represents a poem troid?
92    */
93    protected abstract void load(Integer idIn);
94  
95   /**
96    * The id.
97    */
98    public Integer getId() {
99      return id;
100   }
101 
102  /**
103   * The description.
104   */
105   public String getDescription() {
106     return description;
107   }
108 
109  /**
110   * The quantity on the trolley.
111   */
112   public double getQuantity() {
113     return quantity;
114   }
115 
116  /**
117   * Set the quantity on the trolley.
118   */
119   public void setQuantity(double q) {
120     quantity = q;
121   }
122 
123  /**
124   * Get the quantity on the trolley formatted for display.
125   * If it is an iteger, display it as such.
126   */
127   public String getQuantityDisplay() {
128     try {
129       return (new Double(quantity)).intValue() + "";
130     } catch (NumberFormatException e) {
131       return quantity + "";
132     }
133   }
134 
135  /**
136   * The price of this item.
137   */
138   public double getPrice() {
139     return price;
140   }
141 
142  /**
143   * Set the price of this item.
144   */
145   public void setPrice(double p){
146     price = p;
147   }
148 
149  /**
150   * Display the price of this item.
151   */
152   public String getPriceDisplay(){
153     return displayCurrency(getPrice());
154   }
155 
156  /**
157   * Work out the cost of delivery.
158   */
159   public abstract double getDeliveryValue();
160 
161  /**
162   * Display the cost of delivery.
163   */
164   public String getDeliveryDisplay() {
165     return displayCurrency(getDeliveryValue());
166   }
167 
168  /**
169   * Calculate the value (without delivery).
170   */
171   public double getValue() {
172     return ShoppingTrolley.roundTo2dp(getPrice() * getQuantity());
173   }
174 
175  /**
176   * Display the item value.
177   */
178   public String getValueDisplay() {
179     return displayCurrency(getValue());
180   }
181 
182  /**
183   * Calculate the value (without delivery).
184   */
185   public double getTotalValue() {
186     return getValue() + getDeliveryValue();
187   }
188 
189  /**
190   * Display the item value.
191   */
192   public String getTotalValueDisplay() {
193     return displayCurrency(getTotalValue());
194   }
195 
196  /**
197   * format a number in the locale currency.
198   */
199   public String displayCurrency(double value) {
200     return new String(NumberFormat.getCurrencyInstance(trolley.getLocale())
201                                   .format(value));
202   }
203 
204 }
205