Ejemplo n.º 1
0
        public void PrimaryDispenserCanWithdrawWithEmptyMoneyStack()
        {
            var moneyStacks = new List <EnglishMoneyStack>();

            moneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 2000, Quantity = 0
            });
            moneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 1000, Quantity = 10
            });

            EnglishAtmVault vault         = new EnglishAtmVault(moneyStacks);
            var             cashDispenser = new PrimaryCashDispenser(vault);
            var             atmTerminal   = new AtmTerminal(cashDispenser);

            var withdrawalAmount = 100m;

            atmTerminal.Withdraw(withdrawalAmount);
            var atmBalance = atmTerminal.Balance;
            var expected   = 0m;

            Assert.AreEqual(expected, atmBalance);
        }
Ejemplo n.º 2
0
        public void PrimaryDispenserReturnsLeastPiecesOfMoney()
        {
            EnglishAtmVault vault         = new EnglishAtmVault(atmVault);
            var             cashDispenser = new PrimaryCashDispenser(vault);
            var             atmTerminal   = new AtmTerminal(cashDispenser);

            decimal withdrawalAmount = 120m;

            var result = atmTerminal.Withdraw(withdrawalAmount);

            var fiftyPoundCount  = result.First(x => x.BaseValue == 5000).Quantity;
            var twentyPoundCount = result.First(x => x.BaseValue == 2000).Quantity;

            Assert.AreEqual(2, fiftyPoundCount);
            Assert.AreEqual(1, twentyPoundCount);
        }
Ejemplo n.º 3
0
        public void PrimaryDispenserCanWithdrawMoreThanOnce()
        {
            EnglishAtmVault vault         = new EnglishAtmVault(atmVault);
            var             cashDispenser = new PrimaryCashDispenser(vault);
            var             atmTerminal   = new AtmTerminal(cashDispenser);

            var withdrawalAmount1 = 100m;

            atmTerminal.Withdraw(withdrawalAmount1);
            var atmBalance1 = atmTerminal.Balance;
            var expected1   = 4438m;

            Assert.AreEqual(expected1, atmBalance1);

            var withdrawalAmount2 = 123m;

            atmTerminal.Withdraw(withdrawalAmount2);
            var atmBalance2 = atmTerminal.Balance;
            var expected2   = 4315m;

            Assert.AreEqual(expected2, atmBalance2);
        }
Ejemplo n.º 4
0
        public void PrimaryDispenserDoesntOverdraw()
        {
            // £3 in the vault
            var moneyStacks = new List <EnglishMoneyStack>();

            moneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 1, Quantity = 100
            });
            moneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 2, Quantity = 100
            });

            var vault         = new EnglishAtmVault(moneyStacks);
            var cashDispenser = new PrimaryCashDispenser(vault);
            var atmTerminal   = new AtmTerminal(cashDispenser);

            // £1.20 taken from the vault
            var withdrawalAmount = 3.2m;

            atmTerminal.Withdraw(withdrawalAmount);
        }
Ejemplo n.º 5
0
        private static void Main(string[] args)
        {
            string line = null;

            var atmMoneyStacks = new List <EnglishMoneyStack>();

            // £
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 100, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 100, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 500, Quantity = 50
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 1000, Quantity = 50
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 2000, Quantity = 50
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 5000, Quantity = 50
            });
            // p
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 1, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 2, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 5, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 10, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 20, Quantity = 100
            });
            atmMoneyStacks.Add(new EnglishMoneyStack()
            {
                BaseValue = 50, Quantity = 100
            });

            var           moneyVault = new EnglishAtmVault(atmMoneyStacks);
            CashDispenser cashDispenser;

            string algorithm = ConfigurationManager.AppSettings["DispensingAlgorithm"];

            // choose which algorithm to use based on a setting in the app.config
            if (algorithm == "1")
            {
                cashDispenser = new PrimaryCashDispenser(moneyVault);
            }
            else
            {
                cashDispenser = new SecondaryCashDispenser(moneyVault);
            }

            var atmTerminal = new AtmTerminal(cashDispenser);

            System.Console.WriteLine($"ATM balance is {atmTerminal.Balance.ToString("C")}");
            System.Console.WriteLine("Enter an amount to withdraw");

            while ((line = System.Console.ReadLine()) != "exit")
            {
                decimal valueToWithdraw;
                if (string.IsNullOrEmpty(line) || !decimal.TryParse(line, out valueToWithdraw))
                {
                    System.Console.WriteLine("Enter another amount to withdraw or type 'exit' to close the program");
                    continue;
                }

                decimal withdrawalAmount = decimal.Parse(line);
                var     moneyStackText   = new StringBuilder();

                try
                {
                    var result = atmTerminal.Withdraw(withdrawalAmount);

                    foreach (var MoneyStack in result)
                    {
                        decimal poundFormat = (decimal)MoneyStack.BaseValue / 100;
                        bool    isPound     = MoneyStack.BaseValue % 100 == 0;

                        moneyStackText.Append(isPound ? $"{MoneyStack.CurrencySymbol}{poundFormat}" : $"{MoneyStack.BaseValue}{MoneyStack.FractionalUnit}");
                        moneyStackText.Append($"x{MoneyStack.Quantity},");
                    }
                }
                catch (Exception e)
                {
                    moneyStackText = new StringBuilder();
                    moneyStackText.Append(e.Message);
                }

                System.Console.BackgroundColor = ConsoleColor.Blue;
                System.Console.ForegroundColor = ConsoleColor.White;
                System.Console.WriteLine(moneyStackText.ToString().TrimEnd(','));
                System.Console.ResetColor();
                System.Console.WriteLine($"ATM balance is {atmTerminal.Balance.ToString("C")}");
                System.Console.WriteLine("Enter another amount to withdraw or type 'exit' to close the program");
            }
        }