/** * CheckingAccount: a version of a bank account assesses a service * charge when needed. */ public class CheckingAccount extends BankAccount { // instance variables private double lowBalance; // low balance since account created or // last service charge was deducted /** * Construct a new CheckingAccount with the given name, account number, * and initial balance */ public CheckingAccount(String name, int number, double initialBalance){ super(name, number, initialBalance); this.lowBalance = this.getBalance(); } /** * Withdraw money from this account (may leave account balance * negative if insufficient funds). pre: amount >= 0 * @param amount dollars to be withdrawn from the account */ public void withdraw(double amount) { super.withdraw(amount); if (this.getBalance() < this.lowBalance) { this.lowBalance = this.getBalance(); } } /** * Deduct a service charge if the account balance has gotten too low * since the last time a service charge was deducted (or the account * created, whichever comes first). * * @param minBalance minimum account balance before service * charge applies * @param serviceCharge amount to deduct if charge applies */ public void deductServiceCharge(double minBalance, double serviceCharge){ if (this.lowBalance < minBalance) { this.withdraw(serviceCharge); } this.lowBalance = this.getBalance(); } /** Return a string representation of this CheckingAccount */ public String toString() { return "CheckingAccount[" + this.getName() + ", " + this.getNumber() + ", " + this.getBalance() + "]"; } }