-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATM.h
376 lines (341 loc) · 9.9 KB
/
ATM.h
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
#include <iostream>
#include<fstream>
#include <limits>
#include <string.h>
#include <unistd.h>
#include <windows.h> //for system cls and sleep and goto back: functions.
#include <conio.h> // for get function
//=======================Start of Bank Class==================
using namespace std;
class Bank{ //concept of Abstract Class and Pure Virtual function
public:
virtual void showData(void) = 0;
};
//=======================End of Bank Class==================
//=======================Start of class atm==============================
class atm{ //Concept of Class
public: //Concept of Private Access specifier
char username[30];
int password;
static int balance; //Concept of static.
public: //Concept of Public Access specifier
// atm(string un,int pass){ //Concept of Parameterized Constructor
// username = un;
// password = pass;
// }
// ~atm(){} //Concept of distructor
//getter for username
inline char* usernames(void){ //Use of inlilne function
return username;
}
//getter for password
inline int passwords(void){
return password;
}
// Function to set the data (setter)
void setData(void){
cout << "\n\t\t\t\t-------------------------------"<<endl;
cout << "\t\t\t\t\t>> APPLYING FORM <<" << endl;
cout << "\t\t\t\t-------------------------------" <<endl;
cin.ignore(numeric_limits<streamsize>::max(),'\n'); //This line ignores the rest of the current line, up to '\n'
cout << "\n\t\t\tEnter username:";
cin.getline(username, 30); //using getline function to take input from user
cout << "\n\t\t\tEnter 4-digit password:";
cin >> password;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\n\t\t\tEnter initial balance:";
cin >> balance;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
void showData(void){ // Function displaying the data (getter)
cout << "\t\t\tUsername:" << username<<endl;
cout << "\t\t\tPassword:" << password<<endl;
cout << "\t\t\tBalance:" << balance<<endl;
}
// All Members Function of class atm are defined here
void atmUser();
int add_user();
int view_all_users();
int delete_user(char*);
void update_user_as_deposit(char*);
void update_user_as_withdraw(char*);
int search_specific_user(char*, int);
int search_all_user_to_display(char*);
};
//=======================End of class atm==============================
//Static attributies Iinitializing
int atm::balance=0;
//===========Start Defining all Members Function of class atm-====================
void atm::atmUser(){
atm a;
char uname[30];
int pass, ch, ch1, ch2, found = 0;
mainmenu:
system("cls");
cout<<"\n\n\t\t\t\t===================================\n";
cout<<"\t\t\t\t\t| WELCOME TO ATM |";
cout<<"\n\t\t\t\t====================================\n";
cout<<endl<<endl;
cout<<"\t\t\t\t :: :: MAIN MENU :: ::\n";
cout<<endl;
cout<<"\n\n\t\t\t\t1. ADMIN";
cout<<"\n\t\t\t\t2. CUSTOMER";
cout<<"\n\t\t\t\t3. EXIT";
cout<<"\n\n\t\t\t\tChoose options:[1/2/3] :: ";
cin >> ch;
switch (ch) {
case 1:
rerun:
system("cls");
cout << "\n\t\t\t\tEnter details to login as Admin..!!\n";
cout << "\n\t\t\t\tEnter password for security Purposes : ";
cin >> pass;
if (pass == 1234) {
admin:
system("cls");
cout<<"\n\t\t\t\t-------------------------------"<<endl;
cout << "\t\t\t\t>>Welcome to Admin Menu..!<<";
cout<<"\n\t\t\t\t-------------------------------\n";
cout<<endl<<endl;
cout<<"\n\n\t\t\t\t1. ADD USER";
cout<<"\n\t\t\t\t2. DELETE USER";
cout<<"\n\t\t\t\t3. VIEW USER";
cout<<"\n\t\t\t\t4. EXIT";
cout<<"\n\n\t\t\t\tChoose options:[1/2/3/4] :: ";
cin >> ch1;
switch (ch1) {
case 1:
a.add_user();
goto admin;
case 2:
cout << "\n\t\t\t\tEnter the Username to be deleted : "<<endl;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cin.getline(uname, 30);
a.delete_user(uname);
goto admin;
case 3:
a.view_all_users();
sleep(4);
goto admin;
case 4:
break;
}
}
else {
cout << "\n\t\t\t\tDetails are incorrect ! Please try again.....";
cin.get();
goto rerun;
}
goto mainmenu;
case 2:
system("cls");
cout << "\n\t\t\t\tEnter details to login as User\n";
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\t\t\t\tEnter username:";
cin.getline(uname, 30);
cout << "\n\t\t\t\tEnter password:";
cin >> pass;
found = a.search_specific_user(uname, pass);
if (found) {
user:
system("cls");
cout<<"\n\t\t\t\t-------------------------------";
cout << "\n\t\t\t\t>>Welcome to Uesr Menu..!<<"<<endl;
cout<<"\t\t\t\t-------------------------------\n";
cout<<"\n\n\t\t\t\t1. DEPOSITE";
cout<<"\n\t\t\t\t2. WITHDRAW";
cout<<"\n\t\t\t\t3. VIEW ACCOUTN";
cout<<"\n\t\t\t\t4. EXIT";
cout<<"\n\n\t\t\t\tChoose options:[1/2/3/4] :: ";
cin >> ch2;
switch (ch2) {
case 1:
a.update_user_as_deposit(uname);
goto user;
case 2:
a.update_user_as_withdraw(uname);
goto user;
case 3:
a.search_all_user_to_display(uname);
sleep(4);
goto user;
case 4:
cout << "\nThank you";
break;
}
}
else {
cout << "\nNo account found with username "<<uname;
cin.get();
}
goto mainmenu;
case 3:
cout << "\n\tThankyou for banking with us";
cin.get();
break;
}
}
//ADDING DATA OPERACTION IN FILEING
int atm::add_user(){ // Function to add new recourd of new user
atm a;
ofstream file1;
file1.open("atm.csv",ios::out | ios::app); // Open file in write mode
if (!file1) {
cout << "Error in creating file.." << endl;
return 0;
}
a.setData(); // Read from user
file1.write((char*)&a, sizeof(a)); // Write into file
file1.close();
return 0;
}
//SEARCHING OPERACTION IN FILEING
int atm::view_all_users(){ // View recourds of all Users
atm a;
ifstream file1;
file1.open("atm.csv", ios::in);
if (!file1) {
cout << "\nError in opening file..";
return 0;
}
file1.read((char*)&a, sizeof(a)); // Read data from file
while (!file1.eof()) {
a.showData(); // Display data on screen
file1.read((char*)&a, sizeof(a));
}
file1.close();
return 0;
}
//DELETE OPERACTION IN FILEING
int atm::delete_user(char* uname){ // Function to delete the user
atm a;
fstream original, temp;
original.open("aaa.csv", ios::in);
if (!original) {
cout << "\nFile not found";
return 0;
}
else {
temp.open("temp.csv",ios::out | ios::app);
original.read((char*)&a, sizeof(a));
while (!original.eof()) {
if (!strcmp(a.usernames(),uname)) {
cout << "\nData found and deleted\n"<< a.username<<"\n";
}
else {
temp.write((char*)&a,sizeof(a));
}
original.read((char*)&a,sizeof(a));
}
original.close();
temp.close();
remove("atm.csv");
rename("temp.csv", "atm.csv");
a.view_all_users();
}
return 0;
}
//EDITING OPERACTION IN deposit FILE
void atm::update_user_as_deposit(char* uname){ // Function to update user by
atm a; // depositing money
fstream file, temp;
file.open("aaa.csv",ios::in | ios::out | ios::ate);
temp.open("temp.csv",ios::out | ios::app);
file.seekg(0);
file.read((char*)&a, sizeof(a));
while (!file.eof()) { /// sraech Till end of the file by help of .eof()
if (!strcmp(a.usernames(), uname)) {
int b;
cout << "\nEnter amount to deposit:";
cin >> b;
a.balance = a.balance + b;
cout << "\nBalance is:"<< a.balance;
temp.write((char*)&a, sizeof(a));
}
else {
temp.write((char*)&a, sizeof(a));
}
file.read((char*)&a, sizeof(a));
}
file.close();
temp.close();
remove("atm.csv");
rename("temp.csv", "atm.csv");
}
//EDITING OPERACTION IN withdraw FILE
void atm::update_user_as_withdraw(char* uname){ // Function to update user by
atm a; // depositing withdrawing user givcen amount
fstream file, temp;
file.open("atm.csv",ios::in | ios::out | ios::ate);
temp.open("temp.csv",ios::out | ios::app);
file.seekg(0);
file.read((char*)&a, sizeof(a));
while (!file.eof()) { // Till end of file is reached
if (!strcmp(a.usernames(), uname)) {
int b;
cout << "\nEnter amount to withdraw:";
cin >> b;
if (a.balance < b) {
cout<< "\nInsufficient balance to withdraw";
}
else {
a.balance = a.balance - b;
temp.write((char*)&a,sizeof(a));
cout << "\nBalance is:"<< a.balance;
}
}
else {
temp.write((char*)&a,sizeof(a));
}
file.read((char*)&a, sizeof(a));
}
file.close(); // Close the file
temp.close();
remove("atm.csv");
rename("temp.csv", "atm.csv");
}
//1->SEARCHING OPERACTION IN FILEING
int atm::search_specific_user(char* uname, int pass){ // Search specific user data
atm a;
fstream f;
f.open("atm.csv", ios::in); // Open the file
if (!f) {
cout << "\nError in opening file..";
return 0;
}
f.read((char*)&a, sizeof(a)); // Read data from file
while (!f.eof()) {
if (!strcmp(a.usernames(), uname)) {
if (a.passwords() == pass) {
return 1;
}
}
f.read((char*)&a, sizeof(a));
}
f.close(); // Close the file
return 0;
}
//2->SEARCHING OPERACTION IN FILEING
int atm::search_all_user_to_display(char* uname){ // Search all user data
atm a;
fstream file1;
file1.open("atm.csv", ios::in); // Open the file
if (!file1) {
cout << "\nError in opening file..";
return 0;
} // Read data from file
file1.read((char*)&a, sizeof(a));
while (!file1.eof()) {
if (!strcmp(a.usernames(), uname)) {
a.showData();
return 0;
}
file1.read((char*)&a, sizeof(a));
}
// Close the file
file1.close();
return 0;
}
//
////===========END Defining all Members Function of class atm-====================
////=======================END OF ATM CLASS==================