forked from roerohan/Miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankFunctionalitiesBasic.cpp
More file actions
310 lines (287 loc) · 10.2 KB
/
BankFunctionalitiesBasic.cpp
File metadata and controls
310 lines (287 loc) · 10.2 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
/*Bank Acct Management system: Create Acct, deposit, withdraw, fund transfer within same bank, balance inquiry,
display acct holder details, modify acct details, close acct permanently, loan request*/
#include<iostream>
#include<fstream>
#include<string>
#include <string.h>
#include <stdlib.h>
using namespace std;
class banker;
class acct {
private:
int acctno;
string holdername;
string address;
string accttype;
float balance;
float loan;
float income;
public:
acct() {
acctno = 0;
holdername = "";
address = "";
accttype = "";
balance = 0;
loan = 0;
income = 0;
}
static int no;
void createacct();
void deposit(float n);
void withdraw(float m);
void display_details();
void modify_details();
void close_acct();
void loanrequest(int l);
void transfer(int c, int amt);
int getBalance();
string getName();
};
int acct::no = 100835298;
string acct::getName() {
return holdername;
}
int acct::getBalance() {
return balance;
}
void acct::createacct() {
cout << "Enter the name of the account holder:\n";
cin >> holdername;
cout << "Address of account holder:\n";
cin >> address;
cout << "Which type of account would you like? Savings or Current:\n";
cin >> accttype;
cout << "Please enter your starting balance(Rs.):\n";
cin >> balance;
cout << "For future loan purposes, what is your current annual salary?\n";
cin >> income;
acctno = no++;
cout << "Your new account number: " << acctno << endl;
}
void acct::deposit(float n) {
balance += n;
cout << "You have successfully deposited.\n" << "Balance: " << balance << endl;
}
void acct::withdraw(float n) {
balance -= n;
if (balance < 0) {
cout << "You don't have enough money to withdraw Rs. " << n << endl;
balance += n;
} else
cout << "You have successfully withdrawn Rs. " << n << "\n" << "Remaining balance: " << balance << endl;
}
void acct::loanrequest(int l) {
loan = l;
}
void acct::transfer(int c, int amt) {
if (c == 1)
balance += amt;
else if (c == 2) {
balance -= amt;
cout << "Successfully transferred";
}
}
void acct::display_details() {
cout << "Name: " << holdername << endl <<
"Account number: " << acctno << endl <<
"Address: " << address << endl <<
"Account type: " << accttype << endl <<
"Balance: " << balance << endl <<
"Amount of loan taken: " << loan << endl <<
"Income: " << income << endl;
}
void acct::modify_details() {
cout << "Enter the new name: ";
cin >> holdername;
cout << "Enter the new address: ";
cin >> address;
cout << "Enter the new income: ";
cin >> income;
}
int main() {
fstream file;
int choice, f;
float amount;
long pos;
string name;
acct user;
do {
cout << "\nWelcome. What do you want to do?" <<
"\n1. Create an account." <<
"\n2. Deposit money." <<
"\n3. Withdraw money." <<
"\n4. Display your details." <<
"\n5. Modify your details." <<
"\n6. Request for loan." <<
"\n7. Transfer money to other account" <<
"\n8. Close your account." <<
endl;
cin >> choice;
f = 0;
if (choice == 0)
exit(0);
else if (choice == 1) {
f = 0;
} else if (choice >= 2 && choice <= 8) {
cout << "Enter your name\n";
cin >> name;
} else {
cout << "You seem to have entered a wrong number";
}
//declares an object of the class acct.
switch (choice) {
case 1: //creates an account for the user
user.createacct();
file.open("database.dat", ios::binary | ios::app);
file.write((char * ) & user, sizeof(user));
break;
case 2: //deposits money in the current users account
cout << "Enter the amount you want to deposit" << endl;
fflush(stdin);
scanf("%f", & amount);
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
user.deposit(amount);
pos = file.tellg();
file.seekp(pos - sizeof(user));
file.write((char * ) & user, sizeof(user));
f = 1;
break;
}
}
if (f == 0) {
cout << "Not Found";
}
break;
case 3: //withdraws money from the current users account
cout << "Enter the amount you want to withdraw" << endl;
float amout;
cin >> amount;
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
user.withdraw(amount);
pos = file.tellg();
file.seekp(pos - sizeof(user));
file.write((char * ) & user, sizeof(user));
f = 1;
break;
}
}
if (f == 0) {
cout << "Not Found";
}
break;
case 4: //displays users details
cout << "Here are your details.\n";
file.open("database.dat", ios::binary | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
user.display_details();
f = 1;
break;
}
}
if (f == 0) {
cout << "Not Found";
}
break;
case 5: //modify your account details
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
user.modify_details();
pos = file.tellg();
file.seekp(pos - sizeof(user));
file.write((char * ) & user, sizeof(user));
f = 1;
break;
}
}
if (f == 0) {
cout << "Not Found";
}
break;
case 6: //apply for loan
int l;
cout << "Enter the loan required: ";
cin >> l;
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
user.loanrequest(l);
pos = file.tellg();
file.seekp(pos - sizeof(user));
file.write((char * ) & user, sizeof(user));
f = 1;
break;
}
}
if (f == 0) {
cout << "Not Found";
}
break;
case 7:
{
string rec;
int amt;
cout << "Enter recipient's name: ";
cin >> rec;
cout << "Enter amount: ";
cin >> amt;
int recipientBal,
senderBal;
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name) == 0) {
senderBal = user.getBalance();
}
}
file.close();
if (senderBal - amount > 0) {
file.open("database.dat", ios::binary | ios::out | ios:: in );
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(rec) == 0) {
user.transfer(1, amt);
} else if (user.getName().compare(name) == 0) {
user.transfer(2, amt);
}
pos = file.tellg();
file.seekp(pos - sizeof(user));
file.write((char * ) & user, sizeof(user));
}
} else {
cout << "Sender has insufficient balance" << endl;
}
break;
}
case 8: //close account
{
int deleted;
fstream file2;
file.open("database.dat", ios::binary | ios::out | ios:: in );
file2.open("temporary.dat", ios::binary | ios::out);
while (file.read((char * ) & user, sizeof(user))) {
if (user.getName().compare(name)) {
file2.write((char * ) & user, sizeof(user));
} else {
deleted = 1;
}
}
file.close();
file2.close();
if (deleted == 0) {
cout << "Not Found";
} else {
cout << "Deleted";
}
deleted = remove("database.dat") == 0;
rename("temporary.dat", "database.dat");
break;
}
}
file.close();
} while (choice != 0);
}