Bank Management Using Exception Handling and Interface


The program for bank management using exception Handling and Interface using Java is executed using netbeans

PROGRAM:

/*the program is compiled and executed using Netbeans
author : PcTipsandTweaks
*/

//Interface
packagebank.pkginterface;

interface Account 
 { 
voidCreateAcc(); 
void Deposit(); 
void Withdraw(); 
 } 

//Package to do Exception handling and  BankMangement
packagebank.pkginterface;

importjava.util.Scanner; 
importjava.util.Random; 
importjava.lang.Exception;          //importing for Exception Handling

classMyException extends Exception                     //User Defined Exception Handling
 { 
MyException(String message) 
      { 
super(message); 
      } 
 } 

classBankUtilities                             //Class BankUtilities for Initialization and Declaration
 { 
intacctype,year=1,ssn=0,accnum; 
floatintr; 
      String name=""; 
      String loc=""; 
      Scanner in = new Scanner(System.in); 
      Random rnd = new Random(); 
double temp=0.0,bal=0.0; 

 } 

class Bank extends BankUtilities implements Account      //Class Bank Inheriting from BankUtilities
 { 

voidgetInfo()                     //Getting User Details When Creating New Account
       { 
try
           {                     
System.out.print("Enter Name: "); 
name=in.nextLine();  
System.out.print("Enter Account Number: "); 
ssn=in.nextInt();                 
in.nextLine(); 
System.out.print("Enter Location: "); 
loc=in.nextLine();          

System.out.println("Mention account type:\n1.Savings (7% intr )\t2.Current (5% intr) "); 
acctype=in.nextInt(); 
switch(acctype) 
                     { 
case 1: 
System.out.println("Enter the initial amount of deposit:"); 
temp=in.nextFloat(); 
if(temp<0) 
                               { 
System.out.println("Invalid Amount\nTry again:\n"); 
System.out.println("Enter the initial amount of deposit:"); 
temp=in.nextFloat(); 
                               } 
Deposit(temp);                                 //Passing value through methods
System.out.println("Enter no of years:"); 
year=in.nextInt(); 
if(year<=0) 
                               { 
System.out.println("Invalid year\n Try again."); 
System.out.println("Enter noof years:"); 
year=in.nextInt(); 
                               } 

break; 

case 2: 
System.out.println("Enter the initial amount of deposit:"); 
temp=in.nextFloat(); 
if(temp<0) 
                               { 
System.out.println("Invalid Amount\nTry again:\n"); 
System.out.println("Enter the initial amount of deposit:"); 
temp=in.nextFloat(); 
                               } 
Deposit(temp); 
break; 

default: System.out.println("Invalid Option"); 
                     } //Switch     

           } 
catch(Exception e)          `               //Throw Exception if anything Wrong From User Input
           { 
System.out.println("Inbuilt Exception --> "+e); 
System.exit(0); 
           } 

      }//getInfo

public void CreateAcc()                                 //Create a new account
      { 
try
           { 
getInfo();                                            //CallingetInfo() to get user details
System.out.println("\nAccount Successfully Created!"); 
accnum=rnd.nextInt(1000)+1; 
System.out.println("Hello "+name+" your account no is " +accnum+".\n"); 
        } 
catch(Exception e)                          //To throw Exceptionif Error happens
          { 
System.out.println("Fatal Error"); 
          } 
      } 

void Deposit(double temp) // Initial Deposit 
      { 
try
           { 
if(temp>500) 
                { 
bal+=temp; 
System.out.println("SUCCESSFULLY DEPOSITED: "+temp); 
                } 
else
                { 
throw new MyException("Minimum Deposit Violation");                              //Throw Exception if value is //less than 500
                } 
           } 
catch(MyException e)                                                                                    //Catching the Exception
           { 
System.out.println(e.getMessage()); 
System.out.println("TRANSACTION FAILURE"); 
System.exit(0);                                                                                                 //If Error Exit the Program
           }


      } 


public void Deposit() // regular deposit 
      {
System.out.println("Enter the amount to be deposited :"); 
temp=in.nextFloat(); 
try
           { 
if(temp>0) 
                { 
bal+=temp; 
System.out.println("SUCCESSFULLY DEPOSITED: "+temp); 
                } 
else
                { 
throw new MyException("INVALID AMOUNT EXCEPTION");     //Throws Exception If bal<0
                } 
           } 
catch(MyException e) 
           { 
System.out.println(e.getMessage()); 
System.out.println("TRANSACTION FAILURE"); 
           } 

      } 

public void Withdraw()// regular deposit 
      { 

System.out.println("Enter the amount to be withdrawn :"); 
temp=in.nextFloat(); 
if(temp<=0) 
           { 
System.out.println("Invalid Amount Error."); 
System.exit(0); 
           } 

try
           { 

if(temp<bal) 
                { 
bal-=temp; 
System.out.println("SUCCESSFULLY WIITHDRAWN: "+temp); 
                } 
else
                { 
throw new MyException("INSUFFIECEINT FUND EXCEPTION");                  //Throw Exception if  //balance   is less than money //to withdraw
                } 
           } 
catch(MyException e)                                                                    //Catching the Exception
           { 
System.out.println(e.getMessage()); 
System.out.println("TRANSACTION FAILURE"); 
           } 

      } 
void Interest() 
      { 
try
           { 
if("".equals(name)&&(ssn==0)) 
                { 
throw new MyException("ACCONT NOT FOUND EXCEPTION");                                  //If Account not found          //Throw   Error
                } 
else
                { 
if(acctype==1) 
                     { 
bal+=year*bal*0.07;                                                                                       //Calculating Interest
System.out.println("7% interest added"); 
                     } 
else if(acctype==2) 
                     { 
bal+=year*bal*0.03; 
System.out.println("3% interest added");  
                     } 
                } 
           } 
catch(MyException e) 
           { 
System.out.println(e.getMessage()); 
System.out.println("TRANSACTION FAILURE"); 
           } 
      } 

voidCheckBal() 
      { 
System.out.println("Balance:"+bal); 
      } 

 } 


classBankDemoExc                                          //Main Class BankDemoExc
 { 
public static void main(String arg[]) throws MyException
      { 
           Scanner in = new Scanner(System.in); 
           Bank b = new Bank();                          //Creating Object for Bank()
System.out.println("\n\n\t----Bank Management----"); 
try
           { 
while(true) 
                { 
System.out.println("1.Create Account\t2.Check Balance \n3.Deposit\t\t4.Withdraw\n5.Interest\t\t6.Exit."); 
intch = in.nextInt(); 
switch(ch) 
                     { 
case 1: 
b.CreateAcc(); 
break; 

case 2: 
b.CheckBal(); 
break; 

case 3: 
b.Deposit(); 
break; 

case 4: 
b.Withdraw(); 
break; 

case 5: 
b.Interest(); 
break; 

case 6: 
System.exit(0); 
break; 

default: System.out.println("Invalid Option"); 
                     } 
                } 
           } 
catch(Exception e) 
           { 
System.out.println("SELF THROWN EXCEPTION IS--->"+e); 
           } 
      } 

 } 



OUTPUT:




http://pctipsandtweaks.blogspot.com/
Exception Handling and Interface Output 




Previous
Next Post »
Thanks for your comment