MCPcopy Create free account
hub / github.com/Apress/design-patterns-in-modern-cpp / BankAccountCommand

Class BankAccountCommand

Behavioral/Command/command_undo.cpp:40–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38};
39
40struct BankAccountCommand : Command
41{
42 BankAccount& account;
43 enum Action { deposit, withdraw } action;
44 int amount;
45
46 BankAccountCommand(BankAccount &account, Action action, int amount) : account(account), action(action),
47 amount(amount) {
48 succeeded = false;
49 }
50
51 void call() override {
52 switch (action)
53 {
54 case deposit:
55 account.deposit(amount);
56 succeeded = true;
57 break;
58 case withdraw:
59 succeeded = account.withdraw(amount);
60 break;
61 }
62 }
63
64 void undo() override {
65 if (!succeeded) return;
66
67 switch (action)
68 {
69 case deposit:
70 account.withdraw(amount);
71 break;
72 case withdraw:
73 account.deposit(amount);
74 break;
75 }
76 }
77
78
79};
80
81int main()
82{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected