-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank-Management-System.cpp
More file actions
618 lines (552 loc) · 22.4 KB
/
Copy pathBank-Management-System.cpp
File metadata and controls
618 lines (552 loc) · 22.4 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// ============================================================
// Natnael International Bank - Management System
// ============================================================
// Author : Natnael Tezazu
// Project : C++ Bank Management System
// Language: C++ (OOP & File Handling)
// Notes : Built as part of personal C++ learning journey.
// Concepts applied: structs, file I/O, loops,
// functions, input validation.
// ============================================================
#include <iostream>
#include <fstream>
#include <string>
#include <cstring> // for strlen()
#include <cstdio> // for remove() and rename()
#include <iomanip> // for setw() and formatted output
using namespace std;
// ============================================================
// GLOBAL DATA
// ============================================================
const string VERSION = "1.0.0"; // current build version
const int MAX_ACCOUNTS = 1000; // maximum number of accounts the system can hold
int userCount = 1; // starts at 1 because index 0 is unused (accounts start at index 1)
// Represents a single bank account record stored in the system.
struct Account {
int accNo; // unique account number (auto-generated from base: 1000580)
char username[30]; // full name of the account holder (max 30 characters)
double balance; // current account balance in ETB (Ethiopian Birr)
string type; // account type: Normal, Female's, Children's, or Sheria
};
Account bankUsers[MAX_ACCOUNTS];
// ============================================================
// UI HELPERS - decorative lines and section headers
// ============================================================
// Prints a single horizontal border line for UI boxes.
void printLine() {
cout << " +----------------------------------------------------------+" << endl;
}
// Prints a double-line border for emphasis (used in banners).
void printDoubleLine() {
cout << " +==========================================================" << endl;
}
// Prints a labeled section header box with a title inside.
void printHeader(const string& title) {
printLine();
cout << " | " << left << setw(57) << title << "|" << endl;
printLine();
}
// Displays the bank welcome banner at program startup.
void printBanner() {
cout << endl;
cout << " +=========================================================+" << endl;
cout << " | NATNAEL INTERNATIONAL BANK |" << endl;
cout << " | Bank Management System v1.0 |" << endl;
cout << " +----------------------------------------------------------+" << endl;
cout << " | Author : Natnael Tezazu |" << endl;
cout << " +=========================================================+" << endl;
cout << endl;
}
// ============================================================
// FILE I/O
// ============================================================
// Saves the full accounts array to filename.bin using a safe
// write-to-temp-then-rename strategy to avoid data corruption.
void saveToFile(Account arr[]) {
fstream outFile("temp.bin", ios::out | ios::binary);
if (!outFile) {
cerr << " [ERROR] Could not open file for saving." << endl;
return;
}
outFile.write(reinterpret_cast<const char*>(arr), userCount * sizeof(Account));
outFile.close();
remove("filename.bin");
rename("temp.bin", "filename.bin");
}
// Loads all saved account records from filename.bin into the array.
void loadFromFile(Account arr[]) {
fstream inFile("filename.bin", ios::in | ios::binary);
if (!inFile) {
// No file yet — first run, perfectly normal.
return;
}
inFile.read(reinterpret_cast<char*>(arr), sizeof(Account) * MAX_ACCOUNTS);
inFile.close();
}
// Returns true if a slot in the array is empty (no account stored there).
bool isEmpty(Account arr[], int k) {
string name = arr[k].username;
return name.empty() && arr[k].accNo == 0;
}
// Writes a human-readable account list to usersdata.txt
void writeLog() {
fstream logFile("usersdata.txt", ios::out);
if (!logFile.is_open()) {
cerr << " [ERROR] Could not open usersdata.txt for writing." << endl;
return;
}
logFile << " Natnael International Bank - Account Registry" << endl;
logFile << " Total accounts: " << userCount - 1 << endl;
logFile << " ---------------------------------------------------------------" << endl;
logFile << " No" << "\t" << "Account No" << "\t" << "Username" << "\t"
<< "Balance (ETB)" << "\t" << "Type" << endl;
logFile << " ---------------------------------------------------------------" << endl;
for (int k = 1; k < userCount; k++) {
logFile << " " << k << ".\t"
<< bankUsers[k].accNo << "\t\t"
<< bankUsers[k].username << "\t\t"
<< bankUsers[k].balance << " ETB\t\t"
<< bankUsers[k].type << endl;
logFile << " ---------------------------------------------------------------" << endl;
}
logFile.close();
}
// ============================================================
// INPUT HELPERS
// ============================================================
// Reads and validates an account number — rejects non-numeric and non-positive input.
int readAccountNumber(const string& prompt) {
int accNo;
cout << prompt;
while (!(cin >> accNo) || accNo <= 0) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Invalid account number. Please enter a valid number: ";
}
return accNo;
}
// ============================================================
// CORE FEATURES
// ============================================================
// Registers a new bank account and assigns an auto-generated account number.
void createAccount(int k, Account users[]) {
printHeader(" NEW ACCOUNT REGISTRATION");
cout << endl;
cin.ignore();
cout << " Enter full name : ";
cin.getline(users[k].username, sizeof(users[k].username));
cout << " Enter initial deposit : ";
while (!(cin >> users[k].balance) || users[k].balance < 0) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Invalid amount. Enter a positive number: ";
}
cout << endl;
printLine();
cout << " | Select Account Type |" << endl;
printLine();
cout << " | 1. Normal Account |" << endl;
cout << " | 2. Female's Account |" << endl;
cout << " | 3. Children's Account |" << endl;
cout << " | 4. Sheria Account |" << endl;
printLine();
cout << " Enter choice: ";
int type;
cin >> type;
switch (type) {
case 1: users[k].type = "Normal"; break;
case 2: users[k].type = "Female's"; break;
case 3: users[k].type = "Children's"; break;
case 4: users[k].type = "Sheria"; break;
default:
cout << " [!] Invalid choice. Setting to Normal." << endl;
users[k].type = "Normal";
}
users[k].accNo = 1000580 + userCount;
userCount++;
cout << endl;
printLine();
cout << " | Account created successfully! |" << endl;
cout << " | Your Account Number: " << left << setw(35) << users[k].accNo << "|" << endl;
cout << " | Please save your account number safely. |" << endl;
printLine();
cout << endl;
}
// Handles depositing funds into an existing account with input validation.
void deposit() {
printHeader(" DEPOSIT FUNDS");
cout << endl;
int accNo = readAccountNumber(" Enter your account number: ");
bool found = false;
double amount = 0;
for (int i = 1; i < userCount; i++) {
if (accNo == bankUsers[i].accNo) {
cout << " Enter deposit amount (ETB): ";
while (!(cin >> amount) || amount <= 0) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Invalid amount. Must be greater than 0: ";
}
bankUsers[i].balance += amount;
found = true;
cout << endl;
printLine();
cout << " | " << fixed << setprecision(2)
<< amount << " ETB deposited successfully.";
cout << setw(max(0, (int)(26 - to_string((int)amount).length()))) << "|" << endl;
cout << " | New balance: " << left << setw(43) << bankUsers[i].balance << "|" << endl;
printLine();
break;
}
}
if (!found) {
cout << " [!] Account not found. Please check the account number." << endl;
}
cout << endl;
}
// Handles withdrawing funds with overdraft protection.
void withdraw() {
printHeader(" WITHDRAW FUNDS");
cout << endl;
int accNo = readAccountNumber(" Enter your account number: ");
bool found = false;
double amount = 0;
for (int i = 1; i < userCount; i++) {
if (accNo == bankUsers[i].accNo) {
found = true;
bool valid = false;
do {
cout << " Enter withdrawal amount (ETB): ";
while (!(cin >> amount) || amount <= 0) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Invalid amount. Must be greater than 0: ";
}
if (amount <= bankUsers[i].balance) {
bankUsers[i].balance -= amount;
valid = true;
cout << endl;
printLine();
cout << " | " << amount << " ETB withdrawn successfully. |" << endl;
cout << " | Remaining balance: " << left << setw(37) << bankUsers[i].balance << "|" << endl;
printLine();
} else {
cout << " [!] Insufficient balance. Available: "
<< bankUsers[i].balance << " ETB." << endl;
}
} while (!valid);
break;
}
}
if (!found) {
cout << " [!] Account not found. Please check the account number." << endl;
}
cout << endl;
}
// Transfers funds between two accounts with full validation.
void transfer() {
printHeader(" TRANSFER FUNDS");
cout << endl;
bool foundSender = false, foundReceiver = false;
int senderAccNo = readAccountNumber(" Enter your account number : ");
for (int i = 1; i < userCount; i++) {
if (senderAccNo == bankUsers[i].accNo) {
foundSender = true;
int receiverAccNo = readAccountNumber(" Enter receiver's account number: ");
if (receiverAccNo == senderAccNo) {
cout << " [!] Cannot transfer to your own account." << endl;
cout << endl;
return;
}
for (int j = 1; j < userCount; j++) {
if (receiverAccNo == bankUsers[j].accNo) {
foundReceiver = true;
double amount = 0;
bool valid = false;
do {
cout << " Enter amount to transfer (ETB): ";
while (!(cin >> amount) || amount <= 0) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Invalid amount: ";
}
if (amount <= bankUsers[i].balance) {
bankUsers[i].balance -= amount;
bankUsers[j].balance += amount;
valid = true;
cout << endl;
printLine();
cout << " | Transfer successful! |" << endl;
cout << " | " << amount << " ETB sent to " << bankUsers[j].username
<< setw(max(0, (int)(30 - strlen(bankUsers[j].username)))) << "|" << endl;
cout << " | Your new balance: " << left << setw(38) << bankUsers[i].balance << "|" << endl;
printLine();
} else {
cout << " [!] Insufficient balance. Available: "
<< bankUsers[i].balance << " ETB." << endl;
}
} while (!valid);
break;
}
}
break;
}
}
if (!foundSender) {
cout << " [!] Sender account not found." << endl;
} else if (!foundReceiver) {
cout << " [!] Receiver account not found." << endl;
}
cout << endl;
}
// Looks up and displays full details of a single account by account number.
void retrieveAccount() {
printHeader(" ACCOUNT DETAILS");
cout << endl;
int accNo = readAccountNumber(" Enter your account number: ");
bool found = false;
for (int i = 1; i < userCount; i++) {
if (accNo == bankUsers[i].accNo) {
found = true;
cout << endl;
printLine();
cout << " | Account Number : " << left << setw(39) << bankUsers[i].accNo << "|" << endl;
cout << " | Account Holder : " << left << setw(39) << bankUsers[i].username << "|" << endl;
cout << " | Balance : " << left << setw(35) << bankUsers[i].balance
<< "ETB |" << endl;
cout << " | Account Type : " << left << setw(39) << bankUsers[i].type << "|" << endl;
printLine();
break;
}
}
if (!found) {
cout << " [!] Account not found. Please check the account number." << endl;
}
cout << endl;
}
// Allows updating the username or account type of an existing account.
void modifyAccount() {
printHeader(" MODIFY ACCOUNT");
cout << endl;
int accNo = readAccountNumber(" Enter your account number: ");
bool found = false;
for (int i = 1; i < userCount; i++) {
if (accNo == bankUsers[i].accNo) {
found = true;
cout << endl;
printLine();
cout << " | What would you like to modify? |" << endl;
printLine();
cout << " | 1. Change username |" << endl;
cout << " | 2. Change account type |" << endl;
printLine();
cout << " Enter choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << " Enter new username: ";
cin.ignore();
cin.getline(bankUsers[i].username, sizeof(bankUsers[i].username));
break;
case 2:
cout << endl;
printLine();
cout << " | 1. Normal 2. Female's 3. Children's 4. Sheria |" << endl;
printLine();
cout << " Enter choice: ";
int type;
cin >> type;
switch (type) {
case 1: bankUsers[i].type = "Normal"; break;
case 2: bankUsers[i].type = "Female's"; break;
case 3: bankUsers[i].type = "Children's"; break;
case 4: bankUsers[i].type = "Sheria"; break;
default: cout << " [!] Invalid choice. No changes made." << endl;
}
break;
default:
cout << " [!] Invalid choice. No changes made." << endl;
}
cout << endl;
printLine();
cout << " | Account updated successfully. |" << endl;
printLine();
break;
}
}
if (!found) {
cout << " [!] Account not found. Please check the account number." << endl;
}
cout << endl;
}
// Permanently removes an account from the system after user confirmation.
void deleteAccount() {
printHeader(" DELETE ACCOUNT");
cout << endl;
int accNo = readAccountNumber(" Enter account number to delete: ");
bool found = false;
for (int i = 1; i < userCount; i++) {
if (bankUsers[i].accNo == accNo) {
found = true;
cout << endl;
printLine();
cout << " | Account found: |" << endl;
cout << " | Holder : " << left << setw(47) << bankUsers[i].username << "|" << endl;
cout << " | Balance: " << left << setw(43) << bankUsers[i].balance << "ETB |" << endl;
printLine();
cout << endl;
cout << " [!] Are you sure you want to delete this account? (y/n): ";
cout << " [!] This action is permanent and cannot be undone." << endl;
char confirm;
cin >> confirm;
if (confirm == 'y' || confirm == 'Y') {
for (int j = i; j < userCount - 1; j++) {
bankUsers[j] = bankUsers[j + 1];
}
bankUsers[userCount - 1] = Account();
userCount--;
cout << endl;
printLine();
cout << " | Account deleted successfully. |" << endl;
printLine();
} else {
cout << " [!] Deletion cancelled." << endl;
}
break;
}
}
if (!found) {
cout << " [!] Account not found. Please check the account number." << endl;
}
cout << endl;
}
// Displays a formatted table of all registered accounts in the system.
void listAllAccounts() {
printHeader(" ALL ACCOUNTS");
cout << endl;
if (userCount <= 1) {
cout << " No accounts registered yet." << endl;
cout << endl;
return;
}
cout << " Total accounts: " << userCount - 1 << endl << endl;
cout << " " << left
<< setw(5) << "No"
<< setw(13) << "Acc. No"
<< setw(22) << "Username"
<< setw(18) << "Balance (ETB)"
<< setw(14) << "Type" << endl;
printLine();
for (int k = 1; k < userCount; k++) {
cout << " " << left
<< setw(5) << k
<< setw(13) << bankUsers[k].accNo
<< setw(22) << bankUsers[k].username
<< setw(18) << bankUsers[k].balance
<< setw(14) << bankUsers[k].type << endl;
}
printLine();
cout << endl;
}
// Renders the main navigation menu in the terminal.
void showMenu() {
cout << endl;
printLine();
cout << " | MAIN MENU |" << endl;
printLine();
cout << " | 1. Create Account |" << endl;
cout << " | 2. Deposit Funds |" << endl;
cout << " | 3. Withdraw Funds |" << endl;
cout << " | 4. Transfer Funds |" << endl;
cout << " | 5. View Account Details |" << endl;
cout << " | 6. Modify Account |" << endl;
cout << " | 7. List All Accounts |" << endl;
cout << " | 8. Delete Account |" << endl;
cout << " | 9. Exit |" << endl;
printLine();
cout << " Enter your choice (1-9): ";
}
// ============================================================
// MAIN
// ============================================================
int main() {
// Load any previously saved accounts from binary file
loadFromFile(bankUsers);
// Count how many valid accounts are already stored
for (int i = 1; i < MAX_ACCOUNTS; i++) {
if (!isEmpty(bankUsers, i)) {
userCount++;
}
}
printBanner();
cout << " Welcome back! Current registered accounts: " << userCount - 1 << endl;
int option;
do {
showMenu();
while (!(cin >> option)) {
cin.clear();
cin.ignore(1000, '\n');
cout << " [!] Please enter a valid number (1-9): ";
}
switch (option) {
case 1:
createAccount(userCount, bankUsers);
saveToFile(bankUsers);
writeLog();
break;
case 2:
deposit();
saveToFile(bankUsers);
writeLog();
break;
case 3:
withdraw();
saveToFile(bankUsers);
writeLog();
break;
case 4:
transfer();
saveToFile(bankUsers);
writeLog();
break;
case 5:
retrieveAccount();
break;
case 6:
modifyAccount();
saveToFile(bankUsers);
writeLog();
break;
case 7:
listAllAccounts();
break;
case 8:
deleteAccount();
saveToFile(bankUsers);
writeLog();
break;
case 9: {
cout << endl;
cout << " [?] Are you sure you want to exit? (y/n): ";
char exitConfirm;
cin >> exitConfirm;
if (exitConfirm == 'y' || exitConfirm == 'Y') {
cout << endl;
printLine();
cout << " | Thank you for using Natnael International Bank! |" << endl;
cout << " | Goodbye. |" << endl;
printLine();
cout << endl;
} else {
cout << " [!] Exit cancelled. Returning to menu." << endl;
option = 0; // reset so the loop continues
}
break;
}
default:
cout << " [!] Invalid choice. Please enter a number between 1 and 9." << endl;
}
} while (option != 9);
return 0;
}