Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design the classes: BankAccount CheckingAccount SavingAccount TestDrive with com

ID: 675416 • Letter: D

Question

Design the classes: BankAccount CheckingAccount SavingAccount TestDrive with comments please.
In Java language 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 Vdefine 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 ChackingAccount and SavingAccount which are derived from the class BankAccount CheckingAccount will charge transaction fees. Each month there are 5 free transactions. S3 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.) .

Explanation / Answer

import java.util.Scanner;


public class BankAccountTest {

   public static void main(String []args){
      
       Scanner scan = new Scanner(System.in);
       SavingAccount sa = new SavingAccount(100);
       CheckingAccount ca = new CheckingAccount(100);
       SavingAccount sa2 = new SavingAccount(10);
      
       System.out.println("Saving Account #1 : "+sa.getBalance());
       System.out.println("Checking Account : "+ca.getBalance());
      
       //deposit
       sa.deposit(120);
       ca.deposit(120);
      
      
       System.out.println("After depositing $120 in each account Saving Account #1 : "+sa.getBalance());
       System.out.println("Checking Account : "+ca.getBalance());
      
       try {
           for(int i = 0 ; i < 7; i++) {
               sa.withdraw(10);
               ca.withdraw(10);
           }
          
           System.out.println("After witdrawing $70 in 7 transactions Saving Account #1 : "+sa.getBalance());
           System.out.println("Checking Account : "+ca.getBalance());
           System.out.println("Saving Account #2 : "+sa.getBalance());
          
           ca.transfer(50, sa2);
           sa.transfer(40, sa2);
  
           System.out.println("Saving Account #1 : "+sa.getBalance());
           System.out.println("Checking Account : "+ca.getBalance());
           System.out.println("Saving Account #2 : "+sa.getBalance());      
          
          
           } catch (IllegalArgumentException | InsufficientFundsException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
          
  
}

class BankAccount {
  
   private double balance;
  
   public BankAccount() {
       this.balance = 0;
   }

   public BankAccount(double balance) {
       if(balance < 0)
           throw new IllegalArgumentException("Balance should not be negative");
       this.balance = balance;
   }
  
   public void deposit(double amount) {
       if(amount < 0)
           throw new IllegalArgumentException("Amount should not be negative");
       this.balance += amount;
   }
  
   public void withdraw(double amount) throws InsufficientFundsException,IllegalArgumentException {
       if(amount < 0)
           throw new IllegalArgumentException("Amount should not be negative");
       else if(balance - amount < 0) {
           throw new InsufficientFundsException("Insufficient funds");
       }
          
       this.balance -= amount;
   }
  
   public void transfer(double amount, BankAccount other) throws IllegalArgumentException, InsufficientFundsException {
      
       this.withdraw(amount);
       other.deposit(amount);
   }
  
   public double getBalance() {
       return balance;
   }

   public void setBalance(double balance) {
       this.balance = balance;
   }
  
  
}

class SavingAccount extends BankAccount {
  
   public SavingAccount() {
       super();
   }
  
   public SavingAccount(double balance) {
       super(balance);
   }
  
}

class CheckingAccount extends BankAccount {
  
   static int MAX_FREE_TRANSACTIONS = 5;
   static int SERVICE_CHARGE = 3;
  
   private int noOfTransactions;
  
   public CheckingAccount() {
       super();
       noOfTransactions = 0;
      
   }
  
   public CheckingAccount(double balance) {
      
       super(balance);
       noOfTransactions = 0;
   }
  
   public void deposit(double amount) {
       super.deposit(amount);
       deductServiceCharge();
       noOfTransactions++;
      
   }
  
   public void withdraw(double amount) throws InsufficientFundsException,IllegalArgumentException {
       super.withdraw(amount);
       deductServiceCharge();
       noOfTransactions++;
  
   }
  
   public void transfer(double amount, BankAccount other) throws IllegalArgumentException, InsufficientFundsException {
      
       super.transfer(amount, other);
       deductServiceCharge();

       noOfTransactions++;
           }
  
   private void deductServiceCharge() {
      
       if(noOfTransactions > 5)
           this.setBalance(this.getBalance() - SERVICE_CHARGE);
   }

}



class InsufficientFundsException extends Exception {
  
   public InsufficientFundsException() {
       super();
   }
  
   public InsufficientFundsException(String error) {
       super(error);
   }
}

-----------------output--------------------------

Saving Account #1 : 100.0
Checking Account : 100.0
After depositing $120 in each account
Saving Account #1 : 220.0
Checking Account : 220.0
After witdrawing $70 in 7 transactions
Saving Account #1 : 150.0
Checking Account : 144.0
Saving Account #2 : 150.0
Saving Account #1 : 110.0
Checking Account : 88.0
Saving Account #2 : 110.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote