From fd661adfc5f3eb9aaec48ec29356917954399b00 Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Mon, 23 Mar 2026 17:54:35 +0100 Subject: [PATCH] oppgave: abstractaccount --- .../abstractaccount/AbstractAccount.java | 16 ++++++++++--- .../oving7/abstractaccount/CreditAccount.java | 23 ++++++++++++++----- .../oving7/abstractaccount/DebitAccount.java | 8 ++++++- .../abstractaccount/SavingsAccount.java | 21 ++++++++++++++--- ...a.unimplemented => CreditAccountTest.java} | 0 ...va.unimplemented => DebitAccountTest.java} | 0 ....unimplemented => SavingsAccountTest.java} | 0 7 files changed, 55 insertions(+), 13 deletions(-) rename src/test/java/oving7/abstractaccount/{CreditAccountTest.java.unimplemented => CreditAccountTest.java} (100%) rename src/test/java/oving7/abstractaccount/{DebitAccountTest.java.unimplemented => DebitAccountTest.java} (100%) rename src/test/java/oving7/abstractaccount/{SavingsAccountTest.java.unimplemented => SavingsAccountTest.java} (100%) diff --git a/src/main/java/oving7/abstractaccount/AbstractAccount.java b/src/main/java/oving7/abstractaccount/AbstractAccount.java index b6138c9..02d4198 100644 --- a/src/main/java/oving7/abstractaccount/AbstractAccount.java +++ b/src/main/java/oving7/abstractaccount/AbstractAccount.java @@ -23,6 +23,8 @@ public abstract class AbstractAccount { // TODO: Add fields and potentially a constructor here + protected double balance = 0.0; + /** * Decreases the account balance by the specified amount. Note that the rules * for withdrawals @@ -35,6 +37,8 @@ public abstract class AbstractAccount { */ // TODO: Define abstract method {@code void internalWithdraw} here + protected abstract void internalWithdraw(double amount); + /** * Increases the account balance by the specified amount. * @@ -42,7 +46,10 @@ public abstract class AbstractAccount { * @throws IllegalArgumentException if the amount is not positive */ public void deposit(double amount) { - // TODO: Implement this method + if (amount < 0.0) { + throw new IllegalArgumentException("cannot deposit amount less than zero"); + } + balance += amount; } /** @@ -55,13 +62,16 @@ public abstract class AbstractAccount { */ public void withdraw(double amount) { // TODO: Implement this method + if (amount < 0.0) { + throw new IllegalArgumentException("withdrawal amount cannot be less than zero"); + } + internalWithdraw(amount); } /** * @return the current balance of the account */ public double getBalance() { - // TODO: Implement this method - return 0.0; + return balance; } } diff --git a/src/main/java/oving7/abstractaccount/CreditAccount.java b/src/main/java/oving7/abstractaccount/CreditAccount.java index 9f59073..2310406 100644 --- a/src/main/java/oving7/abstractaccount/CreditAccount.java +++ b/src/main/java/oving7/abstractaccount/CreditAccount.java @@ -14,7 +14,7 @@ package oving7.abstractaccount; */ public class CreditAccount extends AbstractAccount { - // TODO: Add fields here + private double creditLine; /** * Initializes a new {@code CreditAccount} with the specified credit line. @@ -23,10 +23,15 @@ public class CreditAccount extends AbstractAccount { * @throws IllegalArgumentException if the credit line is negative */ public CreditAccount(double creditLine) { - // TODO: Implement this constructor + this.creditLine = creditLine; } - // TODO: Override abstract method here + protected void internalWithdraw(double amount) { + if (amount > (balance + creditLine)) { + throw new IllegalArgumentException("cannot withdraw more than balance plus creditLine"); + } + balance -= amount; + } /** * @return the credit line @@ -34,8 +39,7 @@ public class CreditAccount extends AbstractAccount { * @see CreditAccountTest#testCreditLine() */ public double getCreditLine() { - // TODO: Implement this method - return 0.0; + return creditLine; } /** @@ -49,6 +53,13 @@ public class CreditAccount extends AbstractAccount { * @see CreditAccountTest#testCreditLine() */ public void setCreditLine(double creditLine) { - // TODO: Implement this method + if (creditLine < 0.0) { + throw new IllegalArgumentException("credit line cannot be less than zero"); + } + if (creditLine + balance < 0.0) { + throw new IllegalStateException("credit line plus balance must be greater than or equal to zero"); + } + + this.creditLine = creditLine; } } diff --git a/src/main/java/oving7/abstractaccount/DebitAccount.java b/src/main/java/oving7/abstractaccount/DebitAccount.java index 2c8e074..d3b324a 100644 --- a/src/main/java/oving7/abstractaccount/DebitAccount.java +++ b/src/main/java/oving7/abstractaccount/DebitAccount.java @@ -15,5 +15,11 @@ package oving7.abstractaccount; */ public class DebitAccount extends AbstractAccount { - // TODO: Override abstract method here + @Override + protected void internalWithdraw(double amount) { + if (balance < amount) { + throw new IllegalArgumentException("cannot withdraw amount greater than balance for debit account"); + } + balance -= amount; + } } diff --git a/src/main/java/oving7/abstractaccount/SavingsAccount.java b/src/main/java/oving7/abstractaccount/SavingsAccount.java index b17e9f5..d824fd7 100644 --- a/src/main/java/oving7/abstractaccount/SavingsAccount.java +++ b/src/main/java/oving7/abstractaccount/SavingsAccount.java @@ -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; + } } diff --git a/src/test/java/oving7/abstractaccount/CreditAccountTest.java.unimplemented b/src/test/java/oving7/abstractaccount/CreditAccountTest.java similarity index 100% rename from src/test/java/oving7/abstractaccount/CreditAccountTest.java.unimplemented rename to src/test/java/oving7/abstractaccount/CreditAccountTest.java diff --git a/src/test/java/oving7/abstractaccount/DebitAccountTest.java.unimplemented b/src/test/java/oving7/abstractaccount/DebitAccountTest.java similarity index 100% rename from src/test/java/oving7/abstractaccount/DebitAccountTest.java.unimplemented rename to src/test/java/oving7/abstractaccount/DebitAccountTest.java diff --git a/src/test/java/oving7/abstractaccount/SavingsAccountTest.java.unimplemented b/src/test/java/oving7/abstractaccount/SavingsAccountTest.java similarity index 100% rename from src/test/java/oving7/abstractaccount/SavingsAccountTest.java.unimplemented rename to src/test/java/oving7/abstractaccount/SavingsAccountTest.java