示例#1
0
        public ICurrencyRepo CreateChange(Decimal AmountTendered, Decimal TotalCost)
        {
            USCurrencyRepo repo    = new USCurrencyRepo();
            decimal        newCost = AmountTendered - TotalCost;

            return(CreateChange(newCost));
        }
示例#2
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);
        }
示例#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);
        }
示例#4
0
        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);
        }