Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a0b8d8c
Classdiagram for core-exercise completed. Also I have made all the cl…
Jan 15, 2025
a7f9f3c
Test made for method addAccount() in BankBranch-class.
Jan 15, 2025
6cc70a9
initial method for addAccount() in BankBranch-class completed.
Jan 15, 2025
74c5684
Added another Test for addAccount().
Jan 15, 2025
aa42c32
Created the Transaction-class and populated the ArrayList<Transaction…
Jan 15, 2025
4a997dc
Added getters and setters for the transaction-class. Changed the cons…
Jan 15, 2025
b3e982d
Changed the constructor for the BankAccount class, thus I needed to u…
Jan 15, 2025
51329e1
Test for makeDeposit() method completed.
Jan 15, 2025
83a3148
makeDeposit() initial method completed. Needed to do a little adjustm…
Jan 15, 2025
f36a7c5
makeDeposit() method completed.
Jan 15, 2025
e5b61f2
Initial test made for makeWithDraw() method.
Jan 15, 2025
f56e60f
Initial method makeWithDraw() completed.
Jan 15, 2025
3856d1f
added another test for makeWithDraw().
Jan 15, 2025
97f7cc5
method makeWithDraw() completed. Made a small error in the correspodi…
Jan 15, 2025
89309ea
Deleted the correctlyFormattedTransaction() from the classdiagram, th…
Jan 15, 2025
0b65ddc
Realised that I could add "newBalance" to Transaction class when inst…
Jan 15, 2025
6a68e19
Added a getter for the newBalance in the Transaction class. I also ma…
Jan 15, 2025
248708a
Completed formatTransactionForBankStatement() i Transaction
Jan 15, 2025
b766d93
Test made for generateBankStatements() in BankAccount
Jan 15, 2025
f2f7457
Method generateBankStatements() in BankAccount completed. Noticed tha…
Jan 15, 2025
eaa6a0a
Test for the first extension where im updating my getter for this.bal…
Jan 15, 2025
79800d5
Completed Method for the first extension where im updating my getter …
Jan 15, 2025
2ae6ec5
I already had a BankBranch class that have an Arraylist<BankAccount> …
Jan 15, 2025
f23d996
I already had a BankBranch class that have an Arraylist<BankAccount> …
Jan 15, 2025
8554277
Created test for extension 2
Jan 15, 2025
8bc4e5f
created initial method: belongsBankAccountToBankBranch(). I also chan…
Jan 15, 2025
cd10a14
renamed attribute bankID in BankAccount to branchID.
Jan 15, 2025
d318ba1
Completed belongsBankAccountToBankBranch() method for extension 2
Jan 15, 2025
3aee9d2
Added another test for belongsBankAccountToBankBranch() method for ex…
Jan 15, 2025
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 classDiagramBeforeExtension.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/main/java/com/booleanuk/core/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class Bank {
private String name;
private Integer bankID;
private ArrayList<BankBranch> listOfBranches;

public Bank(String name, Integer bankID, ArrayList<BankBranch> listOfBranches){
this.name = name;
this.bankID = bankID;
this.listOfBranches = listOfBranches;
}

public Bank(String name, Integer bankID){
this.name = name;
this.bankID = bankID;
this.listOfBranches = new ArrayList<BankBranch>();
}
}
88 changes: 88 additions & 0 deletions src/main/java/com/booleanuk/core/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.booleanuk.core;

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

public class BankAccount {
private Integer uniqueBankAccountNumber;
private Double balance;
private ArrayList<Transaction> listOfTransactions;
private Integer branchID;

public BankAccount(Integer uniqueBankAccountNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){
this.uniqueBankAccountNumber = uniqueBankAccountNumber;

Double totalBalance = 0D;
for(Transaction transaction : listOfTransactions){
if(transaction.getTypeOfTransaction().equals("Withdraw")){
totalBalance -= transaction.getAmount();
}
else {
totalBalance += transaction.getAmount();
}
}

this.balance = Math.round(totalBalance * 100.0) / 100.0;
this.listOfTransactions = listOfTransactions;
this.branchID = branchID;
}

public BankAccount(Integer uniqueBankAccountNumber, Integer branchID){
this.uniqueBankAccountNumber = uniqueBankAccountNumber;
this.balance = 0D;
this.listOfTransactions = new ArrayList<Transaction>();
this.branchID = branchID;
}

public Double getBalance() {
Double calculatedBalance = 0D;
for(Transaction transaction : listOfTransactions){
if(transaction.getTypeOfTransaction().equals("Withdraw")){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use an enum here to get strong typing

calculatedBalance -= transaction.getAmount();
}
else {
calculatedBalance += transaction.getAmount();
}
}
return calculatedBalance;
}

public Integer getUniqueBankNumber() {
return uniqueBankAccountNumber;
}

public ArrayList<Transaction> getListOfTransactions() {
return listOfTransactions;
}

public Integer getBranchID() {
return branchID;
}

public Double makeDeposit(Double amount, LocalDateTime dateTime){
if((this.getBalance() + amount) > this.getBalance()){
Transaction deposit = new Transaction(dateTime, amount, "Deposit", (this.getBalance() + amount));
this.listOfTransactions.add(deposit);
this.balance += amount;
}
return this.balance;
}

public Double makeWithDraw(Double amount, LocalDateTime dateTime){
if(amount > 0){
Transaction withdraw = new Transaction(dateTime, amount, "Withdraw", (this.getBalance() - amount));
this.listOfTransactions.add(withdraw);
this.balance -= amount;
}
return this.balance;
}

public String generateBankStatements(){
String correctBankStatement = "Date || Credit || Debit || Balance\n";
System.out.println(this.listOfTransactions.size());
for(int i = (this.listOfTransactions.size() - 1); i >= 0; i--){
correctBankStatement += this.listOfTransactions.get(i).formatTransactionForBankStatement() + "\n";
}
return correctBankStatement;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/booleanuk/core/BankBranch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class BankBranch {
private Integer branchID;
private String location;
private ArrayList<BankAccount> listOfBankAccounts;

public BankBranch(Integer branchID, String location, ArrayList<BankAccount> listOfBankAccounts){
this.branchID = branchID;
this.location = location;
this.listOfBankAccounts = listOfBankAccounts;
}

public BankBranch(Integer branchID, String location){
this.branchID = branchID;
this.location = location;
this.listOfBankAccounts = new ArrayList<BankAccount>();
}

public boolean addAccount(BankAccount accountToAdd){
for(BankAccount account : this.listOfBankAccounts){
if(account.getUniqueBankNumber() == accountToAdd.getUniqueBankNumber()){
return false;
}
}
this.listOfBankAccounts.add(accountToAdd);
return true;
}

public Boolean belongsBankAccountToBankBranch(BankAccount bankAccount){
for(BankAccount account : this.listOfBankAccounts){
if(bankAccount.getUniqueBankNumber() == account.getUniqueBankNumber()){
return true;
}
}
return false;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class CurrentAccount extends BankAccount{
private Integer uniqueBankNumber;
private Double balance;
private ArrayList<Transaction> listOfTransactions;
private Integer branchID;

public CurrentAccount(Integer uniqueBankNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){
super(uniqueBankNumber, listOfTransactions, branchID);
}

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

import java.util.ArrayList;

public class SavingsAccount extends BankAccount{
private Integer uniqueBankNumber;
private Double balance;
private ArrayList<Transaction> listOfTransactions;
private Integer branchID;

public SavingsAccount(Integer uniqueBankNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){
super(uniqueBankNumber, listOfTransactions, branchID);
}

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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Transaction {
private LocalDateTime date;
private Double amount;
private String typeOfTransaction;
private Double newBalance;

public Transaction(LocalDateTime date, Double amount, String typeOfTransaction, Double newBalance){
this.date = date;
this.amount = amount;
this.typeOfTransaction = typeOfTransaction;
this.newBalance = newBalance;
}

public LocalDateTime getDate() {
return date;
}

public Double getAmount() {
return amount;
}

public String getTypeOfTransaction() {
return typeOfTransaction;
}

public Double getNewBalance() {
return newBalance;
}

public String formatTransactionForBankStatement(){
String formatedTransaction = "";

formatedTransaction = this.getDate().toString() + " || ";
if(this.getTypeOfTransaction().equals("Withdraw")){
formatedTransaction += " || " + this.getAmount() + " || " + this.getNewBalance();
} else if (this.getTypeOfTransaction().equals("Deposit")) {
formatedTransaction += this.getAmount() + " || || " + this.getNewBalance();
}

return formatedTransaction;
}
}
88 changes: 88 additions & 0 deletions src/test/java/com/booleanuk/core/BankAccountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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 BankAccountTest {
private ArrayList<Transaction> listOfTransactions = new ArrayList<>(){{
LocalDateTime dateTime1 = LocalDateTime.of(2024, 9, 7, 13, 24, 10);
Transaction transaction1 = new Transaction(dateTime1, 200D, "Deposit", 200D);
add(transaction1);

LocalDateTime dateTime2 = LocalDateTime.of(2024, 11, 5, 10, 30, 8);
Transaction transaction2 = new Transaction(dateTime2, 700D, "Deposit", 900D);
add(transaction2);

LocalDateTime dateTime3 = LocalDateTime.of(2025, 1, 13, 13, 58, 10);
Transaction transaction3 = new Transaction(dateTime3, 500D, "Withdraw", 400D);
add(transaction3);
}};

@Test
public void balanceIncreasesWhenDepositingValidAmount(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);
LocalDateTime dateTime = LocalDateTime.now();
dateTime = dateTime.plusDays(4);

Assertions.assertEquals(600D, account.makeDeposit(200D, dateTime));
}

@Test
public void balanceStaysTheSameWhenDepositingNonValidAmount(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);
LocalDateTime dateTime = LocalDateTime.now();
dateTime = dateTime.plusDays(4);

Assertions.assertEquals(400D, account.makeDeposit(-200D, dateTime));
}

@Test
public void balanceDecreasesWhenWithdrawingValidAmount(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);
LocalDateTime dateTime = LocalDateTime.now();
dateTime = dateTime.plusDays(4);

Assertions.assertEquals(300D, account.makeWithDraw(100D, dateTime));
}

@Test
public void balanceStaysTheSameWhenWithdrawingNonValidAmount(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);
LocalDateTime dateTime = LocalDateTime.now();
dateTime = dateTime.plusDays(4);

Assertions.assertEquals(400D, account.makeWithDraw(-100D, dateTime));
}




@Test
public void correctBankStatementsAreOutputed(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);
String correctBankStatement =
"Date || Credit || Debit || Balance\n" +
"2025-01-13T13:58:10 || || 500.0 || 400.0\n" +
"2024-11-05T10:30:08 || 700.0 || || 900.0\n" +
"2024-09-07T13:24:10 || 200.0 || || 200.0\n";

Assertions.assertEquals(correctBankStatement, account.generateBankStatements());
}





// EXTENSION 1
// Im updating my getter (getBalance()) so it is calculated whenever its called.
@Test
public void getterCalculateCorrectly(){
BankAccount account = new CurrentAccount(1, listOfTransactions, 1);

Assertions.assertEquals(400, account.getBalance());
}

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

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

import java.util.ArrayList;

public class BankBranchTest {
private ArrayList<BankAccount> listOfBankAccounts = new ArrayList<>(){{
BankAccount currentAccount = new CurrentAccount(1, 1);
add(currentAccount);

BankAccount savingsAccount = new SavingsAccount(2, 1);
add(savingsAccount);
}};


@Test
public void bankAccountIdExistsAlready(){
BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts);
BankAccount existingAccount = new CurrentAccount(1, 1);

Assertions.assertFalse(bankBranch.addAccount(existingAccount));
}

@Test
public void bankAccountIdDontExists(){
BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts);
BankAccount newAccount = new CurrentAccount(3, 1);

Assertions.assertTrue(bankBranch.addAccount(newAccount));
}




// EXTENSION 2
@Test
public void bankAccountIsIncludedInBankBranchListOfBankAccounts(){
BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts);
BankAccount newAccount = new CurrentAccount(3, 1);
bankBranch.addAccount(newAccount);

Assertions.assertTrue(bankBranch.belongsBankAccountToBankBranch(newAccount));
}

@Test
public void bankAccountIsNotIncludedInBankBranchListOfBankAccounts(){
BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts);
BankAccount newAccount = new CurrentAccount(3, 1);

Assertions.assertFalse(bankBranch.belongsBankAccountToBankBranch(newAccount));
}


}
Loading