C++ Program For Simple Bank Account

Problem:

Write a C++ program to implement account class with private data members as follows:
  • account number - automatically generated six digit number, fist two digits are used for the bank code and remaining four digits are for account number
  • account type - It can be one of the following type {Saving ,Current, Fixed, Recurring}
  • amout - long integer type variable for the balance amount
  • Owner Name - to store the name of owner
Also define member functions for accepting and displaying the information. Write a main program to accept information from the user and open desired number of accounts.

Algorithm or Solution:

  1. Start
  2. Declare data members as private
  3. Declare variable acc_no for account number as int. Declare another variable tmp with data type as static int
  4. Initialize this static data member outside the class with value 820000.
  5. Declare acctype for account type as object of class string (you can also define it as pointer to character).
  6. Declare amt for balance amount as long int
  7. Define a public member function for accepting a account information.
  8. Define another public (you can also declare this function as private) member function check_type() for checking account type. If the type is correct t the assign otherwise continue to accept account type until it is valid.
  9. Post increment tmp while assigning to id, id=tmp++;
  10. Before assigning account type to acctype check validity with check_type() function.
  11. Define a public member function to display the account information.
  12. Stop

C Program:

// Aim: Write a class to implement account

#include <iostream>
using namespace std;

class account
{
 int acc_no;
 char o_name[20];
 long int amt;
 string acc_type;
 string acctype[8]={"saving","current","fixed","recurring","Saving","Current","Fixed","Recurring"};
 
 public:
 static int temp;

 int check_type()
 {
  int i;
  for(i=0;i<10;i++)
  {
   if(acc_type==acctype[i])
    return 0;
  }
  return 1;
 }
 
 void setdata()
 {
  acc_no=temp++;
  cout<<endl;
  cout<< " Enter owner name, account type, initial amt:- "; 
  cin>>o_name>>acc_type>>amt;

  while(check_type())
  {
   cout<<" Not valid account type. Enter again:- "; 
   cin>>acc_type;
  } 
  }

 void getdata()
 {
 cout<<" Account no \t Owner Name \t Account type \t Balance";
 cout<<endl<<acc_no<<"\t \t "<<o_name<<"\t \t "<<acc_type<<"\t \t "<<amt<<endl;
 }

};

int account::temp=820000;

int main()
{
 account a[5];

 for(int i=0;i<2;i++)
  a[i].setdata();
 
 for(int i=0;i<2;i++)
  a[i].getdata();

 return 0;
}


/* Output of above code:-

 Enter owner name, account type, initial amt:- Shubham Saving 1000

 Enter owner name, account type, initial amt:- Neha none 1000
 Not valid account type. Enter again:- Fixed

 Account no   Owner Name   Account type   Balance
 820000          Shubham         Saving    1000
 Account no   Owner Name   Account type   Balance
 820001    Neha    Fixed          1000

*/