Exemplo n.º 1
0
        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 ICurrencyRepo MakeChange(decimal Amount)
        {
            ICoin coin;

            while (Amount > 0)
            {
                if (Amount >= 1)
                {
                    Amount -= 1;
                    coin    = new US.DollarCoin();
                    Coins.Add(coin);
                    continue;
                }
                else if (Amount >= 0.5m)
                {
                    Amount -= 0.5m;
                    coin    = new US.HalfDollar();
                    Coins.Add(coin);
                    continue;
                }
                else if (Amount >= 0.25m)
                {
                    Amount -= 0.25m;
                    coin    = new US.Quarter();
                    Coins.Add(coin);
                    continue;
                }
                else if (Amount >= 0.10m)
                {
                    Amount -= 0.10m;
                    coin    = new US.Dime();
                    Coins.Add(coin);
                    continue;
                }
                else if (Amount >= 0.05m)
                {
                    Amount -= 0.05m;
                    coin    = new US.Nickel();
                    Coins.Add(coin);
                    continue;
                }
                else if (Amount >= 0.01m)
                {
                    Amount -= 0.01m;
                    coin    = new US.Penny();
                    Coins.Add(coin);
                    continue;
                }
            }
            return(this);
        }
Exemplo n.º 3
0
        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);
        }