#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
// Base class: Account
class Account {
protected:
int accountNumber;
string holderName;
float balance;
public:
Account(int accNo, string name, float initialBalance) {
if (initialBalance < 0)
throw invalid_argument("Initial balance cannot be negative.");
accountNumber = accNo;
holderName = name;
balance = initialBalance;
}
virtual ~Account() {}
virtual void deposit(float amount) {
if (amount < 0)
throw invalid_argument("Deposit amount cannot be negative.");
balance += amount;
}
virtual void withdraw(float amount) {
if (amount < 0)
throw invalid_argument("Withdrawal amount cannot be negative.");
if (amount > balance)
throw runtime_error("Insufficient balance for withdrawal.");
balance -= amount;
}
virtual void displayDetails() const {
cout << "Account Number: " << accountNumber << endl;
cout << "Holder Name: " << holderName << endl;
cout << "Balance: €" << balance << endl;
}
int getAccountNumber() const {
return accountNumber;
}
};
// Derived class: SavingsAccount
class SavingsAccount : public Account {
private:
float interestRate;
public:
SavingsAccount(int accNo, string name, float initialBalance, float rate)
: Account(accNo, name, initialBalance), interestRate(rate) {}
void applyInterest() {
balance += balance * (interestRate / 100.0);
}
void displayDetails() const override {
Account::displayDetails();
cout << "Interest Rate: " << interestRate << "%" << endl;
}
};
// Global list to store accounts
vector<SavingsAccount> accounts;
// Function declarations
void createAccount();
void depositToAccount();
void withdrawFromAccount();
void displayAllAccounts();
void applyInterestToAll();
int main() {
int choice;
while (true) {
cout << "\n--- Banking System Menu ---\n";
cout << "1. Create New Account\n";
cout << "2. Deposit\n";
cout << "3. Withdraw\n";
cout << "4. Display All Accounts\n";
cout << "5. Apply Interest\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
try {
switch (choice) {
case 1:
createAccount();
break;
case 2:
depositToAccount();
break;
case 3:
withdrawFromAccount();
break;
case 4:
displayAllAccounts();
break;
case 5:
applyInterestToAll();
break;
case 6:
cout << "Exiting the program.\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
}
return 0;
}
// Create account
void createAccount() {
int accNo;
string name;
float balance, rate;
cout << "Enter Account Number: ";
cin >> accNo;
cout << "Enter Holder Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Initial Balance: ";
cin >> balance;
cout << "Enter Interest Rate (%): ";
cin >> rate;
accounts.emplace_back(accNo, name, balance, rate);
cout << "Account created successfully.\n";
}
// Deposit
void depositToAccount() {
int accNo;
float amount;
cout << "Enter Account Number: ";
cin >> accNo;
cout << "Enter Amount to Deposit: ";
cin >> amount;
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accNo) {
acc.deposit(amount);
cout << "Deposit successful.\n";
return;
}
}
throw runtime_error("Account not found.");
}
// Withdraw
void withdrawFromAccount() {
int accNo;
float amount;
cout << "Enter Account Number: ";
cin >> accNo;
cout << "Enter Amount to Withdraw: ";
cin >> amount;
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accNo) {
acc.withdraw(amount);
cout << "Withdrawal successful.\n";
return;
}
}
throw runtime_error("Account not found.");
}
// Display all
void displayAllAccounts() {
for (const auto& acc : accounts) {
cout << "\n--- Account Details ---\n";
acc.displayDetails();
}
}
// Apply interest
void applyInterestToAll() {
for (auto& acc : accounts) {
acc.applyInterest();
}
cout << "Interest applied to all accounts.\n";
}