public void TestMakeUpdatesInPursesShouldSuccess() { PurseBase coinsToChange = new TempPurse(); coinsToChange.Coins.Add(new Coin() { Value = "10", Count = 1 }); coinsToChange.Coins.Add(new Coin() { Value = "5", Count = 2 }); coinsToChange.Coins.Add(new Coin() { Value = "2", Count = 3 }); coinsToChange.Coins.Add(new Coin() { Value = "1", Count = 4 }); _ctx.Init(String.Empty); _controller.MakeUpdatesInPursesForTests(coinsToChange); Assert.AreEqual(16, _ctx.UserPurse.Item.Coins[3].Count); Assert.AreEqual(22, _ctx.UserPurse.Item.Coins[2].Count); Assert.AreEqual(33, _ctx.UserPurse.Item.Coins[1].Count); Assert.AreEqual(14, _ctx.UserPurse.Item.Coins[0].Count); Assert.AreEqual(99, _ctx.CoffeMachinePurse.Item.Coins[3].Count); Assert.AreEqual(98, _ctx.CoffeMachinePurse.Item.Coins[2].Count); Assert.AreEqual(97, _ctx.CoffeMachinePurse.Item.Coins[1].Count); Assert.AreEqual(96, _ctx.CoffeMachinePurse.Item.Coins[0].Count); Assert.AreEqual(0, _ctx.PurchaseInfo.Item.PayedSum); }
/// <summary> /// Готовит сдачу к выдаче /// </summary> /// <param name="sum">Сумма сдачи</param> /// <returns>Монеты для сдачи</returns> protected PurseBase MakeChange(Double sum) { if (_ctx.CoffeMachinePurse.Item.Sum <= sum) { throw new Exception("В автомате не хватает денег на сдачу!"); } TempPurse purse = new TempPurse(); purse.Coins.Add(new Coin() { Value = "10", Count = 0 }); purse.Coins.Add(new Coin() { Value = "5", Count = 0 }); purse.Coins.Add(new Coin() { Value = "2", Count = 0 }); purse.Coins.Add(new Coin() { Value = "1", Count = 0 }); Double sumToWork = sum; for (Int32 i = 0; i < purse.Coins.Count; i++) { var coin = purse.Coins[i]; Int32 intCoin = Int32.Parse(coin.Value); Int32 coinInMachine = _ctx.CoffeMachinePurse.Item.Coins.First(x => x.Value == coin.Value).Count; while ((sumToWork >= intCoin) && (coinInMachine > 0)) { sumToWork -= intCoin; purse.Coins[i].Count++; coinInMachine--; } } if (purse.Sum == sum) { return(purse); } else { throw new NotEnoughtMoneyException("1"); } }
/// <summary> /// Готовит сдачу к выдаче /// </summary> /// <param name="sum">Сумма сдачи</param> /// <returns>Монеты для сдачи</returns> protected PurseBase MakeChange(Double sum) { if (_ctx.CoffeMachinePurse.Item.Sum <= sum) { throw new Exception("В автомате не хватает денег на сдачу!"); } TempPurse purse = new TempPurse(); purse.Coins.Add(new Coin() { Value = "10", Count = 0 }); purse.Coins.Add(new Coin() { Value = "5", Count = 0 }); purse.Coins.Add(new Coin() { Value = "2", Count = 0 }); purse.Coins.Add(new Coin() { Value = "1", Count = 0 }); Double sumToWork = sum; for (Int32 i = 0; i < purse.Coins.Count; i++) { var coin = purse.Coins[i]; Int32 intCoin = Int32.Parse(coin.Value); Int32 coinInMachine = _ctx.CoffeMachinePurse.Item.Coins.First(x => x.Value == coin.Value).Count; while ((sumToWork >= intCoin) && (coinInMachine > 0)) { sumToWork -= intCoin; purse.Coins[i].Count++; coinInMachine--; } } if (purse.Sum == sum) { return purse; } else { throw new NotEnoughtMoneyException("1"); } }