Click to See Complete Forum and Search --> : using classes


taz_beginner
November 15th, 2006, 06:22 AM
This program is using classes to perform various banking functions(i am a beginner)
how can i make it withdraw client money and display all clients account balances?


// bank account program

#include <iostream.h>

const m=50;

class BANK
{
char name[10],Atype[10];
int accountno[m],count;
float balance[m],amount[m];

public:
void CNT(void){count=0;} //initialising count
void getitem(void);
void displayAll(void);
void deposit(void);
void remove(void);
void displaySum(void);
};

//===================================================

void BANK :: getitem(void) //creates an account
{
cout<<" \nEnter your name please\n";
cin>>name[count];
cout<<" \nEnter the type of account you'd like,\n";
cout<<" \nEither a \n1. current,\n2. Savings or \n3. Fixed account\n";
cin>>Atype;
cout<<" \nEnter account number: ";
cin>>accountno[count];
}

void BANK :: deposit(void) // depositting amounts
{

cout<<" \nPlease enter your account number\n";
cin>> accountno[count];
cout<<" \nPlease Enter the amount you are depositting\n";
cin>>amount[count];
cout<<"\n The amount has been creditted to your account\n";
cout<<"\n Have a good day\n";
count++;

}
void BANK :: displaySum(void) //displays amount
{
float balance = 0;
for (int i=0; i<count; i++)
balance = balance + amount[i];

cout<<"\n You have :" <<balance;
}


/*void BANK :: remove(void) //withdrawals
int a;
cout<<" \nEnter Account number";
cin>>accountno;
cout<<"\n You have :"<<balance;
cout<"\n Enter amount to withdrawn";
cin>>a;

for (int i=0; i<count; i++)
if (a<balance)
{
balance = balance-a
}
cout<<" Final balance: "<<balance*/

void BANK ::displayAll(void)
{
cout<<"\nName Balance\n";

for(int i=0; i<count; i++)
{
cout<<"\n" <<name[i];
cout<<"\n" <<balance[i];
cout<<"\n";
}
}

//================================================================

int main()
{
BANK client;
client.CNT();
int x;

do
{
cout<<"\n-----------------------------------------------------";
cout<<"\n You can do the following;"
<<" Enter appropriate number\n";
cout<<"\n1 : Add a client ";
cout<<"\n2 : Deposit amounts ";
cout<<"\n3 : Display depositted amount ";
cout<<"\n4 : Withdraw amounts ";
cout<<"\n5 : Display client balance ";
cout<<"\n6 : Quit";
cout<<"\n\n What would you like to do?";

cin>> x;

switch(x)
{
case 1 : client.getitem();break;
case 2 : client.deposit();break;
case 3 : client.displaySum();break;
case 4 : break;
case 5 : client.displayAll();break;
case 6 : cout<<" \nThis program was created by Taz,for more information visit www.taz.com\n";break;
default : cout<<" Please input a choice between 1 and 5\n";
}
} while (x!=6);

return 0;
}


eagerly awaiting a response,

NMTop40
November 15th, 2006, 06:53 AM
Let's first get rid of the bad habits.

1. <iostream> not <iostream.h>

(That's not a bad habit, it's just wrong).

2. Get rid of the char arrays and use std::string.

Then a design issue:

- Remove I/O from the underlying storage type.

Instead have a Bank type (not all in upper-case, regular style would not use an all upper-case class name. Different styles may or may not capitalise the first letter). Give Bank a public interface.

Have an I/O class or a set of I/O functions separately that can modify Bank or display its details using its interface.

3. Methods that do not modify an object should be declared as const. displayAll(), displaySum() - I doubt those modify the Bank object.

4. Don't use function names like CNT. Why not resetCount() ?

5. Do not separate members by commas.

6:

const m=50;

(i) should be typed if it has to be there
(ii) 'm' is meaningless
(iii) pollutes the global namespace. Either put it in the scope of the Bank class or put it in a namespace.
(iv) Why anyway?

Because given that a Bank has multiple accounts, you should have a separate class called BankAccount. Then Bank can have a vector of these, which can be of any length.

7: getitem: This doesn't get any item at all. You have a comment that it creates an account, so why not call it createAnAccount? name[count] is of type char. Are they going to input a char? What happens to name and Atype in account numbers 10-49? (Is account meant to be a transaction anyway?)