Example #1
0
        /// <summary>
        /// Attempts to deposit funds into an account
        /// </summary>
        /// <param name="account">The account to deposit into</param>
        static void DoDeposit(Account account)
        {
            decimal            amount      = ReadDecimal("Enter the amount");
            DepositTransaction transaction = new DepositTransaction(account, amount);

            try
            {
                transaction.Execute();
            }
            catch (InvalidOperationException)
            {
                transaction.Print();
                return;
            }
            transaction.Print();
        }
Example #2
0
        static void Main(string[] args)
        {
            /*********************************************************
             *  TESTS
             ********************************************************/
            Account acc  = new Account("Peter Stacey");
            Account acc1 = new Account("Jane Doe", 100);
            Account acc2 = new Account("John Doe", -500);

            Debug.Assert(acc.Balance == 0);
            Debug.Assert(acc1.Balance == 100);
            Debug.Assert(acc2.Balance == 0);

            // Test deposit success and rollback
            DepositTransaction dep = new DepositTransaction(acc, 500);

            dep.Print();
            dep.Execute();
            Debug.Assert(acc.Balance == 500);
            Debug.Assert(dep.Executed == true);
            Debug.Assert(dep.Success == true);
            dep.Print();

            dep.Rollback();
            Debug.Assert(acc.Balance == 0);
            Debug.Assert(dep.Reversed == true);
            dep.Print();

            Console.WriteLine("\n\n");

            // Test withdraw success and rollback
            WithdrawTransaction with = new WithdrawTransaction(acc1, 50);

            with.Print();
            with.Execute();
            Debug.Assert(acc1.Balance == 50);
            Debug.Assert(with.Executed == true);
            Debug.Assert(with.Success == true);
            with.Print();

            with.Rollback();
            Debug.Assert(acc1.Balance == 100);
            Debug.Assert(with.Reversed == true);
            with.Print();

            Console.WriteLine("\n\n");

            // Test transfer success and rollback
            TransferTransaction tran = new TransferTransaction(acc1, acc, 50);

            tran.Print();
            tran.Execute();
            Debug.Assert(acc.Balance == 50);
            Debug.Assert(acc1.Balance == 50);
            Debug.Assert(tran.Executed == true);
            Debug.Assert(tran.Success == true);

            tran.Rollback();
            Debug.Assert(acc.Balance == 0);
            Debug.Assert(acc1.Balance == 100);
            Debug.Assert(tran.Reversed == true);
            tran.Print();

            Console.WriteLine("\n\n");

            // Test withdraw failure when there is insufficient funds to complete the transaction
            // followed by repeating the withdraw after funds are deposited
            WithdrawTransaction with2 = new WithdrawTransaction(acc, 100);

            with2.Print();
            try
            {
                with2.Execute();
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
            }

            Debug.Assert(acc.Balance == 0);
            Debug.Assert(with2.Success == false);
            Debug.Assert(with2.Executed == true);
            with2.Print();

            DepositTransaction dep2 = new DepositTransaction(acc, 500);

            dep2.Execute();
            dep2.Print();

            try
            {
                with2.Execute();
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
            }

            Debug.Assert(acc.Balance == 400);
            Debug.Assert(with2.Success == true);
            Debug.Assert(with2.Executed == true);
            with2.Print();

            Console.WriteLine("\n\n");

            // Test fail to rollback before deposit or withdraw are
            // complete
            DepositTransaction  dep3  = new DepositTransaction(acc, 500);
            WithdrawTransaction with3 = new WithdrawTransaction(acc, 500);
            TransferTransaction tran2 = new TransferTransaction(acc, acc1, 200);

            try
            {
                dep3.Rollback();
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
            }

            try
            {
                with3.Rollback();
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
            }

            try
            {
                tran2.Rollback();
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
            }

            Console.WriteLine("\n\n");

            // Try to rollback deposit from account with insufficient funds
            DepositTransaction  dep4  = new DepositTransaction(acc2, 100);
            WithdrawTransaction with4 = new WithdrawTransaction(acc2, 100);

            dep4.Execute();
            with4.Execute();
            try
            {
                dep4.Rollback();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            Console.WriteLine("\n\n");

            /*********************************************************
             *  CLI
             ********************************************************/
            do
            {
                MenuOption chosen = ReadUserOption();
                switch (chosen)
                {
                case MenuOption.Withdraw:
                    DoWithdraw(acc); break;

                case MenuOption.Deposit:
                    DoDeposit(acc); break;

                case MenuOption.Transfer:
                    DoTransfer(acc, acc1); break;

                case MenuOption.Print:
                    DoPrint(acc); break;

                case MenuOption.Quit:
                default:
                    Console.WriteLine("Goodbye");
                    System.Environment.Exit(0); // terminates the program
                    break;                      // unreachable
                }
            } while (true);
        }