Write a small application in Java to develop a Banking Application in which the user deposits the amount Rs 1000.00 and then start withdrawing Rs 400.00, Rs 300.00 and it throws exception "Not Sufficient Fund" when the user withdraws Rs. 500 thereafter.

 

AIM: Write a small application in Java to develop a Banking Application in which the user deposits the amount Rs 1000.00 and then start withdrawing Rs 400.00, Rs 300.00 and it throws exception "Not Sufficient Fund" when the user withdraws Rs. 500 thereafter.

CODE:

import java.util.Scanner;

class InsufficientFundException extends Exception

{

            InsufficientFundException()

            {

                        super();

            }

            InsufficientFundException(String s)

            {

                        super(s);

            }

}

class NegativeAmountException extends Exception

{

            NegativeAmountException()

            {

                        super();

            }

            NegativeAmountException(String s)

            {

                        super(s);

            }

}

class fund

{

            public static float BAL = 0;

            public static void main(String args[])

            {

                        char con;

                        do{

                        System.out.print("\nEnter D for Deposite and W for Widrowal: ");

                        Scanner s = new Scanner(System.in);

                        char c = s.next().toUpperCase().charAt(0);

                        switch(c){

                                    case 'D':try{

                                                                        deposite();

                                                            }

                                                            catch(NegativeAmountException e){

                                                                        System.out.println(e);

                                                                        System.out.println("\n-----------------------------");

                                                                        e.printStackTrace();

                                                                        System.out.println("\n------------------------------");

                                                                        System.out.println(e.getMessage());

                                   

                                                            }

                                                            break;

                                    case 'W':try{

                                                                                    withdraw();

                                                                        }

                                                                        catch(InsufficientFundException e){

                                                                                    System.out.println(e);

                                                                                    System.out.println("\n-------------------");

                                                                                    e.printStackTrace();

                                                                                    System.out.println("\n---------------------");

                                                                                    System.out.println(e.getMessage());

                                                                        }

                                                                        break;

 

                                    default:System.out.println("\nInvalid choice");

 

                        }

                        System.out.print("\nDo you want to continue enter y or Y for yes: ");

                        con = s.next().toUpperCase().charAt(0);

                        }

                        while(con=='Y');

            }

            static void deposite() throws NegativeAmountException{

                        float dep;

                        System.out.print("\nEnter Amount for Deposite: ");

                        Scanner s = new Scanner(System.in);

                        dep = s.nextFloat();

                        if(dep<=0){

                                    throw new NegativeAmountException("\nAmount for Deposite can't be zero or negative");

                        }

                        else{

                                    BAL+=dep;

                                    System.out.println("\n"+dep+" Rs. Deposited");

                        }

                        System.out.println("\nTotal balance: "+BAL);

            }

 

            static void withdraw() throws InsufficientFundException{

                        float withd;

                        System.out.print("\nEnter Amount for withdraw: ");

                        Scanner s = new Scanner(System.in);

                        withd = s.nextFloat();

                        if(withd<=0){

                                    System.out.println("\nAmount for Withdrawal can't be zero or negative");

                        }

                        else{

                                    BAL-=withd;

                                    if(BAL<=0){

                                                throw new InsufficientFundException("Balance is negative if you withdraw "+withd+" Rs.");

                                    }

                                    else{

                                                System.out.println("\n"+withd+" Rs. withdowed");

                                    }

                                    System.out.println("\nTotal balance: "+BAL);

                        }

            }

}

OUTPUT:

 


Comments

Popular posts from this blog

Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.

Write a program in Java to demonstrate use of final class.

Write a java program to create 3 threads using Thread class. Three threads should calculate the sum of 1 to 5, 6 to 10 and 11to 15 respectively. After all thread finishes main thread should print the sum and average.