0% found this document useful (0 votes)
15 views

Program 1

Uploaded by

Toni Player936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Program 1

Uploaded by

Toni Player936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Here's a C++ implementation of the bank account application with the specified classes and

functionalities:

```cpp

#include <iostream>

#include <vector>

#include <string>

#include <stdexcept>

class Account {

protected:

std::string CNIC;

std::string accountNumber;

std::string accountTitle;

double balance;

static int count;

public:

Account(const std::string& cnic, const std::string& accNum, const std::string& accTitle, double
initialBalance)

: CNIC(cnic), accountNumber(accNum), accountTitle(accTitle), balance(initialBalance) {

if (!isValidCNIC(CNIC) || !isValidAccountNumber(accountNumber) ||
!isValidAccountTitle(accountTitle) || balance == 0) {

throw std::invalid_argument("Invalid account details.");

count++;

virtual void deposit(double amount) {

if (amount <= 0) {
throw std::invalid_argument("Deposit amount must be greater than zero.");

balance += amount;

virtual void withdraw(double amount) {

if (amount <= 0) {

throw std::invalid_argument("Withdrawal amount must be greater than zero.");

if (amount > balance) {

throw std::runtime_error("Insufficient balance.");

balance -= amount;

static int totalAccounts() {

return count;

static bool isValidCNIC(const std::string& cnic) {

for (char c : cnic) {

if (!std::isdigit(c)) {

return false;

return true;

static bool isValidAccountNumber(const std::string& accNum) {


return accNum.size() == 7 && accNum[0] == '1' && std::all_of(accNum.begin() + 1, accNum.end(),
::isdigit);

static bool isValidAccountTitle(const std::string& accTitle) {

if (accTitle.empty() || !std::isupper(accTitle[0])) {

return false;

for (char c : accTitle) {

if (!std::isalpha(c)) {

return false;

return true;

};

int Account::count = 0;

class CurrentAccount : public Account {

private:

double serviceFee;

public:

CurrentAccount(const std::string& cnic, const std::string& accNum, const std::string& accTitle, double
initialBalance, double fee)

: Account(cnic, accNum, accTitle, initialBalance), serviceFee(fee) {

}
void setServiceFee(double fee) {

serviceFee = fee;

double checkBalance() const {

return balance;

void deposit(double amount) override {

Account::deposit(amount);

balance -= serviceFee; // Deduct service fee

void withdraw(double amount) override {

Account::withdraw(amount);

balance -= serviceFee; // Deduct service fee

static int totalAccounts() {

return count;

};

class SavingAccount : public Account {

private:

double monthlyInterestRate;

public:
SavingAccount(const std::string& cnic, const std::string& accNum, const std::string& accTitle, double
initialBalance, double interestRate)

: Account(cnic, accNum, accTitle, initialBalance), monthlyInterestRate(interestRate) {

if (interestRate == 0) {

throw std::invalid_argument("Interest rate cannot be zero.");

void setInterestRate(double rate) {

monthlyInterestRate = rate;

double checkBalance() const {

return balance;

void deposit(double amount) override {

Account::deposit(amount);

calculateInterest();

void withdraw(double amount) override {

Account::withdraw(amount);

calculateInterest();

static int totalAccounts() {

return count;

}
void calculateInterest() {

balance += balance * (monthlyInterestRate / 100.0);

};

class AccountTest {

private:

std::vector<Account*> accounts;

public:

~AccountTest() {

for (Account* acc : accounts) {

delete acc;

void addCurrentAccount() {

std::string cnic, accNum, accTitle;

double balance, fee;

std::cout << "Enter CNIC: ";

std::cin >> cnic;

std::cout << "Enter Account Number (starts with 1): ";

std::cin >> accNum;

std::cout << "Enter Account Title: ";

std::cin >> accTitle;

std::cout << "Enter Initial Balance: ";

std::cin >> balance;

std::cout << "Enter Service Fee: ";


std::cin >> fee;

try {

CurrentAccount* acc = new CurrentAccount(cnic, accNum, accTitle, balance, fee);

accounts.push_back(acc);

std::cout << "Current Account created successfully." << std::endl;

} catch (const std::invalid_argument& e) {

std::cerr << "Error: " << e.what() << std::endl;

void addSavingAccount() {

std::string cnic, accNum, accTitle;

double balance, rate;

std::cout << "Enter CNIC: ";

std::cin >> cnic;

std::cout << "Enter Account Number (starts with 2): ";

std::cin >> accNum;

std::cout << "Enter Account Title: ";

std::cin >> accTitle;

std::cout << "Enter Initial Balance: ";

std::cin >> balance;

std::cout << "Enter Monthly Interest Rate: ";

std::cin >> rate;

try {

SavingAccount* acc = new SavingAccount(cnic, accNum, accTitle, balance, rate);

accounts.push_back(acc);

std::cout << "Saving Account created successfully." << std::endl;

} catch (const std::invalid_argument& e) {

std::cerr << "Error: " << e.what() << std::endl;


}

void depositMoney() {

int accIndex;

double amount;

std::cout << "Enter Account Index to Deposit Money: ";

std::cin >> accIndex;

if (accIndex >= 0 && accIndex < accounts.size()) {

std::cout << "Enter Amount to Deposit: ";

std::cin >> amount;

try {

accounts[accIndex]->deposit(amount);

std::cout << "Deposit successful. New balance: " << accounts[accIndex]->checkBalance() <<
std::endl;

} catch (const std::invalid_argument& e) {

std::cerr << "Error: " << e.what() << std::endl;

} else {

std::cerr << "Invalid account index." << std::endl;

void withdrawMoney() {

int accIndex;

double amount;

std::cout << "Enter Account Index to Withdraw Money: ";

std::cin >> accIndex;

if (accIndex >= 0 && accIndex < accounts.size()) {

You might also like