예제 #1
0
        //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);
        }