-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.cpp
608 lines (430 loc) · 13.3 KB
/
Bank.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
const string ClientsFileName = "Clients.txt";
void ShowMainMenue();
struct sClient
{
string AccountNumber;
string PinCode;
string Name;
string Phone;
double AccountBalance;
bool MarkForDelete = false;
};
vector<string> SplitString(string S1, string Delim)
{
vector<string> vString;
short pos = 0;
string sWord;
while ((pos = S1.find(Delim)) != std::string::npos)
{
sWord = S1.substr(0, pos);
if (sWord != "")
{
vString.push_back(sWord);
}
S1.erase(0, pos + Delim.length());
}
if (S1 != "")
{
vString.push_back(S1);
}
return vString;
}
sClient ConvertLinetoRecord(string Line, string Seperator = "#//#")
{
sClient Client;
vector<string> vClientData;
vClientData = SplitString(Line, Seperator);
Client.AccountNumber = vClientData[0];
Client.PinCode = vClientData[1];
Client.Name = vClientData[2];
Client.Phone = vClientData[3];
Client.AccountBalance = stod(vClientData[4]);
return Client;
}
string ConvertRecordToLine(sClient Client, string Seperator = "#//#")
{
string stClientRecord = "";
stClientRecord += Client.AccountNumber + Seperator;
stClientRecord += Client.PinCode + Seperator;
stClientRecord += Client.Name + Seperator;
stClientRecord += Client.Phone + Seperator;
stClientRecord += to_string(Client.AccountBalance);
return stClientRecord;
}
bool ClientExistsByAccountNumber(string AccountNumber, string FileName)
{
vector <sClient> vClients;
fstream MyFile;
MyFile.open(FileName, ios::in);
if (MyFile.is_open())
{
string Line;
sClient Client;
while (getline(MyFile, Line))
{
Client = ConvertLinetoRecord(Line);
if (Client.AccountNumber == AccountNumber)
{
MyFile.close();
return true;
}
vClients.push_back(Client);
}
MyFile.close();
}
return false;
}
sClient ReadNewClient()
{
sClient Client;
cout << "Enter Account Number? ";
getline(cin >> ws, Client.AccountNumber);
while (ClientExistsByAccountNumber(Client.AccountNumber, ClientsFileName))
{
cout << "\nClient with [" << Client.AccountNumber << "] already exists, Enter another Account Number? ";
getline(cin >> ws, Client.AccountNumber);
}
cout << "Enter PinCode? ";
getline(cin, Client.PinCode);
cout << "Enter Name? ";
getline(cin, Client.Name);
cout << "Enter Phone? ";
getline(cin, Client.Phone);
cout << "Enter AccountBalance? ";
cin >> Client.AccountBalance;
return Client;
}
vector <sClient> LoadCleintsDataFromFile(string FileName)
{
vector <sClient> vClients;
fstream MyFile;
MyFile.open(FileName, ios::in);
if (MyFile.is_open())
{
string Line;
sClient Client;
while (getline(MyFile, Line))
{
Client = ConvertLinetoRecord(Line);
vClients.push_back(Client);
}
MyFile.close();
}
return vClients;
}
void PrintClientRecordLine(sClient Client)
{
cout << "| " << setw(15) << left << Client.AccountNumber;
cout << "| " << setw(10) << left << Client.PinCode;
cout << "| " << setw(40) << left << Client.Name;
cout << "| " << setw(12) << left << Client.Phone;
cout << "| " << setw(12) << left << Client.AccountBalance;
}
void ShowAllClientsScreen()
{
vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName);
cout << "\n\t\t\t\t\tClient List (" << vClients.size() << ") Client(s).";
cout << "\n_______________________________________________________";
cout << "_________________________________________\n" << endl;
cout << "| " << left << setw(15) << "Accout Number";
cout << "| " << left << setw(10) << "Pin Code";
cout << "| " << left << setw(40) << "Client Name";
cout << "| " << left << setw(12) << "Phone";
cout << "| " << left << setw(12) << "Balance";
cout << "\n_______________________________________________________";
cout << "_________________________________________\n" << endl;
if (vClients.size() == 0)
cout << "\t\t\t\tNo Clients Available In the System!";
else
for (sClient Client : vClients)
{
PrintClientRecordLine(Client);
cout << endl;
}
cout << "\n_______________________________________________________";
cout << "_________________________________________\n" << endl;
}
void PrintClientCard(sClient Client)
{
cout << "\nThe following are the client details:\n";
cout << "-----------------------------------";
cout << "\nAccout Number: " << Client.AccountNumber;
cout << "\nPin Code : " << Client.PinCode;
cout << "\nName : " << Client.Name;
cout << "\nPhone : " << Client.Phone;
cout << "\nAccount Balance: " << Client.AccountBalance;
cout << "\n-----------------------------------\n";
}
bool FindClientByAccountNumber(string AccountNumber, vector <sClient> vClients, sClient& Client)
{
for (sClient C : vClients)
{
if (C.AccountNumber == AccountNumber)
{
Client = C;
return true;
}
}
return false;
}
sClient ChangeClientRecord(string AccountNumber)
{
sClient Client;
Client.AccountNumber = AccountNumber;
cout << "\n\nEnter PinCode? ";
getline(cin >> ws, Client.PinCode);
cout << "Enter Name? ";
getline(cin, Client.Name);
cout << "Enter Phone? ";
getline(cin, Client.Phone);
cout << "Enter AccountBalance? ";
cin >> Client.AccountBalance;
return Client;
}
bool MarkClientForDeleteByAccountNumber(string AccountNumber, vector <sClient>& vClients)
{
for (sClient& C : vClients)
{
if (C.AccountNumber == AccountNumber)
{
C.MarkForDelete = true;
return true;
}
}
return false;
}
vector <sClient> SaveCleintsDataToFile(string FileName, vector <sClient> vClients)
{
fstream MyFile;
MyFile.open(FileName, ios::out);//overwrite
string DataLine;
if (MyFile.is_open())
{
for (sClient C : vClients)
{
if (C.MarkForDelete == false)
{
DataLine = ConvertRecordToLine(C);
MyFile << DataLine << endl;
}
}
MyFile.close();
}
return vClients;
}
void AddDataLineToFile(string FileName, string stDataLine)
{
fstream MyFile;
MyFile.open(FileName, ios::out | ios::app);
if (MyFile.is_open())
{
MyFile << stDataLine << endl;
MyFile.close();
}
}
void AddNewClient()
{
sClient Client;
Client = ReadNewClient();
AddDataLineToFile(ClientsFileName, ConvertRecordToLine(Client));
}
void AddNewClients()
{
char AddMore = 'Y';
do
{
system("cls");
cout << "Adding New Client:\n\n";
AddNewClient();
cout << "\nClient Added Successfully, do you want to add more clients? Y/N? ";
cin >> AddMore;
} while (toupper(AddMore) == 'Y');
}
bool DeleteClientByAccountNumber(string AccountNumber, vector <sClient>& vClients)
{
sClient Client;
char Answer = 'n';
if (FindClientByAccountNumber(AccountNumber, vClients, Client))
{
PrintClientCard(Client);
cout << "\n\nAre you sure you want delete this client? y/n ? ";
cin >> Answer;
if (Answer == 'y' || Answer == 'Y')
{
MarkClientForDeleteByAccountNumber(AccountNumber, vClients);
SaveCleintsDataToFile(ClientsFileName, vClients);
vClients = LoadCleintsDataFromFile(ClientsFileName);
cout << "\n\nClient Deleted Successfully.";
return true;
}
}
else
{
cout << "\nClient with Account Number (" << AccountNumber << ") is Not Found!";
return false;
}
}
bool UpdateClientByAccountNumber(string AccountNumber, vector <sClient>& vClients)
{
sClient Client;
char Answer = 'n';
if (FindClientByAccountNumber(AccountNumber, vClients, Client))
{
PrintClientCard(Client);
cout << "\n\nAre you sure you want update this client? y/n ? ";
cin >> Answer;
if (Answer == 'y' || Answer == 'Y')
{
for (sClient& C : vClients)
{
if (C.AccountNumber == AccountNumber)
{
C = ChangeClientRecord(AccountNumber);
break;
}
}
SaveCleintsDataToFile(ClientsFileName, vClients);
cout << "\n\nClient Updated Successfully.";
return true;
}
}
else
{
cout << "\nClient with Account Number (" << AccountNumber << ") is Not Found!";
return false;
}
}
string ReadClientAccountNumber()
{
string AccountNumber = "";
cout << "\nPlease enter AccountNumber? ";
cin >> AccountNumber;
return AccountNumber;
}
void ShowDeleteClientScreen()
{
cout << "\n-----------------------------------\n";
cout << "\tDelete Client Screen";
cout << "\n-----------------------------------\n";
vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName);
string AccountNumber = ReadClientAccountNumber();
DeleteClientByAccountNumber(AccountNumber, vClients);
}
void ShowUpdateClientScreen()
{
cout << "\n-----------------------------------\n";
cout << "\tUpdate Client Info Screen";
cout << "\n-----------------------------------\n";
vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName);
string AccountNumber = ReadClientAccountNumber();
UpdateClientByAccountNumber(AccountNumber, vClients);
}
void ShowAddNewClientsScreen()
{
cout << "\n-----------------------------------\n";
cout << "\tAdd New Clients Screen";
cout << "\n-----------------------------------\n";
AddNewClients();
}
void ShowFindClientScreen()
{
cout << "\n-----------------------------------\n";
cout << "\tFind Client Screen";
cout << "\n-----------------------------------\n";
vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName);
sClient Client;
string AccountNumber = ReadClientAccountNumber();
if (FindClientByAccountNumber(AccountNumber, vClients, Client))
PrintClientCard(Client);
else
cout << "\nClient with Account Number[" << AccountNumber << "] is not found!";
}
void ShowEndScreen()
{
cout << "\n-----------------------------------\n";
cout << "\tProgram Ends :-)";
cout << "\n-----------------------------------\n";
}
enum enMainMenueOptions
{
eListClients = 1, eAddNewClient = 2,
eDeleteClient = 3, eUpdateClient = 4,
eFindClient = 5, eExit = 6
};
void GoBackToMainMenue()
{
cout << "\n\nPress any key to go back to Main Menue...";
system("pause>0");
ShowMainMenue();
}
short ReadMainMenueOption()
{
short Choice = 0;
cout << "Choose what do you want to do? [1 to 6]? ";
cin >> Choice;
return Choice;
}
void PerfromMainMenueOption(enMainMenueOptions MainMenueOption)
{
switch (MainMenueOption)
{
case enMainMenueOptions::eListClients:
{
system("cls");
ShowAllClientsScreen();
GoBackToMainMenue();
break;
}
case enMainMenueOptions::eAddNewClient:
system("cls");
ShowAddNewClientsScreen();
GoBackToMainMenue();
break;
case enMainMenueOptions::eDeleteClient:
system("cls");
ShowDeleteClientScreen();
GoBackToMainMenue();
break;
case enMainMenueOptions::eUpdateClient:
system("cls");
ShowUpdateClientScreen();
GoBackToMainMenue();
break;
case enMainMenueOptions::eFindClient:
system("cls");
ShowFindClientScreen();
GoBackToMainMenue();
break;
case enMainMenueOptions::eExit:
system("cls");
ShowEndScreen();
break;
}
}
void ShowMainMenue()
{
system("cls");
cout << "===========================================\n";
cout << "\t\tMain Menue Screen\n";
cout << "===========================================\n";
cout << "\t[1] Show Client List.\n";
cout << "\t[2] Add New Client.\n";
cout << "\t[3] Delete Client.\n";
cout << "\t[4] Update Client Info.\n";
cout << "\t[5] Find Client.\n";
cout << "\t[6] Exit.\n";
cout << "===========================================\n";
PerfromMainMenueOption((enMainMenueOptions)ReadMainMenueOption());
}
int main()
{
ShowMainMenue();
system("pause>0");
return 0;
}