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 | Option Explicit On
|
---|
8 |
|
---|
9 | Namespace NUnit.Samples
|
---|
10 |
|
---|
11 | ' A Simple Money.
|
---|
12 | Public Class Money
|
---|
13 | Implements IMoney
|
---|
14 |
|
---|
15 | Private fAmount As Int32
|
---|
16 | Private fCurrency As String
|
---|
17 |
|
---|
18 | ' Constructs a money from a given amount and currency.
|
---|
19 | Public Sub New(ByVal amount As Int32, ByVal currency As String)
|
---|
20 | Me.fAmount = amount
|
---|
21 | Me.fCurrency = currency
|
---|
22 | End Sub
|
---|
23 |
|
---|
24 |
|
---|
25 | ' Adds a money to this money. Forwards the request
|
---|
26 | ' to the AddMoney helper.
|
---|
27 | Public Overloads Function Add(ByVal m As IMoney) As IMoney Implements IMoney.Add
|
---|
28 | Return m.AddMoney(Me)
|
---|
29 | End Function
|
---|
30 |
|
---|
31 | Public Overloads Function AddMoney(ByVal m As Money) As IMoney Implements IMoney.AddMoney
|
---|
32 | If m.Currency.Equals(Currency) Then
|
---|
33 | Return New Money(Amount + m.Amount, Currency)
|
---|
34 | End If
|
---|
35 |
|
---|
36 | Return New MoneyBag(Me, m)
|
---|
37 | End Function
|
---|
38 |
|
---|
39 | Public Function AddMoneyBag(ByVal s As MoneyBag) As IMoney Implements IMoney.AddMoneyBag
|
---|
40 | Return s.AddMoney(Me)
|
---|
41 | End Function
|
---|
42 |
|
---|
43 | Public ReadOnly Property Amount() As Integer
|
---|
44 | Get
|
---|
45 | Return fAmount
|
---|
46 | End Get
|
---|
47 | End Property
|
---|
48 |
|
---|
49 | Public ReadOnly Property Currency() As String
|
---|
50 | Get
|
---|
51 | Return fCurrency
|
---|
52 | End Get
|
---|
53 | End Property
|
---|
54 |
|
---|
55 | Public Overloads Overrides Function Equals(ByVal anObject As Object) As Boolean
|
---|
56 | If IsZero And TypeOf anObject Is IMoney Then
|
---|
57 | Dim aMoney As IMoney = anObject
|
---|
58 | Return aMoney.IsZero
|
---|
59 | End If
|
---|
60 |
|
---|
61 | If TypeOf anObject Is Money Then
|
---|
62 | Dim aMoney As Money = anObject
|
---|
63 | If (IsZero) Then
|
---|
64 | Return aMoney.IsZero
|
---|
65 | End If
|
---|
66 |
|
---|
67 | Return Currency.Equals(aMoney.Currency) And Amount.Equals(aMoney.Amount)
|
---|
68 | End If
|
---|
69 |
|
---|
70 | Return False
|
---|
71 | End Function
|
---|
72 |
|
---|
73 | Public Overrides Function GetHashCode() As Int32
|
---|
74 | Return fCurrency.GetHashCode() + fAmount
|
---|
75 | End Function
|
---|
76 |
|
---|
77 | Public ReadOnly Property IsZero() As Boolean Implements IMoney.IsZero
|
---|
78 | Get
|
---|
79 | Return Amount.Equals(0)
|
---|
80 | End Get
|
---|
81 | End Property
|
---|
82 |
|
---|
83 | Public Function Multiply(ByVal factor As Integer) As IMoney Implements IMoney.Multiply
|
---|
84 |
|
---|
85 | Return New Money(Amount * factor, Currency)
|
---|
86 |
|
---|
87 | End Function
|
---|
88 |
|
---|
89 | Public Function Negate() As IMoney Implements IMoney.Negate
|
---|
90 |
|
---|
91 | Return New Money(-Amount, Currency)
|
---|
92 |
|
---|
93 | End Function
|
---|
94 |
|
---|
95 | Public Function Subtract(ByVal m As IMoney) As IMoney Implements IMoney.Subtract
|
---|
96 |
|
---|
97 | Return Add(m.Negate())
|
---|
98 |
|
---|
99 | End Function
|
---|
100 |
|
---|
101 | Public Overrides Function ToString() As String
|
---|
102 |
|
---|
103 | Return String.Format("[{0} {1}]", Amount, Currency)
|
---|
104 |
|
---|
105 | End Function
|
---|
106 |
|
---|
107 | End Class
|
---|
108 |
|
---|
109 | End Namespace
|
---|