-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank System.cpp
More file actions
2464 lines (1861 loc) · 71.1 KB
/
Bank System.cpp
File metadata and controls
2464 lines (1861 loc) · 71.1 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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Created by Abdulrahman Mohammad Salem
#pragma warning(disable: 4996)
#include <ctime>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
const string clientsFileName = "Clients.txt";
const string usersFileName = "Users.txt";
const string settingsFileName = "Settings.txt";
const string historyFileName = "History.txt";
const string messageFileName = "Messages.txt";
enum enMainMenuOptions {
eManageClients = 1, eManageUsers = 2, eShowActiveUser = 3, eSettings = 4, eLogout = 5
};
enum enManageClientsMenuOptions {
eShowClientList = 1, eAddNewClient = 2, eDeleteClient = 3,
eUpdateClient = 4, eFindClient = 5, eTransactions = 6
};
enum enManageUsersMenuOptions {
eShowUsersList = 1, eAddNewUser = 2, eDeleteUser = 3,
eUpdateUser = 4, eFindUser = 5, eMainMenuFromManage = 6
};
enum enSettingsOptions {
eClientsTableSettings = 1, eUsersTableSettings = 2, eTextColor = 3, eBackgroundColor = 4, eBackToMainMenu = 5
};
enum enClientTableFields {
eAccountNumber, ePinCode, eName, ePhone, eAccountBalance,
eDateCreatedClients, eDateModifiedClients, eStatus
};
enum enUserTableFields {
eUsername, ePassword, ePermissions, eDateCreatedUsers, eDateModifiedUsers
};
enum enTextBackground {
eBackground = 0, eText = 1
};
enum enTransactionsMenuOptions {
eDeposit = 1, eWithdraw = 2, eTotalBalances = 3, eManageClientsScreenFromTransactions = 4
};
enum enWhereToGoBack {
eMainMenuScreen, eManageClientsScreen, eManageUsersScreen
};
enum enClientUpdateType {
eClientCreated, eFullUpdate, eOnlyDeposit, eOnlyWithdraw, eTransferedMoney,
eReceivedMoney, eChangedPhoneNumber, eChangedPinCode, eAccountLocked, eAccountUnlocked
};
struct sClient {
string accountNumber = "";
string pinCode = "";
string name = "";
string phone = "";
string dateCreated;
string dateModified = "-";
double accountBalance = 0;
bool markForDelete = false;
bool markAsLocked = false;
};
struct sClientForHistory {
string accountNumber = "";
string pinCode = "";
string name = "";
string phone = "";
string date = "";
double accountBalance = 0;
enClientUpdateType actionDone = enClientUpdateType::eClientCreated;
};
struct sUser {
string username = "";
string password = "";
short manageClientsPermissions = 0;
short manageUsersPermissions = 0;
string dateCreated;
string dateModified = "-";
bool markForDelete = false;
};
struct sSettings {
short clientTableSettings = 0;
short userTableSettings = 0;
string programColors;
};
vector<sClient> vClients;
vector<sUser> vUsers;
sUser activeUser;
sSettings globalSettings;
void displayMainMenuScreen();
void displayTransactionsMenuScreen();
void displayLoginScreen();
void displayManageUsersScreen();
void displayManageClientsScreen();
void displaySettingsScreen();
void displayFindClientScreen();
void displayClientHistoryScreen(const string &);
void displayPreviousFindUserScreen(const string &);
vector<string> splitStringToVector(string & line, const string delimiter) {
unsigned short delimiterPosition;
vector<string> vector;
while ((delimiterPosition = line.find(delimiter)) != (unsigned short) string::npos) {
vector.push_back(line.substr(0, delimiterPosition));
line.erase(0, delimiterPosition + delimiter.length());
}
vector.push_back(line);
return vector;
}
vector<string> splitStringToVector(string str, const char del) {
str += del;
vector<string> vector;
string wrd = "";
for (unsigned short i = 0; i < str.length(); i++) {
if (str[i] != del) {
wrd += str[i];
if (str[i + 1] == del) {
vector.push_back(wrd);
wrd = "";
}
}
}
return vector;
}
short readNumberInRange(const string message, const short from, const short to, const bool activateCancel = false) {
short number = 0;
while (true) {
cout << message;
cin >> number;
if (cin.fail() || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else if ((activateCancel && number == -99) || (number >= from && number <= to))
return number;
}
}
string readWord(const string message, const short length = 100) {
string str;
while (true) {
cout << message;
cin >> str;
if (cin.peek() != '\n' || cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else if (str.length() <= length)
return str;
}
}
string readName(string message) {
string name = "";
do {
cout << message;
getline(cin >> ws, name);
message = "The name is too long, enter a shorter name: ";
} while (name.length() > 28);
return name;
}
short getDigitCount(const string & str) {
short count = 0;
for (const char & C : str)
if (isdigit(C))
count++;
return count;
}
string readPhoneNumber(string message) {
string phoneNumber;
while (true) {
cout << message;
cin >> phoneNumber;
if (cin.fail() || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else if (getDigitCount(phoneNumber) == phoneNumber.length() && phoneNumber.length() <= 14)
return phoneNumber;
}
}
char readCharacter(const string msg) {
char chr = ' ';
while (true) {
cout << msg;
cin >> chr;
if (cin.fail() || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else
return chr;
}
}
double readPositiveNumber(const string message) {
float number;
while (true) {
cout << message;
cin >> number;
if (cin.fail() || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else if (number > 0)
return number;
}
}
string getSystemDateTime() {
time_t timeNow = time(0);
tm * timeNowStruct = localtime(&timeNow);
ostringstream result;
result << timeNowStruct->tm_mday << '/';
result << timeNowStruct->tm_mon + 1 << '/';
result << timeNowStruct->tm_year + 1900 << ' ';
result << setw(2) << setfill('0') << timeNowStruct->tm_hour << ':';
result << setw(2) << setfill('0') << timeNowStruct->tm_min << ':';
result << setw(2) << setfill('0') << timeNowStruct->tm_sec;
return result.str();
}
bool shouldClientFieldAppear(const short & clientTableSettings, const enClientTableFields & tableField) {
if (clientTableSettings == -1)
return true;
short settingAsPower = pow(2, (short) tableField);
return (clientTableSettings & settingAsPower) == settingAsPower;
}
bool shouldUserFieldAppear(const short & userTableSettings, const enUserTableFields & tableField) {
if (userTableSettings == -1)
return true;
short settingAsPower = pow(2, (short) tableField);
return (userTableSettings & settingAsPower) == settingAsPower;
}
void appendDefaultSettingsToFile(const string & settingsFilePath) {
fstream settingsFile(settingsFilePath, ios::out);
if (settingsFile.is_open()) {
settingsFile << "-1/-1/0F";
settingsFile.close();
} else
cerr << "Error: Cannot open settings file to append default settings.";
}
bool isFileEmpty(const string & fileName) {
fstream file(fileName, ios::in | ios::app);
if (file.is_open()) {
string s;
getline(file, s);
file.close();
return s == "";
}
cerr << "Error: File could not be opened.";
return false;
}
sSettings loadSettingsFromFile(const string & settingsFilePath) {
if (isFileEmpty(settingsFilePath))
appendDefaultSettingsToFile(settingsFilePath);
fstream settingsFile(settingsFileName, ios::in);
sSettings settings;
if (settingsFile.is_open()) {
string settingsString;
getline(settingsFile, settingsString);
vector<string> vSettings = splitStringToVector(settingsString, '/');
settings.clientTableSettings = stoi(vSettings[0]);
settings.userTableSettings = stoi(vSettings[1]);
settings.programColors = vSettings[2];
settingsFile.close();
} else
cerr << "Error: Cannot open settings file.";
return settings;
}
string convertSettingsToLine(const sSettings & settings) {
return to_string(settings.clientTableSettings) + '/' + to_string(settings.userTableSettings) + '/' + settings.programColors;
}
void saveSettingsToFile(const sSettings & settings, const string & settingsFilePath) {
fstream settingsFile(settingsFilePath, ios::out);
if (settingsFile.is_open()) {
settingsFile << convertSettingsToLine(settings);
settingsFile.close();
}
}
void printMainMenuScreen() {
cout << "==================================================";
cout << "\n Main Menu Screen";
cout << "\n==================================================";
cout << "\n\t[1]: Manage Clients.";
cout << "\n\t[2]: Manage Users.";
cout << "\n\t[3]: Show Active User.";
cout << "\n\t[4]: Settings.";
cout << "\n\t[5]: Logout.";
cout << "\n==================================================" << endl;
}
void printManageClientsMenu() {
cout << "==================================================";
cout << "\n Manage Clients Screen";
cout << "\n==================================================";
cout << "\n\t[1]: Show Clients List.";
cout << "\n\t[2]: Add new Client.";
cout << "\n\t[3]: Delete Client.";
cout << "\n\t[4]: Update Client Info.";
cout << "\n\t[5]: Find Client.";
cout << "\n\t[6]: Transactions.";
cout << "\n\t[7]: Main Menu.";
cout << "\n==================================================" << endl;
}
vector<string> copyFileToVector(const string & fileName) {
fstream file(fileName, ios::in);
vector<string> vector;
if (file.is_open()) {
string currentLine;
while (getline(file, currentLine))
if (currentLine != "")
vector.push_back(currentLine);
file.close();
} else
cerr << "Error: Cannot open this file.";
return vector;
}
sClient convertClientLineToRecord(string & line) {
vector<string> vClientData = splitStringToVector(line, "#//#");
sClient client;
client.accountNumber = vClientData.at(0);
client.pinCode = vClientData.at(1);
client.name = vClientData.at(2);
client.phone = vClientData.at(3);
client.accountBalance = stod((vClientData.at(4)));
client.dateCreated = vClientData.at(5);
client.dateModified = vClientData.at(6);
client.markAsLocked = vClientData.at(7) == "1";
return client;
}
vector<sClient> loadClientsFromFile(const string fileName) {
fstream clientsFile(fileName, ios::in);
vector<sClient> vctClients;
if (clientsFile.is_open()) {
string currentLine;
while (getline(clientsFile, currentLine))
if (currentLine != "")
vctClients.push_back(convertClientLineToRecord(currentLine));
clientsFile.close();
} else
cerr << "Error: Connot open clients file.";
return vctClients;
}
void backToMainMenuScreen() {
cout << "\nPress any key to return to Main Menu Screen...";
system("pause>nul");
displayMainMenuScreen();
}
void backToManageClientsScreen() {
cout << "\nPress any key to return to Manage Clients Menu Screen...";
system("pause>nul");
displayManageClientsScreen();
}
void backToManageUsersScreen() {
cout << "\nPress any key to return to Manage Users Menu Screen...";
system("pause>nul");
displayManageUsersScreen();
}
void printDashes(short n) {
for (n; n > 0; n--)
cout << '-';
}
void printLineForClientsTable() {
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountNumber))
printDashes(19);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePinCode))
printDashes(13);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eName))
printDashes(30);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePhone))
printDashes(16);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountBalance))
printDashes(22);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateCreatedClients))
printDashes(21);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateModifiedClients))
printDashes(21);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eStatus))
printDashes(11);
cout << '-';
}
void printClientsTableHeader() {
cout << '\n';
printLineForClientsTable();
cout << "\n|";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountNumber))
cout << " Account Number |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePinCode))
cout << " PIN Code |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eName))
cout << " Client Name |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePhone))
cout << " Phone |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountBalance))
cout << " Balance |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateCreatedClients))
cout << " Date Created |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateModifiedClients))
cout << " Date Modified |";
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eStatus))
cout << " Status |";
cout << '\n';
printLineForClientsTable();
cout << endl;
}
void printClientsTable() {
for (sClient & currentClient : vClients) {
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountNumber))
cout << "| " << left << setw(17) << currentClient.accountNumber;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePinCode))
cout << "| " << left << setw(11) << currentClient.pinCode;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eName))
cout << "| " << left << setw(28) << currentClient.name;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::ePhone))
cout << "| " << left << setw(14) << currentClient.phone;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eAccountBalance))
cout << "| " << left << setw(20) << currentClient.accountBalance;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateCreatedClients))
cout << "| " << left << setw(19) << currentClient.dateCreated;
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eDateModifiedClients))
cout << "| " << left << setw(19) << (currentClient.dateModified == "-" ? " -" : currentClient.dateModified);
if (shouldClientFieldAppear(globalSettings.clientTableSettings, enClientTableFields::eStatus))
cout << "| " << left << setw(9) << (currentClient.markAsLocked ? "Locked" : "Unlocked");
cout << "|\n";
}
printLineForClientsTable();
cout << endl;
}
void displayClientsListScreen() {
system("cls");
if (!vClients.empty()) {
cout << "* Clients List - " << vClients.size() << " Client(s) available";
printClientsTableHeader();
printClientsTable();
} else
cout << "No clients available.\n";
backToManageClientsScreen();
}
bool doesAccountNumberExist(const string & accountNumber) {
for (sClient & currentClient : vClients)
if (currentClient.accountNumber == accountNumber)
return true;
return false;
}
void readClientRecord(sClient & client) {
client.pinCode = readWord("Enter PIN code : ", 17);
client.name = readName("Enter name : ");
client.phone = readPhoneNumber("Enter phone : ");
client.accountBalance = readPositiveNumber("Enter account balance : ");
}
string convertClientRecordToLine(const sClient & client, const string delimiter) {
string result = "";
result += client.accountNumber + delimiter;
result += client.pinCode + delimiter;
result += client.name + delimiter;
result += client.phone + delimiter;
result += to_string(client.accountBalance) + delimiter;
result += client.dateCreated + delimiter;
result += client.dateModified + delimiter;
result += client.markAsLocked ? "1" : "0";
return result;
}
string convertClientHistoryRecordToLine(const sClient & client, const string delimiter, const enClientUpdateType actionDone) {
string result = "";
if (client.dateModified == "-")
result += client.dateCreated + delimiter;
else
result += client.dateModified + delimiter;
result += client.pinCode + delimiter;
result += client.name + delimiter;
result += client.phone + delimiter;
result += to_string(client.accountBalance) + delimiter;
result += to_string(actionDone);
return result;
}
void appendClientRecordToClientsFile(const string & fileName, sClient & client) {
fstream clientsFile(fileName, ios::app);
if (clientsFile.is_open()) {
clientsFile << convertClientRecordToLine(client, "#//#") << endl;
clientsFile.close();
}
}
void appendClientRecordToHistoryFile(const string & fileName, sClient & client) {
fstream historyFile(fileName, ios::app);
if (historyFile.is_open()) {
historyFile << client.accountNumber << '\n';
historyFile << convertClientHistoryRecordToLine(client, "#//#", enClientUpdateType::eClientCreated) << '\n';
historyFile.close();
}
}
void displayAddNewClientScreen() {
char shouldAddNewClient = 'Y';
while (shouldAddNewClient == 'Y' || shouldAddNewClient == 'y') {
system("cls");
cout << "* You can enter [-99] to cancel.\n\n";
cout << "---------------------------------\n";
cout << " Add New Client Screen\n";
cout << "---------------------------------\n";
cout << "Adding New Client:\n";
string newAccountNumber, errorMessage = "";
do {
cout << errorMessage << endl;
newAccountNumber = readWord("Enter account number : ", 17);
if (newAccountNumber == "-99")
backToManageClientsScreen();
errorMessage = "Account number [" + newAccountNumber + "] already exists.\n";
} while (doesAccountNumberExist(newAccountNumber));
sClient newClient;
newClient.accountNumber = newAccountNumber;
readClientRecord(newClient);
newClient.dateCreated = getSystemDateTime();
vClients.push_back(newClient);
appendClientRecordToClientsFile(clientsFileName, newClient);
appendClientRecordToHistoryFile(historyFileName, newClient);
cout << "\nClient added successfully.\n";
shouldAddNewClient = readCharacter("Do you want to add a new client? [Y] - [N]: ");
}
backToManageClientsScreen();
}
void printAccountRecord(const sClient & client) {
cout << "\nThe following are the client's details:";
cout << "\n------------------------------------------";
cout << "\nAccount Number : " << client.accountNumber;
cout << "\nPIN Code : " << client.pinCode;
cout << "\nName : " << client.name;
cout << "\nPhone : " << client.phone;
cout << "\nAccount Balance : " << client.accountBalance;
cout << "\nStatus : " << (client.markAsLocked ? "Locked" : "Unlocked");
cout << "\n------------------------------------------\n";
}
void printAccountRecord(const string & accountNumber) {
for (sClient & currentClient : vClients) {
if (currentClient.accountNumber == accountNumber) {
printAccountRecord(currentClient);
break;
}
}
}
short getClientPosition(const string & accountNumber) {
for (short i = 0; i < vClients.size(); i++)
if (vClients[i].accountNumber == accountNumber)
return i;
return -1;
}
short readClientPosition() {
string accountNumber, errorMessage = "";
short clientPosition;
do {
cout << errorMessage;
accountNumber = readWord("Enter account number: ", 17);
if (accountNumber == "-99")
backToManageClientsScreen();
clientPosition = getClientPosition(accountNumber);
errorMessage = "Account Number [" + accountNumber + "] does not exist.\n\n";
} while (clientPosition == -1);
return clientPosition;
}
void copyVectorToFile(const string & fileName, vector<string> & vector) {
fstream clientsFile(fileName, ios::out);
if (clientsFile.is_open()) {
for (string & currentLine : vector)
clientsFile << currentLine << '\n';
clientsFile.close();
} else
cerr << "Error: Cannot open this file.";
}
void saveClientsToFile(const string fileName) {
fstream clientsFile(fileName, ios::out);
if (clientsFile.is_open()) {
for (const sClient & C : vClients)
if (!C.markForDelete)
clientsFile << convertClientRecordToLine(C, "#//#") << '\n';
clientsFile.close();
} else
cerr << "Error: Cannot open clients file.";
}
void insertStringInVector(vector<string> & vct, const short & pos, const string & element) {
vector<string> vTemp;
for (short i = 0; i < pos; i++)
vTemp.push_back(vct.at(i));
vTemp.push_back(element);
for (short i = pos; i < vct.size(); i++)
vTemp.push_back(vct.at(i));
vct = vTemp;
}
short getNextAccountNumberPosition(vector<string> & vLines, const string & accountNumber) {
bool hasFoundAccountNumber = false;
vLines.push_back(" "); //This is crucial in case the accountNumber is the last in file
for (short i = 0; i < vLines.size(); i++) {
if (vLines[i] == accountNumber) {
hasFoundAccountNumber = true;
continue;
}
if (hasFoundAccountNumber && vLines[i].find("#//#") == string::npos) {
vLines.pop_back(); //To get rid of that white space
return i;
}
}
return -1; //This will never be reached anyways
}
void insertClientRecordInHistoryFile(const string & historyFilePath, const sClient & client, const enClientUpdateType actionDone) {
vector<string> vAllLines = copyFileToVector(historyFilePath);
insertStringInVector(vAllLines, getNextAccountNumberPosition(vAllLines, client.accountNumber), convertClientHistoryRecordToLine(client, "#//#", actionDone));
copyVectorToFile(historyFilePath, vAllLines);
}
void displayDeleteClientScreen() {
system("cls");
if (vClients.empty())
cout << "No clients available.\n";
else {
char shouldDeleteClient = 'Y';
while (shouldDeleteClient == 'Y' || shouldDeleteClient == 'y') {
system("cls");
cout << "* You can enter [-99] to cancel.\n\n";
cout << "---------------------------------\n";
cout << " Delete Client Screen\n";
cout << "---------------------------------\n";
short clientToDeletePosition = readClientPosition();
printAccountRecord(vClients[clientToDeletePosition]);
shouldDeleteClient = readCharacter("\nAre you sure you want to delete this client? [Y] - [N]: ");
if (shouldDeleteClient == 'Y' || shouldDeleteClient == 'y') {
vClients[clientToDeletePosition].markForDelete = true;
saveClientsToFile(clientsFileName);
vClients = loadClientsFromFile(clientsFileName); //To refresh the vector
cout << "\nClient deleted successfully.\n";
shouldDeleteClient = readCharacter("Do you want to delete another client? [Y] - [N]: ");
}
}
}
backToManageClientsScreen();
}
void displayUpdateClientScreen() {
system("cls");
if (vClients.empty())
cout << "No clients available.\n";
else {
char shouldUpdateClient = 'Y';
while (shouldUpdateClient == 'Y' || shouldUpdateClient == 'y') {
system("cls");
cout << "* You can enter [-99] to cancel.\n\n";
cout << "---------------------------------\n";
cout << " Update Client Info Screen\n";
cout << "---------------------------------\n";
short clientToUpdatePosition = readClientPosition();
printAccountRecord(vClients[clientToUpdatePosition]);
shouldUpdateClient = readCharacter("\nAre you sure you want to update this client? [Y] - [N]: ");
if (shouldUpdateClient == 'Y' || shouldUpdateClient == 'y') {
cout << '\n';
readClientRecord(vClients[clientToUpdatePosition]);
vClients[clientToUpdatePosition].dateModified = getSystemDateTime();
saveClientsToFile(clientsFileName);
insertClientRecordInHistoryFile(historyFileName, vClients[clientToUpdatePosition], enClientUpdateType::eFullUpdate);
cout << "\nClient updated successfully.\n";
shouldUpdateClient = readCharacter("Do you want to update another client? [Y] - [N]: ");
}
}
}
backToManageClientsScreen();
}
sClientForHistory convertClientHistoryLineToRecord(string & line) {
vector<string> vHistoryRecordData = splitStringToVector(line, "#//#");
sClientForHistory clientHistoryPoint;
clientHistoryPoint.date = vHistoryRecordData[0];
clientHistoryPoint.pinCode = vHistoryRecordData[1];
clientHistoryPoint.name = vHistoryRecordData[2];
clientHistoryPoint.phone = vHistoryRecordData[3];
clientHistoryPoint.accountBalance = stod(vHistoryRecordData[4]);
clientHistoryPoint.actionDone = (enClientUpdateType) stoi(vHistoryRecordData[5]);
return clientHistoryPoint;
}
vector<sClientForHistory> loadClientRecordsFromHistoryFile(const string & accountNumber, const string & historyFilePath) {
fstream historyFile(historyFilePath, ios::in);
vector<sClientForHistory> vClientHistory;
if (historyFile.is_open()) {
string currentLine = "";
bool shouldPushBack = false;
while (getline(historyFile, currentLine)) {
if (shouldPushBack) {
if (currentLine.find("#//#") == string::npos)
break;
else
vClientHistory.push_back(convertClientHistoryLineToRecord(currentLine));
} else if (currentLine == accountNumber)
shouldPushBack = true;
}
historyFile.close();
} else
cerr << "Error: Cannot open clients file.";
return vClientHistory;
}
string getClientUpdateTypeText(const enClientUpdateType & updateType) {
switch (updateType) {
case enClientUpdateType::eClientCreated: return "Account Created";
case enClientUpdateType::eFullUpdate: return "Full Update";
case enClientUpdateType::eOnlyDeposit: return "Deposit";
case enClientUpdateType::eOnlyWithdraw: return "Withdraw";
case enClientUpdateType::eTransferedMoney: return "Transfered Money";
case enClientUpdateType::eReceivedMoney: return "Received Money";
case enClientUpdateType::eChangedPhoneNumber: return "Changed Phone Number";
case enClientUpdateType::eChangedPinCode: return "Changed PIN Code";
case enClientUpdateType::eAccountLocked: return "Account Locked";
case enClientUpdateType::eAccountUnlocked: return "Account Unlocked";
}
}
void printClientHistoryHeader(const string & accountNumber) {
cout << "\t\t\t\t\t\t\t" << accountNumber;
cout << "\n------------------------------------------------------------------------------------------------------------------------";
cout << "\n| Date | PIN Code | Client Name | Phone | Balance | Action Done |";
cout << "\n------------------------------------------------------------------------------------------------------------------------" << endl;
}
void printClientHistoryData(const string & accountNumber) {
vector<sClientForHistory> vClientRecords = loadClientRecordsFromHistoryFile(accountNumber, historyFileName);
for (sClientForHistory & currentRecord : vClientRecords) {
cout << "| " << setw(19) << left << currentRecord.date;
cout << "| " << setw(11) << left << currentRecord.pinCode;
cout << "| " << setw(27) << left << currentRecord.name;
cout << "| " << setw(14) << left << currentRecord.phone;
cout << "| " << setw(14) << left << currentRecord.accountBalance;
cout << "| " << setw(22) << left << getClientUpdateTypeText(currentRecord.actionDone) << "|\n";
}
cout << "------------------------------------------------------------------------------------------------------------------------" << endl;
}
void addNewMessageToMessagesFile(const string & messageFilePath, const string & accountNumber, const string & message) {
fstream messagesFile(messageFilePath, ios::app);
if (messagesFile.is_open()) {
messagesFile << accountNumber << "#//#" << message << '\n';
messagesFile.close();
} else
cerr << "Error: Cannot open messages file.";
}
short getCharCount(const string & str, const char character) {
short count = 0;
for (const char & C : str)
if (C == character)
count++;
return count;
}
void trimString(string & str) {
if (getCharCount(str, ' ') == str.length()) {
str = "";
return;
}
//Trim left:
while (str[0] == ' ')
str.erase(0, 1);
//Trim right:
while (str[str.length() - 1] == ' ')
str.pop_back();
}
string readMessage(const string msg) {
string message;
while (true) {
cout << msg;
getline(cin >> ws, message);
trimString(message);
if (message != "")
return message;
}
}
void backToPreviousFindClientScreen(const string & accountNumber) {
cout << "\nPress any key to go back...";
system("pause>nul");
displayPreviousFindUserScreen(accountNumber);
}
bool readBoolean(const string message) {
char choice = ' ';
while (true) {
cout << message << " [Y] - [N]: ";
cin >> choice;