-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.cpp
More file actions
374 lines (304 loc) · 9.7 KB
/
ATM.cpp
File metadata and controls
374 lines (304 loc) · 9.7 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
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_USERS = 5;
char userNames[MAX_USERS][100];
int userPINs[MAX_USERS];
int userBalances[MAX_USERS];
int userCount = 0;
void accountMenu() {
cout << "\nAccount Menu:\n";
cout << "1. Sign Up (Create new account)\n";
cout << "2. Log In (Access your account)\n";
cout << "3. View Accounts\n";
cout << "4. Delete Accounts\n";
cout << "5. Exit\n";
}
void atmMenu() {
cout << "\nATM Menu:\n";
cout << "1. Check Balance\n";
cout << "2. Deposit Money\n";
cout << "3. Withdraw Money\n";
cout << "4. Log Out\n";
}
void signUp() {
if (userCount >= MAX_USERS) {
cout << "Sorry, maximum user accounts reached.\n";
return;
}
cin.ignore();
cout << "Enter your full name: ";
cin.getline(userNames[userCount], 100);
int newPin;
int confirmPin;
cout << "Set a 4-digit PIN: ";
while (true){
cin>>newPin;
if (cin.fail()){
cout<<"Invalid Input\n";
cin.clear();
cin.ignore(10000, '\n');
}
if (newPin < 1000 || newPin > 9999) {
cout << "PIN must be a 4-digit number.\n";
continue;}
int pinUsed = false;
for (int i = 0; i < userCount; i++) {
if (userPINs[i] == newPin) {
cout <<"PIN already used. Choose a different one.\n";
pinUsed = true;
break;}
}
if (!pinUsed){
cout<<"Confirm PIN: ";
cin>>confirmPin;
if (confirmPin== newPin){
break;
}
else{
cout<<"PIN did not match\n";
continue;
}
}
}
userPINs[userCount]= newPin;
userBalances[userCount] = 1000;
cout << "Successfully created Account for " << userNames[userCount] << "!\n";
userCount++;
}
int logIn() {
if (userCount == 0) {
cout << "No users signed up yet. Please sign up first.\n";
return -1;
}
int enteredPin;
int attempts=0;
int maxAttempts=3;
while (attempts<maxAttempts){
cout << "\nNow Enter your PIN to LOGIN: ";
cin >> enteredPin;
cout<<endl;
attempts++;
if (cin.fail()) {
cout << "Invalid Input!\nYou have " << maxAttempts - attempts << " attempts left\n";
cin.clear();
cin.ignore(1000, '\n');
if (maxAttempts - attempts == 0) {
cout << "Too many attempts, LOGIN failed....\n";
return -1;
}
continue;
}
for (int i = 0; i < userCount; i++) {
if (userPINs[i] == enteredPin) {
cout << "Welcome back, " << userNames[i] << "!\n";
return i;
}
}
if (attempts == maxAttempts) {
cout << "Incorrect PIN.\nToo many attempts, LOGIN failed....\n";
cin.ignore(1000, '\n');
return -1;
} else {
cout << "Incorrect PIN.\nYou have " << maxAttempts - attempts << " attempts left\n";
cin.ignore(1000, '\n');
}
}
return -1;
}
void view() {
if (userCount == 0) {
cout << "No users to view. Please sign up first.\n";
return ;
}
for (int i=0; i<userCount; i++){
cout<<i+1<<". "<<userNames[i]<<endl;
}
}
void delAccount() {
if (userCount == 0) {
cout << "No users to delete.\n";
return;
}
cout << "Enter the PIN of the account to delete: ";
int enteredPin;
cin >> enteredPin;
int indexToDelete = -1;
for (int i = 0; i < userCount; i++) {
if (userPINs[i] == enteredPin) {
indexToDelete = i;
break;
}
}
if (indexToDelete == -1) {
cout << "No account found with that PIN.\n";
return;
}
for (int i = indexToDelete; i < userCount - 1; i++) {
int j = 0;
while (userNames[i + 1][j] != '\0') {
userNames[i][j] = userNames[i + 1][j];
j++;
}
userNames[i][j] = '\0';
userPINs[i] = userPINs[i + 1];
userBalances[i] = userBalances[i + 1];
}
userCount--;
cout << "Account deleted successfully.\n";
view();
}
void checkBalance(int userIndex) {
cout << "Your current balance is: $" << userBalances[userIndex] << endl;
}
void deposit(int userIndex) {
int amount;
cout << "Enter amount to deposit: $";
cin >> amount;
cout<<endl;
if (amount > 0) {
userBalances[userIndex] += amount;
cout << "Deposited successfully!\n";
cout << "Your updated balance is now: $" << userBalances[userIndex] << endl;
} else {
cout << "Invalid amount.\n";
}
}
void withdraw(int userIndex) {
int amount;
cout << "Enter amount to withdraw: $";
cin >> amount;
cout<<endl;
if (amount > userBalances[userIndex]) {
cout << "Insufficient funds.\n";
} else if (amount <= 0) {
cout << "Invalid amount.\n";
} else {
userBalances[userIndex] -= amount;
cout << "Withdrawn successfully!\n";
cout << "Your updated balance is now: $" << userBalances[userIndex] << endl;
}
}
void saveDataToFile(){
ofstream outFile("data.txt");
outFile<<userCount<<endl;
for (int i=0; i<userCount ;i++){
outFile<<userNames[i];
outFile<<endl;
outFile<<userPINs[i];
outFile<<endl;
outFile<<userBalances[i];
outFile<<endl;
}
outFile.close();
}
void loadDataFromFile(){
ifstream inFile("data.txt");
if(!inFile){
return;
}
inFile>>userCount;
inFile.ignore();
for (int i=0; i<userCount ;i++){
inFile.getline(userNames[i],100);
inFile>>userPINs[i];
inFile>>userBalances[i];
inFile.ignore();
}
inFile.close();
}
int main() {
loadDataFromFile();
cout<<"This ATM is designed by Captain Pakistan......\nWELCOME!!"<<endl;
int mainChoice;
do {
accountMenu();
cout << "Choose an option: ";
cin >> mainChoice;
system("clear");
switch(mainChoice) {
case 1: // Sign Up
{
signUp();
saveDataToFile();
int userIndex = logIn();
if (userIndex != -1) {
int atmChoice;
do {
atmMenu();
cout << "Choose an option: ";
cin >> atmChoice;
switch(atmChoice) {
case 1:
checkBalance(userIndex);
break;
case 2:
deposit(userIndex);
saveDataToFile();
break;
case 3:
withdraw(userIndex);
saveDataToFile();
break;
case 4:
cout << "Logging out...\n";
break;
default:
cout << "Invalid option.\n";
if (cin.fail()) {
cin.clear();
cin.ignore(1000, '\n');}
}
} while (atmChoice != 4);
}
break;}
case 2: {
int userIndex = logIn();
if (userIndex != -1) {
int atmChoice;
do {
atmMenu();
cout << "Choose an option: ";
cin >> atmChoice;
switch(atmChoice) {
case 1:
checkBalance(userIndex);
break;
case 2:
deposit(userIndex);
saveDataToFile();
break;
case 3:
withdraw(userIndex);
saveDataToFile();
break;
case 4:
cout << "Logging out...\n";
break;
default:{ if (cin.fail()){
cout << "Invalid option.\n";
cin.clear();
cin.ignore(1000, '\n');
}
}
}
} while (atmChoice != 4);
}
break;
}
case 3: view() ; break;
case 4: delAccount(); saveDataToFile();
break;
case 5:
cout << "Thank you for using the ATM. Goodbye!\n";
break;
default: { if(cin.fail()){
cout << "Invalid option. Try again.\n";
cin.clear();
cin.ignore(10000, '\n');}
}
}
} while (mainChoice != 5);
saveDataToFile();
return 0;
}