-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMoney.cs
331 lines (292 loc) · 9.89 KB
/
Money.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Havit.Diagnostics.Contracts;
using System;
using System.Collections.Generic;
using System.Text;
namespace Havit.Business;
/// <summary>
/// Třída reprezentující peněžní částky s měnou.
/// </summary>
/// <typeparam name="TCurrency">Typ měny pro třídu Money.</typeparam>
[Serializable]
public class Money<TCurrency>
where TCurrency : class
{
/// <summary>
/// Částka v jednotkách měny. Udává se hodnota v základní
/// jednotce a zlomky měny (např. haléře) se zadávají za desetinou tečkou (např.
/// 57.30).
/// </summary>
public decimal? Amount
{
get
{
return _amount;
}
private set
{
_amount = value;
}
}
private decimal? _amount;
/// <summary>
/// Měna částky.
/// </summary>
public TCurrency Currency
{
get
{
return _currency;
}
private set
{
_currency = value;
}
}
private TCurrency _currency;
/// <summary>
/// Inicializuje třídu money s prázdními hodnotami (Amount i Currency jsou null).
/// </summary>
public Money() : this(null, null)
{
}
/// <summary>
/// Inicializuje třídu money zadanými hodnotami.
/// </summary>
public Money(decimal? amount, TCurrency currency)
{
this._amount = amount;
this._currency = currency;
}
/// <summary>
/// Vrátí true, pokud se hodnoty aktuální instance rovnají hodnotám instance v parametru. Porovnává se částka a měna.
/// </summary>
public virtual bool Equals(Money<TCurrency> money)
{
if ((money == null) || (this.GetType() != money.GetType()))
{
return false;
}
return (this.Amount == money.Amount) && (this.Currency == money.Currency);
}
/// <summary>
/// Vrátí true, pokud se hodnoty aktuální instance rovnají hodnotám instance v parametru. Porovnává se částka a měna.
/// </summary>
public override bool Equals(object obj)
{
if (obj is Money<TCurrency>)
{
Money<TCurrency> money = obj as Money<TCurrency>;
return this.Equals(money);
}
return false;
}
/// <summary>
/// Vrátí true, pokud se rovnají hodnoty instancí (porovnává se částka a měna), nebo pokud jsou obojí null.
/// </summary>
public static bool operator ==(Money<TCurrency> objA, Money<TCurrency> objB)
{
return Object.Equals(objA, objB);
}
/// <summary>
/// Vrátí true, pokud se nerovnají hodnoty instancí (porovnává se částka a měna), nebo pokud má právě jeden z parametrů hodnotu null.
/// </summary>
public static bool operator !=(Money<TCurrency> objA, Money<TCurrency> objB)
{
return !Object.Equals(objA, objB);
}
/// <summary>
/// HashCode je složen jako XOR hash kódů amount a currency, pokud tyto hodnoty nejsou null.
/// </summary>
public override int GetHashCode()
{
int result = 0;
if (_amount != null)
{
result ^= _amount.GetHashCode();
}
if (_currency != null)
{
result ^= _currency.GetHashCode();
}
return result;
}
/// <summary>
/// Porovná měny zadané v parametrech. Pokud se liší, je vyhozena výjimka.
/// </summary>
protected static void AssertSameCurrencies(TCurrency currency1, TCurrency currency2)
{
if ((currency1 is BusinessObjectBase) && (currency2 is BusinessObjectBase))
{
// pokud jde o businessObjekty, pak porovnáme jako business objekty (může jít o různé instance stejné měny (např. objekty z různých identity map)
// raději bych použil přetypování (BusinessObjectBase)currencyX, ale compiler ho z mě neznámého důvodu odmítá
BusinessObjectBase businessObjectCurrency1 = currency1 as BusinessObjectBase;
BusinessObjectBase businessObjectCurrency2 = currency2 as BusinessObjectBase;
Contract.Requires<InvalidOperationException>(businessObjectCurrency1 == businessObjectCurrency2, "Currencies are not same.");
}
else
{
Contract.Requires<InvalidOperationException>(currency1 == currency2, "Currencies are not same.");
}
}
/// <summary>
/// Vyvolá výjimku, pokud má parametr hodnotu null.
/// </summary>
private static void AssertNotNull(object value, string parameterName)
{
Contract.Requires<ArgumentNullException>(value != null, parameterName);
}
/// <summary>
/// Porovná se částka. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static bool operator >(Money<TCurrency> money1, Money<TCurrency> money2)
{
AssertSameCurrencies(money1.Currency, money2.Currency);
return money1.Amount > money2.Amount;
}
/// <summary>
/// Porovná se částka. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static bool operator <(Money<TCurrency> money1, Money<TCurrency> money2)
{
AssertSameCurrencies(money1.Currency, money2.Currency);
return money1.Amount < money2.Amount;
}
/// <summary>
/// Porovná se částka. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static bool operator >=(Money<TCurrency> money1, Money<TCurrency> money2)
{
return (money1 > money2) || (money1 == money2);
}
/// <summary>
/// Porovná se částka. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static bool operator <=(Money<TCurrency> money1, Money<TCurrency> money2)
{
return (money1 < money2) || (money1 == money2);
}
/// <summary>
/// Sečte dvě hodnoty Money. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static Money<TCurrency> operator +(Money<TCurrency> money1, Money<TCurrency> money2)
{
return SumMoney<Money<TCurrency>>(money1, money2);
}
/// <summary>
/// Odečte dvě hodnoty Money. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
public static Money<TCurrency> operator -(Money<TCurrency> money1, Money<TCurrency> money2)
{
return SubtractMoney<Money<TCurrency>>(money1, money2);
}
/// <summary>
/// Vynásobí hodnotu Money konstantou typu decimal.
/// </summary>
public static Money<TCurrency> operator *(Money<TCurrency> money, decimal multiplicand)
{
return MultipleMoney<Money<TCurrency>>(money, multiplicand);
}
/// <summary>
/// Vynásobí hodnotu Money konstantou typu int.
/// </summary>
public static Money<TCurrency> operator *(Money<TCurrency> money, int multiplicand)
{
return MultipleMoney<Money<TCurrency>>(money, multiplicand);
}
/// <summary>
/// Vydělí hodnotu Money konstantou typu decimal.
/// </summary>
public static Money<TCurrency> operator /(Money<TCurrency> money, decimal multiplicand)
{
return DivideMoney<Money<TCurrency>>(money, multiplicand);
}
/// <summary>
/// Vydělí hodnotu Money konstantou typu int.
/// </summary>
public static Money<TCurrency> operator /(Money<TCurrency> money, int multiplicand)
{
return DivideMoney<Money<TCurrency>>(money, multiplicand);
}
/// <summary>
/// Vypočte podíl částek. Např. pro výpočet poměru částek, marže, apod.
/// </summary>
public static decimal operator /(Money<TCurrency> dividend, Money<TCurrency> divisor)
{
return DivideMoney(dividend, divisor);
}
/// <summary>
/// Sečte dvě hodnoty Money. Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
/// <typeparam name="TResult">Cílový typ Money.</typeparam>
public static TResult SumMoney<TResult>(Money<TCurrency> money1, Money<TCurrency> money2)
where TResult : Money<TCurrency>, new()
{
AssertNotNull(money1, nameof(money1));
AssertNotNull(money1.Amount, nameof(money1) + "." + nameof(money1.Amount));
AssertNotNull(money2, nameof(money2));
AssertNotNull(money2.Amount, nameof(money2) + "." + nameof(money2.Amount));
AssertSameCurrencies(money1.Currency, money2.Currency);
TResult result = new TResult();
result.Amount = money1.Amount + money2.Amount;
result.Currency = money1.Currency;
return result;
}
/// <summary>
/// Odečte měny (odčítá se money2 od money1). Pokud se neshoduje měna, operace vyvolá výjimku.
/// </summary>
/// <typeparam name="TResult">Cílový typ Money.</typeparam>
public static TResult SubtractMoney<TResult>(Money<TCurrency> money1, Money<TCurrency> money2)
where TResult : Money<TCurrency>, new()
{
AssertNotNull(money1, nameof(money1));
AssertNotNull(money1.Amount, nameof(money1) + "." + nameof(money1.Amount));
AssertNotNull(money2, nameof(money2));
AssertNotNull(money2.Amount, nameof(money2) + "." + nameof(money2.Amount));
AssertSameCurrencies(money1.Currency, money2.Currency);
TResult result = new TResult();
result.Amount = money1.Amount - money2.Amount;
result.Currency = money1.Currency;
return result;
}
/// <summary>
/// Vynásobí částku konstantou.
/// </summary>
/// <typeparam name="TResult">Cílový typ Money.</typeparam>
public static TResult MultipleMoney<TResult>(Money<TCurrency> money, decimal multiplicand)
where TResult : Money<TCurrency>, new()
{
AssertNotNull(money, nameof(money));
AssertNotNull(money.Amount, nameof(money) + "." + nameof(money.Amount));
TResult result = new TResult();
result.Amount = money.Amount * multiplicand;
result.Currency = money.Currency;
return result;
}
/// <summary>
/// Vydělí částku konstantou.
/// </summary>
/// <typeparam name="TResult">Cílový typ Money.</typeparam>
public static TResult DivideMoney<TResult>(Money<TCurrency> money, decimal divisor)
where TResult : Money<TCurrency>, new()
{
AssertNotNull(money, nameof(money));
AssertNotNull(money.Amount, nameof(money) + "." + nameof(money.Amount));
TResult result = new TResult();
result.Amount = money.Amount / divisor;
result.Currency = money.Currency;
return result;
}
/// <summary>
/// Vypočte podíl částek. Např. pro výpočet poměru částek, marže, apod.
/// </summary>
public static decimal DivideMoney(Money<TCurrency> dividend, Money<TCurrency> divisor)
{
AssertNotNull(dividend, nameof(dividend));
AssertNotNull(dividend.Amount, nameof(dividend) + "." + nameof(dividend.Amount));
AssertNotNull(divisor, nameof(divisor));
AssertNotNull(divisor.Amount, nameof(divisor) + "." + nameof(divisor.Amount));
AssertSameCurrencies(dividend.Currency, divisor.Currency);
decimal result = dividend.Amount.Value / divisor.Amount.Value;
return result;
}
}