-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankingSystem.cpp
188 lines (156 loc) · 5.5 KB
/
bankingSystem.cpp
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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
// Base class Account
class Account {
protected:
string accountHolderName;
double balance;
public:
// Constructor
Account(string name, double initialBalance)
: accountHolderName(name), balance(initialBalance) {}
// Deposit method
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposited: " << amount << " to " << accountHolderName << "'s account." << endl;
} else {
cout << "Invalid deposit amount." << endl;
}
}
// Withdraw method
virtual void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrew: " << amount << " from " << accountHolderName << "'s account." << endl;
} else {
cout << "Insufficient balance or invalid amount." << endl;
}
}
// Display method
virtual void display() const {
cout << "Account Holder: " << accountHolderName << ", Balance: " << balance << endl;
}
// Getter for balance
double getBalance() const {
return balance;
}
// Getter for account holder name
string getAccountHolderName() const {
return accountHolderName;
}
virtual ~Account() = default; // Virtual destructor for proper cleanup
};
// Savings Account
class Savings : public Account {
private:
double interestRate;
public:
Savings(string name, double initialBalance, double rate)
: Account(name, initialBalance), interestRate(rate) {}
// Calculate interest
void calculateInterest() const {
double interest = balance * interestRate;
cout << "Interest for " << accountHolderName << ": " << interest << endl;
}
// Display details
void display() const override {
cout << "Savings Account - ";
Account::display();
cout << "Interest Rate: " << interestRate << endl;
}
};
// Current Account
class Current : public Account {
private:
double minimumBalance;
public:
Current(string name, double initialBalance, double minBalance)
: Account(name, initialBalance), minimumBalance(minBalance) {}
// Check if balance meets the minimum requirement
void checkMinimumBalance() const {
if (balance < minimumBalance) {
cout << accountHolderName << "'s account is below the minimum balance of " << minimumBalance << endl;
} else {
cout << accountHolderName << "'s account meets the minimum balance requirement." << endl;
}
}
// Display details
void display() const override {
cout << "Current Account - ";
Account::display();
cout << "Minimum Balance: " << minimumBalance << endl;
}
};
// Fixed Deposit Account
class FixedDeposit : public Account {
private:
int maturityPeriod; // in years
double interestRate;
public:
FixedDeposit(string name, double initialBalance, int period, double rate)
: Account(name, initialBalance), maturityPeriod(period), interestRate(rate) {}
// Calculate maturity amount
void calculateMaturityAmount() const {
double maturityAmount = balance * pow((1 + interestRate), maturityPeriod);
cout << "Maturity Amount for " << accountHolderName << " after " << maturityPeriod << " years: " << maturityAmount << endl;
}
// Display details
void display() const override {
cout << "Fixed Deposit Account - ";
Account::display();
cout << "Maturity Period: " << maturityPeriod << " years, Interest Rate: " << interestRate << endl;
}
};
// Function to get low balance accounts
Account** getLowBalanceAccounts(Account* accounts[], int size, double threshold, int& lowBalanceCount) {
// Create a dynamic array to store low balance accounts
Account** lowBalanceAccounts = new Account*[size];
lowBalanceCount = 0;
for (int i = 0; i < size; i++) {
if (accounts[i]->getBalance() < threshold) {
lowBalanceAccounts[lowBalanceCount++] = accounts[i];
}
}
return lowBalanceAccounts;
}
int main() {
// Create instances of each account type
Savings savingsAccount("Alice", 5000, 0.05);
Current currentAccount("Bob", 2000, 1000);
FixedDeposit fdAccount("Charlie", 10000, 5, 0.07);
// Create an array of accounts
Account* accounts[] = {&savingsAccount, ¤tAccount, &fdAccount};
int size = sizeof(accounts) / sizeof(accounts[0]);
// Demonstrate functionality
cout << "Displaying all accounts:\n";
for (int i = 0; i < size; i++) {
accounts[i]->display();
cout << endl;
}
cout << "\nPerforming specific operations:\n";
savingsAccount.calculateInterest();
currentAccount.checkMinimumBalance();
fdAccount.calculateMaturityAmount();
// Find low balance accounts
int lowBalanceCount = 0;
Account** lowBalanceAccounts = getLowBalanceAccounts(accounts, size, 3000, lowBalanceCount);
cout << "\nAccounts with balance less than 3000:\n";
for (int i = 0; i < lowBalanceCount; i++) {
lowBalanceAccounts[i]->display();
cout << endl;
}
// Clean up dynamic memory
delete[] lowBalanceAccounts;
// Search for an account by name
string searchName = "Alice";
cout << "\nSearching for account holder: " << searchName << endl;
for (int i = 0; i < size; i++) {
if (accounts[i]->getAccountHolderName() == searchName) {
accounts[i]->display();
}
}
return 0;
}