1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
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
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
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
84 if (this.description == null) this.description = id +"";
85 if (priceIn != null) this.price = priceIn.doubleValue();
86 }
87
88
89
90
91
92
93 protected abstract void load(Integer idIn);
94
95
96
97
98 public Integer getId() {
99 return id;
100 }
101
102
103
104
105 public String getDescription() {
106 return description;
107 }
108
109
110
111
112 public double getQuantity() {
113 return quantity;
114 }
115
116
117
118
119 public void setQuantity(double q) {
120 quantity = q;
121 }
122
123
124
125
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
137
138 public double getPrice() {
139 return price;
140 }
141
142
143
144
145 public void setPrice(double p){
146 price = p;
147 }
148
149
150
151
152 public String getPriceDisplay(){
153 return displayCurrency(getPrice());
154 }
155
156
157
158
159 public abstract double getDeliveryValue();
160
161
162
163
164 public String getDeliveryDisplay() {
165 return displayCurrency(getDeliveryValue());
166 }
167
168
169
170
171 public double getValue() {
172 return ShoppingTrolley.roundTo2dp(getPrice() * getQuantity());
173 }
174
175
176
177
178 public String getValueDisplay() {
179 return displayCurrency(getValue());
180 }
181
182
183
184
185 public double getTotalValue() {
186 return getValue() + getDeliveryValue();
187 }
188
189
190
191
192 public String getTotalValueDisplay() {
193 return displayCurrency(getTotalValue());
194 }
195
196
197
198
199 public String displayCurrency(double value) {
200 return new String(NumberFormat.getCurrencyInstance(trolley.getLocale())
201 .format(value));
202 }
203
204 }
205