-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
203 lines (166 loc) · 5.95 KB
/
client.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "client.h"
#define ONE 1
#define PIN_LIMIT 999999
Client::Client() : pin_(-ONE) {}
Client::Client(const std::string& firstName, const std::string& lastName, const std::string& email, const std::string& password, const int& pin)
: User(firstName, lastName, email, password), pin_(pin), accounts_(ONE, Account()) {}
Client::Client(const std::string& firstName, const std::string& lastName, const std::string& email, const std::string& password, const int& pin, const std::string& accountName)
: User(firstName, lastName, email, password), pin_(pin), accounts_(ONE, Account(accountName)) {}
Client::Client(const std::string& firstName, const std::string& lastName, const std::string& email, const std::string& password, const int& pin, const std::vector<Account>& accounts)
: User(firstName, lastName, email, password), pin_(pin) {
for (int i = 0; i < accounts.size(); ++i) {
accounts_.push_back(accounts[i]);
}
}
Client::Client(const Client& other)
: User(other.getFirstName(), other.getLastName(), other.getEmail(), other.getPassword()), pin_(other.pin_) {
accounts_.clear();
for (int i = 0; i < other.accounts_.size(); ++i) {
accounts_.push_back(other.accounts_[i]);
}
}
Client::~Client() {}
Client& Client::operator=(const Client& other) {
setFirstName(other.getFirstName());
setLastName(other.getLastName());
setEmail(other.getEmail());
setPassword(other.getPassword());
pin_ = other.pin_;
accounts_.clear();
for (int i = 0; i < other.accounts_.size(); ++i) {
accounts_.push_back(other.accounts_[i]);
}
return *this;
}
int Client::getPin() const {
return pin_;
}
Account Client::getAccount(const std::string& accountName) const {
for (int i = 0; i < accounts_.size(); ++i) {
if (accounts_[i].getName() == accountName) {
return accounts_[i];
}
}
return Account();
}
std::vector<Account> Client::getAccounts() const {
return accounts_;
}
int Client::getAccountBalance(const std::string& accountName) const {
for (int i = 0; i < accounts_.size(); ++i) {
if (accounts_[i].getName() == accountName) {
return accounts_[i].getBalance();
}
}
return -1;
}
bool Client::contains(const std::string& accountName) const {
for (int i = 0; i < accounts_.size(); ++i) {
if (accounts_[i].getName() == accountName) {
return true;
}
}
return false;
}
void Client::displayUser() const {
std::cout << "Name: " << getFullName() << '\n'
<< "Email: " << getEmail() << '\n';
std::cout << '\n';
}
void Client::displayAccounts() const {
for (int i = 0; i < accounts_.size(); ++i) {
std::cout << "\"" << accounts_[i].getName() << "\"" << " Account:" << '\n'
<< "Balance: $" << accounts_[i].getBalance() << '\n'
<< "Account Number: " << accounts_[i].getAccountNumber() << '\n'
<< "Routing Number: " << accounts_[i].getRoutingNumber() << '\n';
std::cout << '\n';
}
}
void Client::updateUser(const Client& client) {
if (client.getFirstName() != "NULL") {
setFirstName(client.getFirstName());
}
if (client.getLastName() != "NULL") {
setLastName(client.getLastName());
}
if (client.getEmail() != "NULL") {
setEmail(client.getEmail());
}
if (client.getPassword() != "NULL") {
setPassword(client.getPassword());
}
if (client.getPin() != -1) {
updatePin(client.getPin());
}
}
void Client::updatePin(const int& pin) {
if (pin < 0 || pin > PIN_LIMIT) { //6-digit limit for pin number
std::cout << "Error. Pin Number must be a 4 - 6 digit number." << '\n';
return;
}
pin_ = pin;
}
void Client::updateAccount(const Account& oldAccount, const Account& newAccount) {
if (!contains(oldAccount.getName())) {
return;
}
for (int i = 0 ; i < accounts_.size(); ++i) {
if (accounts_[i].getName() == oldAccount.getName()) {
accounts_[i].setName(newAccount.getName());
accounts_[i].setBalance(newAccount.getBalance());
accounts_[i].setAccountNumber(newAccount.getAccountNumber());
accounts_[i].setRoutingNumber(newAccount.getRoutingNumber());
return;
}
}
}
void Client::updateAccounts(const std::vector<Account>& accounts) {
accounts_ = accounts;
}
void Client::updateAccountName(const std::string& accountName, const std::string& updatedName) {
if (accountName.empty() || updatedName.empty()) {
return;
}
if (!contains(accountName)) {
return;
}
for (int i = 0; i < accounts_.size(); ++ i) {
if (accounts_[i].getName() == accountName) {
accounts_[i].setName(updatedName);
return;
}
}
}
void Client::updateAccountBalance(const std::string& accountName, const int& amount) {
for (int i = 0; i < accounts_.size(); ++i) {
if (accounts_[i].getName() == accountName) {
accounts_[i].setBalance(amount);
return;
}
}
}
void Client::addAccount(const Account& account) {
if (contains(account.getName())) {
std::cout << "Error. \'" << account.getName() << "\' already exists as an account." << '\n';
return;
}
accounts_.push_back(account);
}
bool Client::removeAccount(const std::string& accountName) {
if (!contains(accountName)) {
std::cout << "Error. \'" << accountName << "\' does not exist as an account." << '\n';
return false;
}
std::vector<Account>::iterator it = accounts_.begin();
for (; it != accounts_.end(); ++it) {
if (it->getName() == accountName) {
break;
}
}
if (!it->zeroBalance()) { //can't close an account that still has money in it
std::cout << "Error. \'" << it->getName() << "\' does not have a 0 balance! Please zero the account before closing." << '\n';
return false;
}
accounts_.erase(it);
return true;
}