From fe2266656fbd2cff0272a15408864d730c163c7b Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Fri, 20 Mar 2026 14:48:22 +0100 Subject: [PATCH] oppgave: savingsaccount --- .../java/oving7/savingsaccount/Account.java | 9 ++++ src/main/java/oving7/savingsaccount/BSU.java | 41 +++++++++++++++++++ .../oving7/savingsaccount/ForeldreSpar.java | 31 ++++++++++++++ .../oving7/savingsaccount/SavingsAccount.java | 35 ++++++++++++++++ ...SUTest.java.unimplemented => BSUTest.java} | 0 ...va.unimplemented => ForeldreSparTest.java} | 0 ....unimplemented => SavingsAccountTest.java} | 0 7 files changed, 116 insertions(+) create mode 100644 src/main/java/oving7/savingsaccount/Account.java create mode 100644 src/main/java/oving7/savingsaccount/BSU.java create mode 100644 src/main/java/oving7/savingsaccount/ForeldreSpar.java create mode 100644 src/main/java/oving7/savingsaccount/SavingsAccount.java rename src/test/java/oving7/savingsaccount/{BSUTest.java.unimplemented => BSUTest.java} (100%) rename src/test/java/oving7/savingsaccount/{ForeldreSparTest.java.unimplemented => ForeldreSparTest.java} (100%) rename src/test/java/oving7/savingsaccount/{SavingsAccountTest.java.unimplemented => SavingsAccountTest.java} (100%) diff --git a/src/main/java/oving7/savingsaccount/Account.java b/src/main/java/oving7/savingsaccount/Account.java new file mode 100644 index 0000000..bc89a0a --- /dev/null +++ b/src/main/java/oving7/savingsaccount/Account.java @@ -0,0 +1,9 @@ +package oving7.savingsaccount; + +public interface Account { + void deposit(double amount); + + void withdraw(double amount); + + double getBalance(); +} diff --git a/src/main/java/oving7/savingsaccount/BSU.java b/src/main/java/oving7/savingsaccount/BSU.java new file mode 100644 index 0000000..4c31195 --- /dev/null +++ b/src/main/java/oving7/savingsaccount/BSU.java @@ -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; + } +} diff --git a/src/main/java/oving7/savingsaccount/ForeldreSpar.java b/src/main/java/oving7/savingsaccount/ForeldreSpar.java new file mode 100644 index 0000000..0dc8218 --- /dev/null +++ b/src/main/java/oving7/savingsaccount/ForeldreSpar.java @@ -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; + } +} diff --git a/src/main/java/oving7/savingsaccount/SavingsAccount.java b/src/main/java/oving7/savingsaccount/SavingsAccount.java new file mode 100644 index 0000000..7e0a51a --- /dev/null +++ b/src/main/java/oving7/savingsaccount/SavingsAccount.java @@ -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; + } +} diff --git a/src/test/java/oving7/savingsaccount/BSUTest.java.unimplemented b/src/test/java/oving7/savingsaccount/BSUTest.java similarity index 100% rename from src/test/java/oving7/savingsaccount/BSUTest.java.unimplemented rename to src/test/java/oving7/savingsaccount/BSUTest.java diff --git a/src/test/java/oving7/savingsaccount/ForeldreSparTest.java.unimplemented b/src/test/java/oving7/savingsaccount/ForeldreSparTest.java similarity index 100% rename from src/test/java/oving7/savingsaccount/ForeldreSparTest.java.unimplemented rename to src/test/java/oving7/savingsaccount/ForeldreSparTest.java diff --git a/src/test/java/oving7/savingsaccount/SavingsAccountTest.java.unimplemented b/src/test/java/oving7/savingsaccount/SavingsAccountTest.java similarity index 100% rename from src/test/java/oving7/savingsaccount/SavingsAccountTest.java.unimplemented rename to src/test/java/oving7/savingsaccount/SavingsAccountTest.java