public Machine() { Products = new List<Product>(); Random m = new Random();//создание экземпляра класса Рандом для создания произвольного кол-ва монет разного номинала Money = new Balance(m.Next(1, 1000)); //Money = new Balance(0); //для тестирования случая, когда автомат не может выдать деньги }
/// <summary> /// Calculates change /// </summary> /// <param name="money">input amount of money - dictionary where key - denomination, value - units amount</param> /// <param name="sum">sum that should be composed</param> /// <returns>dictionary where key - denomination and value - amount</returns> public IDictionary<decimal, int> Calculate(IDictionary<decimal, int> money, decimal sum) { if (money == null) { throw new ArgumentNullException("money"); } var balance = new Balance(money); var result = new Stack<decimal>(); var currentSum = 0m; var denomination = balance.PopTheSameOrLess(balance.GetMaxAvailable()); while (currentSum != sum) { if (denomination == 0 && result.Count == 0) { break; } if (currentSum + denomination <= sum && denomination > 0) { result.Push(denomination); currentSum += denomination; denomination = balance.PopTheSameOrLess(denomination); } else if (denomination > 0) { balance.Push(denomination); denomination = balance.PopLessThat(denomination); } else { while (denomination == 0 && result.Count > 0) { var previous = result.Pop(); currentSum -= previous; balance.Push(previous); denomination = balance.PopLessThat(previous); } } } return result.GroupBy(item => item).ToDictionary(g => g.Key, g => g.Count()); }
public Client() { Money = new Balance(150); }
public CurrentTransaction() { Money = new Balance(0); }