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