The following BankAccount class is similar to that of the ex

The following BankAccount class is similar to that of the example in Worked Example 8.1. It should be illegal to construct bank accounts with a negative balance or interest rate.

Modify the constructor in this class, so that an IllegalArgumentException is thrown in these cases.

Complete the following code:

Solution

package fuelgauge;

/**
* A bank account has a balance that can be changed by deposits and withdrawals.
*/
public class BankAccount {
   private double balance;
   private double interestRate;

   /**
   * Constructs a bank account with a zero balance and no interest.
   */
   public BankAccount() {
       balance = 0;
       interestRate = 0;
   }

   /**
   * Constructs a bank account with a given interest rate.
   *
   * @balance initialBalance the initial balance
   * @param rate
   * the interest rate
   */
   public BankAccount(double initialBalance, double rate) {
       if (initialBalance >= 0 && rate >= 0) {
           this.balance = initialBalance;
           interestRate = rate;
       } else
           throw new IllegalArgumentException(
                   \"Cannot create account with a negative balance/interesrate\");

   }

   /**
   * Deposits money into the bank account.
   *
   * @param amount
   * the amount to deposit
   */
   public void deposit(double amount) {
       balance = balance + amount;
   }

   /**
   * Withdraws money from the bank account.
   *
   * @param amount
   * the amount to withdraw
   */
   public void withdraw(double amount) {
       balance = balance - amount;
   }

   /**
   * Gets the current balance of the bank account.
   *
   * @return the current balance
   */
   public double getBalance() {
       return balance;
   }

   /**
   * Adds the earned interest to the account balance.
   */
   public void addInterest() {
       double interest = getBalance() * interestRate / 100;
       deposit(interest);
   }

   // This method checks your work.

   public static String check(double initialBalance, double rate) {
       try {
           BankAccount account = new BankAccount(initialBalance, rate);
           return \"constructed\";
       } catch (IllegalArgumentException ex) {
           return \"illegal argument\";
       } catch (Exception ex) {
           return \"another error\";
       }
   }

   public static void main(String[] args) {

       BankAccount account1 = new BankAccount();
       // exception raised due to negative balance
       BankAccount account2 = new BankAccount(-2, 0.3);

   }
}
OUTPUT:
Exception in thread \"main\" java.lang.IllegalArgumentException: Cannot create account with a negative balance/interesrate
   at fuelgauge.BankAccount.<init>(BankAccount.java:30)
   at fuelgauge.BankAccount.main(BankAccount.java:87)

The following BankAccount class is similar to that of the example in Worked Example 8.1. It should be illegal to construct bank accounts with a negative balance
The following BankAccount class is similar to that of the example in Worked Example 8.1. It should be illegal to construct bank accounts with a negative balance

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site