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
82 changes: 82 additions & 0 deletions src/main/java/com/booleanuk/core/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.booleanuk.core;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

abstract public class Account {

private float balance;
private final int accountId;
private Map<Integer, Transaction> transactions; // <transactionId, Transaction>
private BankManager bm;

public Account(int accountNumber) {
this.accountId = accountNumber;
transactions = new HashMap<>();
this.bm = new BankManager();
}

public float getBalance() {
return this.balance;
}

public int getAccountId() {
return this.accountId;
}

public Map<Integer, Transaction> getTransactions() {
for (Map.Entry<Integer, Transaction> entry : this.transactions.entrySet()) {
System.out.println(entry.getValue().getAmount());
}
return this.transactions;
}

public void deposit(float amount) {
this.balance += amount;
Transaction tr = new Transaction(dtFormatter(LocalDate.now()), amount, "credit");
this.transactions.put(tr.getTransactionId(), tr);
}

public void withdraw(float amount) {
if (amount > this.balance && this.bm.approveOverdraft(this, amount)) {
this.balance -= amount;
}
else if (amount > this.getBalance() && this.bm.approveOverdraft(this, amount)) {
System.out.println("Not sufficient funds.");
return;
}
this.balance -= amount;
Transaction tr = new Transaction(dtFormatter(LocalDate.now()), amount, "debit");
transactions.put(tr.getTransactionId(), tr);
}

private String dtFormatter(LocalDate date) {
DateTimeFormatter dtForm = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return dtForm.format(date);
}

public String getTransactionStatement() {
float b = 0;
String str = "";
str += String.format("%-15s || %-15s || %-15s || %s" , "date", "credit", "debit", "balance" );
str += "\n";
str += String.format("%-15s || %-15s || %-15s || %s" , "-----------", "-----------", "-----------", "-----------" );

for (Map.Entry<Integer, Transaction> entry : this.transactions.entrySet()) {
str += "\n";
if (entry.getValue().getType().equals("credit")) {
b += entry.getValue().getAmount();
str += String.format("%-15s || %-15s || %-15s || %s" , entry.getValue().getTime(), entry.getValue().getAmount(), " ", b);
}
else if (entry.getValue().getType().equals("debit")) {
b -= entry.getValue().getAmount();
str += String.format("%-15s || %-15s || %-15s || %s" , entry.getValue().getTime(), " " , entry.getValue().getAmount() , b);
}
else { break; }
}

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

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

public class BankManager {

private List<Branch> branches;

public BankManager() {
this.branches = new ArrayList<>();
}

public void addBranch(Branch b) {
this.branches.add(b);
}

public List<Branch> getBranches() {
return this.branches;
}

public List<Account> getAccounts(Branch branch) {
for (Branch b : branches) {
if (b.equals(branch) && !branch.getAccounts().isEmpty()) {
return branch.getAccounts();
}
else {
System.out.println("Branch does not have any accounts.");
}
}
return null;
}

public boolean approveOverdraft(Account account, float amount) {
if (account instanceof CurrentAccount && (account.getBalance() - amount > 5000)) {
return true;
}
return false;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/booleanuk/core/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.booleanuk.core;

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

public class Branch {

private List<Account> accounts;

public Branch() {
this.accounts = new ArrayList<>();
}

public void addAccount(Account a) {
this.accounts.add(a);
}

public List<Account> getAccounts() {
return this.accounts;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core;

public class CurrentAccount extends Account {

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

public class SavingsAccount extends Account {

public SavingsAccount(int accountNumber) {
super(accountNumber);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/booleanuk/core/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.booleanuk.core;

public class Transaction {

private final String time;
private final float amount;
private final String type;
private final int transactionId; // bad for testing ;(
private static int nextTransactionId = 1;

public Transaction(String time, float amount, String type) {
this.time = time;
this.amount = amount;
this.type = type;
this.transactionId = nextTransactionId++;
}

public String getType() {
return this.type;
}

public float getAmount() {
return this.amount;
}

public int getTransactionId() {
return this.transactionId;
}

public String getTime() {
return this.time;
}
}
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/domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
| Classes | Members | Methods | Scenarios | Outputs |
|----------------|----------------------------------------------------------------------|-------------------------------|---------------------|--------------------|
| Account | Map<LocalDateTime, float> transactions, float balance, int accountId | deposit(float amount) | amount > 0 | balance += amount |
| | | | amount < 0 | nothing happens |
| | | withdraw(float amount) | amount < balance | balance -= amount |
| | | | amount > balance | nothing happens |
| | | getTransactions() | has transactions | List<Transactions> |
| | | | no transactions | null |
| | | getBalance() | | this.balance |
| | | getAccountId() | | this.accountId |
| | | dtFormatter(LocalDateTime dt) | dt is LocalDateTime | String |
| | | toString() | | this.toString() |
| SavingsAccount | | | | |
| CurrentAccount | | | | |
| BankManager | List<Branch> branches | allowOverdraft(Account acc) | allow | true |
| | | | deny | false |
| | | getAccounts(Branch b) | b has accounts | b.getAccounts() |
| | | | b has no accounts | null |
| Branch | List<Accounts> accounts | getAccounts() | | this.accounts |
| | | addAccount(Account a) | | void |
| Transaction | String time, float amount, String type, int transactionId | getType() | | int |
| | | getAmount() | | float |
| | | getTransactionId() | | int |
| | | getTime() | | String |





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

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

import java.util.List;
import java.util.Map;

public class CoreTest {

// BRANCH
@Test
void addAndGetAccount() {
Account a = new SavingsAccount(1);
Branch b = new Branch();
b.addAccount(a);
Assertions.assertEquals(List.of(a), b.getAccounts());
}

// ACCOUNT
@Test
void getBalance() {
Account a = new SavingsAccount(1);
a.deposit(100.0f);
a.withdraw(60.0f);
Assertions.assertEquals(40.0f, a.getBalance());
}

@Test
void getAccountNumber() {
Account a = new CurrentAccount(2);
Assertions.assertEquals(2, a.getAccountId());
}

@Test
void getTransactionsAccount() {
Account a = new SavingsAccount(1);
a.deposit(100.0f);
a.withdraw(50.0f);
Map<Integer, Transaction> map = a.getTransactions();
Assertions.assertEquals(2, map.size());
}

@Test
void getAccountId() {
Account a = new SavingsAccount(1);
Assertions.assertEquals(1, a.getAccountId());
}

@Test
void deposit() {
Account a = new CurrentAccount(1);
a.deposit(100);
Assertions.assertEquals(100, a.getBalance());
}

@Test
void withdraw() {
Account a = new CurrentAccount(1);
a.deposit(100);
a.withdraw(1);
Assertions.assertEquals(99, a.getBalance());
}

@Test
void approveOverdraft() {
BankManager bm = new BankManager();
Account a = new CurrentAccount(1);
a.deposit(100);
Assertions.assertFalse(bm.approveOverdraft(a, 10000));
}

// BANK MANAGER
@Test
void addBranch() {
BankManager bm = new BankManager();
Branch b = new Branch();
bm.addBranch(b);
Assertions.assertEquals(List.of(b), bm.getBranches());
}

@Test
void getAccounts() {
BankManager bm = new BankManager();
Branch b = new Branch();
Account a = new SavingsAccount(1);
b.addAccount(a);
bm.addBranch(b);
Assertions.assertEquals(List.of(a), bm.getAccounts(b));
}

// TRANSACTION
@Test
void getType() {
Transaction tr = new Transaction("123", 123, "credit");
Assertions.assertEquals("123", tr.getTime());
}

@Test
void getAmount() {
Transaction tr = new Transaction("123", 123, "credit");
Assertions.assertEquals(123, tr.getAmount());

}

@Test
void getTransactionId() {
Transaction tr = new Transaction("123", 123, "credit");
Assertions.assertEquals("credit", tr.getType());

}
}
Loading