public ICurrencyRepo Reduce() { USCurrencyRepo repo = new USCurrencyRepo(); decimal Amount = 0m; for (int i = 0; i < Coins.Count; i++) { Amount = Amount + Coins[i].MonetaryValue; } US.Penny p = new US.Penny(); US.HalfDollar h = new US.HalfDollar(); US.Quarter q = new US.Quarter(); US.Nickel n = new US.Nickel(); US.DollarCoin d = new US.DollarCoin(); US.Dime m = new US.Dime(); while ((Amount % 1m) < Amount) { repo.AddCoin(d); Amount = Amount - 1m; } while ((Amount % .50m) < Amount) { repo.AddCoin(h); Amount = Amount - .50m; } while ((Amount % .25m) < Amount) { repo.AddCoin(q); Amount = Amount - .25m; } while ((Amount % .10m) < Amount) { repo.AddCoin(m); Amount = Amount - .10m; } while ((Amount % .05m) < Amount) { repo.AddCoin(n); Amount = Amount - .05m; } while ((Amount % .01m) < Amount) { repo.AddCoin(p); Amount = Amount - .01m; } return(repo); }
public static ICurrencyRepo MakeChange(decimal Amount) { USCurrencyRepo currencyRepo = new USCurrencyRepo(); foreach (ICoin coin in USCoin.GetUSCoinList()) { int count = (int)(Amount / coin.MonetaryValue); Amount -= count * coin.MonetaryValue; if (count > 0) { for (int i = 0; i < count; i++) { currencyRepo.AddCoin(coin); } } } return(currencyRepo); }
public ICurrencyRepo CreateChange(Decimal Amount) { USCurrencyRepo repo = new USCurrencyRepo(); US.Penny p = new US.Penny(); US.HalfDollar h = new US.HalfDollar(); US.Quarter q = new US.Quarter(); US.Nickel n = new US.Nickel(); US.DollarCoin d = new US.DollarCoin(); US.Dime m = new US.Dime(); // this is the greedy algorithm for making change. while ((Amount % 1m) < Amount) { repo.AddCoin(d); Amount = Amount - 1m; } while ((Amount % .50m) < Amount) { repo.AddCoin(h); Amount = Amount - .50m; } while ((Amount % .25m) < Amount) { repo.AddCoin(q); Amount = Amount - .25m; } while ((Amount % .10m) < Amount) { repo.AddCoin(m); Amount = Amount - .10m; } while ((Amount % .05m) < Amount) { repo.AddCoin(n); Amount = Amount - .05m; } while ((Amount % .01m) < Amount) { repo.AddCoin(p); Amount = Amount - .01m; } return(repo); }