private IList <CoinWithQuantity> GetChange() { var insertedAmount = State.InsertedCoins.Sum(x => (int)x); var change = insertedAmount - State.PricesProvider.GetPrice(State.SelectedProduct); var insertedCoinsWithQuantity = State.InsertedCoins .GroupBy(x => x) .ToDictionary(grp => grp.Key, v => v.Count()); var coinsWithQuantity = new Dictionary <Coin, int>( State.Wallet.GetAll() ); foreach (var c in insertedCoinsWithQuantity) { coinsWithQuantity.TryGetValue(c.Key, out var quantity); coinsWithQuantity[c.Key] = quantity + c.Value; } var coinsToReturn = _changeCalculator.CalculateMinimum( coinsWithQuantity.Select(x => new CoinWithQuantity(x.Key, x.Value)) .OrderByDescending(x => x.Denomination).ToList(), change ); return(coinsToReturn); }
public void MinimumCoins() { var coinsWithQuantity = new List <CoinWithQuantity> { new CoinWithQuantity(Coin.OneEuro, 3), new CoinWithQuantity(Coin.HalfEuro, 5), new CoinWithQuantity(Coin.TwentyCent, 5), new CoinWithQuantity(Coin.TenCent, 10) }; var changeCoins = _changeCalculator.CalculateMinimum(coinsWithQuantity, 370) .ToDictionary(grp => grp.Denomination, v => v.Quantity); //100 100 100 50 20 Assert.Equal(3, changeCoins.Count); Assert.Equal(3, changeCoins[100]); Assert.Equal(1, changeCoins[50]); Assert.Equal(1, changeCoins[20]); }