//Creates a new repo with a list of coins that //equal the amount passed in public static ICurrencyRepo CreateChange(double Amount) { //Create a temporary repo CurrencyRepo temp = new CurrencyRepo(); //While the amount is above zero add coins while (Amount > 0) { //round to 2 decimal places to avoid decimal place error Amount = Math.Round(Amount, 2); //if amount is greater than 1, add a dollar coin if (Amount >= 1) { DollarCoin dollar = new DollarCoin(); temp.AddCoin(dollar); Amount--; } //if amount is less than a dollar but greater than .5 then add a half dollar else if (Amount >= .5) { HalfDollar dollar = new HalfDollar(); temp.AddCoin(dollar); Amount -= .5; } //if the amount is greater than or equal to .25 and less than above then add a quarter else if (Amount >= .25) { Quarter quater = new Quarter(); temp.AddCoin(quater); Amount -= .25; } //if the amount is greater than or equal to .1 and less than above then add a dime else if (Amount >= .1) { Dime dime = new Dime(); temp.AddCoin(dime); Amount -= .1; } //if the amount is greater than or equal to .05 and less than above then add a nickel else if (Amount >= .05) { Nickel nickel = new Nickel(); temp.AddCoin(nickel); Amount -= .05; } //if the amount is greater than or equal to .01 and less than above then add a penny else if (Amount >= .01) { Penny penny = new Penny(); temp.AddCoin(penny); Amount -= .01; } } //return the temporary repo return(temp); }
//return a repo of change based on the amount of coins currently in this repo public ICurrencyRepo MakeChange(double Amount) { //create temporary repo CurrencyRepo temp = new CurrencyRepo(); //while the amount is greater than 0, add coins while (Amount > 0) { Amount = Math.Round(Amount, 2); //If the amount is greater than or equal to 1 and the current repo has a dollar coin, //add the dollar coin to the new repo and remove it from the current repo if (Amount >= 1 && Coins.Exists(x => x.MonetaryValue == 1)) { DollarCoin dollar = new DollarCoin(); temp.AddCoin(dollar); RemoveCoin(Coins.Find(x => x.MonetaryValue == 1)); Amount--; } //If the amount is greater than or equal to .5 and the current repo has a half dollar, //add the half dollar to the new repo and remove it from the current repo else if (Amount >= .5 && Coins.Exists(x => x.MonetaryValue == .5)) { HalfDollar dollar = new HalfDollar(); temp.AddCoin(dollar); RemoveCoin(Coins.Find(x => x.MonetaryValue == .5)); Amount -= .5; } //If the amount is greater than or equal to .25 and the current repo has a quarter, //add the quarter to the new repo and remove it from the current repo else if (Amount >= .25 && Coins.Exists(x => x.MonetaryValue == .25)) { Quarter quater = new Quarter(); temp.AddCoin(quater); RemoveCoin(Coins.Find(x => x.MonetaryValue == .25)); Amount -= .25; } //If the amount is greater than or equal to .1 and the current repo has a dime, //add the dime to the new repo and remove it from the current repo else if (Amount >= .1 && Coins.Exists(x => x.MonetaryValue == .1)) { Dime dime = new Dime(); temp.AddCoin(dime); RemoveCoin(Coins.Find(x => x.MonetaryValue == .1)); Amount -= .1; } //If the amount is greater than or equal to .05 and the current repo has a nickel, //add the nickel to the new repo and remove it from the current repo else if (Amount >= .05 && Coins.Exists(x => x.MonetaryValue == .05)) { Nickel nickel = new Nickel(); temp.AddCoin(nickel); RemoveCoin(Coins.Find(x => x.MonetaryValue == .05)); Amount -= .05; } //If the amount is greater than or equal to .01 and the current repo has a penny, //add the penny to the new repo and remove it from the current repo else if (Amount >= .01 && Coins.Exists(x => x.MonetaryValue == .01)) { Penny penny = new Penny(); temp.AddCoin(penny); RemoveCoin(Coins.Find(x => x.MonetaryValue == .01)); Amount -= .01; } //If there aren't enough coins then zero out the amount to break the loop else if (this.TotalValue() > Amount) { Amount = 0; } } return(temp); }