oppgave: abstractaccount

This commit is contained in:
2026-03-23 17:54:35 +01:00
parent fe2266656f
commit fd661adfc5
7 changed files with 55 additions and 13 deletions

View File

@@ -15,7 +15,8 @@ package oving7.abstractaccount;
*/
public class SavingsAccount extends AbstractAccount {
// TODO: Add fields here
private int withdrawals;
private double fee;
/**
* Initializes a new {@code SavingsAccount} with the specified number of
@@ -27,8 +28,22 @@ public class SavingsAccount extends AbstractAccount {
* negative
*/
public SavingsAccount(int withdrawals, double fee) {
// TODO: Implement this constructor
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;
}
// TODO: Override abstract method here
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;
}
}