Exemplo n.º 1
0
        public override ICurrencyRepo Reduce()
        {
            ICurrencyRepo originalRepo = this;
            ICurrencyRepo reducedRepo  = new USCurrencyRepo();

            for (int x = (PossibleCoins.Count - 1); x >= 0; x--)
            {
                ICoin         smallerCoin  = PossibleCoins[x]; //coin we're reducing
                ICurrencyRepo currentCoins = originalRepo.GetOnlyCoinsOfType(PossibleCoins[x].GetType());

                for (int y = 0; y < PossibleCoins.Count; y++)
                {
                    ICoin largerCoin = PossibleCoins[y]; //coin we're trying to reduce the smaller coin to

                    double amountNeededToReduce = largerCoin.MonetaryValue / smallerCoin.MonetaryValue;
                    if (currentCoins.GetCoinCount() >= amountNeededToReduce)
                    {
                        int amountPossible = (int)(currentCoins.GetCoinCount() / amountNeededToReduce);
                        for (int z = 0; z < amountPossible; z++)
                        {
                            reducedRepo.AddCoin(largerCoin);
                            for (int a = 0; a < amountNeededToReduce; a++) //removing reduced coins
                            {
                                currentCoins.Coins.RemoveAt(currentCoins.GetCoinCount() - 1);
                            }
                        }
                    }
                }
            }

            return(reducedRepo);
        }
Exemplo n.º 2
0
        //Makes change from existing coins
        public override ICurrencyRepo MakeChangeFromCurrentCoins(double amount)
        {
            USCurrencyRepo changeRepo    = new USCurrencyRepo();
            List <ICoin>   coinsToRemove = new List <ICoin>();

            decimal change = (decimal)amount;

            this.Coins = this.Coins.OrderByDescending(x => x.MonetaryValue).ToList();

            foreach (USCoin coin in this.Coins)
            {
                if (change <= 0)
                {
                    break;
                }

                decimal coinValue = (decimal)coin.MonetaryValue;

                change -= coinValue;

                changeRepo.AddCoin(coin);
                coinsToRemove.Add(coin);
            }

            this.Coins = this.Coins.Except(coinsToRemove).ToList(); //remove change from this repo

            return(changeRepo);
        }
Exemplo n.º 3
0
        //Static Make Change
        public static ICurrencyRepo CreateChange(double amount)
        {
            USCurrencyRepo repo = new USCurrencyRepo();

            decimal change = (decimal)amount;

            foreach (USCoin coin in PossibleCoins)
            {
                if (change <= 0)
                {
                    break;
                }

                decimal coinValue = (decimal)coin.MonetaryValue;

                int count = (int)(change / coinValue);
                change -= count * coinValue;

                for (int x = 0; x < count; x++)
                {
                    repo.AddCoin(coin);
                }

                count = 0;
            }

            return(repo);
        }
Exemplo n.º 4
0
        public override ICurrencyRepo GetOnlyCoinsOfType(Type coinType)
        {
            ICurrencyRepo originalRepo = this;
            ICurrencyRepo repo         = new USCurrencyRepo();

            foreach (ICoin c in originalRepo.Coins)
            {
                if (coinType == c.GetType())
                {
                    repo.AddCoin(c);
                }
            }

            return(repo);
        }
Exemplo n.º 5
0
        public static ICurrencyRepo CreateChange(double amount)
        {
            USCurrencyRepo newRepo       = new USCurrencyRepo();
            double         currentAmount = amount;

            #region AddApropriateCoins
            while (currentAmount > 0)
            {
                if (currentAmount - 1 >= 0)
                {
                    DollarCoin dollar = new DollarCoin();
                    newRepo.AddCoin(dollar);
                    currentAmount -= dollar.MonetaryValue;
                }
                else if (currentAmount - 0.5 >= 0)
                {
                    HalfDollar halfDollar = new HalfDollar();
                    newRepo.AddCoin(halfDollar);
                    currentAmount -= halfDollar.MonetaryValue;
                }
                else if (currentAmount - 0.25 >= 0)
                {
                    Quarter quarter = new Quarter();
                    newRepo.AddCoin(quarter);
                    currentAmount -= quarter.MonetaryValue;
                }
                else if (currentAmount - 0.1 >= 0)
                {
                    Dime dime = new Dime();
                    newRepo.AddCoin(dime);
                    currentAmount -= dime.MonetaryValue;
                }
                else if (currentAmount - 0.05 >= 0)
                {
                    Nickel nickel = new Nickel();
                    newRepo.AddCoin(nickel);
                    currentAmount -= nickel.MonetaryValue;
                }
                else
                {
                    Penny penny = new Penny();
                    newRepo.AddCoin(penny);
                    currentAmount -= penny.MonetaryValue;
                }
            }
            #endregion
            return(newRepo);
        }
Exemplo n.º 6
0
        public override ICurrencyRepo MakeChange(double amount)
        {
            USCurrencyRepo change       = new USCurrencyRepo();
            Decimal        changeToMake = (Decimal)amount;

            List <ICoin> coinList = GetCoinList();

            foreach (USCoin coin in coinList)
            {
                while (changeToMake >= (Decimal)coin.MonetaryValue)
                {
                    change.AddCoin(coin);
                    changeToMake -= (Decimal)coin.MonetaryValue;
                }
            }
            return(change);
        }