-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanking_main.cpp
More file actions
281 lines (237 loc) · 7.8 KB
/
Copy pathbanking_main.cpp
File metadata and controls
281 lines (237 loc) · 7.8 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#ifdef _WIN32
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
using namespace std;
// Structure for Bank Account
struct BankAccount {
string accountNumber;
string name;
double balance;
string pin;
};
// Function to clear the screen
void clearScreen() {
system(CLEAR);
}
// Function to create a new account
void createAccount() {
clearScreen();
BankAccount acc;
ofstream outFile("accounts.dat", ios::app);
cout << "\n========= CREATE BANK ACCOUNT =========\n";
cout << "Enter Account Number (12 digits): ";
cin >> acc.accountNumber;
cout << "Enter Account Holder Name: ";
cin.ignore();
getline(cin, acc.name);
cout << "Set a 4-digit PIN: ";
cin >> acc.pin;
cout << "Enter Initial Deposit Amount: ";
cin >> acc.balance;
outFile << acc.accountNumber << " " << acc.pin << " " << acc.name << " " << acc.balance << "\n";
outFile.close();
cout << "\n Account Created Successfully!\n";
}
// Function to modify account details
void modifyAccount() {
clearScreen();
string accNum, enteredPin;
BankAccount acc;
ifstream inFile("accounts.dat");
ofstream outFile("temp.dat");
bool found = false;
cout << "\n========= MODIFY AN ACCOUNT =========\n";
cout << "Enter Account Number: ";
cin >> accNum;
cout << "Enter PIN: ";
cin >> enteredPin;
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
if (acc.accountNumber == accNum && acc.pin == enteredPin) {
cout << "\nEnter New Account Holder Name: ";
cin.ignore();
getline(cin, acc.name);
cout << "Enter New PIN: ";
cin >> acc.pin;
found = true;
cout << "\n Account Modified Successfully!\n";
}
outFile << acc.accountNumber << " " << acc.pin << " " << acc.name << " " << acc.balance << "\n";
}
inFile.close();
outFile.close();
remove("accounts.dat");
rename("temp.dat", "accounts.dat");
if (!found) cout << " Invalid Account Number or PIN!\n";
}
// Function to check balance
void balanceEnquiry() {
clearScreen();
string accNum, enteredPin;
BankAccount acc;
ifstream inFile("accounts.dat");
cout << "\n========= BALANCE ENQUIRY =========\n";
cout << "Enter Account Number: ";
cin >> accNum;
cout << "Enter PIN: ";
cin >> enteredPin;
bool found = false;
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
if (acc.accountNumber == accNum && acc.pin == enteredPin) {
cout << "\n Account Holder: " << acc.name;
cout << "\n Available Balance: Rs. " << fixed << setprecision(2) << acc.balance << "\n";
found = true;
break;
}
}
inFile.close();
if (!found) cout << " Invalid Account Number or PIN!\n";
}
// Function to deposit money
void depositAmount() {
clearScreen();
string accNum;
double deposit;
BankAccount acc;
ifstream inFile("accounts.dat");
ofstream outFile("temp.dat");
bool found = false;
cout << "\n========= DEPOSIT IN ACCOUNT =========\n";
cout << "Enter Account Number: ";
cin >> accNum;
cout << "Enter Amount to Deposit: ";
cin >> deposit;
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
if (acc.accountNumber == accNum) {
acc.balance += deposit;
found = true;
cout << "\n Deposit Successful! New Balance: Rs. " << fixed << setprecision(2) << acc.balance << "\n";
}
outFile << acc.accountNumber << " " << acc.pin << " " << acc.name << " " << acc.balance << "\n";
}
inFile.close();
outFile.close();
remove("accounts.dat");
rename("temp.dat", "accounts.dat");
if (!found) cout << " Account Not Found!\n";
}
// Function to withdraw money
void withdrawAmount() {
clearScreen();
string accNum, enteredPin;
double withdraw;
BankAccount acc;
ifstream inFile("accounts.dat");
ofstream outFile("temp.dat");
bool found = false;
cout << "\n========= WITHDRAW FROM ACCOUNT =========\n";
cout << "Enter Account Number: ";
cin >> accNum;
cout << "Enter PIN: ";
cin >> enteredPin;
cout << "Enter Amount to Withdraw: ";
cin >> withdraw;
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
if (acc.accountNumber == accNum && acc.pin == enteredPin) {
if (acc.balance >= withdraw) {
acc.balance -= withdraw;
found = true;
cout << "\n Withdrawal Successful! New Balance: Rs. " << fixed << setprecision(2) << acc.balance << "\n";
} else {
cout << " Insufficient Balance!\n";
}
}
outFile << acc.accountNumber << " " << acc.pin << " " << acc.name << " " << acc.balance << "\n";
}
inFile.close();
outFile.close();
remove("accounts.dat");
rename("temp.dat", "accounts.dat");
if (!found) cout << " Invalid Account Number or PIN!\n";
}
// Function to display all accounts
void allAccountList() {
clearScreen();
BankAccount acc;
ifstream inFile("accounts.dat");
cout << "\n========= ALL ACCOUNT HOLDER LIST =========\n";
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
cout << "\n Account Number: " << acc.accountNumber;
cout << "\n Name: " << acc.name;
cout << "\n Balance: Rs. " << fixed << setprecision(2) << acc.balance << "\n----------------------------\n";
}
inFile.close();
}
// Function to delete an account
void closeAccount() {
clearScreen();
string accNum, enteredPin;
BankAccount acc;
ifstream inFile("accounts.dat");
ofstream outFile("temp.dat");
bool found = false;
cout << "\n========= CLOSE AN ACCOUNT =========\n";
cout << "Enter Account Number: ";
cin >> accNum;
cout << "Enter PIN: ";
cin >> enteredPin;
while (inFile >> acc.accountNumber >> acc.pin >> ws) {
getline(inFile, acc.name, ' ');
inFile >> acc.balance;
if (acc.accountNumber == accNum && acc.pin == enteredPin) {
found = true;
} else {
outFile << acc.accountNumber << " " << acc.pin << " " << acc.name << " " << acc.balance << "\n";
}
}
inFile.close();
outFile.close();
remove("accounts.dat");
rename("temp.dat", "accounts.dat");
if (found) cout << "\n Account Deleted Successfully!\n";
else cout << " Invalid Account Number or PIN!\n";
}
// Function to display the main menu
void Display_Menu() {
cout << "\n================ SecurePay Bank ================\n";
cout << "01. CREATE NEW ACCOUNT\n02. MODIFY AN ACCOUNT\n03. BALANCE ENQUIRY\n";
cout << "04. DEPOSIT\n05. WITHDRAW\n06. ACCOUNT LIST\n07. CLOSE ACCOUNT\n08. EXIT\n";
cout << "\nSELECT OPTION: ";
}
int main() {
char choice;
do {
clearScreen();
Display_Menu();
cin >> choice;
switch (choice) {
case '1': createAccount(); break;
case '2': modifyAccount(); break;
case '3': balanceEnquiry(); break;
case '4': depositAmount(); break;
case '5': withdrawAmount(); break;
case '6': allAccountList(); break;
case '7': closeAccount(); break;
case '8': cout << "\n Exiting. Thank you!\n"; break;
default: cout << "\n Invalid Option! Try Again.\n";
}
cin.ignore();
cin.get();
} while (choice != '8');
return 0;
}