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
Binary file added class-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions src/main/java/com/booleanuk/core/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.booleanuk.core;

import java.util.HashMap;
import java.util.Objects;

public class Bank {
private HashMap<String, BankAccount> accounts;

public Bank(HashMap<String, BankAccount> accounts) {
this.accounts = accounts;
}

public String createAccount(String branch, String type, int ID) {
if (Objects.equals(type, "currentAccount")) {
HashMap<Integer, Transaction> transactions = new HashMap<>();
double initialBalance = 0.0;
CurrentAccount currentAccount = new CurrentAccount(branch, transactions, ID, initialBalance);
accounts.put(branch, currentAccount);
return "currentAccount created successfully!";
} else if (Objects.equals(type, "savingsAccount")) {
HashMap<Integer, Transaction> transactions = new HashMap<>();
double initialBalance = 0.0;
SavingsAccount savingsAccount = new SavingsAccount(branch, transactions, ID, initialBalance);
accounts.put(branch, savingsAccount);
return "savingsAccount created successfully!";
}
return "Account could not be created, invalid account type";
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/booleanuk/core/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.booleanuk.core;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public abstract class BankAccount {
private String branch;
private HashMap<Integer, Transaction> transactions;
private int accountNumber;
private double balance;

public BankAccount(String branch, HashMap<Integer, Transaction> transactions, int accountNumber, double balance) {
this.branch = branch;
this.transactions = transactions;
this.accountNumber = accountNumber;
this.balance = balance;
}

public double getBalance() {
Collection<Transaction> collection = transactions.values();
double balance = 0;
for (Transaction transaction : collection) {
if (Objects.equals(transaction.getType(), "credit")) {
balance += transaction.getAmount();
} else {
balance -= transaction.getAmount();
}
}
return balance;
}

public String deposit(double amount) {
int ID = (int) (Math.random()* 90000) + 10000;
String type = "credit";
String date = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
double balanceAfter = balance += amount;
transactions.put(ID, new Transaction(ID, type, amount, date, balanceAfter));
return "Deposit successfull!";
}

public String withdraw(double amount) {
int ID = (int) (Math.random()* 90000) + 10000;
String type = "debit";
String date = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
double balanceAfter = balance -= amount;
transactions.put(ID, new Transaction(ID, type, amount, date, balanceAfter));
return "Withdraw successfull!";
}

public String generateBankStatement() {
StringBuilder bankStatement = new StringBuilder();
List<Transaction> sortedTransactions = new ArrayList<>(transactions.values());
sortedTransactions.sort(Comparator.comparing(Transaction::getDate).reversed());
bankStatement.append("date || credit || debit || balance\n");
for (Transaction transaction : sortedTransactions) {
String date = LocalDateTime.parse(transaction.getDate()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String credit = transaction.getType().equals("credit") ? String.valueOf(transaction.getAmount()) : "";
String debit = transaction.getType().equals("debit") ? String.valueOf(transaction.getAmount()) : "";
String balance = String.valueOf(transaction.getBalanceAfter());

bankStatement.append(date).append(" || ").append(credit).append(" || ").append(debit).append(" || ").append(balance).append("\n");
}
return bankStatement.toString();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.booleanuk.core;

import java.util.HashMap;

public class CurrentAccount extends BankAccount{
public CurrentAccount(String branch, HashMap<Integer, Transaction> transactions, int accountNumber, double balance) {
super(branch, transactions, accountNumber, balance);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/booleanuk/core/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.booleanuk.core;

import java.util.HashMap;

public class SavingsAccount extends BankAccount{
public SavingsAccount(String branch, HashMap<Integer, Transaction> transactions, int accountNumber, double balance) {
super(branch, transactions, accountNumber, balance);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/booleanuk/core/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.booleanuk.core;

public class Transaction {
private int ID;
private String type;
private double amount;
private String date;
private double balanceAfter;

public Transaction(int ID, String type, double amount, String date, double balanceAfter) {
this.ID = ID;
this.type = type;
this.amount = amount;
this.date = date;
this.balanceAfter = balanceAfter;
}

public int getID() {
return ID;
}

public String getType() {
return type;
}

public double getAmount() {
return amount;
}

public String getDate() {
return date;
}

public double getBalanceAfter() {
return balanceAfter;
}
}
61 changes: 61 additions & 0 deletions src/test/java/com/booleanuk/core/BankAccountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

public class BankAccountTest {
private SavingsAccount savingsAccount;
private CurrentAccount currentAccount;

@BeforeEach
public void setUp() {
HashMap<Integer, Transaction> savingsTransactions = new HashMap<>() {{
put(12345, new Transaction(12345, "credit", 200.0, "2025-01-14T10:00:00", 200.0));
put(54321, new Transaction(54321, "debit", 100.0, "2025-01-15T08:00:00", 100.0));
put(32145, new Transaction(32145, "credit", 50.0, "2025-01-16T01:00:00", 150.0));
}};
HashMap<Integer, Transaction> currentTransactions = new HashMap<>() {{
put(12345, new Transaction(12345, "credit", 200.0, "2025-01-14T10:00:00", 200.0));
put(54321, new Transaction(54321, "debit", 100.0, "2025-01-15T08:00:00", 100.0));
put(32145, new Transaction(32145, "credit", 50.0, "2025-01-16T01:00:00", 150.0));
}};
savingsAccount = new SavingsAccount("Gothenburg", savingsTransactions, 87654321, 0.0);
currentAccount = new CurrentAccount("Stockholm", currentTransactions, 54321678, 0.0);
}

@Test
public void testGetBalance() {
Assertions.assertEquals(150.0, savingsAccount.getBalance());
Assertions.assertEquals(150.0, currentAccount.getBalance());
}

@Test
public void testDeposit() {
Assertions.assertEquals("Deposit successfull!", savingsAccount.deposit(30.0));
Assertions.assertEquals("Deposit successfull!", currentAccount.deposit(40.0));
Assertions.assertEquals(180.0, savingsAccount.getBalance());
Assertions.assertEquals(190.0, currentAccount.getBalance());
}

@Test
public void testWithdraw() {
Assertions.assertEquals("Withdraw successfull!", savingsAccount.withdraw(60.0));
Assertions.assertEquals("Withdraw successfull!", currentAccount.withdraw(20.0));
Assertions.assertEquals(90.0, savingsAccount.getBalance());
Assertions.assertEquals(130.0, currentAccount.getBalance());
}

@Test
public void testGenerateBankStatement() {
String expectedOutput = "date || credit || debit || balance\n" +
"2025-01-16T01:00:00 || 50.0 || || 150.0\n" +
"2025-01-15T08:00:00 || || 100.0 || 100.0\n" +
"2025-01-14T10:00:00 || 200.0 || || 200.0\n";
Assertions.assertEquals(expectedOutput, savingsAccount.generateBankStatement());
Assertions.assertEquals(expectedOutput, currentAccount.generateBankStatement());

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

public class BankTest {
private HashMap<String, BankAccount> bankAccounts;
private Bank bank;

@BeforeEach
public void setUp() {
this.bankAccounts = new HashMap<>();
this.bank = new Bank(bankAccounts);
}

@Test
public void testCreateAccount() {
String branch = "Gothenburg";
String accountType = "currentAccount";
int ID = 12345;
String invalidType = "otherAccount";

Assertions.assertEquals("currentAccount created successfully!", bank.createAccount(branch, accountType, ID));
Assertions.assertEquals("Account could not be created, invalid account type", bank.createAccount(branch, invalidType, ID));
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/booleanuk/core/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TransactionTest {

@Test
public void testTransactionConstructor() {
int ID = 12345;
String type = "credit";
double amount = 50.0;
String date = "2025-01-15";
double balanceAfter = 50.0;
Transaction transaction = new Transaction(ID, type, amount, date, balanceAfter);

Assertions.assertEquals(12345, transaction.getID());
Assertions.assertEquals("credit", transaction.getType());
Assertions.assertEquals(50.0, transaction.getAmount());
Assertions.assertEquals("2025-01-15", transaction.getDate());
Assertions.assertEquals(50.0, transaction.getBalanceAfter());
}

}
Binary file added updated-class-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.