Wednesday, 13 June 2012

Simple Bank Account - Java


Account.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bank;

/**
 *
 * @author ravikiran
 */
public class Account {
    private long AccNumber,balance;
    private String AccHolder;

    public Account(long num,long bal,String name){
        AccNumber=num;
        balance=bal;
        AccHolder=name;
    }
    public void withdraw(long amount)throws InsufficientFundsException
    {
     
             if(amount>balance)
                 throw new InsufficientFundsException(amount);
             else
             {
                 balance=balance-amount;
            System.out.print("The current balance is"+balance);

            }
    }
      public void deposit(long amount)throws InvalidTransactionException
              {

             if(amount<=0)
                 throw new InvalidTransactionException(amount);
             else
             {
                 balance=balance+amount;
            System.out.print("The current balance is"+balance);

            }

    }
}
 
InsufficientFundsException.java



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bank;

import java.util.Scanner;

/**
 *
 * @author ravikiran
 */
class InsufficientFundsException extends Exception {
    long amt;
    public InsufficientFundsException(long amount)
    {
     
       
        amt=amount;
    }
   public  String tostring(){
        return"InsufficientFundsException";
    }


}


InvalidTransactionException.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bank;

/**
 *
 * @author ravikiran
 */
class InvalidTransactionException extends Exception {
    long amt;
    public  InvalidTransactionException(long amount)
    {


        amt=amount;
    }
   public  String tostring(){
        return" Invalid Transaction Exception";
    }


}


Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bank;

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 *
 * @author ravikiran
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("select your operation");
        System.out.println("1.withdraw");
        System.out.println("2.Deposit");

        Account a=new Account(215,20000,"ravi");
        Scanner input=new Scanner(System.in);
        int b=input.nextInt();

        switch(b)
        {

        case 1:
        try{

            a.withdraw(20000);
        }
        catch(InsufficientFundsException e){
            JOptionPane.showMessageDialog(null,"Dear Customer You Have insufficient amount in your Account.","Error",JOptionPane.ERROR_MESSAGE);
            
        }
        break;
        case 2:try {

            a.deposit(10);
        }
        catch(InvalidTransactionException e){
            JOptionPane.showMessageDialog(null,"Dear Customer You Entered Invalid Amount","Error",JOptionPane.ERROR_MESSAGE);
            
        }
        break;
        default: System.out.print("Invalid operation");
        }
    }
}




No comments:

Post a Comment