In: Computer Science
A banking system should contain a few essential features which are a must in any banking system
1. Account adding capacity and also able to search the account already present
2. able to deposit and withdraw money
3. able to check the balance in the account and enable the system to maintain a minimum balance.
an example of the bank system is given below
import java.util.Scanner;
class Bank
{
private String accno;
private String user_name;
private long balance;
Scanner K =new Scanner(System.in);
//method to open an account
void openAccount()
{
System.out.print("Enter Account No:
");
accno=K.next();
System.out.print("Enter Name: ");
user_name=K.next();
System.out.print("Enter Balance:
");
balance=K.nextLong();
}
//method to display account details
void showAccount()
{
System.out.println(accno+","+name+","+balance);
}
//method to deposit money
void deposit()
{
long amt;
System.out.println("Enter the
Amount you Want to Deposit : ");
amt=K.nextLong();
balance=balance+amt;
}
//method to withdraw money
void withdrawal()
{
long amt;
System.out.println("Enter the
Amount you Want to withdraw : ");
amt=K.nextLong();
if(balance>=amt)
{
balance=balance-amt;
}
else
{
System.out.println("Less Balance..Transaction Failed..");
}
}
//method to search an account number
boolean search(String acn)
{
if(accno.equals(acn))
{
showAccount();
return(true);
}
return(false);
}
}
class ExBank
{
public static void main(String arg[])
{
Scanner KB=new
Scanner(System.in);
//create initial accounts
System.out.print("How Many Customer
you Want to Input : ");
int n=K.nextInt();
Bank C[]=new Bank[n];
for(int
i=0;i<C.length;i++)
{
C[i]=new
Bank();
C[i].openAccount();
}
//run loop until menu 5 is not
pressed
int ch;
do
{
System.out.println("Main Menu\n
1.Display
All\n
2.Search By
Account\n
3.Deposit\n
4.Withdrawal\n
5.Exit");
System.out.println("Ur Choice :");
ch=K.nextInt();
switch(ch)
{
case 1:
for(int
i=0;i<C.length;i++)
{
C[i].showAccount();
}
break;
case 2:
System.out.print("Enter
Account to be Search ");
String acn=K.next();
boolean found=false;
for(int
i=0;i<C.length;i++)
{
found=C[i].search(acn);
if(found)
{
break;
}
}
if(!found)
{
System.out.println("Search Failed..Account Not Exist..");
}
break;
case 3:
System.out.print("Enter
Account No : ");
acn=K.next();
found=false;
for(int
i=0;i<C.length;i++)
{
found=C[i].search(acn);
if(found)
{
C[i].deposit();
break;
}
}
if(!found)
{
System.out.println("Search Failed..Account Not Exist..");
}
break;
case 4:
System.out.print("Enter
Account No : ");
acn=K.next();
found=false;
for(int
i=0;i<C.length;i++)
{
found=C[i].search(acn);
if(found)
{
C[i].withdrawal();
break;
}
}
if(!found)
{
System.out.println("Search Failed..Account Not Exist..");
}
break;
case 5:
System.out.println("transaction over ");
break;
}
}
while(ch!=5);
}
}