Esempio n. 1
0
        public void TestTwistyMathCalculations()
        {
            //Test code path when customer owes money
            Change owedMoney = new Change();
            owedMoney.AmountOwed = 532;
            owedMoney.MoneyOwed = true;

            TwistyMath TwistOwed = new TwistyMath();
            Change owedMoneyTestResult = TwistOwed.CalculateChange((decimal)7.25, (decimal)12.57);

            Assert.AreEqual(owedMoney.MoneyOwed, owedMoneyTestResult.MoneyOwed);
            Assert.AreEqual(owedMoney.AmountOwed, owedMoneyTestResult.AmountOwed);
            Assert.AreEqual(owedMoney.Dollars, owedMoneyTestResult.Dollars);

            //Test code path when change is due and the amount owed is not divisible by 3
            Change StandardChange = new Change();
            StandardChange.Dollars = 1;
            StandardChange.Quarters = 3;
            StandardChange.Pennies = 3;
            StandardChange.IsRandomChange = false;
            StandardChange.ChangeOwed = 178;

            TwistyMath TwistStandardChange = new TwistyMath();
            Change StandardChangeTestResult = TwistStandardChange.CalculateChange((decimal)5.00, (decimal)3.22);

            Assert.AreEqual(StandardChange.Dollars, StandardChangeTestResult.Dollars);
            Assert.AreEqual(StandardChange.Quarters, StandardChangeTestResult.Quarters);
            Assert.AreEqual(StandardChange.Dimes, StandardChangeTestResult.Dimes);
            Assert.AreEqual(StandardChange.Nickels, StandardChangeTestResult.Nickels);
            Assert.AreEqual(StandardChange.Pennies, StandardChangeTestResult.Pennies);
            Assert.AreEqual(StandardChange.IsRandomChange, StandardChangeTestResult.IsRandomChange);
            Assert.AreEqual(StandardChange.ChangeOwed, StandardChangeTestResult.ChangeOwed);
        }
Esempio n. 2
0
 public void GetChange()
 {
     try
     {
         Change = new TwistyMath().CalculateChange(AmountPaid, AmountOwed);
     }
     catch(Exception ex)
     {
         Console.WriteLine("An error has occured calculating the changed owed to the customer. Error details: " + ex.Message + " ----- " + ex.StackTrace);
     }
 }
Esempio n. 3
0
        public Change CalculateChange(decimal AmountPaid, decimal AmountOwed)
        {
            Change _change = new Change();
            int _calculatedChange = 0;
            int _amountPaidHundreds = 0;
            int _amountOwedHundreds = 0;

            List<int> Denominations = new List<int>();
            Denominations.Add(100);
            Denominations.Add(25);
            Denominations.Add(10);
            Denominations.Add(5);
            Denominations.Add(1);

            try
            {
                _amountPaidHundreds = (int)(AmountPaid * 100);
                _amountOwedHundreds = (int)(AmountOwed * 100);
                _calculatedChange = _amountPaidHundreds - _amountOwedHundreds;
                if (_calculatedChange < 0)
                {
                    //If the customer has not provided enough money to cover thier bill tell the cashier that they owe more money.
                    _change.MoneyOwed = true;
                    _change.AmountOwed = _calculatedChange * -1;
                }
                else
                {
                    _change.ChangeOwed = _calculatedChange;
                    if (_calculatedChange % 3 == 0)
                    {
                        _change.IsRandomChange = true;
                        //Twisty Math -- If the "owed" amount is divisible by 3, the app should randomly generate the change denominations
                        Random rand = new Random();
                        while (_calculatedChange > 0)
                        {
                            var randInt = Denominations[rand.Next(0, Denominations.Count)];
                            if (randInt <= _calculatedChange)
                            {
                                switch (randInt)
                                {
                                    case 100:
                                        _change.Dollars = _change.Dollars + (int)(_calculatedChange / _dollar);
                                        _calculatedChange = _calculatedChange - ((int)(_calculatedChange / _dollar) * _dollar);
                                        break;
                                    case 25:
                                        _change.Quarters = _change.Quarters + (int)(_calculatedChange / _quarter);
                                        _calculatedChange = _calculatedChange - ((int)(_calculatedChange / _quarter) * _quarter);
                                        break;
                                    case 10:
                                        _change.Dimes = _change.Dimes + (int)(_calculatedChange / _dime);
                                        _calculatedChange = _calculatedChange - ((int)(_calculatedChange / _dime) * _dime);
                                        break;
                                    case 5:
                                        _change.Nickels = _change.Nickels + (int)(_calculatedChange / _nickel);
                                        _calculatedChange = _calculatedChange - ((int)(_calculatedChange / _nickel) * _nickel);
                                        break;
                                    case 1:
                                        _change.Pennies = _change.Pennies + (int)(_calculatedChange / _penny);
                                        _calculatedChange = _calculatedChange - ((int)(_calculatedChange / _penny) * _penny);
                                        break;
                                }
                            }
                        }
                    }
                    else
                    {
                        //The function of the application is to tell the cashier how much change is owed, and what denominations should be used. 
                        //In most cases the app should return the minimum amount of physical change.
                        _change.Dollars = (int)(_calculatedChange / _dollar);
                        _change.Quarters = (int)((_calculatedChange % _dollar) / _quarter);
                        _change.Dimes = (int)(((_calculatedChange % _dollar) % _quarter) / _dime);
                        _change.Nickels = (int)((((_calculatedChange % _dollar) % _quarter) % _dime) / _nickel);
                        _change.Pennies = (int)(((((_calculatedChange % _dollar) % _quarter) % _dime) % _nickel) / _penny);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return _change;
        }
Esempio n. 4
0
        private static void ProcessFile(string file)
        {
            List <string> lines    = HandleFile(file);
            List <string> writeOut = new List <string>();
            int           t        = lines.Count;
            int           count    = 0;

            l.Output(String.Format("Lines in file:{0}", t));
            foreach (string m in lines)
            {
                if (m.Contains(","))
                {
                    string[] arr = m.Split(',');
                    if (arr.Length == 2 && !string.IsNullOrWhiteSpace(arr[0]) && !string.IsNullOrWhiteSpace(arr[1]))
                    {
                        decimal ttl, given;
                        decimal.TryParse(arr[0], out ttl);
                        decimal.TryParse(arr[1], out given);
                        if (given > ttl)
                        {
                            decimal chng    = given - ttl;
                            decimal dollars = Math.Truncate(chng);
                            chng = chng - dollars;
                            decimal outofCoin = 0.5m;
                            if ((ttl * 100) % 3 == 0)
                            {
                                // always between 1, 2, or 3
                                int rnd = _r.Next(1, 4);
                                if (rnd == 3)
                                {
                                    rnd = 5;
                                }
                                outofCoin = 0.05m * rnd;
                            }
                            string fmt = Change.GetListOfChange(chng, outofCoin);
                            writeOut.Add(string.Format("{0} {1}", (dollars > 0 ? (dollars + " dollar" + (dollars == 1 ? "" : "s")) : ""), fmt));
                        }
                        else
                        {
                            decimal chng = ttl - given;
                            if (chng == 0)
                            {
                                writeOut.Add("No change needed");
                            }
                            else
                            {
                                writeOut.Add(string.Format("Still need {0}", chng));
                            }
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("Format not correct in file {1} at line {0}", count, file));
                    }
                }
                else
                {
                    throw new Exception(string.Format("Expected comma in file {1} at line {0}", count, file));
                }
            }
            if (writeOut.Count > 0)
            {
                // try catch in preceding method
                // output results to new file in same folder ending with _cmpl
                string newfilename = file.Substring(0, file.Length - 4) + "_cmpl.txt";
                string curfilepath = Path.GetDirectoryName(file) + "\\";
                if (!Directory.Exists(outPutFolderPath))
                {
                    Directory.CreateDirectory(outPutFolderPath);
                }
                newfilename = newfilename.Replace(curfilepath, outPutFolderPath);
                File.WriteAllLines(newfilename, writeOut);
            }
        }