-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.cpp
More file actions
337 lines (311 loc) · 12.5 KB
/
Copy pathproject1.cpp
File metadata and controls
337 lines (311 loc) · 12.5 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
// Student name: Sharan Prahanth S Registration number: 23CS216
// the account number and passwords are present inside data.txt file
#include <iostream>
#include <fstream>
class Atm
{
private:
double _accbal;
public:
double accbal = _accbal;
double withdraw(double am)
{
_accbal = _accbal - am;
accbal = _accbal;
return accbal;
}
double deposit(double am)
{
_accbal = _accbal + am;
accbal = _accbal;
return accbal;
}
double checkbal()
{
accbal = _accbal;
return accbal;
}
void setAccBal(double y)
{
_accbal = y;
accbal = y;
}
};
// function for input validation on numbers
int checkNum(std::string n)
{
int number;
int ref = 1;
while (ref)
{
try
{
int num = stoi(n);
if (std::to_string(num) != n) // or n.size() != to_string(num).size())
{
ref = 1;
std::cout << "Please enter a valid number: " << std::endl;
getline(std::cin, n);
}
else
{
ref = 0;
}
}
catch (std::invalid_argument)
{
std::cout << "Please enter a valid number: " << std::endl;
getline(std::cin, n);
}
}
number = stoi(n);
return number;
}
int main()
{
std::cout << "Student name: Sujan P, Registration number: 23CS227" << std::endl;
std::string a, b;
std::string acc_no, pass;
int option, inp_amount, acc_index, pass_trycount = 1, acc_number_trycount = 1;
bool is_present = false;
// fetching the data from data.txt file and storing it in an array named data
std::string data[60];
std::ifstream readFile("./data.txt", std::ifstream::in);
std::string line;
int line_no = 1, i = 0;
while (getline(readFile, line))
{
data[i] = line;
i++;
}
readFile.close(); // closing the file after fetching the data
Atm obj; // creating object for Atm
std::cout << "Welcome to ABC Bank" << std::endl
<< "Please enter your account number:";
std::cin >> acc_no;
// iterating through the array to check if the given account number is present
for (int i = 1; i < 56; i += 6)
{
if (data[i] == acc_no)
{
is_present = true;
acc_index = i;
}
}
// getting the account number again if the given account number is incorrect till 5 times
while (!is_present && acc_number_trycount < 5)
{
std::cout << "wrong account number" << std::endl;
std::cout << "Exited successfully..." << std::endl
<< std::endl;
std::cout << "Welcome to ABC Bank" << std::endl
<< "Please enter your account number:";
std::cin >> acc_no;
acc_number_trycount += 1;
for (int i = 1; i < 56; i += 6)
{
if (data[i] == acc_no)
{
is_present = true;
acc_index = i;
}
}
}
// 5 unsuccessful attempts result in termination of the program with visit later message
if (acc_number_trycount == 5)
{
std::cout << "Too many wrong tries" << std::endl;
std::cout << "Visit later!..." << std::endl;
}
else
{
obj.setAccBal(std::stod(data[acc_index + 4])); // converting the string value to double
std::cout << "Enter your password: ";
std::cin >> pass;
// asking for password again if the given password is incorrect till 3 times
while (pass != data[acc_index + 2] && pass_trycount < 3)
{
std::cout << "Wrong password" << std::endl
<< "Enter the correct password: ";
std::cin >> pass;
pass_trycount++;
}
if (pass == data[acc_index + 2])
{
std::cout << std::endl
<< "To withdraw money (maximum 10000 per transaction), press 1"
<< std::endl
<< "To deposit money (maximum 10000 per transaction), press 2"
<< std::endl
<< "To check available balance, press 3"
<< std::endl
<< "To exit, press 4"
<< std::endl;
getline(std::cin, a);
option = checkNum(a);
while (option < 1 || option > 4)
{
std::cout << "Please enter a valid number: " << std::endl;
getline(std::cin, a);
option = checkNum(a);
}
while (option != 4)
{
if (option == 1)
{
std::string avail_notes;
if (obj.accbal < 200)
{
std::cout << "The minimum balance is 100, so cannot withdraw money" << std::endl;
}
else
{
if (obj.accbal < 600)
{
avail_notes = "100 ";
}
else
{
avail_notes = "100 and 500 ";
}
std::cout << "Available notes " << avail_notes << std::endl;
double max_withdraw = (obj.checkbal() - 100 <= 10000) ? obj.checkbal() - 100 : 10000;
std::cout << "Please enter an amount less than or equal to " << max_withdraw << " to withdraw: " << std::endl;
getline(std::cin, b);
inp_amount = checkNum(b);
while (inp_amount < 100 || inp_amount > 10000 || inp_amount % 100 != 0 || inp_amount > obj.accbal - 100)
{
// different reasons for why the transaction failed
if (inp_amount > obj.accbal - 100)
{
std::cout << "Available balance is only: " << obj.accbal << std::endl
<< "Minimum balance is 100" << std::endl
<< "please enter a valid amount: " << std::endl;
}
else if (inp_amount > 10000)
{
std::cout << "Cannot withdraw more than 10000 in a single transaction" << std::endl
<< "please enter a valid amount: " << std::endl;
}
else if (inp_amount < 100)
{
std::cout << "cannot withdraw less than 100: " << std::endl
<< "Please enter a valid amount: " << std::endl;
}
else
{
std::cout << "Invalid amount, available notes are only " << avail_notes << std::endl
<< "Please enter a valid amount: " << std::endl;
}
// getting the valid amount from the user again
getline(std::cin, b);
inp_amount = checkNum(b);
}
std::cout << "Here's your money " << inp_amount << std::endl;
std::cout << "Available balance: " << obj.withdraw(inp_amount) << std::endl
<< std::endl;
}
}
else if (option == 2)
{
std::string accept_notes;
if (obj.checkbal() > 99900)
{
std::cout << "cannot deposit money as your balance will exceed the limit 100000" << std::endl;
}
else
{
if (obj.checkbal() > 99500)
{
accept_notes = "100 ";
}
else
{
accept_notes = "100 and 500 ";
}
std::cout << "Accepted notes to deposit " << accept_notes << std::endl;
double max_deposit = (100000 - obj.checkbal() <= 10000) ? 100000 - obj.checkbal() : 10000;
std::cout << "Please enter an amount less than or equal to " << max_deposit << " to deposit: " << std::endl;
getline(std::cin, b);
inp_amount = checkNum(b);
while (inp_amount < 100 || inp_amount > 10000 || inp_amount % 100 != 0 || inp_amount + obj.checkbal() > 100000)
{
// different reasons for why the transaction failed
if (inp_amount > 10000)
{
std::cout << "Cannot deposit more than 10000 in a single transaction" << std::endl
<< "Please enter a valid amount: " << std::endl;
}
else if (inp_amount + obj.checkbal() > 100000)
{
std::cout << "cannot deposit this amount of money as your balance will exceed 100000" << std::endl
<< "Please enter a valid amount: " << std::endl;
}
else if (inp_amount < 100)
{
std::cout << "cannot deposit less than 100" << std::endl
<< "Please enter a valid amount: " << std::endl;
}
else
{
std::cout << "Invalid amount, accepted notes are only " << accept_notes << std::endl
<< "please enter a valid amount: " << std::endl;
}
// getting the valid amount from the user again
getline(std::cin, b);
inp_amount = checkNum(b);
}
std::cout << "Money successfully deposited to account: " << b << std::endl;
std::cout << "Available balance: " << obj.deposit(inp_amount) << std::endl
<< std::endl;
}
}
else if (option == 3)
{
std::cout << "Available Balance: " << obj.checkbal() << std::endl
<< std::endl;
}
std::cout << std::endl
<< "To withdraw money, press 1"
<< std::endl
<< "To deposit money, press 2"
<< std::endl
<< "To check available balance, press 3"
<< std::endl
<< "To exit, press 4"
<< std::endl;
getline(std::cin, a);
option = checkNum(a);
while (option < 1 || option > 4)
{
std::cout << "Please enter a valid number: " << std::endl;
getline(std::cin, a);
option = checkNum(a);
}
}
double updated_bal = obj.checkbal();
// updating the balance to the data.txt file
int target_line = acc_index + 4;
std::fstream writeFile("./data.txt", std::ofstream::out);
for (int i = 0; i < 60; i++)
{
if (i == target_line)
{
writeFile << updated_bal << "\n";
}
else
{
writeFile << data[i] << "\n";
}
}
writeFile.close(); // closing the file after updating
}
// 3 unsuccessful attempts results in termination of the program with try again later message
else
{
std::cout << "Too many wrong attempts, try again later!" << std::endl;
}
}
std::cout << "Exited successfully..." << std::endl;
return 0;
}