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.Text;
|
---|
12 |
|
---|
13 | /// <summary>A simple Money.</summary>
|
---|
14 | class Money: IMoney
|
---|
15 | {
|
---|
16 |
|
---|
17 | private int fAmount;
|
---|
18 | private String fCurrency;
|
---|
19 |
|
---|
20 | /// <summary>Constructs a money from the given amount and
|
---|
21 | /// currency.</summary>
|
---|
22 | public Money(int amount, String currency)
|
---|
23 | {
|
---|
24 | fAmount= amount;
|
---|
25 | fCurrency= currency;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>Adds a money to this money. Forwards the request to
|
---|
29 | /// the AddMoney helper.</summary>
|
---|
30 | public IMoney Add(IMoney m)
|
---|
31 | {
|
---|
32 | return m.AddMoney(this);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public IMoney AddMoney(Money m)
|
---|
36 | {
|
---|
37 | if (m.Currency.Equals(Currency) )
|
---|
38 | return new Money(Amount+m.Amount, Currency);
|
---|
39 | return new MoneyBag(this, m);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public IMoney AddMoneyBag(MoneyBag s)
|
---|
43 | {
|
---|
44 | return s.AddMoney(this);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public int Amount
|
---|
48 | {
|
---|
49 | get { return fAmount; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public String Currency
|
---|
53 | {
|
---|
54 | get { return fCurrency; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override bool Equals(Object anObject)
|
---|
58 | {
|
---|
59 | if (IsZero)
|
---|
60 | if (anObject is IMoney)
|
---|
61 | return ((IMoney)anObject).IsZero;
|
---|
62 | if (anObject is Money)
|
---|
63 | {
|
---|
64 | Money aMoney= (Money)anObject;
|
---|
65 | return aMoney.Currency.Equals(Currency)
|
---|
66 | && Amount == aMoney.Amount;
|
---|
67 | }
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override int GetHashCode()
|
---|
72 | {
|
---|
73 | return fCurrency.GetHashCode()+fAmount;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public bool IsZero
|
---|
77 | {
|
---|
78 | get { return Amount == 0; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public IMoney Multiply(int factor)
|
---|
82 | {
|
---|
83 | return new Money(Amount*factor, Currency);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public IMoney Negate()
|
---|
87 | {
|
---|
88 | return new Money(-Amount, Currency);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public IMoney Subtract(IMoney m)
|
---|
92 | {
|
---|
93 | return Add(m.Negate());
|
---|
94 | }
|
---|
95 |
|
---|
96 | public override String ToString()
|
---|
97 | {
|
---|
98 | StringBuilder buffer = new StringBuilder();
|
---|
99 | buffer.Append("["+Amount+" "+Currency+"]");
|
---|
100 | return buffer.ToString();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|