-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank-Account_Management-System.CPP
More file actions
308 lines (270 loc) · 7.62 KB
/
Copy pathBank-Account_Management-System.CPP
File metadata and controls
308 lines (270 loc) · 7.62 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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <sstream>
#include <string>
using namespace std;
// Class representing a bank account
class Account
{
private:
string AccountNo; // Account number
string Password; // Account password
int Balance; // Account balance
public:
// Default constructor initializing member variables
Account() : AccountNo(""), Password(""), Balance(0) {}
// Setter for AccountNo
void setAccountNo(const string& id)
{
AccountNo = id;
}
// Setter for Password
void setPassword(const string& pw)
{
Password = pw;
}
// Setter for Balance
void setBalance(int balance)
{
Balance = balance;
}
// Getter for AccountNo
string getAccountNo() const
{
return AccountNo;
}
// Getter for Password
string getPassword() const
{
return Password;
}
// Getter for Balance
int getBalance() const
{
return Balance;
}
};
// Function to open a new account
void openAccount()
{
string id, pw;
cout << "\tEnter Account No: ";
cin >> id;
cout << "\tEnter A Strong Password: ";
cin >> pw;
Account user;
user.setAccountNo(id);
user.setPassword(pw);
user.setBalance(0);
// Open file to save account details
ofstream outfile("Account.txt", ios::app);
if (!outfile)
{
cerr << "\tError: File Can't Open!" << endl;
return;
}
// Write account details to file
outfile << user.getAccountNo() << " : " << user.getPassword() << " : " << user.getBalance() << endl;
cout << "\tAccount Created Successfully!" << endl;
Sleep(5000);
}
// Function to add cash to an existing account
void addCash()
{
string id;
cout << "\tEnter Account No: ";
cin >> id;
ifstream infile("Account.txt");
ofstream outfile("Account_Temp.txt");
if (!infile || !outfile)
{
cout<< "\tError: File Can't Open!" << endl;
return;
}
string line;
bool found = false;
while (getline(infile, line))
{
stringstream ss(line);
string userID, userPW;
int userBalance;
char delimiter;
ss >> userID >> delimiter >> userPW >> delimiter >> userBalance;
if (id == userID)
{
found = true;
int cash;
cout << "\tEnter Cash: ";
cin >> cash;
if (cash > 0)
{ // Ensure positive cash value
int newBalance = userBalance + cash;
outfile << userID << " : " << userPW << " : " << newBalance << endl;
cout << "\tNew Balance Is: " << newBalance << endl;
}
else
{
cout << "\tInvalid Cash Amount!" << endl;
outfile << line << endl; // Keep old line if cash is invalid
}
}
else
{
outfile << line << endl; // Write unchanged lines to temp file
}
}
if (!found)
{
cout << "\tInvalid Account No!" << endl;
}
infile.close();
outfile.close();
remove("Account.txt");
rename("Account_Temp.txt", "Account.txt");
Sleep(5000);
}
// Function to withdraw cash from an existing account
void withdraw()
{
string id, pw;
cout << "\tEnter Account No: ";
cin >> id;
cout << "\tEnter Password: ";
cin >> pw;
ifstream infile("Account.txt");
ofstream outfile("Account_Temp.txt");
if (!infile || !outfile)
{
cerr << "\tError: File Can't Open!" << endl;
return;
}
string line;
bool found = false;
while (getline(infile, line))
{
stringstream ss(line);
string userID, userPW;
int userBalance;
char delimiter;
ss >> userID >> delimiter >> userPW >> delimiter >> userBalance;
if (id == userID && pw == userPW)
{
found = true;
int cash;
cout << "\tEnter Cash: ";
cin >> cash;
if (cash > 0 && cash <= userBalance)
{ // Ensure valid withdrawal
int newBalance = userBalance - cash;
outfile << userID << " : " << userPW << " : " << newBalance << endl;
cout << "\tTransaction Was Successful!" << endl;
cout << "\tRemaining Balance: " << newBalance << endl;
}
else if (cash > userBalance)
{
cout << "\tInsufficient Funds!" << endl;
outfile << line << endl; // Write unchanged line if insufficient funds
}
else
{
cout << "\tInvalid Cash Amount!" << endl;
outfile << line << endl; // Write unchanged line if invalid amount
}
}
else
{
outfile << line << endl; // Write unchanged lines to temp file
}
}
if (!found)
{
cout << "\tInvalid Account No or Password!" << endl;
}
infile.close();
outfile.close();
remove("Account.txt");
rename("Account_Temp.txt", "Account.txt");
Sleep(5000);
}
// Function to check balance of an existing account
void checkBalance()
{
string id, pw;
cout << "\tEnter Account No: ";
cin >> id;
cout << "\tEnter Password: ";
cin >> pw;
ifstream infile("Account.txt");
if (!infile)
{
cerr << "\tError: File Can't Open!" << endl;
return;
}
string line;
bool found = false;
while (getline(infile, line))
{
stringstream ss(line);
string userID, userPW;
int userBalance;
char delimiter;
ss >> userID >> delimiter >> userPW >> delimiter >> userBalance;
if (id == userID && pw == userPW)
{
found = true;
cout << "\tYour Balance is: " << userBalance << endl;
break;
}
}
if (!found)
{
cout << "\tInvalid Account No or Password!" << endl;
}
infile.close();
Sleep(5000);
}
// Main function to run the bank account management system
int main()
{
bool exit = false;
while (!exit)
{
int val;
cout << "\tWelcome To Bank Account Management System" << endl;
cout << "\t*****************************************" << endl;
cout << "\t-----------------Main Menu---------------" << endl;
cout << "\t1. Open New Account." << endl;
cout << "\t2. Add Cash." << endl;
cout << "\t3. Withdraw Cash." << endl;
cout << "\t4. Check Balance." << endl;
cout << "\t5. Exit." << endl;
cout << "\tEnter Your Choice: ";
cin >> val;
switch (val)
{
case 1:
openAccount();
break;
case 2:
addCash();
break;
case 3:
withdraw();
break;
case 4:
checkBalance();
break;
case 5:
exit = true;
cout << "\tGoodbye!" << endl;
break;
default:
cout << "\tInvalid Choice! Please try again." << endl;
break;
Sleep(2000); // Pause for user to see message before clearing screen
}
Sleep(3000); // Pause for user to read output
}
return 0;
}