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

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Locale;

public class BankAccount {
private static int accountNumberCounter;
protected int accountNumber;
protected double balance;
protected ArrayList<Transaction> transactions;
protected Branch branch;

public Branch getBranch() {
return branch;
}

public double getBalance() {
return balance;
}

public BankAccount(Branch branch){
accountNumber = accountNumberCounter++;
balance = 0;
transactions = new ArrayList<>();
this.branch = branch;
}

public void deposit(double amount, LocalDateTime localDateTime){
balance += amount;
transactions.add(new Transaction(localDateTime, amount, balance, "+"));

}

public void withdraw(double amount, LocalDateTime localDateTime){
balance -= amount;
transactions.add(new Transaction(localDateTime, amount, balance, "-"));
}

public String generateStatement(){
StringBuilder statement = new StringBuilder();
if(transactions.isEmpty()){
statement = new StringBuilder("No transaction history found");
return statement.toString();
}
for(Transaction transaction : transactions){
statement.append(transaction.toString());
}
return statement.toString();
}


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

import java.util.ArrayList;

public class Branch {
private String name;
private ArrayList<BankAccount> accounts;

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

public Boolean addAccount(BankAccount account){
accounts.add(account);

return accounts.contains(account);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.booleanuk.core;

import java.time.LocalDateTime;

public class CurrentAccount extends BankAccount{

private double overdraftLimit;

public CurrentAccount(Branch branch) {
super(branch);
overdraftLimit = -500;
}

public void withdraw(double amount, LocalDateTime localDateTime){
double newValue = balance - amount;
if(newValue < 0 || newValue > 0){
// Request for overdraft
if(requestOverdraft(newValue)){
balance -= amount;
transactions.add(new Transaction(localDateTime, amount, balance, "-"));
}
}
}

private Boolean requestOverdraft(double belowZero){
// Reject or accept request
return (belowZero > overdraftLimit);
}

}
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;

public class SavingsAccount extends BankAccount{

public SavingsAccount(Branch branch) {
super(branch);
}

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

import java.time.LocalDateTime;

public class Transaction {
private static int idCounter;
private int id;
private LocalDateTime localDateTime;
private double amount;
private double balance;
private String type;

public Transaction(LocalDateTime localDateTime, double amount, double balance, String type){
this.localDateTime = localDateTime;
this.amount = amount;
this.balance = balance;
this.type = type;
id = idCounter++;
}

public String toString(){
return "Date: " + localDateTime + " || amount: " + type + amount + " || balance: " + balance + "\n";
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/booleanuk/core/BranchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.booleanuk.core;

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

public class BranchTest {

@Test
public void addAccountToBranch(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);

Assertions.assertTrue( branch.addAccount(currentAccount));
}
}
92 changes: 92 additions & 0 deletions src/test/java/com/booleanuk/core/CurrentAccountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.booleanuk.core;

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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;

public class CurrentAccountTest {

@Test
public void depositToAccount(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
currentAccount.deposit(5000, localDateTime1);


Assertions.assertEquals(5000, currentAccount.getBalance());
}

@Test
public void withdrawFromAccount(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
currentAccount.deposit(5000, localDateTime1);
currentAccount.withdraw(4550, localDateTime2);


Assertions.assertEquals(450, currentAccount.getBalance());
}

@Test
public void generateStatementWithNoTransactions(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);

Assertions.assertEquals("No transaction history found",currentAccount.generateStatement());
}

@Test
public void generateStatementWithTransactions(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
currentAccount.deposit(5000, localDateTime1);
currentAccount.withdraw(450, localDateTime2);

String statement = currentAccount.generateStatement();

Assertions.assertEquals("Date: " + localDateTime1 + " || amount: " + "+" + 5000.0 +
" || balance: " + 5000.0 + "\n" +
"Date: " + localDateTime2 + " || amount: " + "-" + 450.0 + " || balance: " + 4550.0 + "\n"
,statement);

}


@Test
public void requestOverdraftOverTheLimit(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
currentAccount.deposit(500, localDateTime1);
currentAccount.withdraw(850, localDateTime2);

Assertions.assertEquals(-350, currentAccount.getBalance());

}

@Test
public void requestOverdraftUnderTheLimit(){
Branch branch = new Branch("Gothenburg");
BankAccount currentAccount = new CurrentAccount(branch);

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
currentAccount.deposit(500, localDateTime1);
currentAccount.withdraw(1100, localDateTime2);

Assertions.assertEquals(500, currentAccount.getBalance());

}
}
61 changes: 61 additions & 0 deletions src/test/java/com/booleanuk/core/SavingsAccountTest.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.Test;

import java.time.LocalDateTime;
import java.util.ArrayList;

public class SavingsAccountTest {

@Test
public void depositToAccount(){
Branch branch = new Branch("Gothenburg");
BankAccount savingsAccount = new SavingsAccount(branch);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
savingsAccount.deposit(5000, localDateTime1);


Assertions.assertEquals(5000, savingsAccount.getBalance());
}

@Test
public void withdrawFromAccount(){
Branch branch = new Branch("Gothenburg");
BankAccount savingsAccount = new SavingsAccount(branch);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
savingsAccount.deposit(5000, localDateTime1);
savingsAccount.withdraw(450, localDateTime2);


Assertions.assertEquals(4550, savingsAccount.getBalance());
}

@Test
public void generateStatementWithNoTransactions(){
Branch branch = new Branch("Gothenburg");
BankAccount savingsAccount = new SavingsAccount(branch);

Assertions.assertEquals("No transaction history found",savingsAccount.generateStatement());
}

@Test
public void generateStatementWithTransactions(){
Branch branch = new Branch("Gothenburg");
BankAccount savingsAccount = new SavingsAccount(branch);

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 4, 15, 14, 46, 55);
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 5, 13, 11, 52, 25);
savingsAccount.deposit(5000, localDateTime1);
savingsAccount.withdraw(450, localDateTime2);

String statement = savingsAccount.generateStatement();

Assertions.assertEquals("Date: " + localDateTime1 + " || amount: " + "+" + 5000.0 +
" || balance: " + 5000.0 + "\n" +
"Date: " + localDateTime2 + " || amount: " + "-" + 450.0 + " || balance: " + 4550.0 + "\n"
,statement);

}
}