示例#1
0
        /// <summary>
        /// Give change back to the machine operator.
        /// </summary>
        /// <returns></returns>
        public List <Money> FinishTransaction()
        {
            List <Money> changeCurrency = new List <Money>();

            if (TotalChangeAvailable > 0 && Balance > 0)
            {
                decimal currencyThreshold = 0.25M;
                if (currencyDenominations[currencyThreshold].Count > 0 && Balance >= currencyThreshold)
                {
                    Money quarters = new Quarter(true);
                    quarters.MakeChange(currencyDenominations[currencyThreshold], Balance);
                    changeCurrency.Add(quarters);
                    Balance -= quarters.TotalValue;
                }

                currencyThreshold = 0.10M;
                if (currencyDenominations[currencyThreshold].Count > 0 && Balance >= currencyThreshold)
                {
                    Money dimes = new Dime(true);
                    dimes.MakeChange(currencyDenominations[currencyThreshold], Balance);
                    changeCurrency.Add(dimes);
                    Balance -= dimes.TotalValue;
                }

                currencyThreshold = 0.05M;
                if (currencyDenominations[currencyThreshold].Count > 0 && Balance >= currencyThreshold)
                {
                    Money nickels = new Nickel(true);
                    nickels.MakeChange(currencyDenominations[currencyThreshold], Balance);
                    changeCurrency.Add(nickels);
                    Balance -= nickels.TotalValue;
                }

                decimal changeGiven = 0;
                foreach (Money currency in changeCurrency)
                {
                    changeGiven += currency.TotalValue;
                }

                TransactionLog($"GIVE CHANGE: {changeGiven:C}");
            }

            return(changeCurrency);

            // Old version before Money class

            //decimal changeGiven = Balance;
            //int quarters = 0;
            //int dimes = 0;
            //int nickels = 0;
            //while (Balance > 0)
            //{
            //    if (Balance >= (decimal).25)
            //    {
            //        Balance -= (decimal).25;
            //        quarters++;
            //    }
            //    else if (Balance >= (decimal).10)
            //    {
            //        Balance -= (decimal).10;
            //        dimes++;
            //    }
            //    else if (Balance >= (decimal).05)
            //    {
            //        Balance -= (decimal).05;
            //        nickels++;
            //    }
            //}

            //TransactionLog($"GIVE CHANGE: {changeGiven:C}");

            //string totalChange = $"Your Change is {quarters} Quarter(s), {dimes} Dime(s), and {nickels} Nickel(s)";
            //return totalChange;
        }