1 //package junit.samples.money;
2 package com.cenqua.samples.money;
7 * A MoneyBag defers exchange rate conversions. For example adding
8 * 12 Swiss Francs to 14 US Dollars is represented as a bag
9 * containing the two Monies 12 CHF and 14 USD. Adding another
10 * 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
11 * the deferred exchange rate conversion we can later value a
12 * MoneyBag with different exchange rates.
14 * A MoneyBag is represented as a list of Monies and provides
15 * different constructors to create a MoneyBag.
17 class MoneyBag implements IMoney {
18 private Vector fMonies = new Vector(5);
20 static IMoney create(IMoney m1, IMoney m2) {
21 MoneyBag result = new MoneyBag();
24 return result.simplify();
27 public IMoney add(IMoney m) {
28 return m.addMoneyBag(this);
31 public IMoney addMoney(Money m) {
32 return MoneyBag.create(m, this);
35 public IMoney addMoneyBag(MoneyBag s) {
36 return MoneyBag.create(s, this);
39 void appendBag(MoneyBag aBag) {
40 for (Enumeration e = aBag.fMonies.elements(); e.hasMoreElements();)
41 appendMoney((Money) e.nextElement());
44 void appendMoney(Money aMoney) {
45 if (aMoney.isZero()) return;
46 IMoney old = findMoney(aMoney.currency());
48 fMonies.addElement(aMoney);
51 fMonies.removeElement(old);
52 IMoney sum = old.add(aMoney);
55 fMonies.addElement(sum);
58 public boolean equals(Object anObject) {
60 if (anObject instanceof IMoney)
61 return ((IMoney) anObject).isZero();
63 if (anObject instanceof MoneyBag) {
64 MoneyBag aMoneyBag = (MoneyBag) anObject;
65 if (aMoneyBag.fMonies.size() != fMonies.size())
68 for (Enumeration e = fMonies.elements(); e.hasMoreElements();) {
69 Money m = (Money) e.nextElement();
70 if (!aMoneyBag.contains(m))
78 private Money findMoney(String currency) {
79 for (Enumeration e = fMonies.elements(); e.hasMoreElements();) {
80 Money m = (Money) e.nextElement();
81 if (m.currency().equals(currency))
87 private boolean contains(Money m) {
88 Money found = findMoney(m.currency());
89 if (found == null) return false;
90 return found.amount() == m.amount();
93 public int hashCode() {
95 for (Enumeration e = fMonies.elements(); e.hasMoreElements();) {
96 Object m = e.nextElement();
102 public boolean isZero() {
103 return fMonies.size() == 0;
106 public IMoney multiply(int factor) {
107 MoneyBag result = new MoneyBag();
109 for (Enumeration e = fMonies.elements(); e.hasMoreElements();) {
110 Money m = (Money) e.nextElement();
111 result.appendMoney((Money) m.multiply(factor));
117 public IMoney negate() {
118 MoneyBag result = new MoneyBag();
119 for (Enumeration e = fMonies.elements(); e.hasMoreElements();) {
120 Money m = (Money) e.nextElement();
121 result.appendMoney((Money) m.negate());
126 private IMoney simplify() {
127 if (fMonies.size() == 1)
128 return (IMoney) fMonies.elements().nextElement();
132 public IMoney subtract(IMoney m) {
133 return add(m.negate());
136 public String toString() {
137 StringBuffer buffer = new StringBuffer();
139 for (Enumeration e = fMonies.elements(); e.hasMoreElements();)
140 buffer.append(e.nextElement());
142 return buffer.toString();
145 public void appendTo(MoneyBag m) {