Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Task 3 Lab 10.cpp

Uploaded by

latif qasim
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Task 3 Lab 10.cpp

Uploaded by

latif qasim
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>
using namespace std;

class Bank;
class Account;

class Customer {
int id;
string name;
friend class Account;
public:
Customer( const int id = 0, const string name = "" ) : id(id), name(name) {}

void display()
{
cout << "\t\t\t\tName: " << this->name << ", ID: " << this->id << endl;
}

int get_id() const { return this->id; }


string get_name() const { return this->name; }
void set_id( const int id ) { this->id = id; }
void set_name( const string name ) { this->name = name; }
};

class Account {
int account_number;
static int account_number_giver;
double balance;
Customer owner;

friend class Bank;


public:
Account( const double balance = 0 ) : account_number(account_number_giver++),
balance(balance) {}

void deposit( const int balance_to_deposit )


{
this->balance += balance_to_deposit;
cout << "\tDeposited RS." << balance_to_deposit << " from the account" <<
endl;
}

void withdraw( const int balance_to_withdraw )


{
this->balance -= balance_to_withdraw;
cout << "\tWithdrawn RS." << balance_to_withdraw << " from the account" <<
endl;
}

void check_balance() { cout << "\t\t\tBalance: " << this->balance << endl; }

void display_details()
{
cout << "\t\t\tAccount number: " << this->account_number << '\n';
this->check_balance();
cout << "\t\t\tOwner details:\n";
this->owner.display();
}
friend void operator+ ( Account &account_1, Account &account_2 );
};

int Account::account_number_giver = 10001;

void operator+ ( Account &account_1, Account &account_2 )


{
int amount_to_transfer;
cout << "\n\tEnter the amount to transfer from account 1 to account 2: ";
cin >> amount_to_transfer;
if ( account_1.balance >= amount_to_transfer )
{
account_1.withdraw( amount_to_transfer );
account_2.deposit( amount_to_transfer );
cout << "\tSuccessfully transferred funds from account 1 to account 2" <<
endl;
}
else cout << "\tInsufficient funds. Cannot conduct transaction" << endl;
}

class Bank {
vector<Account> accounts;
public:
void open_account( const Customer &customer )
{
double initial_deposit_amount;

cout << "\tEnter the initial deposit amount before opening account of the
customer: ";
cin >> initial_deposit_amount;
Account temp( initial_deposit_amount );
temp.owner.set_name( customer.get_name() );
temp.owner.set_id( customer.get_id() );

this->accounts.push_back( temp );
}

void display_accounts()
{
cout << "\n\tDisplaying details of accounts:\n";
for ( int i = 0; i < this->accounts.size(); i++ )
{
cout << "\t\tFor account no." << i+1 << ":\n";
this->accounts[i].display_details();
}
}
};

int main ()
{
cout << "Creating and giving values to customer object(s):\n";
Customer customer_1(1, "Mahira Khan"), customer_2(2, "Central Cee"),
customer_3(3, "Mohammad Ali");

cout << "Creating bank object(s):\n";


Bank bank;
cout << "Creating Account objects:\n";
Account account_1( 20000 ), account_2(5000), account_3;

cout << "Opening accounts in the bank:\n";


bank.open_account( customer_1 );
bank.display_accounts();
bank.open_account( customer_2 );
bank.display_accounts();
bank.open_account( customer_3 );
bank.display_accounts();

cout << "\nWithdrawing and depositing amount from accounts:\n";


account_1.deposit(400);
account_1.check_balance();
account_2.withdraw(1000);
account_2.check_balance();

cout << "\nUsing + operator to add funds from one account to another account:\
n";
account_1 + account_2; // transferring 20000 into account_2
account_2 + account_3; // depositing 4000 into account_3
account_3 + account_1; // depositing 5000 into account_1

You might also like