-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
488 lines (406 loc) · 13.4 KB
/
functions.h
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "classes.h"
char rchars[] = { '+', '-', '\\', '/', ':', '*', '?', '"', '<', '>', '|', '(', ')', '{', '}', '\'', '%', ';', '^', '$', '&', '~', '`', '€' };
string GenerateIBAN();
Management::Management() {
sql_driver = sql::mysql::get_mysql_driver_instance();
connection = sql_driver->connect(sql_server, sql_username, sql_password);
connection->setSchema("bank_management");
entry();
}
void Management::entry() {
vmanagement ventry;
cout << "##################################\n";
cout << "###### WELCOME TO THE BANK #######\n";
cout << "##################################\n";
cout << "##################################\n";
cout << "##################################\n";
cout << "#### 1. Create a bank account ####\n";
cout << "## 2. Login to the bank account ##\n";
cout << "###### 3. Bank manager login #####\n";
cout << "############ 4. Exit #############\n";
cout << "##################################\n";
cin >> ventry.button;
switch (ventry.button)
{
case 1:
create(); break;
case 2:
login(); break;
case 3:
man_login(); break;
case 4:
exit(0); break;
default:
cerr << "!!TYPED WRONG NUMBER!!\n\n";
entry();
break;
}
}
void Management::login() {
vmanagement vlogin;
cout << "##################################\n";
cout << "############# LOGIN ##############\n";
cout << "##################################\n";
cout << "##type -1 for back to entry page##\n";
cout << "############# Username: ";
cin >> vlogin.username;
if (vlogin.username == "-1") {
entry();
}
if (vlogin.username.length() > 16) {
cerr << "!!Maximum username character is 16!!\n\n";
create();
}
else if (vlogin.username.length() <= 0) {
cerr << "!!You didn't type username!!\n\n";
create();
}
search(rchars, vlogin.username);
cout << "############# Password: ";
cin >> vlogin.password;
if (vlogin.password == "-1") {
entry();
}
if (vlogin.password.length() > 16) {
cerr << "!!Maximum password character is 16!!\n\n";
create();
}
else if (vlogin.password.length() <= 0) {
cerr << "!!You didn't type password!!\n\n";
create();
}
else if (vlogin.password.length() <= 5) {
cerr << "!!Minimum password length is 6!!\n\n";
create();
}
search(rchars, vlogin.password);
db_login(vlogin.username, vlogin.password, connection);
cout << "##################################\n";
cout << "######Successfully logged in######\n";
cout << "##################################\n\n";
main_page(vlogin.username, connection);
}
void Management::create(){
vmanagement vcreate;
cout << "##################################\n";
cout << "############# CREATE #############\n";
cout << "##################################\n";
cout << "##type -1 for back to entry page##\n";
cout << "##################################\n";
cout << "############# Username: ";
cin >> vcreate.username;
if (vcreate.username == "-1") {
entry();
}
if (vcreate.username.length() > 16) {
cerr << "!!Maximum username character is 16!!\n\n";
create();
}
else if (vcreate.username.length() <= 0) {
cerr << "!!You didn't type username!!\n\n";
create();
}
search(rchars, vcreate.username);
cout << "############# Name: ";
cin >> vcreate.name;
if (vcreate.name == "-1") {
entry();
}
if (vcreate.name.length() > 32) {
cerr << "!!Maximum name character is 32!!\n\n";
create();
}
else if (vcreate.name.length() <= 0) {
cerr << "!!You didn't type name!!\n\n";
create();
}
cout << "############# Surname: ";
cin >> vcreate.surname;
if (vcreate.surname == "-1") {
entry();
}
if (vcreate.surname.length() > 32) {
cerr << "!!Maximum surname character is 32!!\n\n";
create();
}
else if (vcreate.surname.length() <= 0) {
cerr << "!!You didn't type surname!!\n\n";
create();
}
cout << "############# Password: ";
cin >> vcreate.password;
if (vcreate.password == "-1") {
entry();
}
if (vcreate.password.length() > 16) {
cerr << "!!Maximum password character is 16!!\n\n";
create();
}
else if (vcreate.password.length() <= 0) {
cerr << "!!You didn't type password!!\n\n";
create();
}
else if (vcreate.password.length() <= 5) {
cerr << "!!Minimum password length is 6!!\n\n";
create();
}
search(rchars, vcreate.password);
cout << "##### Confirm Password: ";
cin >> vcreate.confirm_password;
if (vcreate.confirm_password != vcreate.password) {
cerr << "!!Confirm password and password didn't match!!\n\n";
create();
}
db_create(vcreate.username, vcreate.name, vcreate.surname, vcreate.password, connection);
cout << "##################################\n";
cout << "#Successfully created an account##\n";
cout << "##################################\n\n";
entry();
}
void Management::man_login() {
vmanagement manlogin;
cout << "Manager Password (-1 for back to entry page): ";
cin >> manlogin.button;
if (manlogin.button == 12345) {
manager_page();
}
else if (manlogin.button == -1) {
entry();
}
else {
cerr << "!!WRONG PASSWORD!!\n\n";
man_login();
}
}
void Management::manager_page() {
vmanagement vmanager;
cout << "##################################\n";
cout << "########## MANAGER PAGE ##########\n";
cout << "##################################\n";
cout << "##type -1 for back to entry page##\n";
cout << "##################################\n";
cout << "########## 1. User List ##########\n";
cout << "######## 2. Transfer Logs ########\n";
cout << "##################################\n";
cin >> vmanager.button;
switch (vmanager.button)
{
case 1: {
sql::Statement* usernameStatement = connection->createStatement();
sql::ResultSet* usernameResult = usernameStatement->executeQuery("SELECT username, money FROM users");
int num = 1;
while (usernameResult->next()) {
cout << num << ": " << usernameResult->getString("username") << "| Money: "<< usernameResult->getInt("money") << endl;
cout << "--------------------------------------------------" << endl;
num++;
}
delete usernameStatement;
delete usernameResult;
manager_page();
break;
}
case 2: {
sql::Statement* logStatement = connection->createStatement();
sql::ResultSet* logResult = logStatement->executeQuery("SELECT sender_username, receiver_username, transfered_money, send_date FROM logs");
int num2 = 1;
while (logResult->next()) {
cout << num2 << ". Sender Username: " << logResult->getString("sender_username") << "| Receiver Username: " << logResult->getString("receiver_username") << "| Transfered Money: " << logResult->getInt("transfered_money") << "| Send Date: " << logResult->getString("send_date") << endl;
cout << "--------------------------------------------------" << endl;
num2++;
}
delete logStatement;
delete logResult;
manager_page();
break;
}
case -1:
entry();
break;
default:
cerr << "!!TYPED WRONG NUMBER!!\n\n";
manager_page();
break;
}
}
void Management::search(char rchars[], string username) {
int rchars_size = sizeof(rchars) / sizeof(rchars[0]);
int lusername = username.length();
for (int i = 0; i < lusername; i++) {
if (username[i] == ' ') {
cerr << "!!You can't leave space!!\n\n";
create();
}
for (int j = 0; j < rchars_size; j++) {
if (username[i] == rchars[j]) {
cerr << "!!You can't type restricted characters!!\n\n";
create();
}
}
}
}
void Management::db_login(string username, string password, sql::Connection* connection) {
sql::PreparedStatement* printStatement = connection->prepareStatement("SELECT password FROM users WHERE username = ?");
printStatement->setString(1, username);
sql::ResultSet* result = printStatement->executeQuery();
if (result->next()) {
string storedPassword = result->getString("password");
if (storedPassword != password) {
cerr << "!!Uncorrect password!!\n\n";
entry();
}
}
else {
cerr << "!!Username not found!!\n\n";
entry();
}
delete printStatement;
delete result;
}
void Management::db_create(string username, string name, string surname, string password, sql::Connection* connection) {
int default_money = 100000;
sql::PreparedStatement* checkStatement = connection->prepareStatement("SELECT COUNT(*) FROM users WHERE username = ?");
checkStatement->setString(1, username);
sql::ResultSet* result = checkStatement->executeQuery();
int count = 0;
if (result->next()) {
count = result->getInt(1);
}
delete checkStatement;
delete result;
if (count > 0) {
cerr << username << " already using. Please type another username!!\n\n";
create();
}
string iban = GenerateIBAN();
sql::PreparedStatement* checkIBAN = connection->prepareStatement("SELECT COUNT(*) FROM users WHERE iban = ?");
checkIBAN->setString(1, iban);
sql::ResultSet* ibanResult = checkIBAN->executeQuery();
while (ibanResult->next() && ibanResult->getInt(1) > 0) {
iban = GenerateIBAN();
}
delete checkIBAN;
delete ibanResult;
sql::PreparedStatement* printStatement = connection->prepareStatement("INSERT INTO users (username, name, surname, password, money, iban) VALUES (?, ?, ?, ?, ?, ?)");
printStatement->setString(1, username);
printStatement->setString(2, name);
printStatement->setString(3, surname);
printStatement->setString(4, password);
printStatement->setInt(5, default_money);
printStatement->setString(6, iban);
printStatement->executeUpdate();
delete printStatement;
}
void Management::main_page(string username, sql::Connection* connection) {
string name, surname, iban;
vmanagement vmain;
int money;
sql::PreparedStatement* printInfo = connection->prepareStatement("SELECT name, surname, money, iban FROM users WHERE username = ?");
printInfo->setString(1, username);
sql::ResultSet* resultInfo = printInfo->executeQuery();
if (resultInfo->next()) {
name = resultInfo->getString("name");
surname = resultInfo->getString("surname");
iban = resultInfo->getString("iban");
money = resultInfo->getInt("money");
}
cout << "##################################\n";
cout << "############## MAIN ##############\n";
cout << "##################################\n";
cout << "##type -1 for back to entry page##\n";
cout << "##################################\n";
cout << "##################################\n";
cout << "#Welcome to the bank, " << name << " " << surname << endl;
cout << "##################################\n";
cout << "#IBAN: " << iban << endl;
cout << "##################################\n";
cout << "#Money: " << money << endl;
cout << "##################################\n";
cout << "##################################\n";
cout << "########## 1. Send money #########\n";
cout << "############# 2. Exit ############\n";
cout << "##################################\n";
cin >> vmain.button;
switch (vmain.button)
{
case 1:
sending_money(username, connection, money);
case 2:
entry(); break;
default:
cerr << "!!TYPED WRONG NUMBER!!\n\n";
main_page(username, connection);
break;
}
}
void Management::sending_money(string username, sql::Connection* connection, int money) {
vmanagement vsending;
cout << "#How much you gonna send? ";
cin >> vsending.sending_money;
if (vsending.sending_money > money) {
cerr << "!!YOU DON'T HAVE ENOUGH MONEY!!\n\n";
main_page(username, connection);
}
if (vsending.sending_money <= 0) {
cerr << "!!YOU ENTERED WRONG NUMBER!!\n\n";
main_page(username, connection);
}
cout << "#What IBAN you gonna send? ";
cin >> vsending.sending_iban;
if (vsending.sending_iban.length() != 22) {
cerr << "!!ENTERED WRONG IBAN!!";
main_page(username, connection);
}
//update receiver
sql::PreparedStatement* rmoneyStatement = connection->prepareStatement("SELECT money, username FROM users WHERE iban = ?");
rmoneyStatement->setString(1, vsending.sending_iban);
sql::ResultSet* rmoneyResult = rmoneyStatement->executeQuery();
int rmoney;
string rusername;
if (rmoneyResult->next()) {
rmoney = rmoneyResult->getInt("money");
rusername = rmoneyResult->getString("username");
}
else {
cerr << "!!THERE IS NO SUCH IBAN!!\n\n";
main_page(username, connection);
}
delete rmoneyStatement;
delete rmoneyResult;
int total_money = rmoney + vsending.sending_money;
sql::PreparedStatement* setrmoneyStatement = connection->prepareStatement("UPDATE users SET money = ? WHERE iban = ?");
setrmoneyStatement->setInt(1, total_money);
setrmoneyStatement->setString(2, vsending.sending_iban);
setrmoneyStatement->executeUpdate();
delete setrmoneyStatement;
//update sender
int remaining_money = money - vsending.sending_money;
sql::PreparedStatement* printStatement = connection->prepareStatement("UPDATE users SET money = ? WHERE username = ?");
printStatement->setInt(1, remaining_money);
printStatement->setString(2, username);
printStatement->executeUpdate();
delete printStatement;
cout << "##################################\n";
cout << "#########SUCCESSFULLY SENT########\n";
cout << "##################################\n\n";
//save log
time_t currentTime = time(0);
char* tm = ctime(¤tTime);
string cTime = tm;
sql::PreparedStatement* saveLogStatement = connection->prepareStatement("INSERT INTO logs (sender_username, receiver_username, transfered_money, send_date) VALUES (?, ?, ?, ?)");
saveLogStatement->setString(1, username);
saveLogStatement->setString(2, rusername);
saveLogStatement->setInt(3, vsending.sending_money);
saveLogStatement->setString(4, cTime);
saveLogStatement->executeUpdate();
delete saveLogStatement;
main_page(username, connection);
}
string GenerateIBAN() {
ostringstream iban;
iban << "TR";
iban << setw(4) << setfill('0') << rand() % 10000;
for (int i = 0; i < 16; i++) {
iban << rand() % 10;
}
return iban.str();
}