-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBankAccount.cpp
More file actions
146 lines (122 loc) · 3.54 KB
/
BankAccount.cpp
File metadata and controls
146 lines (122 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ===========================================================================
// ConceptualExample01.cpp // Memento Pattern
// ===========================================================================
#include <iostream>
#include <string>
#include <vector>
#include <memory>
namespace BankAccountMemento
{
class BankAccount
{
friend std::ostream& operator<<(std::ostream& os, const BankAccount& ac);
private:
int m_balance;
struct Memento
{
Memento(int balance) : m_balance{ balance } {}
int m_balance;
};
size_t m_index;
std::vector<std::shared_ptr<Memento>> m_mementos;
public:
// c'tors
BankAccount();
BankAccount(int balance);
// public interface
void deposit(int amount);
void withdraw(int amount);
void undo();
void redo();
private:
void restore(const std::shared_ptr<Memento>& memento);
};
BankAccount::BankAccount() : BankAccount{ 0 } {}
BankAccount::BankAccount(int balance)
: m_balance{ balance }, m_index{ 0 }
{
m_mementos.push_back(std::make_shared<Memento>(m_balance));
}
void BankAccount::deposit(int amount) {
m_balance += amount;
m_mementos.push_back(std::make_shared<Memento>(m_balance));
m_index++;
}
void BankAccount::withdraw(int amount) {
m_balance -= amount;
m_mementos.push_back(std::make_shared<Memento>(m_balance));
m_index++;
}
void BankAccount::restore(const std::shared_ptr<BankAccount::Memento>& memento) {
if (memento) {
m_balance = memento->m_balance;
m_mementos.push_back(memento);
m_index = m_mementos.size() - 1;
}
}
void BankAccount::undo() {
if (m_index > 0) {
--m_index;
m_balance = m_mementos[m_index]->m_balance;
}
}
void BankAccount::redo() {
if ((m_index + 1) < m_mementos.size()) {
++m_index;
m_balance = m_mementos[m_index]->m_balance;
}
}
std::ostream& operator<<(std::ostream& os, const BankAccount& ac) {
return os << "Balance: " << ac.m_balance;
}
static void clientCode_01()
{
BankAccount ba{ 0 };
ba.deposit(50);
ba.deposit(100);
ba.withdraw(75);
std::cout << ba << std::endl; // 75
ba.undo();
std::cout << "Undo 1: " << ba << std::endl;
ba.undo();
std::cout << "Undo 2: " << ba << std::endl;
ba.redo();
std::cout << "Redo 1: " << ba << std::endl;
ba.redo();
std::cout << "Redo 2: " << ba << std::endl;
}
/*
balance: 175
Undo 1: balance: 150
Undo 2: balance: 100
Redo 2: balance: 150
*/
static void clientCode_02()
{
BankAccount ba{ 100 };
ba.deposit(50);
ba.deposit(25);
std::cout << ba << std::endl; // 175
ba.undo();
std::cout << "Undo 1: " << ba << std::endl;
ba.undo();
std::cout << "Undo 2: " << ba << std::endl;
ba.redo();
std::cout << "Redo 2: " << ba << std::endl;
}
/*
balance: 175
Undo 1: balance: 150
Undo 2: balance: 100
Redo 2: balance: 150
*/
}
void test_bank_account_example()
{
using namespace BankAccountMemento;
clientCode_01();
clientCode_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================