-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
437 lines (378 loc) · 12.2 KB
/
main.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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace std;
enum enFields { AccountID = 0, FullName, PhoneNumber, PinCode, Balance,FiledsCount };
struct Client{
string AccountID;
string FullName;
string PhoneNumber;
string PinCode;
double Balance;
};
//Intitations values
string filename="clients.csv";
vector<Client> vClients;
vector<string> vAccountIds;
double TotalBalances=0;
//Core functions Declaration
Client _ParseClientFromLineString(const string& line,char delimiter);
void LoadFileToVector(const string& filename,vector<Client>& vClients);
void _PrintSingleRecord(const Client& stClient);
void PrintAllRecords(vector<Client>& vClients);
// void AddNewClient(vector<Client> vClients);
// Screens
void ClearScreen();
void ShowClientListScreen();
void AddNewClientScreen();
void ReadClientFromUser(Client& stClient);
void DeleteClientScreen();
void UpdateClientInfoScreen();
void FindClientScreen();
void TransactionsScreen();
void DepositScreen();
void WithdrawScreen();
void MainMenuScreen();
void ExitScreen();
void NumberToScreen(short ScreenNumber);
void ShowTotalBalancesScreen();
void UpdateAccountIdVector(const vector<Client>& vClients,vector<string>& vAccountIds){
vAccountIds.clear();
TotalBalances=0;
for (short i = 0; i < vClients.size(); i++)
{
vAccountIds.push_back(vClients[i].AccountID);
TotalBalances+=vClients[i].Balance;
}
}
void clearScreen(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void cinflush(){
cin.clear();
cin.ignore(10000, '\n');
}
void backToMainMenu(string Message =""){
cout <<"\n"<<Message;
cout << "\n\nPress Enter to go back to Main Menu...";
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear the input buffer
cin.get();
MainMenuScreen();
}
void backToTransactionMenu(string Message =""){
cout <<"\n"<<Message;
cout << "\n\nPress Enter to go back to Main Menu...";
cin.ignore(1000, '\n'); // Clear the input buffer
cin.get();
TransactionsScreen();
}
void MainMenuScreen() {
clearScreen();
UpdateAccountIdVector(vClients,vAccountIds);
cout << "===========================================\n";
cout << "\t\tMain Menu 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] Transactions.\n";
cout << "\t[7] Exit.\n";
cout << "===========================================\n";
short ScreenNumber;
cin>>ScreenNumber;
if (ScreenNumber>7 || ScreenNumber<1) {
cinflush();
MainMenuScreen();
};
NumberToScreen(ScreenNumber);
}
void NumberToScreen(short ScreenNumber){
switch (ScreenNumber)
{
case 1:ShowClientListScreen();break;
case 2:AddNewClientScreen();break;
case 3:DeleteClientScreen();break;
case 4:UpdateClientInfoScreen();break;
case 5:FindClientScreen();break;
case 6:TransactionsScreen();break;
case 7:ExitScreen();break;
default:backToMainMenu("Error , Please Enter a valid Option") ;break;
}
}
// Screens
void PrintClientRecord(const Client& client) {
cout << "| " << setw(15) << left << client.AccountID;
cout << "| " << setw(30) << left << client.FullName;
cout << "| " << setw(15) << left << client.PhoneNumber;
cout << "| " << setw(15) << left << client.PinCode;
cout << "| " << setw(12) << left << client.Balance;
}
void PrintAllClientsData(const vector<Client>& vClients) {
cout << "\n\t\t\t\t\tClient List (" << vClients.size() << ") Client(s).";
cout << "\n_______________________________________________________";
cout << "_________________________________________\n" << endl;
cout << "| " << left << setw(15) << "Account Id";
cout << "| " << left << setw(30) << "Full Name";
cout << "| " << left << setw(15) << "Phone ";
cout << "| " << left << setw(15) << "Pin Code";
cout << "| " << left << setw(12) << "Balance";
cout << "\n_______________________________________________________";
cout << "_________________________________________\n" << endl;
for (const Client& client : vClients) {
PrintClientRecord(client);
cout << endl;
}
cout << "_______________________________________________________";
cout << "_________________________________________\n" << endl;
}
void PrintSingleClientInfo(const Client& client) {
cout << "\nClient Data:\n" << endl;
cout << "Account Number : " << client.AccountID << endl;
cout << "Name : " << client.FullName << endl;
cout << "Phone : " << client.PhoneNumber << endl;
cout << "PinCode : " << client.PinCode << endl;
cout << "Account Balance: " << client.Balance << endl;
}
void ShowClientListScreen(){
clearScreen();
cout<<"\nShowClientListScreen\n";
PrintAllClientsData(vClients);
backToMainMenu();
}
void AppendClientToVectorClients(vector<Client>& vClients ,Client& stClient ){
char addOrNot;
cout<<"\nAre you sure to add this client (Y / N) ? ";
cin>>addOrNot;
if (addOrNot == 'y' || addOrNot == 'Y')
{
vClients.push_back(stClient);
cout<<"\n Added Successfully ! ";
}
}
void AddNewClient(vector<Client>& vClients){
Client stClient;
ReadClientFromUser(stClient);
PrintSingleClientInfo(stClient);
AppendClientToVectorClients(vClients,stClient);
backToMainMenu();
}
//single client
void ReadClientFromUser(Client& stClient){
cout << "Please Enter Client Data" << endl;
cout << "Enter Account Id? ";
cin >> stClient.AccountID;
cinflush();
cout << "Enter Full Name? ";
getline(cin,stClient.FullName);
// cinflush();
cout << "Enter PhoneNumber? ";
getline(cin, stClient.PhoneNumber);
// cinflush();
cout << "Enter Pin Code? ";
cin >> stClient.PinCode;
// cinflush();
cout << "Enter Account Balance? ";
cin >> stClient.Balance;
// cinflush();
}
void AddNewClientScreen() {
clearScreen();
AddNewClient(vClients);
backToMainMenu();
}
string ReadClientAccountId(){
string AccountID;
cout<<"Please Enter Account ID ";
cin>>AccountID;
return AccountID;
}
short FindByAccountNumber(const string& AccountNumber, const vector<Client>& vClients) {
for (size_t i = 0; i < vClients.size(); ++i) {
if (vClients[i].AccountID == AccountNumber) {
return i; // Return the Location if account number matches
}
}
return -1; // Return -1 if no match is found
}
Client retriveAccountByLocation(const vector<Client>& vClients, short ClientLocation){
return vClients[ClientLocation];
}
void DeleteClientScreen() {
string AccountID;
clearScreen();
AccountID=ReadClientAccountId();
short ClientLocation = FindByAccountNumber(AccountID,vClients);
if (ClientLocation == -1) {
cout <<AccountID<< " Client not found.\n" ;
backToMainMenu();
}
vClients.erase(vClients.begin() + ClientLocation);
cout <<AccountID<<" Client removed.\n";
backToMainMenu();
}
void UpdateClientInfoScreen() {
string AccountID;
Client stClient;
clearScreen();
AccountID=ReadClientAccountId();
short ClientLocation = FindByAccountNumber(AccountID,vClients);
if (ClientLocation == -1) {
cout <<AccountID<< "Client not found.\n" ;
backToMainMenu();
}
cout<<"\nClient with ID "<<AccountID<<" Founded \n";
PrintSingleClientInfo(vClients[ClientLocation]);
cout<<"\nUpdate Client\n";
ReadClientFromUser(vClients[ClientLocation]);
PrintSingleClientInfo(vClients[ClientLocation]);
cout <<AccountID<<" Client Added Successfully.\n";
backToMainMenu();
}
void FindClientScreen() {
clearScreen();
string AccountID=ReadClientAccountId();
short ClientLocation = FindByAccountNumber(AccountID,vClients);
if (ClientLocation == -1) {
cout <<AccountID<< "Client not found.\n" ;
backToMainMenu();
}
cout<<"\nClient with ID "<<AccountID<<" Founded \n";
PrintSingleClientInfo(vClients[ClientLocation]);
backToMainMenu();
}
void NumberToTransactionsScreens(short& ScreenNumber){
switch (ScreenNumber)
{
case 1:DepositScreen();break;
case 2:WithdrawScreen();break;
case 3:ShowTotalBalancesScreen();break;
case 4:MainMenuScreen();break;
default:backToMainMenu("Error , Please Enter a valid Option") ;break;
}
}
void TransactionsScreen() {
clearScreen();
UpdateAccountIdVector(vClients,vAccountIds);
cout << "===========================================\n";
cout << "\t\tTransactions Screen\n";
cout << "===========================================\n";
cout << "\t[1] Deposit.\n";
cout << "\t[2] Withdraw.\n";
cout << "\t[3] Total Balances.\n";
cout << "\t[4] Main Menu.\n";
cout << "===========================================\n";
short ScreenNumber;
cin>>ScreenNumber;
if (ScreenNumber>4 || ScreenNumber<1) {
cinflush();
TransactionsScreen();
};
NumberToTransactionsScreens(ScreenNumber);
}
void ReadDepositFromUser(Client& stClient){
double amount=0;
cout << "Enter Ammount to deposit Balance? ";
cin >> amount;
stClient.Balance += amount;
}
void ReadWithdrawFromUser(Client& stClient){
double amount=0;
cout << "Enter Ammount to Withdraw Balance? ";
cin >> amount;
stClient.Balance -= amount;
}
void DepositScreen() {
string AccountID;
Client stClient;
clearScreen();
AccountID=ReadClientAccountId();
short ClientLocation = FindByAccountNumber(AccountID,vClients);
if (ClientLocation == -1) {
cout <<AccountID<< "Client not found.\n" ;
backToTransactionMenu();
}
cout<<"\nClient with ID "<<AccountID<<" Founded \n";
PrintSingleClientInfo(vClients[ClientLocation]);
cout<<"\nUpdate Client\n";
ReadDepositFromUser(vClients[ClientLocation]);
PrintSingleClientInfo(vClients[ClientLocation]);
cout <<AccountID<<" Money Added Successfully.\n";
backToTransactionMenu();
}
void WithdrawScreen() {
string AccountID;
Client stClient;
clearScreen();
AccountID=ReadClientAccountId();
short ClientLocation = FindByAccountNumber(AccountID,vClients);
if (ClientLocation == -1) {
cout <<AccountID<< "Client not found.\n" ;
backToTransactionMenu();
}
cout<<"\nClient with ID "<<AccountID<<" Founded \n";
PrintSingleClientInfo(vClients[ClientLocation]);
cout<<"\nUpdate Client\n";
ReadWithdrawFromUser(vClients[ClientLocation]);
cout <<AccountID<<" Money Withdrawl Successfully.\n";
backToTransactionMenu();
}
void ShowTotalBalancesScreen(){
clearScreen();
cout<<"\nShowClientListScreen\n";
PrintAllClientsData(vClients);
cout<<setw(50)<<left<<" Total Balances = "<< TotalBalances;
backToTransactionMenu();
}
void ExitScreen() {
exit(0); // Terminate the application
}
//Core Functions implemetatoin
vector <string> _ParseTokensFromLineString(const string& line,short NumberOfFileds,char delimiter=','){
short i=0,start=0;
Client stClient;
vector <string> tokens;
for (size_t incrementer = 0; incrementer < NumberOfFileds; incrementer++)
{
i=line.find(delimiter,start);
tokens.push_back(line.substr(start,i-start));
start=i+1;
}
return tokens;
}
Client _TokenstoClient(vector <string> tokens){
Client stClient;
stClient.AccountID =tokens[enFields::AccountID];
stClient.FullName =tokens[enFields::FullName];
stClient.PhoneNumber =tokens[enFields::PhoneNumber];
stClient.PinCode =tokens[enFields::PinCode];
stClient.Balance =stod(tokens[enFields::Balance]);
return stClient;
}
void LoadFileToVector(const string& filename,vector<Client>& vClients){
fstream fsFile;
string line;
fsFile.open(filename,ios::in);
if(!fsFile.is_open())
{
cout<<"File not found";
return ;
}
while (getline(fsFile,line) && line != "")
{
vClients.push_back(_TokenstoClient(_ParseTokensFromLineString(line,enFields::FiledsCount)));
}
fsFile.close();
}
int main() {
LoadFileToVector(filename,vClients);
MainMenuScreen();
return 0;
}