コード例 #1
0
        public static void TestSimpleNumber(IChangeSolver solver)
        {
            ICurrencyHolder holder = new CurrencyHolder();

            solver.FindReturnFor(holder, 200);
            var result = holder.GetUsedNotations();

            Assert.AreEqual(holder.SumTaken(), 200);

            // There should be one entry in results, and it should be 2 of that notation.
            Assert.AreEqual(result.Count, 1);
            Assert.AreEqual(result["100 Dollars"], 2);
        }
コード例 #2
0
        public static void TestSimpleRounder(IChangeSolver solver)
        {
            ICurrencyHolder holder = new CurrencyHolder(new List <ICurrencyNotation>()
            {
                new CurrencyNotation("10 Cent", 0.1M),
                new CurrencyNotation("25 Cent", 0.25M),
                new CurrencyNotation("75 Cent", 0.75M),
                new CurrencyNotation("5 Dollar", 5),
            });

            solver.FindReturnFor(holder, 24.41M);
            Assert.AreEqual(24.45M, holder.SumTaken());
        }
コード例 #3
0
        public static void TestComplicatedNumber(IChangeSolver solver)
        {
            ICurrencyHolder holder = new CurrencyHolder();

            solver.FindReturnFor(holder, 367);
            var result = holder.GetUsedNotations();

            Assert.AreEqual(367, holder.SumTaken());

            // There should be one entry in results, and it should be 2 of that notation.
            Assert.AreEqual(result.Count, 4);
            Assert.AreEqual(result["100 Dollars"], 3);
            Assert.AreEqual(result["10 Dollars"], 6);
            Assert.AreEqual(result["5 Dollars"], 1);
            Assert.AreEqual(result["2 Dollars"], 1);
        }
コード例 #4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Get a CurrencyHolder holding notations made from dataGrid data in notationsList
            var holder = new CurrencyHolder(notationsList.Select(f => f.GetNotation()).ToList());
            var value  = inputTextBox.Text;

            IChangeRounder rounder = null;

            if (NoroundRadioButton.IsChecked.GetValueOrDefault(false))
            {
                rounder = new NoRounding();
            }
            if (NaiveroundingRadioButton.IsChecked.GetValueOrDefault(false))
            {
                rounder = new NaiveRounder();
            }

            IChangeSolver solver = null;

            if (recursiveRadioButton.IsChecked.GetValueOrDefault(false))
            {
                solver = new RecursiveSolver(rounder);
            }
            if (iterativeRadioButton.IsChecked.GetValueOrDefault(false))
            {
                solver = new IterativeSolver(rounder);
            }

            if (solver != null && decimal.TryParse(value, out decimal amount))
            {
                solver.FindReturnFor(holder, amount);

                // Update dataGrid with result
                notationsList.ForEach(f => f.Update());
                dataGrid.Items.Refresh();

                changeLabel.Content = $"Amount left: {amount - holder.SumTaken()}";
            }
            else
            {
                changeLabel.Content = "Input is not a valid number";
                inputTextBox.Text   = "111.55";
            }
        }
コード例 #5
0
        public static void TestCustomNotationsWithLeftover(IChangeSolver solver)
        {
            ICurrencyHolder holder = new CurrencyHolder(new List <ICurrencyNotation>()
            {
                new CurrencyNotation("10 Cent", 0.1M),
                new CurrencyNotation("25 Cent", 0.25M),
                new CurrencyNotation("75 Cent", 0.75M),
                new CurrencyNotation("5 Dollar", 5),
            });

            solver.FindReturnFor(holder, 24.38M);

            var result = holder.GetUsedNotations();

            Assert.AreEqual(holder.SumTaken(), 24.35M);

            // There should be one entry in results, and it should be 2 of that notation.
            Assert.AreEqual(result.Count, 4);
            Assert.AreEqual(result["10 Cent"], 1);
            Assert.AreEqual(result["25 Cent"], 2);
            Assert.AreEqual(result["75 Cent"], 5);
            Assert.AreEqual(result["5 Dollar"], 4);
        }