示例#1
0
        /// <summary>
        /// Optimizes the change returned.
        /// Gives the most efficient change back possible.
        /// </summary>
        /// <param name="changeValue">The change value.</param>
        /// <param name="regionCurrency">The region currency.</param>
        /// <returns></returns>
        private List <string> OptimizedChangeReturned(decimal changeValue, IRegionCurrency regionCurrency)
        {
            List <string> toReturn = new List <string>();

            foreach (var c in regionCurrency.GetDescendingOrderedCurrencies())
            {
                if (c.Value <= changeValue)
                {
                    int times = (int)(changeValue / c.Value);
                    toReturn.Add(string.Format("{0} {1}", times, times > 1 ? c.PluralName : c.Name));
                    changeValue -= (times * c.Value);
                }
            }
            return(toReturn);
        }
示例#2
0
        /// <summary>
        /// If the change due is divisible by 3, give random denominations
        /// these random denominations must equal the appropriate total amount
        /// of change due
        /// </summary>
        /// <param name="changeValue">The change value.</param>
        /// <param name="regionCurrency">The region currency.</param>
        /// <param name="r">The random seed</param>
        /// <returns></returns>
        private List <string> Mod3ChangeGeneration(decimal changeValue, IRegionCurrency regionCurrency, Random r)
        {
            Dictionary <Currency, int> changeDue = new Dictionary <Currency, int>();
            List <string> toReturn = new List <string>();

            foreach (Currency c in regionCurrency.Denominations)
            {
                changeDue.Add(c, 0);
            }
            while (changeValue > 0)
            {
                Currency tmp = regionCurrency.Denominations[r.Next(regionCurrency.Denominations.Count)];

                if (changeValue >= tmp.Value)
                {
                    try
                    {
                        int maxNumberOfTimes = (int)(changeValue / tmp.Value) + 1;                         //adding 1 because random max is exclusive
                        int actualTimes      = r.Next(maxNumberOfTimes);
                        changeDue[tmp] += actualTimes;
                        changeValue    -= (actualTimes * tmp.Value);
                    }
                    catch (OverflowException)
                    {
                        //eat the overflow exception and try again
                        //this happens when the initial change amount due is at or near the limit for Int32
                        //and we try to generate change using something less than 1 ex: a nickel.
                        //allow the system to try larger denominations to reduce the total change due being tracked
                    }
                }
            }
            //populate the change denominations to return
            foreach (Currency c in regionCurrency.GetDescendingOrderedCurrencies())
            {
                if (changeDue[c] > 0)
                {
                    toReturn.Add(string.Format("{0} {1}", changeDue[c], changeDue[c] > 1 ? c.PluralName : c.Name));
                }
            }
            return(toReturn);
        }