Repeat the problem of Exercise P17.5

Repeat the problem of Exercise P17.4, but refine the bound of the type parameter to extend the generic Comparable type.

Complete the following file:

PairUtil.java

Use the following files:

BankAccount.java

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

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

   /**
      Constructs a bank account with a given balance.
      @param initialBalance the initial balance
   */
   public BankAccount(double initialBalance)
   {   
      balance = initialBalance;
   }

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

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

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

   public int compareTo(BankAccount other)
   {
      double d = balance - other.balance;
      if (d < 0) return -1;
      if (d > 0) return 1;
      return 0;
   }
}

MinMaxTester.java

public class MinMaxTester
{
   public static void main(String[] args)
   {
      SavingsAccount[] accounts = 
      {
         new SavingsAccount(10),
         new SavingsAccount(10),
         new SavingsAccount(5),
      };

      accounts[0].deposit(1000);
      accounts[1].deposit(10000);
      accounts[2].deposit(10000);

      for (SavingsAccount a : accounts) a.addInterest();

      Pair<SavingsAccount, SavingsAccount> mm 
         = PairUtil.minmax(accounts);
      System.out.println(mm.getFirst().getBalance());
      System.out.println("Expected: 1100");
      System.out.println(mm.getSecond().getBalance());
      System.out.println("Expected: 11000");
   }
}

Pair.java

public class Pair<T, S>
{
   private T first;
   private S second;

   public Pair(T firstElement, S secondElement)
   {
      first = firstElement;
      second = secondElement;
   }

   public T getFirst()
   {
      return first;
   }
   
   public S getSecond()
   {
      return second;
   }
}

SavingsAccount.java

/**
   An account that earns interest at a fixed rate.
*/
public class SavingsAccount extends BankAccount
{  
   private double interestRate;

   /**
      Constructs a bank account with a given interest rate.
      @param rate the interest rate
   */
   public SavingsAccount(double rate) 
   {  
      interestRate = rate;
   }

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