-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbankmanagement.cpp
More file actions
342 lines (294 loc) · 11.2 KB
/
bankmanagement.cpp
File metadata and controls
342 lines (294 loc) · 11.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
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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
// Base class
class Account {
private:
static int nextAccNum;
int AccNo;
string AccHolderName;
double balance; // Keep balance private
string password;
public:
Account(const string &holderName, double initBalance, const string &pwd)
: AccHolderName(holderName), balance(initBalance), password(pwd) {
AccNo = nextAccNum++;
}
virtual double calculateInterest() = 0; // Pure virtual function for polymorphism
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Successfully deposited: Rs" << amount << endl;
} else {
cout << "Invalid deposit amount!" << endl;
}
}
void withdraw(double amount, double overdraftLimit) {
const double withdrawalFee = 5.00;
const double minBalance = 100.00;
if (amount > 0 && (balance + overdraftLimit) >= (amount + withdrawalFee)) {
if ((balance - amount) < minBalance) {
cout << "Withdrawal denied! Minimum balance of Rs100.00 is required." << endl;
return;
}
balance -= (amount + withdrawalFee);
cout << "Successfully withdrew: Rs" << amount << " (with a fee of Rs" << withdrawalFee << ")" << endl;
} else {
cout << "Withdrawal amount exceeds balance and overdraft limit!" << endl;
}
}
virtual void displayAccDetails() const { // Marked as virtual
cout << fixed << setprecision(2);
cout << "\n---------------------------------------\n";
cout << " Account Details \n";
cout << "---------------------------------------\n";
cout << setw(20) << "Account Number:" << setw(11) << AccNo << endl;
cout << setw(20) << "Account Holder:" << setw(11) << AccHolderName << endl;
cout << setw(20) << "Balance:" << setw(9) << "Rs " << balance << endl;
}
bool authenticate(const string &enteredPassword) const {
return enteredPassword == password;
}
int getAccNo() const {
return AccNo;
}
protected: // Change access to protected
double getBalance() const {
return balance; // Getter for balance
}
void addBalance(double amount) {
balance += amount; // Setter to modify balance directly
}
};
int Account::nextAccNum = 10001;
// Derived class for Savings Account
class SavingsAccount : public Account {
private:
double interestRate;
public:
SavingsAccount(const string &holderName, double initBalance, const string &pwd)
: Account(holderName, initBalance, pwd), interestRate(0.05) {}
double calculateInterest() override {
double interest = getBalance() * interestRate; // Use getter for balance
addBalance(interest); // Add interest to balance
cout << "Interest of Rs" << interest << " added to your Savings Account." << endl;
return interest;
}
void displayAccDetails() const override { // Now correctly overrides
Account::displayAccDetails();
cout << setw(20) << "Account Type:" << setw(13) << "Savings" << endl;
cout << setw(20) << "Interest Rate:" << setw(10) << interestRate * 100 << "%" << endl;
cout << setw(20) << "Overdraft Limit:" << setw(9) << "N/A" << endl; // Savings accounts do not have overdraft
cout << "---------------------------------------\n";
}
};
// Derived class for Current Account
class CurrentAccount : public Account {
private:
double overdraftLimit;
public:
CurrentAccount(const string &holderName, double initBalance, const string &pwd)
: Account(holderName, initBalance, pwd), overdraftLimit(5000.0) {}
double calculateInterest() override {
double interest = getBalance() * 0.04; // Use getter for balance
addBalance(interest); // Add interest to balance
cout << "Interest of Rs" << interest << " added to your Current Account." << endl;
return interest;
}
void displayAccDetails() const override { // Now correctly overrides
Account::displayAccDetails();
cout << setw(20) << "Account Type:" << setw(13) << "Current" << endl;
cout << setw(20) << "Interest Rate:" << setw(10) << "4.00%" << endl; // Current accounts fixed interest
cout << setw(20) << "Overdraft Limit:" << setw(9) << "Rs " << overdraftLimit << endl;
cout << "---------------------------------------\n";
}
double getOverdraftLimit() const {
return overdraftLimit;
}
};
class Bank {
private:
vector<Account*> accounts; // Use pointers for polymorphism
public:
void createAcc() {
string holderName, password, AccType;
double initBalance;
cout << "Enter Account Holder Name: ";
cin.ignore();
getline(cin, holderName);
cout << "Enter Initial Balance: ";
cin >> initBalance;
cout << "Enter Account Type (Savings/Current): ";
cin >> AccType;
cout << "Set your account password: ";
cin >> password;
Account* newAccount = nullptr;
if (AccType == "Savings") {
newAccount = new SavingsAccount(holderName, initBalance, password);
} else if (AccType == "Current") {
newAccount = new CurrentAccount(holderName, initBalance, password);
} else {
cout << "Invalid account type!" << endl;
return;
}
accounts.push_back(newAccount);
cout << "Account created successfully!" << endl;
}
void deleteAccount(int accNum) {
auto it = accounts.begin();
while (it != accounts.end()) {
if ((*it)->getAccNo() == accNum) {
string enteredPassword;
cout << "Enter password to delete account: ";
cin >> enteredPassword;
if ((*it)->authenticate(enteredPassword)) {
delete *it; // Free memory
it = accounts.erase(it);
cout << "Account deleted successfully!" << endl;
return;
} else {
cout << "Incorrect password! Account deletion denied." << endl;
return;
}
} else {
++it;
}
}
cout << "Account not found!" << endl;
}
void transactionManagement() {
int accNum;
cout << "Enter Account Number: ";
cin >> accNum;
for (Account* account : accounts) {
if (account->getAccNo() == accNum) {
string enteredPassword;
cout << "Enter password for transaction: ";
cin >> enteredPassword;
if (account->authenticate(enteredPassword)) {
int choice;
double amount;
cout << "1. Deposit\n2. Withdraw\nChoose an option: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter amount to deposit: ";
cin >> amount;
account->deposit(amount);
break;
case 2:
if (dynamic_cast<CurrentAccount*>(account)) {
cout << "Enter amount to withdraw: ";
cin >> amount;
account->withdraw(amount, dynamic_cast<CurrentAccount*>(account)->getOverdraftLimit());
} else {
cout << "Enter amount to withdraw: ";
cin >> amount;
account->withdraw(amount, 0); // Savings account has no overdraft
}
break;
default:
cout << "Invalid option!" << endl;
}
} else {
cout << "Incorrect password! Transaction denied." << endl;
}
return;
}
}
cout << "Account not found!" << endl;
}
void AccBalanceInquiry() {
int accNum;
cout << "Enter Account Number: ";
cin >> accNum;
for (Account* account : accounts) {
if (account->getAccNo() == accNum) {
string enteredPassword;
cout << "Enter password to view details: ";
cin >> enteredPassword;
if (account->authenticate(enteredPassword)) {
account->displayAccDetails();
} else {
cout << "Incorrect password! Access denied." << endl;
}
return;
}
}
cout << "Account not found!" << endl;
}
void interestCal() {
int accNum;
cout << "Enter Account Number: ";
cin >> accNum;
for (Account* account : accounts) {
if (account->getAccNo() == accNum) {
string enteredPassword;
cout << "Enter password to calculate interest: ";
cin >> enteredPassword;
if (account->authenticate(enteredPassword)) {
account->calculateInterest();
} else {
cout << "Incorrect password! Access denied." << endl;
}
return;
}
}
cout << "Account not found!" << endl;
}
void displayAllAcc() {
cout << "---------------------------------------\n";
cout << " All Accounts in Indian Bank \n";
cout << "---------------------------------------\n";
for (Account* account : accounts) {
account->displayAccDetails();
}
cout << "---------------------------------------\n";
}
~Bank() {
for (Account* account : accounts) {
delete account; // Free memory
}
}
};
int main() {
Bank bank;
int choice;
while (true) {
cout << "\nWelcome to Indian Bank" << endl;
cout << "Your Trusted Partner in Financial Services." << endl;
cout << "\n1. Create Account\n2. Delete Account\n3. Transaction Management\n4. Account Balance Inquiry\n5. Calculate Interest\n6. Display All Accounts\n7. Exit\nChoose an option: ";
cin >> choice;
switch (choice) {
case 1:
bank.createAcc();
break;
case 2: {
int accNum;
cout << "Enter Account Number to delete: ";
cin >> accNum;
bank.deleteAccount(accNum);
break;
}
case 3:
bank.transactionManagement();
break;
case 4:
bank.AccBalanceInquiry();
break;
case 5:
bank.interestCal();
break;
case 6:
bank.displayAllAcc();
break;
case 7:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid option! Please try again." << endl;
}
}
}