Repository navigation
Expand file tree
/
Copy pathbanking_system.cpp
More file actions
132 lines (107 loc) · 2.78 KB
/
Copy pathbanking_system.cpp
File metadata and controls
132 lines (107 loc) · 2.78 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
#include <iostream>
#include <fstream>
using namespace std;
class Account {
public:
int accNo;
char name[50];
float balance;
void create() {
cout << "Enter Account No: ";
cin >> accNo;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Balance: ";
cin >> balance;
}
void display() {
cout << "\nAccount No: " << accNo;
cout << "\nName: " << name;
cout << "\nBalance: " << balance << endl;
}
};
// Create account
void addAccount() {
Account a;
ofstream file("bank.dat", ios::binary | ios::app);
a.create();
file.write((char*)&a, sizeof(a));
file.close();
cout << "Account Created!\n";
}
// Display account
void showAccount(int num) {
Account a;
ifstream file("bank.dat", ios::binary);
while(file.read((char*)&a, sizeof(a))) {
if(a.accNo == num) {
a.display();
return;
}
}
cout << "Account not found!\n";
}
// Deposit / Withdraw
void updateAccount(int num, int type) {
Account a;
fstream file("bank.dat", ios::binary | ios::in | ios::out);
while(file.read((char*)&a, sizeof(a))) {
if(a.accNo == num) {
float amt;
cout << "Enter amount: ";
cin >> amt;
if(type == 1)
a.balance += amt;
else if(type == 2 && amt <= a.balance)
a.balance -= amt;
else
cout << "Insufficient balance!\n";
int pos = -1 * sizeof(a);
file.seekp(pos, ios::cur);
file.write((char*)&a, sizeof(a));
cout << "Updated successfully!\n";
return;
}
}
cout << "Account not found!\n";
}
// Main menu
int main() {
int ch, num;
do {
cout << "\n--- BANK MENU ---\n";
cout << "1. Create Account\n";
cout << "2. Deposit\n";
cout << "3. Withdraw\n";
cout << "4. Balance Enquiry\n";
cout << "5. Exit\n";
cout << "Enter choice: ";
cin >> ch;
switch(ch) {
case 1:
addAccount();
break;
case 2:
cout << "Enter Account No: ";
cin >> num;
updateAccount(num, 1);
break;
case 3:
cout << "Enter Account No: ";
cin >> num;
updateAccount(num, 2);
break;
case 4:
cout << "Enter Account No: ";
cin >> num;
showAccount(num);
break;
case 5:
cout << "Thank you!\n";
break;
default:
cout << "Invalid choice!\n";
}
} while(ch != 5);
return 0;
}