Written by Sai Sandeep Thota on July 16, 2013 in Java > Programming

Java Program to implement Banking using Array of Objects

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In our previous article we have discussed about Java Program to Solve simple Banking Problem, The disadvantage of the previous program is that we can do operations only on one account, but using Array of objects we can create multiple objects and perform operations on multiple accounts. Ofcourse we can also solve this without using Array of objects by using Array for each variable but it will be overhead.

So what are we doing here? We are just defining Array of Objects and then populating data into the object. We are using a switch case to perform operations like: Adding account details, Depositing Money, Withdrawing Money and Checking balance.

Download Source Code

Java Program to implement Banking using Array of Objects

The variable flag is used to raise a flag if account number is found. We reset the flag value to 1 every time do is executed.

import java.util.*;

class bankInternal {
int ac;
float bal, amt;
Scanner get = new Scanner(System.in);
void getdetails()
{
System.out.print(“Enter Account Number:”);
ac = get.nextInt();
System.out.println();
System.out.print(“Enter Initial Balance:”);
bal = get.nextFloat();
}
void deposit()
{
System.out.println();
System.out.print(“Enter Amount:”);
amt = get.nextInt();
bal = bal+amt;
System.out.println(“Success! Amount deposited. Balance Available:”+bal);
}

void withdraw()
{
System.out.println();
System.out.print(“Enter Amount:”);
amt = get.nextInt();
if(amt>bal)
{
System.out.println(“Insufficient Funds!!”);
}
else
{
bal = bal-amt;
System.out.println(“Amount Withdrawn! Available Balance:”+bal);
}
}

void balchk()
{
System.out.println(“Balance available in Account Number “+ac+” is: “+bal);
}
}

class bankArray {
public static void main(String args[])
{
boolean a=true;
int acno, flag;
Scanner in = new Scanner(System.in);
bankInternal obj[] = new bankInternal[100];
for(int i=0;i<3;i++) { obj[i] = new bankInternal(); } int ch; do { flag=1; //Reset flag value. System.out.println("1.Add Account"+"\n"+"2.Deposit"+"\n"+"3.Withdraw"+"\n"+"4.Check Balance"+"\n"+"5.Exit"); System.out.println(); System.out.print("Enter your Choice:"); ch = in.nextInt(); switch(ch) { case 1:for(int i=0;i<3;i++) { obj[i].getdetails(); } break; case 2: System.out.println("Enter Account Number:"); acno = in.nextInt(); for(int i=0;i<3;i++) { if(acno == obj[i].ac) { obj[i].deposit(); flag=0; } } if(flag==1) { System.out.println("Account number not found!!"+"\n"); } break; case 3: System.out.println("Enter Account Number:"); acno = in.nextInt(); for(int i=0;i<3;i++) { if(acno == obj[i].ac) { obj[i].withdraw(); flag=0; } } if(flag==1) { System.out.println("Account number not found!!"+"\n"); } break; case 4: System.out.println("Enter Account Number:"); acno = in.nextInt(); for(int i=0;i<3;i++) { if(acno == obj[i].ac) { obj[i].balchk(); flag=0; } } if(flag==1) { System.out.println("Account number not found!!"+"\n"); } break; case 5: System.exit(0); default: System.out.println("Enter a valid Option!"+"\n\n"); } }while(a); } } [/html] Output: Java Program to implement Banking using Array of Objects

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: