Exemple #1
0
        // Prompts the user from stdin to record a withdrawal from their account
        private static void Withdrawal(Services.BankLedger ledger)
        {
            Console.WriteLine("Enter amount to withdraw:");
            Console.WriteLine($"Type {EXIT_KEYWORD} to return to menu.");
            Console.Write("$");
            var amount         = -1.0;
            var input          = Console.ReadLine();
            var currentBalance = ledger.GetCurrentBalance();

            // TODO add regex for $xx.xx
            //verifying user typed valid input (can be parsed as double and > 0, or exit keyword)
            while (!input.Equals(EXIT_KEYWORD, StringComparison.OrdinalIgnoreCase) && !(double.TryParse(input, out amount) && amount <= currentBalance && amount >= 0))
            {
                Console.WriteLine();
                if (amount > 0)
                {
                    // user entered amount greater than their balance. Reset amount to -1 to continue loop
                    Console.WriteLine($"Insufficient funds to withdraw ${amount}");
                    amount = -1.0;
                }
                else
                {
                    Console.WriteLine("Invalid amount entered.");
                }
                Console.WriteLine($"Enter an amount to withdraw, or type {EXIT_KEYWORD} to return to menu:");
                Console.Write("$");
                input = Console.ReadLine();
            }

            // don't withdraw if they typed the exit keyword
            if (!input.Equals(EXIT_KEYWORD, StringComparison.OrdinalIgnoreCase))
            {
                ledger.Withdrawal(amount);
                Console.WriteLine();
                Console.WriteLine($"Withdrawal successful. New account balance: {ledger.GetCurrentBalance()}");
            }
            Console.WriteLine();
        }
 public void TestWithdrawal_LoggedOut()
 {
     Assert.ThrowsException <NullReferenceException>(() => ledger.Withdrawal(0));
 }