diff --git a/domain.md b/domain.md new file mode 100644 index 000000000..1b13cff49 --- /dev/null +++ b/domain.md @@ -0,0 +1,88 @@ +# Domain Model + +## Classes + +### Account (abstract) +- Variables: + - `balance: double` + - `transactions: List` +- Methods: + - `deposit(amount: double, date: LocalDate)` + - `withdraw(amount: double, date: LocalDate)` + - `getTransactions(): List` + - `getStatement(): List` + +### CurrentAccount (extends Account) +- Variables: - +- Methods: inherits everything from Account + +### SavingsAccount (extends Account) +- Variables: - +- Methods: inherits everything from Account + +### Transaction +- Variables: + - `date: LocalDate` + - `amount: double` + - `resultingBalance: double` +- Methods: + - `toString(): String` + +## Scenario + +1. A customer creates a `SavingsAccount`. +2. The customer deposits €1000 on 13/08/2025. +3. The customer deposits €2000 on 23/09/2025. +4. The customer withdraws €1500 on 25/09/2025. +5. The customer requests the account statement. + +## Output + +1. 25/09/2025 || 1500.00 || 1500.00 +2. 23/09/2025 || 2000.00 || 3000.00 +3. 13/08/2025 || 1000.00 || 1000.00 + + +# Class diagram + +``` +------------------ +| Account | <> +------------------ +| - balance: double +| - transactions: List +------------------ +| + deposit(amount: double, date: LocalDate) +| + withdraw(amount: double, date: LocalDate) +| + deposit(amount: double) +| + withdraw(amount: double) +| + getTransactions(): List +| + getStatement(): List +----------------- +--------------------- + +------------------ +| CurrentAccount | +------------------ +| (no extra) | +------------------ + +--------------------- + +------------------ +| SavingsAccount | +-----------------+ +| (no extra) | +------------------ + +--------------------- + +------------------ +| Transaction | +------------------ +| - date: LocalDate +| - amount: double +| - balance: double +------------------ +| + toString(): String +------------------ diff --git a/src/main/java/com/booleanuk/core/Account.java b/src/main/java/com/booleanuk/core/Account.java new file mode 100644 index 000000000..861c65c89 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Account.java @@ -0,0 +1,53 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collections; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public abstract class Account { + protected List transactions; + protected double balance; + + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + public Account(){ + this.transactions = new ArrayList<>(); + this.balance = 0; + } + + public List getTransactions(){ + return transactions; + } + + public void deposit(double amount, LocalDate date) { + balance += amount; + transactions.add(new Transaction(date, amount, balance)); + } + + public void withdraw(double amount, LocalDate date) { + balance -= amount; + transactions.add(new Transaction(date, -amount, balance)); + } + + public void deposit(double amount) { + deposit(amount, LocalDate.now()); + } + + public void withdraw(double amount) { + withdraw(amount, LocalDate.now()); + } + + public List getStatement() { + List reversed = new ArrayList<>(transactions); + Collections.reverse(reversed); + + List statementLines = new ArrayList<>(); + for (Transaction t : reversed) { + statementLines.add(t.toString()); + } + return statementLines; + } + +} diff --git a/src/main/java/com/booleanuk/core/CurrentAccount.java b/src/main/java/com/booleanuk/core/CurrentAccount.java new file mode 100644 index 000000000..e89c60da4 --- /dev/null +++ b/src/main/java/com/booleanuk/core/CurrentAccount.java @@ -0,0 +1,10 @@ +package com.booleanuk.core; + +import java.util.List; +import java.util.ArrayList; + +public class CurrentAccount extends Account { + public CurrentAccount() { + super(); + } +} diff --git a/src/main/java/com/booleanuk/core/SavingsAccount.java b/src/main/java/com/booleanuk/core/SavingsAccount.java new file mode 100644 index 000000000..b9353a10e --- /dev/null +++ b/src/main/java/com/booleanuk/core/SavingsAccount.java @@ -0,0 +1,11 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class SavingsAccount extends Account{ + public SavingsAccount(){ + super(); + } + +} diff --git a/src/main/java/com/booleanuk/core/Transaction.java b/src/main/java/com/booleanuk/core/Transaction.java new file mode 100644 index 000000000..c7addd84d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Transaction.java @@ -0,0 +1,44 @@ +package com.booleanuk.core; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +public class Transaction { + private LocalDate date; + private double amount; + private double balanceAfter; + + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + public Transaction(LocalDate date, double amount, double balanceAfter) { + this.date = date; + this.amount = amount; + this.balanceAfter = balanceAfter; + } + + public LocalDate getDate() { + return date; + } + + public double getAmount() { + return amount; + } + + public double getBalanceAfter() { + return balanceAfter; + } + + @Override + public String toString() { + if (amount > 0) { + return date.format(formatter) + " || " + + String.format(Locale.US, "%.2f", amount) + " || " + + String.format(Locale.US, "%.2f", balanceAfter); + } else { + return date.format(formatter) + " || " + + String.format(Locale.US, "%.2f", Math.abs(amount)) + " || " + + String.format(Locale.US, "%.2f", balanceAfter); + } + } +} diff --git a/src/test/java/com/booleanuk/core/BankTest.java b/src/test/java/com/booleanuk/core/BankTest.java new file mode 100644 index 000000000..dd5424ba3 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BankTest.java @@ -0,0 +1,71 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeEach; + +public class BankTest { + CurrentAccount currentAccount; + SavingsAccount savingsAccount; + + @BeforeEach + void setUp() { + currentAccount = new CurrentAccount(); + savingsAccount = new SavingsAccount(); + } + + @Test + public void createCurrentAccount(){ + assertNotNull(currentAccount, "Current account created"); + } + + @Test + public void newCurrentAccount_ShouldHaveNoTransactions(){ + List transactions = currentAccount.getTransactions(); + assertTrue(transactions.isEmpty(), "new current account with no Transactions"); + } + + @Test + public void createSavingsAccount(){ + assertNotNull(savingsAccount, "Savings account created"); + } + + @Test + public void newSavingsAccount_ShouldHaveNoTransactions(){ + List transactions = savingsAccount.getTransactions(); + assertTrue(transactions.isEmpty(), "new savings account with no Transactions"); + } + + @Test + public void savingsAccount_statement_ShouldDisplayAllTransactionsWithBalance(){ + savingsAccount.deposit(1000, LocalDate.of(2025, 8, 13)); + savingsAccount.deposit(2000, LocalDate.of(2025, 9, 23)); + savingsAccount.withdraw(1500, LocalDate.of(2025, 9, 25)); + + List statement = savingsAccount.getStatement(); + + assertEquals("25/09/2025 || 1500.00 || 1500.00", statement.get(0)); + assertEquals("23/09/2025 || 2000.00 || 3000.00", statement.get(1)); + assertEquals("13/08/2025 || 1000.00 || 1000.00", statement.get(2)); + + } + + @Test + public void currentAccount_statement_ShouldDisplayAllTransactionsWithBalance(){ + currentAccount.deposit(10000, LocalDate.of(2025, 8, 12)); + currentAccount.withdraw(1000, LocalDate.of(2025, 8, 13)); + currentAccount.withdraw(2000, LocalDate.of(2025, 9, 23)); + currentAccount.deposit(1500, LocalDate.of(2025, 9, 25)); + + List statement2 = currentAccount.getStatement(); + + assertEquals("25/09/2025 || 1500.00 || 8500.00", statement2.get(0)); + assertEquals("23/09/2025 || 2000.00 || 7000.00", statement2.get(1)); + assertEquals("13/08/2025 || 1000.00 || 9000.00", statement2.get(2)); + assertEquals("12/08/2025 || 10000.00 || 10000.00", statement2.get(3)); + } +}