View Javadoc

1   package org.paneris.jammyjoes.shopping;
2   
3   import java.text.NumberFormat;
4   import java.util.Locale;
5   
6   import org.melati.Melati;
7   import org.melati.util.InstantiationPropertyException;
8   
9   public abstract class ShoppingTrolleyItem  {
10  
11    protected Integer id;
12    protected Integer quantity;
13    protected double price;
14    protected Locale locale;
15    protected String description;
16    // the shopping trolley to which this item belongs
17    protected ShoppingTrolley trolley;
18    public Melati melati;
19  
20    public static synchronized ShoppingTrolleyItem newTrolleyItem(MelatiShoppingConfig config)
21     throws InstantiationPropertyException {
22      return config.getShoppingTrolleyItem();
23    }
24  
25    /**
26     * public Constructor to build a trolley item from some id
27    **/
28    public void initialise(ShoppingTrolley trolley, Melati melati,
29                           Integer id, String description, Double price) {
30      this.trolley = trolley;
31      this.id = id;
32      this.melati = melati;
33      load(id);
34      if (description != null) this.description = description;
35        // set something in the description!
36        if (this.description == null) this.description = id +"";
37        if (this.quantity == null) this.quantity = new Integer(0);
38      if (price != null) this.price = price.doubleValue();
39    }
40  
41  
42    /* load in information about this product given an id.
43       perhaps this id represents a poem troid?
44    */
45    protected abstract void load(Integer id);
46  
47    /* the id
48    */
49    public Integer getId() {
50      return id;
51    }
52  
53    /* the description
54    */
55    public String getDescription() {
56      return description;
57    }
58  
59    /* the quantity on the trolley
60    */
61    public Integer getQuantity() {
62      return quantity;
63    }
64  
65    /* set the quantity on the trolley
66    */
67    public void setQuantity(Integer q) {
68      quantity = q;
69    }
70  
71    /* get the quantity on the trolley formatted for display
72    *  if it is an iteger, display it as such
73    */
74    public String getQuantityDisplay() {
75        return quantity + "";
76    }
77  
78    /* the price of this item
79    */
80    public double getPrice() {
81      return price;
82    }
83  
84    /* set the price of this item
85    */
86    public void setPrice(double p){
87      price = p;
88    }
89  
90    /* display the price of this item
91    */
92    public String getPriceDisplay(){
93      return displayCurrency(getPrice());
94    }
95  
96    /* work out the cost of delivery
97    */
98    public abstract double getDeliveryValue();
99  
100   /* display the cost of delivery
101   */
102   public String getDeliveryDisplay() {
103     return displayCurrency(getDeliveryValue());
104   }
105 
106   /* calculate the value (without delivery)
107   */
108   public double getValue() {
109     return ShoppingTrolley.roundTo2dp(getPrice() * getQuantity().intValue());
110   }
111 
112   /* display the item value
113   */
114   public String getValueDisplay() {
115     return displayCurrency(getValue());
116   }
117 
118   /* calculate the value (without delivery)
119   */
120   public double getTotalValue() {
121     return getValue() + getDeliveryValue();
122   }
123 
124   /* display the item value
125   */
126   public String getTotalValueDisplay() {
127     return displayCurrency(getTotalValue());
128   }
129 
130   /* format a number in the locale currency
131   */
132   public String displayCurrency(double value) {
133     return new String(NumberFormat.getCurrencyInstance(trolley.getLocale())
134                                   .format(value));
135   }
136 
137 }
138