Completely Solved C, C++ Programs Assignment.




C++ program to create a banking system.

Filed Under:

Problem Statement : 
 Write a C++ program to create a banking system. 

Logical Approach :
 The above program is done using an Abstract Data Type (ADT) called class. A class is a mechanism to create objects. In this program a class named Bank_account is created.

It contains the following data members : 
  •  Account number. 
  •  Pin number. 
  •  Customer name. 
  •  Account type. 
  •  Balance.
It contains the following member functions : 
  •  get_data() - It is used to get all the data required as input. 
  •  print_data() - It is used to print all the information. 
  •  deposit_balance() - It is used to deposit balance in an account. 
  •  withdrawn_balance() - It is used to withdraw balance from an account. 
  •  update_pin_no() - It is used to update a customer's pin number. 
  •  check_acc() - It is used to check whether an account number exists or not. 
  •  check_pin() - It is used to check whether a pin number exists or not. 
  •  getName()-It is used to return a customer name. 
  •  get_acctype()-It is used to return an account type. 
  •  check_duplicate()-It is used to check uniqueness of an account number. 
  •  del_data()-It is used to remove all the account information of an account to be closed. 
 First main menu will be appeared on the screen from which user can choose following operations to perform:
1.ADMINISTRATOR(create account(s)).
2.CUSTOMER(View/Modify existing account(s)).
3.EXIT.

  User has to create at least one account by selecting ADMINISTRATOR menu at first. If user tries to enter in CUSTOMER menu without creating any accounts an error message will be shown informing there is no account present in the bank. In ADMINISTRATOR menu user can create any number of accounts. While creating accounts user has to enter all information (account no., pin no., account type, account balance) manually for each customer. After that if user selects CUSTOMER menu it prompts user to enter any valid account number and check whether the account number exists or not. If it exists then it prints “Hello , welcome to your account”. After that it prompts user to enter correct pin number of the corresponding account and checks whether it is proper or not. If it matches then a menu driven screen is displayed for that customer otherwise operation is aborted for the present customer. 

1. Account details. 
2. Depositing balance. 
3. Withdraw balance. 
4. Update pin no. 
5. Exit operation for this customer. 

 User can check for different valid account numbers and continue as long he or she wants. If user chooses to leave CUSTOMER menu then again main menu will appear. Now user can again create accounts by selecting ADMINISTRATOR menu, can select CUSTOMER menu again or he or she can exit from the program.


Algorithm : 
Creating an ADT with name Bank_account 

Step 1: Creating an ADT Bank_Account


Step 2: Declaring private data members 
                      int acc_no;//Declaring an integer 
                     char customer_name[100];//Declaring a character array of size 100 
                     char acc_type[100]; //Declaring a character array of size 100 
                     int pin_no;//Declaring an integer
                     double balance;//Declaring a double


Step 3: Declaring public member functions 
                                get_data(int); 
                               del_data();
                               print_data(); 
                               deposit_balance(); 
                               withdrawn_balance(); 
                               update_pin_no(); 
                               check_acc(int); 
                               check_pin(); 
                               getName(); 
                               get_acctype();
                               check_duplicate();

This function is used to input all the information 

get_data() 

                    Read acc_no; 
 Read customer_name; 
 Read acc_type; 
 Read pin_no; 
 Read balance; 
}
This function is used to print all the information of a customer 

print_data() 

 Print acc_no;
 Print acc_type; 
 Print balance;
 }
This function is used to deposit balance in an account 

deposit_balance(double blnc) 

 balance+=blnc; 
 }
This function is used to withdraw balance from an account 

withdrawn_balance() 

 balance-=blnc;
 }
This function is used to update a customer's pin number 

update_pin_no() 

 Read old pin_no.; 
If(old pin_no. is valid) 
 Read new pin no.; 
 pin_no=New pin_no.; 
}
 This function checks the uniqueness of an account number
check_duplicate() 
{
 if(prev[] array contains a same account no.)
 return True;
 return False; 
}
This function is used to check whether an account number exists or not 

check_acc()
 {
 if(acc_no exists) 
 return True;
 else 
 return False; 
}
 This function is used to check whether a pin number exists or not 
check_pin() 

 if(pin_no matches) 
 return True; 
 else
 return False; 

 This function returns type of an account(Current/Savings) 
get_acctype() 
{
 return acc_type;
 }
 This function returns name of a customer 
getName() 

 return customer_name;
 } 
 This function removes all information of an account which is going to be closed 
del_data()
 { 
 acc_no=0; 
 pin_no=0; 
 balance=0.0; 
}



Main procedure: 
main()
{ Create an array of instances of ADT Bank_account with size 99999;
while(1)
{
//Main menu
Print “BANK---------------------
1.ADMINISTRATOR(create account(s)).
2.CUSTOMER(View/Modify existing account(s)).
3.EXIT.”;
Read choice from user;
switch(choice)
{
case 1:
cust[num1].get_data();
num1=num1+1;
cr=cr+1;
break;
case 2:
if(cr<=0)
{
Print”Operation failed!!There is no account
present in the bank.”;
break;
}
while(1)
{
do
{
Read account no. acc;
for(i=0 to num1)
{
if(flag=cust[i].check_acc(acc))
break;
}
if(i==num1)
Print"Account no. does not exist!!";
}while(flag==false);
Print"Hello
"cust[i].getName()",welcome to your account...";
Read pin;
if((flag=cust[i].check_pin(pin))==false)
{
Print”Invalid pin no.
entered!!Operation aborted for this customer!!";
op='N';
break;
}
while(1)
{
op='Y';
del=0;
//Menu for operations to be performed on an account
Print"Customer menu---------------------"
1.Account Details.
2.Depositing Balance.
3.Withdraw Balance.
4.Update Pin no.
5.Exit operation for this customer.
Read choice ch from user;
switch(ch)
{
case 1:
cust[i].print_data();
break;
case 2:
Read amount to deposit;
cust[i].deposit_balance(amount);
break;
case 3:
Read amount to withdraw;
if(cust[i].get_acctype()="Savings")
ac_flag=1;
else
ac_flag=0;
del=cust[i].withdrawn_balance(amount,ac_flag);
if(del==1)
{
Print"Are you sure to close account?(Y/N):";
Read choice in c;
if(c!='Y'&&c!='y')
break;
cust[i].del_data();
cust[i].get_data(acc);
cr=cr-1;
Print"Account Closed";
}
break;
case 4:
cust[i].update_pin_no();
break;
case 5:
op='N';
break;
}
if(del==1)
break;
if(op=='N')
break;
}
if(del==1)
break;
op='N';
Print"Do you want to continue operation for another customer(Y/N):";
Read choice in op;
if(op!='Y'&&op!='y')
break;
}
break;
case 3:
Print”Thank You,visit again..";
return;
}
}
Deallocate memory space;
}


Program listing :
//C++ program to create a banking system
#include<iostream>
#include<cstring>//Used for string library functions
using namespace std;
class Bank_account//Creating a class Bank_account
{
private:
//Data members
int acc_no;
char customer_name[100];
char acc_type[100];
int pin_no;
double balance;
public:
//Member functions
void get_data(int);
void del_data();
void print_data();
void deposit_balance(double);
int withdrawn_balance(double,int);
void update_pin_no();
bool check_acc(int);
bool check_pin(int);
char* getName();
char* get_acctype();
int check_duplicate(int*,int,int);
};
//This function calculates the number of digits of an account number int chk_accdigit(int acc)
{
int c=1;
if(acc<1000000||acc>9999999)
c=0;
return c;
}
//This function calculates the number of digits of a pin number
int chk_pindigit(int pin)
{
int c=1;
if(pin<1000||pin>9999)
c=0;
return c;
}
//This function returns type of an account(Current/Savings)
char* Bank_account::get_acctype()
{
return acc_type;
}
//This function checks uniqueness of an account number
int Bank_account::check_duplicate(int prev[],int j,int acc_no)
{
for(int i=0;i<j;i++)
{
if(prev[i]==acc_no)
return 1;
}
return 0;
}
//This function is used to input all the information from user
void Bank_account::get_data(int f)
{
static int count=0;
static int prev[99999];
if(f!=0)
{
for(int i=0;i<count;i++)
{
if(prev[i]==f)
prev[i]=0;
}
return;
}
while(1)
{
cout<<"\nEnter account no.(7 digit)-";
cin>>acc_no;
if(chk_accdigit(acc_no)!=1)
{
cout<<"Invalid account no. entered!!(check no. of digits)\n";
continue;
}
prev[count]=acc_no;
if(check_duplicate(prev,count,acc_no)!=0)
{
cout<<"\nThis account no. already exists!!Please enter a different account no.\n";
continue;
}
break;
}
count++;
cout<<"\nEnter customer name:";
cin.ignore();
cin.getline(customer_name,100);
while(1)
{
cout<<"\nEnter account type(Current/Savings):";
cin.getline(acc_type,100);
if(strcmp(acc_type,"Current")==0||
strcmp(acc_type,"Savings")==0)
break;
cout<<"Invalid account type entered!!";
}
while(1)
{
cout<<"\nEnter pin no.(4 digit)-";
cin>>pin_no;
if((chk_pindigit(pin_no))==1)
break;
cout<<"Invalid pin no. entered!!(check no. of digits)\n";
}
if(strcmp(acc_type,"Savings")==0)
{
while(1)
{
cout<<"\nEnter account balance(Rs.):";
cin>>balance;
if(balance>=100)
break;
cout<<"Invalid amount given!!Minimum Rs. 100 must be deposited initially... \n";
}
}
if(strcmp(acc_type,"Current")==0)
{
while(1)
{
cout<<"\nEnter account balance(Rs.):";
cin>>balance;
if(balance>=1000)
break;
cout<<"Invalid amount given!!Minimum Rs. 1000 must be deposited initially... \n";
}
}
cout<<"\nCongratulations!"<<customer_name<<",Your account is successfully created.\n";
}
//This function removes all information of an account which is going to be closed void Bank_account::del_data()
{
acc_no=0;
pin_no=0;
balance=0.0;
}
//This function is used to deposit a valid amount of balance in an account void Bank_account::deposit_balance(double blnc)
{
cout<<"Please wait...\n\n";
if(blnc<=0)
{
cout<<"\nTransaction unsucessful!!Please enter a valid amount.\n";
return;
}
balance+=blnc;
cout<<"Transaction completed succcessfully.\n";
}
//This function is used to withdraw a valid amount of balance from an account int Bank_account::withdrawn_balance(double blnc,int f)
{
char ch;
cout<<"Please wait...\n\n";
if(blnc<=0)
{
cout<<"\nTransaction unsucessful!!Please enter a valid amount.\n";
return 0;
}
if(blnc>balance)
{
cout<<"\nTransaction unsucessful!!Your current balance is:"<<balance<<"\n"<<"you are unable to request for:"<<blnc<<"\n\n";
return 0;
}
if(f==1)
{
if((double)(balance-blnc)<100)
{
cout<<"\nTransaction unsucessful!!Minimum Rs.100 must be stored to maintain a savings account."<<"\n\n";
cout<<"Do you want to withdraw all money and close your account?(Y/N):";
cin>>ch;
if(ch=='Y'||ch=='y')
return 1;
return 0;
}
}
else if(f==0)
{
if((double)(balance-blnc)<1000)
{
cout<<"\nTransaction unsucessful!!Minimum Rs.1000 must be stored to maintain a current account."<<"\n\n"; cout<<"Do you want to withdraw all money and close your account?(Y/N):";
cin>>ch;
if(ch=='Y'||ch=='y')
return 1;
return 0;
}
}
balance-=blnc;
cout<<"Transaction completed succcessfully.\n";
}
//This function is used to check whether an account number exists or not bool Bank_account::check_acc(int acc)
{
if(acc_no==acc)
return true;
else
return false;
}
//This function is used to check whether a pin number exists or not
bool Bank_account::check_pin(int pin)
{
if(pin_no==pin)
return true;
else
return false;
}
//This function is used to update a customer's pin number
void Bank_account::update_pin_no()
{
int pin1,pin2;
while(1)
{
cout<<"Enter old pin no.";
cin>>pin1;
if(pin1==pin_no)
break;
cout<<"Invalid pin no. entered!!\n";
return;
}
while(1)
{
cout<<"\nEnter new pin no.(4 digit)-";
cin>>pin2;
if((chk_pindigit(pin2))==1)
break;
cout<<"Pin no. can't be changed,invalid no. of digits entered!!\n";
}
pin_no=pin2;
cout<<"Pin no. successfully updated.\n";
}
//This function is used to print all the information of a customer void Bank_account::print_data()
{
cout<<"\n\nAccount no.-:"<<acc_no;
cout<<"\n\nAccount type:"<<acc_type;
cout<<"\n\nAccount
balance:Rs."<<balance<<"\n";
}
//This function returns name of a customer char* Bank_account::getName()
{
return customer_name;
}
int main()
{
int num1=0,i,acc,pin,ch,ac_flag,ch1,ch2,del,cr=0,admin=109789;
bool flag;
double amount;
char c,op;
Bank_account *cust=new Bank_account[99999];
cout<<"\n\n";
while(1)
{
//Main menu
cout<<"\nWELCOME TO BANK\n---------------------\n""\n1.ADMINISTRATOR(create account(s)).\n2.CUSTOMER(View/Modify existing account(s)).\n3.EXIT.\n\nEnter your choice:";
cin>>ch1;
switch(ch1)
{
case 1:
cout<<"Enter administrator
password:";
cin>>admin;
if(admin!=109789)
{
cout<<"\nInvalid
Password!!!";
break;
}
cust[num1].get_data(0);
num1++;
cr++;
break;
case 2:
if(cr<=0)
{
cout<<"Operation failed!!There is no account present in the bank.\n";
break;
}
while(1)
{
do
{
cout<<"\nEnter account no.:";
cin>>acc;
for(i=0;i<num1;i++)
{
if(flag=cust[i].check_acc(acc))
break;
}
if(i==num1)
cout<<"\nAccount no. does not exist!!\n";
}while(flag==false);
cout<<"\nHello "<<cust[i].getName()<<",welcome to your account...\n";
cout<<"\nEnter Pin number(Be careful):";
cin>>pin;
if((flag=cust[i].check_pin(pin))==false)
{
cout<<"Invalid pin no. entered!!\nOperation aborted for this customer!!\n";
op='N';
break;
}
while(1)
{
op='Y';
del=0;
//Menu for operations to be performed on an account cout<<"\nCustomer menu\n---------------------\n""\n1.Account Details.\n2.Depositing Balance.\n3.Withdraw Balance.\n4.Update Pin no.\n5.Exit operation for this customer.\n\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cust[i].print_data(); break;
case 2:
cout<<"Enter amount to deposit:";
cin>>amount;
cust[i].deposit_balance(amount);
break;
case 3:
cout<<"Enter amount to withdraw; cin>>amount;
if(strcmp((cust[i].get_acctype()),"Savings"))
ac_flag=1;
else
ac_flag=0;
del=cust[i].withdrawn_balance(amount,ac_flag);
if(del==1)
{
cout<<"\nAre you sure?
(Y/N):";
cin>>c;
if(c!='Y'&&c!='y')
break;
cust[i].del_data();
cust[i].get_data(acc);
cr--;
cout<<"\nAccount Closed.\n";
}
break;
case 4:
cust[i].update_pin_no();
break;
case 5:
op='N';
break;
default:
cout<<"Invalid choice!!!!\n";
}
if(del==1)
break;
if(op=='N')
break;
}
if(del==1)
break;
op='N';
cout<<"\nDo you want to continue operation for another customer(Y/N):";
cin>>op;
if(op!='Y'&&op!='y')
break;
}
break;
case 3:
cout<<"\nThank You,visit again..\n"; return 0;
default:
cout<<"Invalid choice!!!!\n";
}
}
delete[]cust;//Deallocate memory space
}



Output: 

WELCOME TO BANK 
 --------------------- 

 1.ADMINISTRATOR(create account(s)). 
 2.CUSTOMER(View/Modify existing account(s)). 
 3.EXIT. 

 Enter your choice:2 

 Operation failed!!There is no account present in the bank. 

 WELCOME TO BANK 
 --------------------- 

 1.ADMINISTRATOR(create account(s)). 
 2.CUSTOMER(View/Modify existing account(s)). 
 3.EXIT. 

 Enter your choice:1 

 Enter administrator password:109789 
 Enter account no.(7 digit)-7000001 
 Enter customer name:Sanket Basu 
 Enter account type(Current/Savings):Current 
 Enter pin no.(4 digit)-1234 
 Enter account balance(Rs.):12000 

 Congratulations!Sanket Basu,Your account is successfully created. 

 WELCOME TO BANK 
 --------------------- 

 1.ADMINISTRATOR(create account(s)). 
 2.CUSTOMER(View/Modify existing account(s)). 
 3.EXIT. 

Enter your choice:1 
Enter administrator password:109789 
Enter account no.(7 digit)-7000001 

This account no. already exists!!Please enter a different account no. 
Enter account no.(7 digit)-7000002 
 Enter customer name:Ankit Basu 
 Enter account type(Current/Savings):Savings 
 Enter pin no.(4 digit)-2345 
Enter account balance(Rs.):2345

 Congratulations!Ankit Basu,Your account is successfully created.

WELCOME TO BANK
 ---------------------
 1.ADMINISTRATOR(create account(s)).
 2.CUSTOMER(View/Modify existing account(s)).
 3.EXIT.

 Enter your choice:1
Enter administrator password:109789
Enter account no.(7 digit)-7000003
Enter customer name:Anjan Halder
Enter account type(Current/Savings):Current
Enter pin no.(4 digit)-4567
Enter account balance(Rs.):890000

 Congratulations!Anjan Halder,Your account is successfully created.

 WELCOME TO BANK
 ---------------------
 1.ADMINISTRATOR(create account(s)).
 2.CUSTOMER(View/Modify existing account(s)).
 3.EXIT.

 Enter your choice:2
Enter account no.:7000002
 Hello Ankit Basu,welcome to your account...

 Enter Pin number(Be careful):2345

Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
5.Exit operation for this customer.

 Enter your choice:1
 Account no.-:7000002
 Account type:Savings
 Account balance:Rs.2345

 Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
 5.Exit operation for this customer.

 Enter your choice:2
 Enter amount to deposit:1200
 Please wait... Transaction completed succcessfully.

 Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
 5.Exit operation for this customer.

 Enter your choice:3
 Enter amount to withdraw:5000

 Please wait... Transaction unsucessful!!Your current balance is:3545 you are unable to request for:5000 

Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
 5.Exit operation for this customer.
 Enter your choice:3
 Enter amount to withdraw:3500
 Please wait... Transaction unsucessful!!Minimum Rs.100 must be stored to maintain a savings account.

 Do you want to withdraw all money and close your account?(Y/N):Y
 Are you sure?(Y/N):Y

 Account Closed,

WELCOME TO BANK
 ---------------------
 1.ADMINISTRATOR(create account(s)).
 2.CUSTOMER(View/Modify existing account(s)).
 3.EXIT.

  Enter your choice:2
 Enter account no.:70000001
 Account no. does not exist!!

 Enter account no.:7000001
 Hello Sanket Basu,welcome to your account...
 Enter Pin number(Be careful):1234

Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
 5.Exit operation for this customer.

 Enter your choice:1

 Account no.-:7000001
 Account type:Current
 Account balance:Rs.12000

Customer menu
 ---------------------
 1.Account Details.
 2.Depositing Balance.
 3.Withdraw Balance.
 4.Update Pin no.
 5.Exit operation for this customer.

 Enter your choice:3
 Enter amount to withdraw:12000

 Please wait... Transaction unsucessful!!Minimum Rs.1000 must be stored to maintain a current account.
 Do you want to withdraw all money and close your account?(Y/N):Y
 Are you sure?(Y/N):N

WELCOME TO BANK
 --------------------- 

 1.ADMINISTRATOR(create account(s)). 
 2.CUSTOMER(View/Modify existing account(s)). 
 3.EXIT. 

 Enter your choice:3 

 Thank You,visit again.. 

 Discussions : 

  • Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. 
  • All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream. 
  •  cin.ignore(); is used to clear the buffer for standard input. 
  •  We have used ‘new’ for dynamically creating array of instances. A very very large number(99999) of instances are created .This is the maximum no. of accounts can exist at one time in the banking system(tense to infinity).The syntax is:*variableName= new[SIZE] 
  •  We should deallocate the memory space(by using delete) which was allocated previously for creating array of instances(After it is no longer required). The syntax is: delete[]variableName 
  •  There are a few error checks are included in the program which are not there in the algorithm. The following error checks are done: 

 1. At first account(s) should be created by choosing ADMINISTRATOR menu. If user tries to enter in CUSTOMER menu without creating any account then an error message will be displayed. 
 2. User must know administrator password to enter in ADMINISTRATOR menu,which is defined in the program. 
 3. If number of customers is less than or equal to 0, then an error message is displayed. 
 4. The account number should be of seven digits otherwise an error message is displayed. 
 5. The pin number should be of four digits otherwise an error message is displayed. 
 6. Uniqueness of an account number is also checked. 
 7. Account type should be entered as 'Current' or 'Savings'(case sensitive) otherwise an error message is displayed.
 8.Depositing balance and withdrawing balance amount should not be negative. 
 9.There must be Rs. 100 in a savings account and Rs. 1000 in a current account to maintain the accounts. Otherwise customer can choose to withdraw all money and close the account.



Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments