using System;
using PokerTournamentManager.Framework.Exceptions;
namespace PokerTournamentManager.Framework
{
///
/// Immutable class that holds currency info
///
public class Money
{
#region Properties
private readonly string _CurrencyCode;
///
/// Gets the currency code.
///
/// The currency code.
public string CurrencyCode
{
get { return _CurrencyCode.ToUpper(); }
}
private readonly double _Amount;
///
/// Gets the amount.
///
/// The amount.
public double Amount
{
get { return _Amount; }
}
#endregion
#region Constructors
public Money(double amount) : this(amount, CurrencyCodes.US_DOLLAR) { }
public Money(double amount, string currencyCode)
{
if (String.IsNullOrEmpty(currencyCode)) throw new ArgumentNullException("currencyCode");
if (amount < 0) throw new ArgumentOutOfRangeException("amount");
this._Amount = amount;
this._CurrencyCode = currencyCode;
}
#endregion
#region Overrides
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
///
/// true if the specified is equal to the current ; otherwise, false.
///
public override bool Equals(object obj)
{
bool equals = false;
if (obj != null && obj is Money)
{
Money tmp = (obj as Money);
equals = tmp.Amount == this.Amount && tmp.CurrencyCode == this.CurrencyCode;
}
return equals;
}
#endregion
#region Operators
///
/// Implements the operator +.
///
/// The money1.
/// The money2.
/// The result of the operator.
public static Money operator +(Money money1, Money money2)
{
if (money1 == null) throw new ArgumentNullException("money1");
if (money2 == null) throw new ArgumentNullException("money2");
if (money1.CurrencyCode != money2.CurrencyCode)
throw new IncorrectCurrencyException();
return new Money(money1.Amount + money2.Amount);
}
#endregion
#region Inner Classes
///
/// Static class serving as a Container for
/// supported Currency Code constants
///
public static class CurrencyCodes
{
///
/// United States Dollar
///
public const string US_DOLLAR = "USD";
public const string GB_POUND = "GBP";
}
#endregion
}
}