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 an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.