/// <summary> /// Münze einwerfen. /// Wurde zumindest der Produktpreis eingeworfen, Münzen in Depot übernehmen /// und für Order Retourgeld festlegen. Bestellug abschließen. /// </summary> /// <returns>true, wenn die Bestellung abgeschlossen ist</returns> public bool InsertCoin(Order order, int coinValue) { if (order == null) { throw new ArgumentNullException(nameof(order)); } bool finished = order.InsertCoin(coinValue); if (finished) { List <Coin> trownInCoins = ParseCoinsFromString(order.ThrownInCoinValues); _coinRepository.AddCoins(trownInCoins.ToArray()); int returnCents = order.ThrownInCents - order.Product.PriceInCents; Coin[] coinsInDepot = GetCoinDepot().ToArray(); List <Coin> returnCoins = new List <Coin>(); foreach (Coin coin in coinsInDepot) { if (coin.CoinValue <= returnCents) { int amount = returnCents / coin.CoinValue; amount = Math.Min(amount, coin.Amount); returnCents -= amount * coin.CoinValue; returnCoins.Add(new Coin { Amount = amount, CoinValue = coin.CoinValue }); } } _coinRepository.RemoveCoins(returnCoins.ToArray()); order.FinishPayment(returnCoins); _orderRepository.AddOrder(order); } return(finished); }