Click here to Skip to main content
15,888,155 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys i have been building this bank account programme for 4 bank accounts and i have been struggling in calculating the total balance and total interest and display both.

Could anyone help me i tried many things yet i am just 2 weeks into C++ and i would love to have some pro help.

The problem is that i do not know how to calculate totals a bit of code would be great the output should add all the final bal and all final interest and give total bal and total interes

What I have tried:

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;

class bank_account
{
public:
    bank_account();
    bank_account(int account_no, float opening_balance);
    void deposit(float amount);
    void withdraw(float amount);
    float interest(float interest_rate);
    void balance();
    void display_balance();
    void display_interest_earned();
    void display_bank_heading();
    void display_bank_body();
    void display_bank_bottom();
    void display_total_balance();
    void display_total_interest();
private:
    int dm_account_no;
    float dm_balance;
    float dm_interest_rate;
    float dm_interest_earned;
    float dm_total_balance;
    float dm_total_interest;
};
class validation
{
public:
    bool test_userid(int user_id);
    bool test_userpin(int user_pin);

private:
    int dm_userid;
    int dm_userpin;
};

bank_account::bank_account()
{
    dm_account_no = 0;
    dm_balance =0.0;
}


bank_account::bank_account(int account_no, float opening_balance)
{
    dm_account_no = account_no;

    dm_balance = opening_balance*1.1;
}

void bank_account::deposit(float amount)
{
     dm_balance += amount;
}

void bank_account::withdraw(float amount)
{
    dm_balance -= amount;

}
void bank_account::display_total_balance()
{
    balance = ;
}


float bank_account::interest(float interest_rate)
{ dm_interest_rate = interest_rate;
    dm_interest_earned = dm_balance * dm_interest_rate;
    return dm_interest_earned;
}
void bank_account::display_interest_earned()

{   cout<<fixed<<setprecision(2)<<dm_interest_earned<<endl;}

void bank_account::display_balance()

{   cout<<fixed<<setprecision(2)<<dm_balance<<endl;}


void bank_account::display_bank_heading()
{
    cout<<" Bank Account System"<<endl;
    cout<<" ______________________________________"<<endl;
    cout<<"|                                      |"<<endl;
    cout<<"|"<<left << setw(13) <<"" << "Bank Report 1"  << right << setw(13) <<"|"<<endl;
    cout<<"|" <<left << setw(15) <<"" << "Closing"  << left << setw(8) <<""<<"Interest"<<"|" <<endl;
    cout<<"|" << left << setw(15) << "Account No" << "Balance" <<left<<setw(8) <<""<<"Earned"<<left<<setw(2)<<""<<"|" << endl;}

void bank_account::display_bank_body()
{   cout <<"|" << left << setw(15) << dm_account_no<< setw(15) << fixed << setprecision(2)
    << dm_balance <<setw(8)<<dm_interest_earned<<""<<"|"<<endl;}

void bank_account::display_bank_bottom()
{
    cout<<"|                                      |"<<endl;
    cout<<"|"<<"Total" <<right<<setw(10)<<balance<<"|"<<endl;
    cout<<"|______________________________________|"<<endl;
}

///float bank_account::balance()
///{dm_total_balance = 1905 ;
///dm_total_interest = 61.05;}

int reply;
int account_no[4]={80045001,80045002,80045003,80045004};
float opening_balance[4]={100,200,300,400};
int deposit[5]={100,200,300,400,10};
int withdraw[2]={50,155};
float interest_rate[4]={0.01,0.02,0.03,0.04};


bank_account account1,account2,account3,account4;
int main()
{

    class bank_account account1(account_no[0],opening_balance[0]);
    account1.display_bank_heading();
    account1.deposit(deposit[0]);
    account1.withdraw(withdraw[0]);
    account1.interest(interest_rate[0]);
    account1.display_bank_body();


    class bank_account account2(account_no[1],opening_balance[1]);
    account2.deposit(deposit[1]);
    account2.withdraw(withdraw[0]);
    account2.interest(interest_rate[1]);
    account2.display_bank_body();

    class bank_account account3(account_no[2],opening_balance[2]);
    account3.deposit(deposit[2]);
    account3.withdraw(withdraw[0]);
    account3.interest(interest_rate[2]);
    account3.display_bank_body();

    class bank_account account4(account_no[3],opening_balance[3]);
    account4.deposit(deposit[3]);
    account4.withdraw(withdraw[0]);
    account4.interest(interest_rate[0]);
    account4.display_bank_body();
    account4.display_bank_bottom();

    cout<< "Do you want to continue Yes or not"<<endl;
    cout<< "Enter Y to continue or N to exit"<<endl;
    cin>>reply;


    if (reply == 'N'){
        cout<<"Goodbye!"<<endl;
        exit(EXIT_FAILURE);}


    account1.display_bank_heading();
    account1.deposit(deposit[4]);
    account1.withdraw(withdraw[1]);
    account1.interest(interest_rate[0]);
    account1.display_bank_body();

    account2.deposit(deposit[1]);
    account2.withdraw(withdraw[0]);
    account2.interest(interest_rate[1]);
    account2.display_bank_body();

    account3.deposit(deposit[2]);
    account3.withdraw(withdraw[0]);
    account3.interest(interest_rate[2]);
    account3.display_bank_body();

    account4.deposit(deposit[3]);
    account4.withdraw(withdraw[1]);
    account4.interest(interest_rate[0]);
    account4.display_bank_body();
    account4.display_bank_bottom();



    cout<< "Enter 1 to continue or 2 to exit"<<endl;
    cin>>reply;
    if (reply == 2){
        cout<<"Goodbye!"<<endl;
        exit(EXIT_FAILURE);
    }
    cout<<"Continue processing"<<endl;
    return 0;
}
Posted
Updated 18-Mar-22 12:28pm
v2
Comments
OriginalGriff 30-Jan-18 5:53am    
So what does it do that you didn't expect, or not do that you did?
What have you tired in order to fix it?
What help do you need?
All you have done is dump some undocumented code on us, and give us a rough idea what you want - that doesn't help us to identify what exactly you need help with.

Use the "Improve question" widget to edit your question and provide better information.
saide_a 30-Jan-18 6:38am    
I think you can't do that in bank_account class you need another class or another function that get bank_account as input object and add or withdraw value of that.

You must implement some functions in the bank account class which are returning the needed values.

C++
class bank_account
{
public:
  float getBalance();
float getInterest();

So you access these values in your main routine and can compute the total sums of it.

Some additional tip: use an array of accounts to ease writing code.
C++
bank_account account[4]; //create 4 accounts
int sum = 0;
for(int i = 0: i < 4; i++ ) {
  account[i].deposit(1000);//access a function and deposit 1000 to all accounts
  sum = sum + account[i] + getBalance();
}

And you are done ,-)
 
Share this answer
 
I propose to formally implement setters and getters for all sizes.
A method that calculates interest and a method that posts interest to the account.
C++
class bank_account {
public:
	bank_account();
	// bank_account(int account_no, float opening_balance);
	void SetAccountNo(int account_no);
	int GetAccountNo();
	void deposit(float amount);     // bank deposit
	void withdraw(float amount);    // bank withdraw 
	void setInterestRate(float interest_rate);
	float getInterestRate();
	float getinterest(); // bank interest (Zinsen)
	float getBalance();
	void  calcInterest(int years);
	// ...
private:
	int dm_account_no;
	float dm_balance;
	float dm_interest_rate;
	float dm_interest_earned;
	// float dm_total_balance;
	// float dm_total_interest;
};

bank_account::bank_account()
{
	dm_account_no = 0;
	dm_balance = 0.0;
	dm_interest_rate = 0.0;
	dm_interest_earned = 0.0;
}

void bank_account::calcInterest(int years)
{
	for (int i = 0; i < years; i++) {
		dm_interest_earned += getinterest();
		dm_balance += getinterest();
	}
}

// ....

int account_no[4] = { 80045001,80045002,80045003,80045004 };
float opening_balance[4] = { 100,200,300,400 };
float interest_rate[4] = { 0.01f,0.02f,0.03f,0.04f };
// int deposit[5] = { 100,200,300,400,10 };
// int withdraw[2] = { 50,155 };

const int N = 4;

int main()
{
	// define vector for N accounts
	std::vector <bank_account>account(N);

	for (int i = 0; i < N; i++) {
		account[i].SetAccountNo(account_no[i]);
		account[i].deposit(opening_balance[i]);  //Set opening balance 
		account[i].setInterestRate(interest_rate[i]);
	}

	// account update after 5 years 
	for (int i = 0; i < N; i++) {
		account[i].calcInterest(5);
	}

	// ...

Notice:
It's usually not a good idea to store money in a float variable. A long int that stores cents would be worth considering.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900