-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2bank.cpp
320 lines (310 loc) · 9.95 KB
/
task2bank.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
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
#include <conio.h>
using namespace std;
char ch1;
string star=" ";
string name(string user)
{
fstream fin;
fin.open("task2data.txt",ios::in);
vector<string> row;
string line="",word="",temp="",ter="";
double t=0.0;
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while(getline(s,word,','))
row.push_back(word);
if(user==row[0])
{
ter=row[0];
break;
}
}
return ter;
}
string code(string user1)
{
fstream fin;
fin.open("task2data.txt",ios::in);
vector<string> row;
string line="",word="",temp="",ter="";
double t=0.0;
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while(getline(s,word,','))
row.push_back(word);
if(user1==row[0])
{
ter=row[1];
break;
}
}
return ter;
}
int pen(string user2)
{
fstream fin;
fin.open("task2data.txt",ios::in);
vector<string> row;
string line="",word="",temp="",ter="";
int t=0;
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while(getline(s,word,','))
row.push_back(word);
if(user2==row[0])
{
t=stoi(row[3]);
break;
}
}
return t;
}
double viewbal(string user2)
{
fstream fin;
fin.open("task2data.txt",ios::in);
vector<string> row;
string line="",word="",temp="",ter="";
double t=0.0;
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while(getline(s,word,','))
row.push_back(word);
if(user2==row[0])
{
t=stod(row[2]);
break;
}
}
return t;
}
double deposit(string user3,double amt)
{
fstream fin,fout;
vector<string> row;
string line="",word="",temp="",ter="";
double t=0.0;
fin.open("task2data.txt", ios::in);
fout.open("task2datanew.txt", ios::out);
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while (getline(s, word, ','))
row.push_back(word);
if(user3==row[0])
fout<<row[0]<<','<<row[1]<<','<<stod(row[2])+amt<<','<<row[3]<<'\n';
else
fout<<row[0]<<','<<row[1]<<','<<row[2]<<','<<row[3]<<'\n';
}
fin.close();
fout.close();
fstream kin,kout;
kin.open("task2datanew.txt",ios::in);
kout.open("task2data.txt",ios::out);
while(!kin.eof())
{
row.clear();
getline(kin,line);
stringstream s(line);
while (getline(s, word, ','))
row.push_back(word);
kout<<row[0]<<','<<row[1]<<','<<row[2]<<','<<row[3]<<','<<'\n';
}
kin.close();
kout.close();
ofstream ofs;
ofs.open("task2datanew.txt", ofstream::out | ofstream::trunc);
ofs.close();
return viewbal(user3);
}
double withdraw(string user4,double with)
{
fstream fin,fout;
vector<string> row;
string line="",word="",temp="",ter="";
double t=0.0;
fin.open("task2data.txt", ios::in);
fout.open("task2datanew.txt", ios::out);
while(!fin.eof())
{
row.clear();
getline(fin,line);
stringstream s(line);
while (getline(s, word, ','))
row.push_back(word);
if(user4==row[0])
fout<<row[0]<<','<<row[1]<<','<<stod(row[2])-with<<','<<row[3]<<'\n';
else
fout<<row[0]<<','<<row[1]<<','<<row[2]<<','<<row[3]<<'\n';
}
fin.close();
fout.close();
fstream kin,kout;
kin.open("task2datanew.txt",ios::in);
kout.open("task2data.txt",ios::out);
while(!kin.eof())
{
row.clear();
getline(kin,line);
stringstream s(line);
while (getline(s, word, ','))
row.push_back(word);
kout<<row[0]<<','<<row[1]<<','<<row[2]<<','<<row[3]<<','<<'\n';
}
kin.close();
kout.close();
ofstream ofs;
ofs.open("task2datanew.txt", ofstream::out | ofstream::trunc);
ofs.close();
return viewbal(user4);
}
int main()
{
fstream fout, fin;
fout.open("task2data.txt",ios::out|ios::app);
cout<<endl<<"Welcome to the IITI ATM."<<endl<<"========================"<<endl<<"This program was created by a first year B. Tech. student at IIT, Indore."<<endl;
cout<<"========================================================================="<<endl;
cout<<endl<<"Select an option to start:"<<endl;
cout<<"1. Create a new Account."<<endl<<"2. Login to your Account."<<endl<<"(press any key to quit)"<<endl<<endl;
char ch;
cin>>ch;
string uname="",pword="",confirm;
int pin,c,pinl;
double b=0.0;
char w;
switch(ch)
{
case '1':
cout<<endl<<"Enter new Account username (without any blank spaces): ";
cin>>uname;
do
{
cout<<"Enter new Account password (without any blank spaces): ";
cin>>pword;
cout<<"Confirm new Account password: ";
cin>>confirm;
if(confirm!=pword)
cout<<"Confirm password does not match. Retry."<<endl;
} while (confirm!=pword);
do
{
cout<<"Enter new Account PIN: ";
cin>>pin;
cout<<"Confirm new Account PIN: ";
cin>>c;
if(c!=pin)
cout<<"Confirm PIN does not match. Retry."<<endl;
} while (c!=pin);
cout<<endl<<"Account creation successful."<<endl;
cout<<"Username: "<<uname<<endl<<"Password: ";
for(int i=1;i<=pword.size();i++)
cout<<"*";
cout<<endl<<"PIN: ****"<<endl<<"DO NOT SHARE YOUR ACCOUNT PASSWORD/PIN WITH ANYONE."<<endl<<endl;
fout<<uname<<','<<pword<<','<<b<<','<<pin<<'\n';
break;
case '2':
do
{
cout<<endl<<"Enter username: ";
cin>>uname;
cout<<"Enter password: ";
cin>>pword;
if(uname!=name(uname) || pword!=code(uname))
cout<<"Invalid username or password entered. Try again."<<endl;
}while(uname!=name(uname) || pword!=code(uname));
cout<<"User authentication successful. Welcome."<<endl<<"DO NOT SHARE YOUR ACCOUNT PASSWORD/PIN WITH ANYONE."<<endl<<endl;
cout<<"Select an operation:"<<endl<<"1. Cash deposit."<<endl<<"2. Cash withdrawal."<<endl<<"3. Transfer money to another Account."<<endl<<"4. View current Account balance only."<<endl<<"(press any key to quit)"<<endl<<endl;
cin>>ch1;
double b=0.0;
string transfer="";
switch(ch1)
{
case '1':
cout<<endl<<"Enter your 4-digit Account PIN: ";
do
{
cin>>pinl;
if(pinl!=pen(uname))
cout<<"Invlaid PIN entered. Please try again."<<endl;
}while(pinl!=pen(uname));
cout<<"Enter the amount that you want to deposit: ";
cin>>b;
cout<<endl<<"Transaction successful."<<endl<<"Current Account balance: INR "<<deposit(uname,b)<<endl<<"========================================================================="<<endl;
cout<<"How would you rate this ATM? (1 to 5) (Any other key to skip) "<<endl;
cin>>star;
if(stod(star)>0.0)
cout<<"Thanks for rating us "<<star<<" stars!"<<endl<<endl;
break;
case '2':
cout<<endl<<"Enter your 4-digit Account PIN: ";
do
{
cin>>pinl;
if(pinl!=pen(uname))
cout<<"Invlaid PIN entered. Please try again."<<endl;
}while(pinl!=pen(uname));
cout<<"Enter the amount that you want to withdraw: ";
cin>>b;
cout<<endl<<"Transaction successful."<<endl<<"Current Account balance: INR "<<withdraw(uname,b)<<endl<<"========================================================================="<<endl;
cout<<"How would you rate this ATM? (1 to 5) (Any other key to skip) "<<endl;
cin>>star;
if(stod(star)>0.0)
cout<<"Thanks for rating us "<<star<<" stars!"<<endl<<endl;
break;
case '3':
cout<<endl<<"Enter your 4-digit Account PIN: ";
do
{
cin>>pinl;
if(pinl!=pen(uname))
cout<<"Invalid PIN entered. Please try again."<<endl;
}while(pinl!=pen(uname));
cout<<"Enter the username of the recipient: ";
cin>>transfer;
cout<<endl<<"Enter the amount to be transferred: ";
cin>>b;
deposit(transfer,b);
withdraw(uname,b);
cout<<endl<<"Transaction successful."<<endl<<"Current Account balance: INR "<<viewbal(uname)<<endl<<"========================================================================="<<endl;
cout<<"How would you rate this ATM? (1 to 5) (Any other key to skip) "<<endl;
cin>>star;
if(stod(star)>0.0)
cout<<"Thanks for rating us "<<star<<" stars!"<<endl<<endl;
break;
case '4':
cout<<endl<<"Enter your 4-digit Account PIN: ";
do
{
cin>>pinl;
if(pinl!=pen(uname))
cout<<"Invlaid PIN entered. Please try again."<<endl;
}while(pinl!=pen(uname));
cout<<"Current Account balance: INR "<<viewbal(uname)<<" only."<<endl<<"========================================================================="<<endl;
cout<<"How would you rate this ATM? (1 to 5) (Any other key to skip) "<<endl;
cin>>star;
if(stod(star)>0.0)
cout<<"Thanks for rating us "<<star<<" stars!"<<endl<<endl;
break;
}
}
cout<<endl<<"Exiting... (press any key)"<<endl<<endl;
getch();
return 0;
}