public override ICurrencyRepo Reduce() { ICurrencyRepo originalRepo = this; ICurrencyRepo reducedRepo = new USCurrencyRepo(); for (int x = (PossibleCoins.Count - 1); x >= 0; x--) { ICoin smallerCoin = PossibleCoins[x]; //coin we're reducing ICurrencyRepo currentCoins = originalRepo.GetOnlyCoinsOfType(PossibleCoins[x].GetType()); for (int y = 0; y < PossibleCoins.Count; y++) { ICoin largerCoin = PossibleCoins[y]; //coin we're trying to reduce the smaller coin to double amountNeededToReduce = largerCoin.MonetaryValue / smallerCoin.MonetaryValue; if (currentCoins.GetCoinCount() >= amountNeededToReduce) { int amountPossible = (int)(currentCoins.GetCoinCount() / amountNeededToReduce); for (int z = 0; z < amountPossible; z++) { reducedRepo.AddCoin(largerCoin); for (int a = 0; a < amountNeededToReduce; a++) //removing reduced coins { currentCoins.Coins.RemoveAt(currentCoins.GetCoinCount() - 1); } } } } } return(reducedRepo); }
//Makes change from existing coins public override ICurrencyRepo MakeChangeFromCurrentCoins(double amount) { USCurrencyRepo changeRepo = new USCurrencyRepo(); List <ICoin> coinsToRemove = new List <ICoin>(); decimal change = (decimal)amount; this.Coins = this.Coins.OrderByDescending(x => x.MonetaryValue).ToList(); foreach (USCoin coin in this.Coins) { if (change <= 0) { break; } decimal coinValue = (decimal)coin.MonetaryValue; change -= coinValue; changeRepo.AddCoin(coin); coinsToRemove.Add(coin); } this.Coins = this.Coins.Except(coinsToRemove).ToList(); //remove change from this repo return(changeRepo); }
//Static Make Change public static ICurrencyRepo CreateChange(double amount) { USCurrencyRepo repo = new USCurrencyRepo(); decimal change = (decimal)amount; foreach (USCoin coin in PossibleCoins) { if (change <= 0) { break; } decimal coinValue = (decimal)coin.MonetaryValue; int count = (int)(change / coinValue); change -= count * coinValue; for (int x = 0; x < count; x++) { repo.AddCoin(coin); } count = 0; } return(repo); }
public override ICurrencyRepo GetOnlyCoinsOfType(Type coinType) { ICurrencyRepo originalRepo = this; ICurrencyRepo repo = new USCurrencyRepo(); foreach (ICoin c in originalRepo.Coins) { if (coinType == c.GetType()) { repo.AddCoin(c); } } return(repo); }
public static ICurrencyRepo CreateChange(double amount) { USCurrencyRepo newRepo = new USCurrencyRepo(); double currentAmount = amount; #region AddApropriateCoins while (currentAmount > 0) { if (currentAmount - 1 >= 0) { DollarCoin dollar = new DollarCoin(); newRepo.AddCoin(dollar); currentAmount -= dollar.MonetaryValue; } else if (currentAmount - 0.5 >= 0) { HalfDollar halfDollar = new HalfDollar(); newRepo.AddCoin(halfDollar); currentAmount -= halfDollar.MonetaryValue; } else if (currentAmount - 0.25 >= 0) { Quarter quarter = new Quarter(); newRepo.AddCoin(quarter); currentAmount -= quarter.MonetaryValue; } else if (currentAmount - 0.1 >= 0) { Dime dime = new Dime(); newRepo.AddCoin(dime); currentAmount -= dime.MonetaryValue; } else if (currentAmount - 0.05 >= 0) { Nickel nickel = new Nickel(); newRepo.AddCoin(nickel); currentAmount -= nickel.MonetaryValue; } else { Penny penny = new Penny(); newRepo.AddCoin(penny); currentAmount -= penny.MonetaryValue; } } #endregion return(newRepo); }
public override ICurrencyRepo MakeChange(double amount) { USCurrencyRepo change = new USCurrencyRepo(); Decimal changeToMake = (Decimal)amount; List <ICoin> coinList = GetCoinList(); foreach (USCoin coin in coinList) { while (changeToMake >= (Decimal)coin.MonetaryValue) { change.AddCoin(coin); changeToMake -= (Decimal)coin.MonetaryValue; } } return(change); }