Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Domain Model

## Classes

### Account (abstract)
- Variables:
- `balance: double`
- `transactions: List<Transaction>`
- Methods:
- `deposit(amount: double, date: LocalDate)`
- `withdraw(amount: double, date: LocalDate)`
- `getTransactions(): List<Transaction>`
- `getStatement(): List<String>`

### 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 | <<abstract>>
------------------
| - balance: double
| - transactions: List<Transaction>
------------------
| + deposit(amount: double, date: LocalDate)
| + withdraw(amount: double, date: LocalDate)
| + deposit(amount: double)
| + withdraw(amount: double)
| + getTransactions(): List<Transaction>
| + getStatement(): List<String>
-----------------
---------------------

------------------
| CurrentAccount |
------------------
| (no extra) |
------------------

---------------------

------------------
| SavingsAccount |
-----------------+
| (no extra) |
------------------

---------------------

------------------
| Transaction |
------------------
| - date: LocalDate
| - amount: double
| - balance: double
------------------
| + toString(): String
------------------
53 changes: 53 additions & 0 deletions src/main/java/com/booleanuk/core/Account.java
Original file line number Diff line number Diff line change
@@ -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<Transaction> 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<Transaction> 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<String> getStatement() {
List<Transaction> reversed = new ArrayList<>(transactions);
Collections.reverse(reversed);

List<String> statementLines = new ArrayList<>();
for (Transaction t : reversed) {
statementLines.add(t.toString());
}
return statementLines;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.booleanuk.core;

import java.util.List;
import java.util.ArrayList;

public class CurrentAccount extends Account {
public CurrentAccount() {
super();
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/core/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;

public class SavingsAccount extends Account{
public SavingsAccount(){
super();
}

}
44 changes: 44 additions & 0 deletions src/main/java/com/booleanuk/core/Transaction.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
71 changes: 71 additions & 0 deletions src/test/java/com/booleanuk/core/BankTest.java
Original file line number Diff line number Diff line change
@@ -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<Transaction> 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<Transaction> 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<String> 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<String> 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));
}
}
Loading