1
|
|
package com.instantiations.example.money;
|
2
|
|
|
3
|
|
import java.util.*;
|
4
|
|
|
5
|
|
/**
|
6
|
|
* A MoneyBag defers exchange rate conversions. For example adding
|
7
|
|
* 12 Swiss Francs to 14 US Dollars is represented as a bag
|
8
|
|
* containing the two Monies 12 CHF and 14 USD. Adding another
|
9
|
|
* 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
|
10
|
|
* the deferred exchange rate conversion we can later value a
|
11
|
|
* MoneyBag with different exchange rates.
|
12
|
|
*
|
13
|
|
* A MoneyBag is represented as a list of Monies and provides
|
14
|
|
* different constructors to create a MoneyBag.
|
15
|
|
*/
|
16
|
|
class MoneyBag implements IMoney {
|
17
|
|
private Vector fMonies= new Vector(5);
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
static IMoney create(IMoney m1, IMoney m2) {
|
22
|
|
MoneyBag result= new MoneyBag();
|
23
|
|
|
24
|
|
m1.appendTo(result);
|
25
|
|
m2.appendTo(result);
|
26
|
|
|
27
|
|
/* $if eclipse.version < 3.5 $ */
|
28
|
|
System.out.println("foo");
|
29
|
|
/* $endif $ */
|
30
|
|
|
31
|
|
/* $if eclipse.version == 3.5 $
|
32
|
|
System.out.println("foo");
|
33
|
|
$else $ */
|
34
|
|
System.out.println("bar");
|
35
|
|
/* $endif $ */
|
36
|
|
|
37
|
|
/* $if eclipse.version != 3.5 $ */
|
38
|
|
System.out.println("foo");
|
39
|
|
/* $else $
|
40
|
|
System.out.println("bar");
|
41
|
|
$endif $ */
|
42
|
|
|
43
|
|
return result.simplify();
|
44
|
|
}
|
45
|
|
public IMoney add(IMoney m) {
|
46
|
|
return m.addMoneyBag(this);
|
47
|
|
}
|
48
|
|
public IMoney addMoney(Money m) {
|
49
|
|
return MoneyBag.create(m, this);
|
50
|
|
}
|
51
|
|
public IMoney addMoneyBag(MoneyBag s) {
|
52
|
|
return MoneyBag.create(s, this);
|
53
|
|
}
|
54
|
|
void appendBag(MoneyBag aBag) {
|
55
|
|
for (Enumeration e= aBag.fMonies.elements(); e.hasMoreElements(); )
|
56
|
|
appendMoney((Money)e.nextElement());
|
57
|
|
}
|
58
|
|
void appendMoney(Money aMoney) {
|
59
|
|
if (aMoney.isZero()) return;
|
60
|
|
IMoney old= findMoney(aMoney.currency());
|
61
|
|
if (old == null) {
|
62
|
|
fMonies.addElement(aMoney);
|
63
|
|
return;
|
64
|
|
}
|
65
|
|
fMonies.removeElement(old);
|
66
|
|
IMoney sum= old.add(aMoney);
|
67
|
|
if (sum.isZero())
|
68
|
|
return;
|
69
|
|
fMonies.addElement(sum);
|
70
|
|
}
|
71
|
 |
public boolean equals(Object anObject) {
|
72
|
|
if (isZero())
|
73
|
|
if (anObject instanceof IMoney)
|
74
|
|
return ((IMoney)anObject).isZero();
|
75
|
|
|
76
|
|
if (anObject instanceof MoneyBag) {
|
77
|
|
MoneyBag aMoneyBag= (MoneyBag)anObject;
|
78
|
|
if (aMoneyBag.fMonies.size() != fMonies.size())
|
79
|
|
return false;
|
80
|
|
|
81
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
|
82
|
|
Money m= (Money) e.nextElement();
|
83
|
|
if (!aMoneyBag.contains(m))
|
84
|
|
return false;
|
85
|
|
}
|
86
|
|
return true;
|
87
|
|
}
|
88
|
|
return false;
|
89
|
|
}
|
90
|
|
private Money findMoney(String currency) {
|
91
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
|
92
|
|
Money m= (Money) e.nextElement();
|
93
|
|
if (m.currency().equals(currency))
|
94
|
|
return m;
|
95
|
|
}
|
96
|
|
return null;
|
97
|
|
}
|
98
|
|
private boolean contains(Money m) {
|
99
|
|
Money found= findMoney(m.currency());
|
100
|
|
if (found == null) return false;
|
101
|
|
return found.amount() == m.amount();
|
102
|
|
}
|
103
|
|
public int hashCode() {
|
104
|
|
int hash= 0;
|
105
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
|
106
|
|
Object m= e.nextElement();
|
107
|
|
hash^= m.hashCode();
|
108
|
|
}
|
109
|
|
return hash;
|
110
|
|
}
|
111
|
|
public boolean isZero() {
|
112
|
|
return fMonies.size() == 0;
|
113
|
|
}
|
114
|
|
public IMoney multiply(int factor) {
|
115
|
|
MoneyBag result= new MoneyBag();
|
116
|
|
if (factor != 0) {
|
117
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
|
118
|
|
Money m= (Money) e.nextElement();
|
119
|
|
result.appendMoney((Money)m.multiply(factor));
|
120
|
|
}
|
121
|
|
}
|
122
|
|
return result;
|
123
|
|
}
|
124
|
|
public IMoney negate() {
|
125
|
|
MoneyBag result= new MoneyBag();
|
126
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
|
127
|
|
Money m= (Money) e.nextElement();
|
128
|
|
result.appendMoney((Money)m.negate());
|
129
|
|
}
|
130
|
|
return result;
|
131
|
|
}
|
132
|
|
private IMoney simplify() {
|
133
|
|
if (fMonies.size() == 1)
|
134
|
|
return (IMoney)fMonies.elements().nextElement();
|
135
|
|
return this;
|
136
|
|
}
|
137
|
|
public IMoney subtract(IMoney m) {
|
138
|
|
return add(m.negate());
|
139
|
|
}
|
140
|
|
public String toString() {
|
141
|
|
StringBuffer buffer = new StringBuffer();
|
142
|
|
buffer.append('{');
|
143
|
|
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); )
|
144
|
|
buffer.append(e.nextElement());
|
145
|
 |
buffer.append("}");
|
146
|
|
return buffer.toString();
|
147
|
|
}
|
148
|
|
public void appendTo(MoneyBag m) {
|
149
|
|
m.appendBag(this);
|
150
|
|
}
|
151
|
|
}
|