50 lines
1.4 KiB
Java
50 lines
1.4 KiB
Java
package oving7.abstractaccount;
|
|
|
|
/**
|
|
* A {@code SavingsAccount} can only have a positive balance. In addition, the
|
|
* account has
|
|
* withdrawal restrictions. A {@code SavingsAccount} has {@code x} number of
|
|
* {@code withdrawals}. If
|
|
* you want to withdraw money after all withdrawals have been used up, the
|
|
* balance should be charged
|
|
* a {@code fee}. If the balance is too low to cover the fee, an
|
|
* {@code IllegalArgumentException}
|
|
* should be thrown.
|
|
*
|
|
* @see AbstractAccount
|
|
*/
|
|
public class SavingsAccount extends AbstractAccount {
|
|
|
|
private int withdrawals;
|
|
private double fee;
|
|
|
|
/**
|
|
* Initializes a new {@code SavingsAccount} with the specified number of
|
|
* withdrawals and fee.
|
|
*
|
|
* @param withdrawals the number of withdrawals
|
|
* @param fee the fee
|
|
* @throws IllegalArgumentException if the number of withdrawals or the fee is
|
|
* negative
|
|
*/
|
|
public SavingsAccount(int withdrawals, double fee) {
|
|
if (withdrawals < 0 || fee < 0.0) {
|
|
throw new IllegalArgumentException("cannot have less than zero withdrawals or fee of less than zero");
|
|
}
|
|
this.withdrawals = withdrawals;
|
|
this.fee = fee;
|
|
}
|
|
|
|
protected void internalWithdraw(double amount) {
|
|
withdrawals -= 1;
|
|
double s = amount;
|
|
if (withdrawals < 0) {
|
|
s += fee;
|
|
}
|
|
if (balance < s) {
|
|
throw new IllegalArgumentException("balance not great enough to cover fees");
|
|
}
|
|
balance -= s;
|
|
}
|
|
}
|