1 | // ****************************************************************
|
---|
2 | // This is free software licensed under the NUnit license. You
|
---|
3 | // may obtain a copy of the license as well as information regarding
|
---|
4 | // copyright ownership at http://nunit.org/?p=license&r=2.4.
|
---|
5 | // ****************************************************************
|
---|
6 |
|
---|
7 | namespace NUnit.Samples.Money
|
---|
8 | {
|
---|
9 |
|
---|
10 | using System;
|
---|
11 | using System.Collections;
|
---|
12 | using System.Text;
|
---|
13 |
|
---|
14 | /// <summary>A MoneyBag defers exchange rate conversions.</summary>
|
---|
15 | /// <remarks>For example adding
|
---|
16 | /// 12 Swiss Francs to 14 US Dollars is represented as a bag
|
---|
17 | /// containing the two Monies 12 CHF and 14 USD. Adding another
|
---|
18 | /// 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
|
---|
19 | /// the deferred exchange rate conversion we can later value a
|
---|
20 | /// MoneyBag with different exchange rates.
|
---|
21 | ///
|
---|
22 | /// A MoneyBag is represented as a list of Monies and provides
|
---|
23 | /// different constructors to create a MoneyBag.</remarks>
|
---|
24 | class MoneyBag: IMoney
|
---|
25 | {
|
---|
26 | private ArrayList fMonies= new ArrayList(5);
|
---|
27 |
|
---|
28 | private MoneyBag()
|
---|
29 | {
|
---|
30 | }
|
---|
31 | public MoneyBag(Money[] bag)
|
---|
32 | {
|
---|
33 | for (int i= 0; i < bag.Length; i++)
|
---|
34 | {
|
---|
35 | if (!bag[i].IsZero)
|
---|
36 | AppendMoney(bag[i]);
|
---|
37 | }
|
---|
38 | }
|
---|
39 | public MoneyBag(Money m1, Money m2)
|
---|
40 | {
|
---|
41 | AppendMoney(m1);
|
---|
42 | AppendMoney(m2);
|
---|
43 | }
|
---|
44 | public MoneyBag(Money m, MoneyBag bag)
|
---|
45 | {
|
---|
46 | AppendMoney(m);
|
---|
47 | AppendBag(bag);
|
---|
48 | }
|
---|
49 | public MoneyBag(MoneyBag m1, MoneyBag m2)
|
---|
50 | {
|
---|
51 | AppendBag(m1);
|
---|
52 | AppendBag(m2);
|
---|
53 | }
|
---|
54 | public IMoney Add(IMoney m)
|
---|
55 | {
|
---|
56 | return m.AddMoneyBag(this);
|
---|
57 | }
|
---|
58 | public IMoney AddMoney(Money m)
|
---|
59 | {
|
---|
60 | return (new MoneyBag(m, this)).Simplify();
|
---|
61 | }
|
---|
62 | public IMoney AddMoneyBag(MoneyBag s)
|
---|
63 | {
|
---|
64 | return (new MoneyBag(s, this)).Simplify();
|
---|
65 | }
|
---|
66 | private void AppendBag(MoneyBag aBag)
|
---|
67 | {
|
---|
68 | foreach (Money m in aBag.fMonies)
|
---|
69 | AppendMoney(m);
|
---|
70 | }
|
---|
71 | private void AppendMoney(Money aMoney)
|
---|
72 | {
|
---|
73 | IMoney old= FindMoney(aMoney.Currency);
|
---|
74 | if (old == null)
|
---|
75 | {
|
---|
76 | fMonies.Add(aMoney);
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | fMonies.Remove(old);
|
---|
80 | IMoney sum= old.Add(aMoney);
|
---|
81 | if (sum.IsZero)
|
---|
82 | return;
|
---|
83 | fMonies.Add(sum);
|
---|
84 | }
|
---|
85 | private bool Contains(Money aMoney)
|
---|
86 | {
|
---|
87 | Money m= FindMoney(aMoney.Currency);
|
---|
88 | return m.Amount == aMoney.Amount;
|
---|
89 | }
|
---|
90 | public override bool Equals(Object anObject)
|
---|
91 | {
|
---|
92 | if (IsZero)
|
---|
93 | if (anObject is IMoney)
|
---|
94 | return ((IMoney)anObject).IsZero;
|
---|
95 |
|
---|
96 | if (anObject is MoneyBag)
|
---|
97 | {
|
---|
98 | MoneyBag aMoneyBag= (MoneyBag)anObject;
|
---|
99 | if (aMoneyBag.fMonies.Count != fMonies.Count)
|
---|
100 | return false;
|
---|
101 |
|
---|
102 | foreach (Money m in fMonies)
|
---|
103 | {
|
---|
104 | if (!aMoneyBag.Contains(m))
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | private Money FindMoney(String currency)
|
---|
112 | {
|
---|
113 | foreach (Money m in fMonies)
|
---|
114 | {
|
---|
115 | if (m.Currency.Equals(currency))
|
---|
116 | return m;
|
---|
117 | }
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 | public override int GetHashCode()
|
---|
121 | {
|
---|
122 | int hash= 0;
|
---|
123 | foreach (Money m in fMonies)
|
---|
124 | {
|
---|
125 | hash^= m.GetHashCode();
|
---|
126 | }
|
---|
127 | return hash;
|
---|
128 | }
|
---|
129 | public bool IsZero
|
---|
130 | {
|
---|
131 | get { return fMonies.Count == 0; }
|
---|
132 | }
|
---|
133 | public IMoney Multiply(int factor)
|
---|
134 | {
|
---|
135 | MoneyBag result= new MoneyBag();
|
---|
136 | if (factor != 0)
|
---|
137 | {
|
---|
138 | foreach (Money m in fMonies)
|
---|
139 | {
|
---|
140 | result.AppendMoney((Money)m.Multiply(factor));
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return result;
|
---|
144 | }
|
---|
145 | public IMoney Negate()
|
---|
146 | {
|
---|
147 | MoneyBag result= new MoneyBag();
|
---|
148 | foreach (Money m in fMonies)
|
---|
149 | {
|
---|
150 | result.AppendMoney((Money)m.Negate());
|
---|
151 | }
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 | private IMoney Simplify()
|
---|
155 | {
|
---|
156 | if (fMonies.Count == 1)
|
---|
157 | return (IMoney)fMonies[0];
|
---|
158 | return this;
|
---|
159 | }
|
---|
160 | public IMoney Subtract(IMoney m)
|
---|
161 | {
|
---|
162 | return Add(m.Negate());
|
---|
163 | }
|
---|
164 | public override String ToString()
|
---|
165 | {
|
---|
166 | StringBuilder buffer = new StringBuilder();
|
---|
167 | buffer.Append("{");
|
---|
168 | foreach (Money m in fMonies)
|
---|
169 | buffer.Append(m);
|
---|
170 | buffer.Append("}");
|
---|
171 | return buffer.ToString();
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|