-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM_Project.cpp
More file actions
486 lines (381 loc) · 12.4 KB
/
Copy pathATM_Project.cpp
File metadata and controls
486 lines (381 loc) · 12.4 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
#include <iostream>
#include "MyString.h"
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
using namespace MyString;
const string ClientsFileName = "ClientsData.txt";
struct stClient {
string AccountNumber, Name, PhoneNumber;
string PINCode;
double AccountBalance;
bool MarkForDelete = false;
};
stClient CurrentClient;
void Login();
void ShowATMMainManueScreen();
void ShowCheckBalanceScreen();
void GoBackToATMMainMenu();
void ShowAmountExceedsBalanceMessage();
void ShowQuickWithdrawScreen();
void NormalWithdrawScreen();
bool DepositBalanceToClientByAccountNumber(string AccountNumber, double Amount, vector <stClient>& vClients);
bool UpdateClientByAccountNumber(string AccountNumber);
stClient ConvertLineToRecord(string S1, string Seperator = "#//#") {
vector <string> vString;
stClient Client;
vString = SplitString(S1, Seperator);
Client.AccountNumber = vString[0];
Client.PINCode = vString[1];
Client.Name = vString[2];
Client.PhoneNumber = vString[3];
Client.AccountBalance = stod(vString[4]);
return Client;
}
string ConvertRecordToLine(stClient ClientData, string Seperator = "#//#") {
string S1 = "";
S1 = ClientData.AccountNumber + Seperator;
S1 += ClientData.PINCode + Seperator;
S1 += ClientData.Name + Seperator;
S1 += ClientData.PhoneNumber + Seperator;
S1 += to_string(ClientData.AccountBalance);
return S1;
}
vector<stClient> LoadClientsDataFromFile(string FileName) {
vector <stClient> vClients;
fstream MyFile;
MyFile.open(FileName, ios::in);
if (MyFile.is_open()) {
string Line;
stClient Client;
while (getline(MyFile, Line)) {
Client = ConvertLineToRecord(Line);
vClients.push_back(Client);
}
MyFile.close();
}
return vClients;
}
bool FindClientByAccountNumberAndPinCode(string AccountNumber, string PinCode,stClient &Client) {
vector <stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
for (stClient C : vClients) {
if (C.AccountNumber == AccountNumber && C.PINCode == PinCode) {
Client = C;
return true;
}
}
return false;
}
bool LoadClientInfo(string AccountNumber, string PinCode) {
if (FindClientByAccountNumberAndPinCode(AccountNumber, PinCode,CurrentClient))
return true;
else
return false;
}
vector <stClient> SaveClientsDataToFile(string FileName,vector <stClient>& vClients) {
fstream MyFile;
MyFile.open(FileName, ios::out);
string DataLine;
if (MyFile.is_open()) {
for (stClient& C : vClients) {
if (C.MarkForDelete == false) {
DataLine = ConvertRecordToLine(C);
MyFile << DataLine << endl;
}
}
MyFile.close();
}
return vClients;
}
bool UpdateClientByAccountNumber(string AccountNumber) {
vector <stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
//stClient Client;
//char Answer = 'n';
/* if (FindClientByAccountNumber(AccountNumber, vClients, Client)) {
PrintClientCard(Client);*/
//cout << "\n\nAre you sure you want to update this client? y/n? ";
//cin >> Answer;
// if (toupper(Answer) == 'Y') {
for (stClient& C : vClients) {
if (C.AccountNumber == AccountNumber) {
C.AccountBalance = CurrentClient.AccountBalance;
break;
}
}
SaveClientsDataToFile(ClientsFileName, vClients);
//cout << "\n\nClient Updated Sucssefuly.\n";
return true;
//}
// return false;
}
//Quick Withdraw
enum enQuickWithdrawOptions
{
e20 = 1, e50 = 2, e100 = 3, e200 = 4,
e400 = 5, e600 = 6, e800 = 7, e1000 = 8, eExit = 9
};
short getQuickWithDrawAmount(short QuickWithDrawOption)
{
switch (QuickWithDrawOption) {
case 1:
return 20;
case 2:
return 50;
case 3:
return 100;
case 4:
return 200;
case 5:
return 400;
case 6:
return 600;
case 7:
return 800;
case 8:
return 1000;
default:
return 0;
}
}
int WithdrawBalance(short Amount, int Balance) {
return Balance - Amount;
}
bool CheckWithdrawAmount(short Amount, int Balance) {
if (Amount > Balance)
return false;
else
return true;
}
short ReadQuickWithdrawOption() {
short num = 0;
cout << "Choose what do you want to do? [1 to 9]? ";
cin >> num;
while (num <= 0 || num > 9) {
cout << "\nInvalid Input, Please try again.\n";
cout << "Choose what do you want to do? [1 to 9]? ";
cin >> num;
}
return num;
}
bool QuickWithdraw(short Amount, int Balance) {
char Answer = 'n';
/* if (!CheckWithdrawAmount(Amount, Balance)) {
cout << "\nAmount [ " << Amount << " ] exceeds your balance ( " << Balance << " ).\n";
return false;
}*/
//else {
cout << "\n\nAre you sure you want to perform this transaction? y/n? ";
cin >> Answer;
if (tolower(Answer) == 'y') {
CurrentClient.AccountBalance = WithdrawBalance(Amount, Balance);
UpdateClientByAccountNumber(CurrentClient.AccountNumber);
//vector<stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
//SaveClientsDataToFile(ClientsFileName, CurrentClient);
cout << "\n\nDone Sucssufully. New Balance is : " << CurrentClient.AccountBalance << endl;
return true;
}
return false;
}
void PerformQuickWithdrawMenueOption(short Option) {
if (Option == 9) {
GoBackToATMMainMenu();
return;
}
short WithdrawBalance = getQuickWithDrawAmount(Option);
if (WithdrawBalance > CurrentClient.AccountBalance) {
ShowAmountExceedsBalanceMessage();
ShowQuickWithdrawScreen();
return;
}
vector <stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
DepositBalanceToClientByAccountNumber(CurrentClient.AccountNumber, WithdrawBalance*-1, vClients);
CurrentClient.AccountBalance -= WithdrawBalance;
}
void ShowQuickWithdrawScreen() {
system("cls");
cout << "=========================================================\n";
cout << "\t\t Quick Withdraw Screen\n";
cout << "=========================================================\n";
cout << "\t [1] 20." << right << setw(15) << "[2] 50. \n";
cout << "\t [3] 100." << right << setw(15) << "[4] 200. \n";
cout << "\t [5] 400." << right << setw(15) << "[6] 600. \n";
cout << "\t [7] 800. " << right << setw(15) << "[8] 1000. \n";
cout << "\t [9] Exit.\n";
cout << "=========================================================\n";
cout << "\nYour Balance is: " << CurrentClient.AccountBalance << endl;
PerformQuickWithdrawMenueOption((enQuickWithdrawOptions)ReadQuickWithdrawOption());
}
//Deposit Screen
int ReadDepositAmount() {
int amount;
do {
cout << "\nEnter a positive Deposit Amount? ";
cin >> amount;
} while (amount <= 0);
return amount;
}
void PerformDepositOption() {
int DepositAmount = ReadDepositAmount();
vector <stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
DepositBalanceToClientByAccountNumber(CurrentClient.AccountNumber, DepositAmount, vClients);
CurrentClient.AccountBalance += DepositAmount;
}
void ShowDepositScreen() {
cout << "=========================================================\n";
cout << "\t\t Deposit Screen\n";
cout << "=========================================================\n";
PerformDepositOption();
}
//Normal Withdraw
int ReadWithdrawAmount() {
int number;
do {
cout << "\nEnter an amount multiple of 5`s ? ";
cin >> number;
} while (number % 5 != 0);
return number;
}
bool DepositBalanceToClientByAccountNumber(string AccountNumber, double Amount, vector <stClient>& vClients)
{
char Answer = 'n';
cout << "\n\nAre you sure you want perfrom this transaction? y/n ? ";
cin >> Answer;
if (Answer == 'y' || Answer == 'Y') {
for (stClient& C : vClients) {
if (C.AccountNumber == AccountNumber)
{
C.AccountBalance += Amount;
SaveClientsDataToFile(ClientsFileName, vClients);
cout << "\n\nDone Successfully. New balance is: " << C.AccountBalance;
return true;
}
} return false;
}
}
void ShowAmountExceedsBalanceMessage() {
cout << "\nThe amount exceeds your balance, make another choice.";
cout << "\nPress any key to continue\n";
system("pause >0");
}
void PerformNormalWithdrawOption() {
int WithdrawBalance = ReadWithdrawAmount();
if (WithdrawBalance > CurrentClient.AccountBalance) {
ShowAmountExceedsBalanceMessage();
NormalWithdrawScreen();
return;
}
vector <stClient> vClients = LoadClientsDataFromFile(ClientsFileName);
DepositBalanceToClientByAccountNumber(CurrentClient.AccountNumber, WithdrawBalance * -1, vClients);
CurrentClient.AccountBalance -= WithdrawBalance;
}
void NormalWithdrawScreen() {
system("cls");
cout << "=========================================================\n";
cout << "\t\t Normal Withdraw Screen\n";
cout << "=========================================================\n";
PerformNormalWithdrawOption();
}
//Check Balance
void ShowCheckBalanceScreen() {
//system("cls");
cout << "=========================================================\n";
cout << "\t\t Check Balance Screen\n";
cout << "=========================================================\n";
cout << "Your Balance is : " << CurrentClient.AccountBalance << endl;
}
//ATM Main Manue SCreen
enum enATMManueOptions
{
eQuickWithdraw = 1, eNormalWithdraw = 2,
eDeposit = 3, eCheckBalanse = 4, eLogout = 5
};
void GoBackToATMMainMenu() {
cout << "\n\nPress any key to go back to Main Menu...";
system("pause>0");
ShowATMMainManueScreen();
}
short ReadATMMainMenuOption() {
short num = 0;
cout << "Choose what do you want to do? [1 to 5]? ";
cin >> num;
while (num <= 0 || num > 5) {
cout << "\nInvalid Input, Please try again.\n";
cout << "Choose what do you want to do? [1 to 5]? ";
cin >> num;
}
return num;
}
void PerformATMMainMenuOptions(enATMManueOptions Option) {
switch (Option)
{
case enATMManueOptions::eQuickWithdraw: {
system("cls");
ShowQuickWithdrawScreen();
GoBackToATMMainMenu();
break;
}
case enATMManueOptions::eNormalWithdraw: {
system("cls");
NormalWithdrawScreen();
GoBackToATMMainMenu();
break;
}
case enATMManueOptions::eDeposit: {
system("cls");
ShowDepositScreen();
GoBackToATMMainMenu();
break;
}
case enATMManueOptions::eCheckBalanse: {
system("cls");
ShowCheckBalanceScreen();
GoBackToATMMainMenu();
break;
}
case enATMManueOptions::eLogout: {
system("cls");
Login();
break;
}
}
}
void ShowATMMainManueScreen() {
short Option = 0;
system("cls");
cout << "=========================================================\n";
cout << "\t\t ATM Main Menu Screen\n";
cout << "=========================================================\n";
cout << "\t [1] Quick Withdraw. \n";
cout << "\t [2] Normal Withdraw. \n";
cout << "\t [3] Deposit. \n";
cout << "\t [4] Check Balance. \n";
cout << "\t [5] Logout. \n";
cout << "=========================================================\n";
PerformATMMainMenuOptions((enATMManueOptions)ReadATMMainMenuOption());
}
void Login() {
bool LoginFailed = false;
string AccountNumber,PinCode;
do {
system("cls");
cout << "=========================================================\n";
cout << "\t\t Login Screen\n";
cout << "=========================================================\n";
if (LoginFailed) {
cout << "\nInvalide AccountNumber / PIN Code!\n";
}
cout << "\nEnter AccountNumber? ";
cin >> AccountNumber;
cout << "\nEnter PIN Code? ";
cin >> PinCode;
LoginFailed = !LoadClientInfo(AccountNumber,PinCode);
} while (LoginFailed);
ShowATMMainManueScreen();
}
int main()
{
Login();
system("pause >0");
}