Пример #1
0
 /// <summary>
 /// Overload + operator to properly calculate cent overflow
 /// </summary>
 /// <param name="addition1">First amount</param>
 /// <param name="addition2">Second amount</param>
 /// <returns>Calculated result</returns>
 public static Money operator +(Money addition1, Money addition2)
 {
     var result = new Money();
     result.Euros = addition1.Euros + addition2.Euros;
     result.Cents = addition1.Cents + addition2.Cents;
     if (result.Cents >= 100)
     {
         result.Euros += 1;
         result.Cents = 100 - result.Cents;
     }
     return result;
 }
Пример #2
0
 /// <summary>
 /// Simple validation for coin values. Assuming coints are inserted one by one
 /// </summary>
 /// <param name="money">Added amount of money</param>
 /// <returns>True coin is supported</returns>
 public static Boolean ValidateInsertedCoin(Money money)
 {
     if (money.Euros > 0)
     {
         return SupportedEuroCoins.Contains(money.Euros);
     }
     else if (money.Cents > 0)
     {
         return SupportedCentCoins.Contains(money.Cents);
     }
     else
     {
         throw new Exception("Invalid money amout");
     }
 }
Пример #3
0
 /// <summary>
 /// Overload - operator to properly calculate cent overflow
 /// </summary>
 /// <param name="substraction1">First element</param>
 /// <param name="substraction2">Second element</param>
 /// <returns>Calculated result</returns>
 public static Money operator -(Money substraction1, Money substraction2)
 {
     var result = new Money();
     result.Euros = substraction1.Euros - substraction2.Euros;
     result.Cents = substraction1.Cents - substraction2.Cents;
     if (result.Cents < 0)
     {
         result.Euros -= 1;
         result.Cents = 100 + result.Cents;
     }
     if (result.Euros < 0)
     {
         throw new ArgumentOutOfRangeException("Not enough money on balance");
     }
     return result;
 }
Пример #4
0
 /// <summary>
 /// Substract amount from buffered account
 /// </summary>
 /// <param name="money">Amount to substract</param>
 public void SubstractBufferedAmount(Money money)
 {
     _bufferedAccount -= money;
 }
Пример #5
0
 /// <summary>
 /// Inserting coints to the machine. Saving them in order buffer
 /// </summary>
 /// <param name="amount">Amount of coins inserted</param>
 /// <returns></returns>
 public Money InsertCoin(Money amount)
 {
     if (!MoneyHelper.ValidateInsertedCoin(amount))
     {
         throw new ArgumentException("Coin not supported");
     }
     var _backedBuffer = _moneyHolder.GetBufferedAmount();
     try
     {
         _moneyHolder.AddBufferedAmount(amount);
         return _moneyHolder.GetBufferedAmount();
     }
     catch (Exception ex)
     {
         // in case of calculation failed revert buffer to previous state
         // and throw exception
         _moneyHolder.SetBuffedAmount(_backedBuffer);
         throw ex;
     }
 }
Пример #6
0
 /// <summary>
 /// Substract amount from account
 /// </summary>
 /// <param name="money">Amount to substract</param>
 public void SubstractAccounterAmount(Money money)
 {
     _account -= money;
 }
Пример #7
0
 /// <summary>
 /// Sets buffered amount of money
 /// </summary>
 /// <param name="amount">Amount to set</param>
 public void SetBuffedAmount(Money amount)
 {
     _bufferedAccount = amount;
 }
Пример #8
0
 /// <summary>
 /// Sets accounted money
 /// </summary>
 /// <param name="amount">Amount to set</param>
 public void SetAccountedAmount(Money amount)
 {
     _account = amount;
 }
Пример #9
0
 /// <summary>
 /// Add amount to buffered account
 /// </summary>
 /// <param name="money">Amount to add</param>
 public void AddBufferedAmount(Money money)
 {
     _bufferedAccount += money;
 }
Пример #10
0
 /// <summary>
 /// Add amount of money to the account
 /// </summary>
 /// <param name="money">Money to add</param>
 public void AddAccountedAmount(Money money)
 {
     _account += money;
 }