oppgave: savingsaccount

This commit is contained in:
2026-03-20 14:48:22 +01:00
parent c79555e133
commit fe2266656f
7 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package oving7.savingsaccount;
public interface Account {
void deposit(double amount);
void withdraw(double amount);
double getBalance();
}

View File

@@ -0,0 +1,41 @@
package oving7.savingsaccount;
public class BSU extends SavingsAccount {
private double depositedThisYear = 0.0;
private double depositMax;
BSU(double interestRate, double depositMax) {
super(interestRate);
this.depositMax = depositMax;
}
public double getTaxDeduction() {
return depositedThisYear * 0.2;
}
@Override
public void deposit(double amount) {
if ((depositedThisYear + amount) > depositMax) {
throw new IllegalStateException(
"cannot deposit more than the maximum deposit, which has a value of " + depositMax);
}
super.deposit(amount);
depositedThisYear += amount;
}
@Override
public void withdraw(double amount) {
if (amount > depositedThisYear) {
throw new IllegalStateException(
"cannot withdraw more than has been deposited this year, which has a value of "
+ depositedThisYear);
}
super.withdraw(amount);
}
@Override
public void endYearUpdate() {
super.endYearUpdate();
depositedThisYear = 0.0;
}
}

View File

@@ -0,0 +1,31 @@
package oving7.savingsaccount;
public class ForeldreSpar extends SavingsAccount {
private int withdrawalsMax;
private int remainingWithdrawals;
ForeldreSpar(double interestRate, int withdrawalsMax) {
super(interestRate);
this.withdrawalsMax = withdrawalsMax;
remainingWithdrawals = withdrawalsMax;
}
int getRemainingWithdrawals() {
return remainingWithdrawals;
}
@Override
public void withdraw(double amount) {
if (remainingWithdrawals <= 0) {
throw new IllegalStateException("Cannot withdraw more than " + withdrawalsMax + " times a year");
}
super.withdraw(amount);
remainingWithdrawals -= 1;
}
@Override
public void endYearUpdate() {
super.endYearUpdate();
remainingWithdrawals = withdrawalsMax;
}
}

View File

@@ -0,0 +1,35 @@
package oving7.savingsaccount;
public class SavingsAccount implements Account {
private double balance = 0.0;
private double interestRate;
SavingsAccount(double interestRate) {
this.interestRate = interestRate;
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("deposit must be greater than zero");
}
balance += amount;
}
public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("withdraw must be greater than zero");
}
if (amount > balance) {
throw new IllegalStateException("cannot withdraw more than current balance");
}
balance -= amount;
}
public double getBalance() {
return balance;
}
public void endYearUpdate() {
balance += balance * interestRate;
}
}