package cse1030;
/**
* A class that represents a savings account that pays
* interest on the account balance. Withdrawals from the
* account are penalized by a withdrawal fee that is
* debited from the balance.
*
* @author CSE1030
*
*/
public class SavingsAccount extends AbstractBankAccount {
private double interestRate;
private long withdrawalFee;
/**
* Create a savings account given the amounts of the starting balance,
* the daily interest rate, and withdrawal fee.
*
* @param balance the starting balance
* @param interestRate the daily interest rate
* @param withdrawalFee the fee associated with each withdrawal
*/
public SavingsAccount(long balance, double interestRate, long withdrawalFee) {
super(balance);
if (interestRate < 0) {
throw new IllegalArgumentException("interest rate: " + interestRate
+ " is negative");
}
if (withdrawalFee < 0) {
throw new IllegalArgumentException("withdrawal fee: " + withdrawalFee
+ " is negative");
}
this.interestRate = interestRate;
this.withdrawalFee = withdrawalFee;
}
/**
* Get the interest rate
*
* @return the interest rate
*/
public final double getInterestRate() {
return this.interestRate;
}
/**
* Set the interest rate.
*
* @param interestRate the new interest rate
* @throws IllegalArgumentException if interestRate < 0
*/
public final void setInterestRate(double interestRate) {
if (interestRate < 0) {
throw new IllegalArgumentException("interest rate: " + interestRate
+ " is negative");
}
this.interestRate = interestRate;
}
/**
* Get the withdrawal fee.
*
* @return the withdrawal fee
*/
public final long getWithdrawalFee() {
return this.withdrawalFee;
}
/**
* Set the withdrawal fee.
*
* @param withdrawalFee the new withdrawal fee
* @throws IllegalArgumentException if withdrawalFee < 0
*/
public final void setWithdrawalFee(long withdrawalFee) {
if (withdrawalFee < 0) {
throw new IllegalArgumentException("withdrawal fee: " + withdrawalFee
+ " is negative");
}
this.withdrawalFee = withdrawalFee;
}
/**
* Withdraw an amount of currency from the account. The amount
* is subtracted from the balance and the withdrawal fee is
* subtracted from the balance.
*
* @see cse1030.AbstractBankAccount#withdraw(long)
*
* @param amount the amount to withdraw from the account
*/
@Override
public void withdraw(long amount) {
super.withdraw(amount);
this.balance -= this.withdrawalFee;
}
/**
* Update the account balance to add interest earned on the
* account balance. Assume that this is called once each day,
* and that the interest earned is calculated based on the
* current balance.
*
*
* The interest earned is calculated using floating point * arithmetic and then rounded to the nearest integer before * being added to the balance. * * @see cse1030.AbstractBankAccount#update() * */ @Override public void update() { double interest = (1 + this.interestRate) * this.balance; this.balance += Math.round(interest); } }