Example #1
0
        public override void Call()
        {
            switch (action)
            {
            case Action.Deposit:
                account.Deposit(amount);
                succeeded = true;
                break;

            case Action.Withdraw:
                succeeded = account.Withdraw(amount);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            BankAccount ba       = new BankAccount();
            var         commands = new List <BankAcoountCommand>
            {
                new BankAcoountCommand(ba, BankAcoountCommand.Action.Deposit, 100),
                new BankAcoountCommand(ba, BankAcoountCommand.Action.Withdraw, 1500),
            };

            WriteLine(ba);

            foreach (var c in commands)
            {
                c.Call();
            }

            WriteLine(ba);

            foreach (var c in Enumerable.Reverse(commands))
            {
                c.Undo();
            }

            WriteLine(ba);


            var ba2        = new BankAccount2();
            var cmdDeposit = new BankAccountCommand(ba2,
                                                    BankAccountCommand.Action.Deposit, 100);
            var cmdWithdraw = new BankAccountCommand(ba2,
                                                     BankAccountCommand.Action.Withdraw, 1000);

            cmdDeposit.Call();
            cmdWithdraw.Call();
            WriteLine(ba);
            cmdWithdraw.Undo();
            cmdDeposit.Undo();
            WriteLine(ba);


            var from = new BankAccount2();

            from.Deposit(100);
            var to = new BankAccount2();

            var mtc = new MoneyTransferCommand(from, to, 1000);

            mtc.Call();


            // Deposited $100, balance is now 100
            // balance: 100
            // balance: 0

            WriteLine(from);
            WriteLine(to);
        }