//Illustrate Inheritance strategies // --Additional fields (instance variables) // --Overriding a method to add to its functionality // **Overriding a method to alter functionality (hiding) is not shown // --Adding additional methods for increased functionality // //An instance of the SuperSavings class is a Bank Account that // pays interest at a negotiated interest higher rate. A minimum // initial deposit is required, and the number of withdrawals per // billing cycle is limited. Excess withdrawals are charged. // import java.text.*; public class SuperSavingsAccount extends BankAccount { //Class Constants public static final double MINIMUM_DEPOSIT = 10000.00; public static final int WITHDRAWAL_LIMIT = 2; public static final double WITHDRAWAL_FEE = 5.00; //Instance Variables private double interestRate; //Variable Interest-Rate private int withdrawalCount; //# Withdrawals in current billing cycle //Constructor //Initial balance and an interest-rate must be provided public SuperSavingsAccount(double deposit, double interestRate) { super(deposit); if (deposit < MINIMUM_DEPOSIT) throw new RuntimeException("Deposit does not qualify for Super-Savings"); this.interestRate = interestRate; this.withdrawalCount = 0; } //Accessor: returns the interest-rate of this SuperSavings account public double getInterestRate() { return this.interestRate; } //Accessor: returns the #-withdrawals for this SuperSavings account // in the current billing cycle public int getWithdrawalCount() { return this.withdrawalCount; } //Override //Inherited toString() is EXTENDED to describe the ADDITIONAL state // of this SuperSavings account public String toString() { return super.toString() + " Interest Rate: " + this.interestRate + "%" + " # Period Withdraws: " + this.withdrawalCount; } //Override public boolean equals(Object other) { if (!super.equals(other)) return false; SuperSavingsAccount that = (SuperSavingsAccount)other; return this.interestRate == that.interestRate && this.withdrawalCount == that.withdrawalCount; } //Override //In addition to the functionality of the inherited withdraw() // a SuperSavings withdraw() must count each call public void withdraw(double amount) { super.withdraw(amount); this.withdrawalCount++; } //Propietary Method //Performs end-of-billing-cycle processing // 1) add interest 2) debit withdrawalfees public void billingCycle() { this.deposit( this.getBalance() * this.interestRate / 100.0); if ( this.withdrawalCount > WITHDRAWAL_LIMIT) { double penalty = (this.withdrawalCount - WITHDRAWAL_LIMIT) * WITHDRAWAL_FEE; this.withdraw(penalty); } this.withdrawalCount = 0; } }