Design a class named BankAccount containing the following da
Design a class named BankAccount containing the following data field and methods.
 • One double data field named balance with default
 values 0.0 to denote the balance of the account.
 • A no-arg constructor that creates a default bank
 account.
 • A constructor that creates a bank account with the
 specified balance.
  throw an IllegalArgumentException when
 constructing an account with a negative balance
 • The accessor method for the data field.
 • A method named deposit(double amount) that deposits
 money into the bank account.
  throw an IllegalArgumentException when deposit a
 negative amount into an account
 • A method named withdraw(double amount) that withdraws
 the money from the bank account.
  throw an IllegalArgumentException when
 withdrawing a negative amount from an account
  define the exception class
 InsufficientFundsException
  throw an InsufficientFundsException
 when the balance is less than the
 withdrawal amount.
 • A method transfer(double amount, BankAccount other)
 that transfer money from the bank account to another
 account.
 • A method named toString() that returns a string
 description for the account.
 Design two classes CheckingAccount and SavingAccount which are derived from the
 class BankAccount.
 • CheckingAccount will charge transaction fees. Each month there are 5 free
 transactions. $3 will be deducted from the checking account for every extra
 transaction. (Hint: You need to add a method to deduct the fees from the
 checking account if there are more than 5 tractions in a month. You need to
 modify the deposit and withdrawal method as well, and you need to add an
 instance field to remember the number of transactions. You may need to define
 two constants, one for number of allowed free transactions and the other for fee
 per extra transaction.)
 • SavingAccount will earn income at fixed interest rate. (Hint: You need to add a
 method to add interest each month. Also you need to add an instance field for
 interest rate.)
Write your test program to create objects and test all the methods. Your test program
 should cause all three exceptions to occur and catches them all.
Solution
 //driver class
 public class Driver{
     public static void main(String []args){
      
          //declare req variables
         //create a savings acct object
          try
          {
              BankAccount accounts1=new BankAccount();
            
                System.out.print(\"\ ********Bank Account********\");
              accounts1=new BankAccount();
              //uncomment this step for throwing negatuve amount exception
              //accounts1.deposit(-1);
              accounts1.deposit(1234);
               System.out.println(accounts1);
                System.out.println(\"******************************\");
             
                 System.out.print(\"\ *********Savings Account*******\");
               //create a sacings account object
              SavingsAccount accounts2 =new SavingsAccount (60000,21);
               System.out.println(accounts2);
               accounts2.deposit(21000);
               //uncomment this step to throw insuffieict funds exception
              // accounts2.withdraw(1000000);
                System.out.println(\"Balance before interest cal:\" +accounts2.getBalance());
               accounts2.postInterest();
               System.out.println(\"Balance after interest cal:\" +accounts2.getBalance());
               System.out.println(\"*********************************\");
            
                System.out.print(\"\ *********Checking Account*******\");
              //create a checking account obejct
              CheckingAccount accounts3 =new CheckingAccount (60000,2);
               accounts3.deposit(1000);
               accounts3.deposit(1000);
               accounts3.withdraw(1000);
              //unccooment to get negaive amount withdrawn
              // accounts3.withdraw(-12);
              System.out.println(accounts3);
               System.out.println(\"Balance :\" +accounts3.getBalance());
                accounts3.withdraw(1000);
                 System.out.println(\"After crosing 5 transactions\");
                 System.out.println(\"Balance :\" +accounts3.getBalance());
              System.out.println(\"**********************************\");
          }
          catch(IllegalArgumentException e)
          {
              System.out.println(e);
          }
          catch(InsufficientFundsException e)
          {
              System.out.println(e);
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
      }
 }
class InsufficientFundsException extends Exception{
 InsufficientFundsException(String s){
 super(s);
 }
 }
 class BankAccount
 {
     double balance;
   
     public BankAccount()
     {
         balance=0;
     }
 
     public BankAccount(double balance)throws IllegalArgumentException
     {
         // set name and balance  
         // make sure balance is not negative
         // throw exception if balance is negative
         if(balance<0)
         throw new IllegalArgumentException (\"Balance cant\'e be negative\");
         else
         this.balance=balance;
     }
    // update balance by adding deposit amount
     // make sure deposit amount is not negative
     // throw exception if deposit is negative
     public void deposit(double amount) throws IllegalArgumentException
     {
         if(amount<0)
         throw new IllegalArgumentException (\"Negative amount can\'t be deposited\");
         else
         balance+=amount;
     }
 
     // update balance by subtracting withdrawal amount
     // throw exception if funds are not sufficient
     // make sure withdrawal amount is not negative
     // throw NegativeAmountException if amount is negative
     // throw InsufficientFundsException if balance < amount
     public void withdraw(double amount)   throws InsufficientFundsException, IllegalArgumentException
     {
         if(amount<0)
         throw new IllegalArgumentException (\"Negative amount can\'t be withdrawn\");
         else
         if(amount>balance)
         throw new InsufficientFundsException(\"No sufficient funds\");
         else
         balance-=amount;
     }
 
     // return current balance
      public double getBalance()
      {
          return balance;
      }
    
      //transfer
      public void transfer(double amount, BankAccount other) throws InsufficientFundsException, IllegalArgumentException
      {
          this.withdraw(amount);
          other.deposit(amount);
      }
 
      // print bank statement including customer name
      // and current account balance
      public String toString()
      {
          return \"\ Balance: \"+balance;
      }
 }
class CheckingAccount extends BankAccount
 {
     int noOfTransactions;
     final int FREETRANS=5;
     final int FEE=3;
   
    public CheckingAccount(double balance,int noOfTransactions) throws IllegalArgumentException
     {
         // set name and balance  
         // throw exception if balance is negative
         super(balance);
         this.noOfTransactions=noOfTransactions;
     }
 
     public CheckingAccount()
     {
         // set name and balance  
         // throw exception if balance is negative
         super(0);
         this.noOfTransactions=0;
     }
   
     public void deductFee()
     {
         if(noOfTransactions>FREETRANS)
         balance-=5;
     }
   
     // update balance by adding deposit amount
     // make sure deposit amount is not negative
     // throw exception if deposit is negative
     public void deposit(double amount) throws IllegalArgumentException
     {
         if(amount<0)
         throw new IllegalArgumentException (\"Negative amount can\'t be deposited\");
         else
         balance+=amount;
         noOfTransactions++;
         deductFee();
     }
 
     // update balance by subtracting withdrawal amount
     // throw exception if funds are not sufficient
     // make sure withdrawal amount is not negative
     // throw NegativeAmountException if amount is negative
     // throw InsufficientFundsException if balance < amount
     public void withdraw(double amount)   throws InsufficientFundsException, IllegalArgumentException
     {
         if(amount<0)
         throw new IllegalArgumentException (\"Negative amount can\'t be withdrawn\");
         else
         if(amount>balance)
         throw new InsufficientFundsException(\"No sufficient funds\");
         else
         balance-=amount;
         noOfTransactions++;
         deductFee();
     }
   
   
     // print bank statement including customer name
     // and current account balance (use printStatement from
     // the BankAccount superclass)
     // following this also print current interest rate
     public String toString()
     {
         return super.toString()+\"\ No Of Transactions: \"+noOfTransactions;
     }
 }
 class SavingsAccount extends BankAccount
 {
     double interestRate;
 
     public SavingsAccount(double balance,double interestRate) throws IllegalArgumentException
     {
         // set name and balance  
         // throw exception if balance is negative
         super(balance);
         this.interestRate=interestRate;
     }
   
      public SavingsAccount()
     {
         // set name and balance  
         // throw exception if balance is negative
         super(0);
         this.interestRate=0;
     }
 
     // post monthly interest by multiplying current balance
     // by current interest rate divided by 12 and then adding
     // result to balance by making deposit
     public void postInterest()
     {
         balance+=(getBalance()*interestRate)/12;
     }
 
     // print bank statement including customer name
     // and current account balance (use printStatement from
     // the BankAccount superclass)
     // following this also print current interest rate
     public String toString()
     {
         return super.toString()+\"\ Interest Rate: \"+interestRate;
     }
 }





