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
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
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
43
44
45 protected abstract void load(Integer id);
46
47
48
49 public Integer getId() {
50 return id;
51 }
52
53
54
55 public String getDescription() {
56 return description;
57 }
58
59
60
61 public Integer getQuantity() {
62 return quantity;
63 }
64
65
66
67 public void setQuantity(Integer q) {
68 quantity = q;
69 }
70
71
72
73
74 public String getQuantityDisplay() {
75 return quantity + "";
76 }
77
78
79
80 public double getPrice() {
81 return price;
82 }
83
84
85
86 public void setPrice(double p){
87 price = p;
88 }
89
90
91
92 public String getPriceDisplay(){
93 return displayCurrency(getPrice());
94 }
95
96
97
98 public abstract double getDeliveryValue();
99
100
101
102 public String getDeliveryDisplay() {
103 return displayCurrency(getDeliveryValue());
104 }
105
106
107
108 public double getValue() {
109 return ShoppingTrolley.roundTo2dp(getPrice() * getQuantity().intValue());
110 }
111
112
113
114 public String getValueDisplay() {
115 return displayCurrency(getValue());
116 }
117
118
119
120 public double getTotalValue() {
121 return getValue() + getDeliveryValue();
122 }
123
124
125
126 public String getTotalValueDisplay() {
127 return displayCurrency(getTotalValue());
128 }
129
130
131
132 public String displayCurrency(double value) {
133 return new String(NumberFormat.getCurrencyInstance(trolley.getLocale())
134 .format(value));
135 }
136
137 }
138