示例#1
0
            // RunTransactions works on BankAccount and ProperBankAccount
            public void RunTransactions(BankAccount acct)
            {
                // if it has an overdraft facility, initialise its value
                if (acct is ProperBankAccount pacct)
                {
                    pacct.Overdraft = 200M;
                }

                /* or:
                 * if (acct is ProperBankAccount) {
                 *  ((ProperBankAccount)acct).overdraft = 200;
                 * }
                 */
                acct.ShowAccount();
                acct.ShowBalance();

                // first, deposit something
                var x = 600M;

                Console.WriteLine("Depositing " + x);
                acct.Deposit(x);
                acct.ShowBalance();

                // then, try to withdraw something
                var y = 400M;

                Console.WriteLine("Withdrawing " + y);
                try
                {
                    acct.Withdraw(y);
                }
                catch (InsufficientBalanceException e)
                {
                    Console.WriteLine("InsufficientBalance {0} for withdrawl of {1}", acct.GetBalance(), y);
                }

                acct.ShowBalance();
                // then, try to withdraw the same amount again
                Console.WriteLine("Withdrawing " + y);
                try
                {
                    acct.Withdraw(y);
                }
                catch (InsufficientBalanceException e)
                {
                    Console.WriteLine("InsufficientBalance {0} for withdrawl of {1}", acct.GetBalance(), y);
                }

                acct.ShowBalance();
                acct.ShowAccount();
            }