C++ Program For Simple Bank Account

Simple Banking System C++ Program

Write a C++ program to implement bank 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>
#include <cstdio>
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;
		
		// function to check if account type is valid
		int check_type()
		{
			int i;
			for(i=0;i<10;i++)
			{
				if(acc_type==acctype[i])
					return 0;
			}
            
			return 1;
		}
		
		// function to check if initial balance is entered valid or not (negative or positive)
		int check_initial_balance()
		{
			if(amt < 0)
				return 1;
				
			return 0;
		}
 
		void setdata()
		{
			acc_no=temp++;
			cout<<endl;
            cout<< "Enter owner name: ";
            cin>> o_name;
            cout<< "Enter account type: ";
            cin>> acc_type;
            cout<< "Enter account opening amout (initial): "; 
            cin>> amt;

			while(check_type())
			{
            	cout<<" Not valid account type. Enter again:- "; 
				cin>>acc_type;
            }
            
            while(check_initial_balance())
			{
            	cout<<" Account opening amount cannot be negative. Enter again:- "; 
				cin>>amt;
            }
		}
		void display_account_details()
		{
			cout<<"\n\nAccount 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;
		}
		
		void deposit(float amount)
		{
		    amt += amount; 
		    cout<< amount << " successfully deposited to your account\n";
		    cout<<"Previous Balance: " << amt - amount << endl;
		    cout<<"Current Balance: " << amt << endl;
		}
		

		void withdraw(float amount)
		{
		    amt -= amount; 
		    cout<< amount << " has been successfully withdrawn from you bank account\n";
		    cout<<"Previous Balance: " << amt + amount << endl;
		    cout<<"Current Balance: " << amt << endl;
		}    		
};
int account::temp=820000;
int main()
{
	account acc;
	int choice;
	float amount;
	
	acc.setdata();

	
	do
    {
        cout<<"\n\n************ Banking Operations ************";
        cout<<"\n1) Deposit Amount\n";
        cout<<"2) Withdraw Money\n";
        cout<<"3) Display All Account Details\n";
        cout<<"4) Exit\n";
        cout<<"What do you want to do? : ";
        cin>>choice;
        
        switch(choice)
        {
            case 1: 
                cout<<"Enter amount to deposit: ";
                cin>>amount;
                acc.deposit(amount);
                break;
            case 2: 
                cout<<"Enter amount to withdraw: ";
                cin>>amount;
                acc.withdraw(amount);
                break;
            case 3: acc.display_account_details();
                break;
        }
    }while(choice!=4);
	            
	return 0;
}

Output

Enter owner name: John
Enter account type: saving
Enter account opening amout (initial): 10000


************ Banking Operations ************
1) Deposit Amount
2) Withdraw Money
3) Display All Account Details
4) Exit
What do you want to do? : 1
Enter amount to deposit: 100
100 successfully deposited to your account
Previous Balance: 10000
Current Balance: 10100


************ Banking Operations ************
1) Deposit Amount
2) Withdraw Money
3) Display All Account Details
4) Exit
What do you want to do? : 2
Enter amount to withdraw: 99
99 has been successfully withdrawn from you bank account
Previous Balance: 10100
Current Balance: 10001


************ Banking Operations ************
1) Deposit Amount
2) Withdraw Money
3) Display All Account Details
4) Exit
What do you want to do? : 3


Account no 	 Owner Name 	 Account type 	 Balance
820000	 	 John	 	 saving	 	 10001


************ Banking Operations ************
1) Deposit Amount
2) Withdraw Money
3) Display All Account Details
4) Exit
What do you want to do? : 4