Repository navigation
Expand file tree
/
Copy pathBankDB.cpp
More file actions
318 lines (256 loc) · 5.7 KB
/
Copy pathBankDB.cpp
File metadata and controls
318 lines (256 loc) · 5.7 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "BankDB.h"
void BankDB::loadFromFile(const std::string& path)
{
accounts.clear();
std::ifstream file(path);
if (!file.is_open())
{
std::cout << "File error\n";
return;
}
std::string line;
while (std::getline(file, line))
{
std::stringstream ss(line);
std::string accountNumberStr;
std::string clientName;
std::string moneySumStr;
std::string interestRateStr;
std::string dateStr;
std::string cardStr;
std::getline(ss, accountNumberStr, ';');
std::getline(ss, clientName, ';');
std::getline(ss, moneySumStr, ';');
std::getline(ss, interestRateStr, ';');
std::getline(ss, dateStr, ';');
std::getline(ss, cardStr, ';');
int accountNumber = std::stoi(accountNumberStr);
float moneySum = std::stof(moneySumStr);
float interestRate = std::stof(interestRateStr);
CDate openingDate = stringToDate(dateStr);
bool hasCard = std::stoi(cardStr);
BankAccount account(
accountNumber,
clientName,
moneySum,
interestRate,
openingDate,
hasCard
);
accounts.push_back(account);
}
file.close();
std::cout << "Database successfully loaded from " << path << '\n';
fileLoaded = true;
}
void BankDB::saveToFile(const std::string& filename) const
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
std::ofstream file(filename);
if (!file.is_open())
{
std::cout << "File error\n";
return;
}
for (const BankAccount& account : accounts)
{
file << account.toFileString() << "\n";
}
file.close();
std::cout << "Successfully saved file to " << filename << "\n";
}
void BankDB::addAccount()
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
int accountNumber;
string name;
float money;
float interest;
bool hasCard;
CDate today = getCurrentDate();
while (true)
{
std::cout << "Account number: ";
std::cin >> accountNumber;
if (!accountNumberExists(accountNumber) && accountNumber>0)
break;
std::cout << "You cannot add " << accountNumber << " as account number\n";
}
std::cin.ignore();
std::cout << "Full name: ";
std::getline(std::cin, name);
while (true)
{
std::cout << "Money on account (in dollars): ";
std::cin >> money;
if (money >=0)
break;
std::cout << "A client cannot have negative balance on their account\n";
}
while (true)
{
std::cout << "Interest rate (%): ";
std::cin >> interest;
if (interest >= 0)
break;
std::cout << "A client cannot have negative interest on their account\n";
}
while (true)
{
char temp;
std::cout << "Card connected (1/0): ";
std::cin >> temp;
if (temp == '1') {
hasCard = true;
break;
}
else if (temp == '0') {
hasCard = false;
break;
}
std::cout << "Please enter 1 for yes or 0 for no\n";
}
BankAccount account(
accountNumber,
name,
money,
interest,
today,
hasCard
);
accounts.push_back(account);
std::cout << "Account added\n";
}
void BankDB::removeAccount()
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
int index;
if (accounts.empty())
{
std::cout << "Database is empty\n";
return;
}
std::cout << "Enter index (1 - " << accounts.size() << "): ";
std::cin >> index;
if (index < 0 || index >= accounts.size()+1)
{
std::cout << "Invalid index\n";
return;
}
accounts.erase(accounts.begin() + index-1);
std::cout << "Account removed\n";
}
void BankDB::sortByAccountNumber()
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
std::sort(accounts.begin(), accounts.end(),
[](const BankAccount& a, const BankAccount& b)
{
return a.getAccountNumber() < b.getAccountNumber();
});
}
void BankDB::findByNumber()
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
int number;
std::cout << "Enter account number: ";
std::cin >> number;
for (int i = 0; i < accounts.size(); i++)
{
if (accounts[i].getAccountNumber() == number)
{
accounts[i].printAccount();
return;
}
}
std::cout << "Account not found\n";
}
void BankDB::filteredByDateRange(CDate from, CDate to) const
{
if (!fileLoaded) {
std::cout << "Please load the database file first\n";
return;
}
BankDB filtered;
for (const auto& acc : accounts)
{
CDate d = acc.getOpeningDate();
if (!(d < from) && !(to < d))
{
filtered.accounts.push_back(acc);
}
}
filtered.printAll();
}
bool BankDB::accountNumberExists(const int number) const
{
for (const BankAccount& account : accounts)
{
if (account.getAccountNumber() == number)
{
return true;
}
}
return false;
}
void BankDB::printAll() const
{
if (accounts.empty())
{
std::cout << "Database is empty\n";
return;
}
for (int i = 0; i < accounts.size(); i++)
{
std::cout << (i < 10 ? "0" : "") << i+1 << ". ";
accounts[i].printAccount();
}
}
std::vector<std::string> BankDB::getClientsForProlongation()
{
const CDate& currentDate = getCurrentDate();
std::set<std::string> uniqueClients;
for (const BankAccount& acc : accounts)
{
if (differenceInYears(acc.getOpeningDate(), currentDate) >= 1)
{
uniqueClients.insert(acc.getClientFullName());
}
}
std::cout << "Clients for prolongation: \n";
int c = 0;
for (auto i : uniqueClients) {
c++;
std::cout << c << ". " << i << "\n";
}
return std::vector<std::string>(uniqueClients.begin(), uniqueClients.end());
}
void BankDB::calculateFutureDeposits(int months)
{
BankDB newDB = *this;
for (BankAccount& acc : newDB.accounts)
{
double monthlyRate = acc.getInterestRate() / 100.0 / 12.0;
double newSum = acc.getMoneySum() *
std::pow(1.0 + monthlyRate, months);
acc.setMoneySum(newSum);
}
std::string filename = "DB_" + std::to_string(months) + ".txt";
newDB.saveToFile(filename);
newDB.printAll();
}