diff --git a/classDiagramBeforeExtension.png b/classDiagramBeforeExtension.png new file mode 100644 index 000000000..a7f2980c6 Binary files /dev/null and b/classDiagramBeforeExtension.png differ diff --git a/src/main/java/com/booleanuk/core/Bank.java b/src/main/java/com/booleanuk/core/Bank.java new file mode 100644 index 000000000..c4ff72702 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bank.java @@ -0,0 +1,21 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Bank { + private String name; + private Integer bankID; + private ArrayList listOfBranches; + + public Bank(String name, Integer bankID, ArrayList 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(); + } +} diff --git a/src/main/java/com/booleanuk/core/BankAccount.java b/src/main/java/com/booleanuk/core/BankAccount.java new file mode 100644 index 000000000..ee141795e --- /dev/null +++ b/src/main/java/com/booleanuk/core/BankAccount.java @@ -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 listOfTransactions; + private Integer branchID; + + public BankAccount(Integer uniqueBankAccountNumber, ArrayList 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(); + this.branchID = branchID; + } + + public Double getBalance() { + Double calculatedBalance = 0D; + for(Transaction transaction : listOfTransactions){ + if(transaction.getTypeOfTransaction().equals("Withdraw")){ + calculatedBalance -= transaction.getAmount(); + } + else { + calculatedBalance += transaction.getAmount(); + } + } + return calculatedBalance; + } + + public Integer getUniqueBankNumber() { + return uniqueBankAccountNumber; + } + + public ArrayList 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; + } +} diff --git a/src/main/java/com/booleanuk/core/BankBranch.java b/src/main/java/com/booleanuk/core/BankBranch.java new file mode 100644 index 000000000..ca0f17696 --- /dev/null +++ b/src/main/java/com/booleanuk/core/BankBranch.java @@ -0,0 +1,40 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class BankBranch { + private Integer branchID; + private String location; + private ArrayList listOfBankAccounts; + + public BankBranch(Integer branchID, String location, ArrayList 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(); + } + + 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; + } +} diff --git a/src/main/java/com/booleanuk/core/CurrentAccount.java b/src/main/java/com/booleanuk/core/CurrentAccount.java new file mode 100644 index 000000000..2a3154ea9 --- /dev/null +++ b/src/main/java/com/booleanuk/core/CurrentAccount.java @@ -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 listOfTransactions; + private Integer branchID; + + public CurrentAccount(Integer uniqueBankNumber, ArrayList listOfTransactions, Integer branchID){ + super(uniqueBankNumber, listOfTransactions, branchID); + } + + public CurrentAccount(Integer uniqueBankNumber, Integer branchID){ + super(uniqueBankNumber, branchID); + } +} diff --git a/src/main/java/com/booleanuk/core/SavingsAccount.java b/src/main/java/com/booleanuk/core/SavingsAccount.java new file mode 100644 index 000000000..81b7eb711 --- /dev/null +++ b/src/main/java/com/booleanuk/core/SavingsAccount.java @@ -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 listOfTransactions; + private Integer branchID; + + public SavingsAccount(Integer uniqueBankNumber, ArrayList listOfTransactions, Integer branchID){ + super(uniqueBankNumber, listOfTransactions, branchID); + } + + public SavingsAccount(Integer uniqueBankNumber, Integer branchID){ + super(uniqueBankNumber, branchID); + } +} diff --git a/src/main/java/com/booleanuk/core/Transaction.java b/src/main/java/com/booleanuk/core/Transaction.java new file mode 100644 index 000000000..dc648ec6c --- /dev/null +++ b/src/main/java/com/booleanuk/core/Transaction.java @@ -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; + } +} diff --git a/src/test/java/com/booleanuk/core/BankAccountTest.java b/src/test/java/com/booleanuk/core/BankAccountTest.java new file mode 100644 index 000000000..a7e0aee74 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BankAccountTest.java @@ -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 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()); + } + +} diff --git a/src/test/java/com/booleanuk/core/BankBranchTest.java b/src/test/java/com/booleanuk/core/BankBranchTest.java new file mode 100644 index 000000000..c095aa15b --- /dev/null +++ b/src/test/java/com/booleanuk/core/BankBranchTest.java @@ -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 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)); + } + + +} diff --git a/src/test/java/com/booleanuk/core/TransactionTest.java b/src/test/java/com/booleanuk/core/TransactionTest.java new file mode 100644 index 000000000..6d4a7464d --- /dev/null +++ b/src/test/java/com/booleanuk/core/TransactionTest.java @@ -0,0 +1,27 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +public class TransactionTest { + @Test + public void correctFormattedTransactionCredit(){ + LocalDateTime dateTime = LocalDateTime.of(2024, 9, 7, 13, 24, 10); + Transaction transaction = new Transaction(dateTime, 200D, "Deposit", 300D); + String correctlyFormattedTransaction = "2024-09-07T13:24:10 || 200.0 || || 300.0"; + + Assertions.assertEquals(correctlyFormattedTransaction, transaction.formatTransactionForBankStatement()); + } + + @Test + public void correctFormattedTransactionDebit(){ + LocalDateTime dateTime = LocalDateTime.of(2024, 9, 7, 13, 24, 10); + Transaction transaction = new Transaction(dateTime, 200D, "Withdraw", 300D); + String correctlyFormattedTransaction = "2024-09-07T13:24:10 || || 200.0 || 300.0"; + + + Assertions.assertEquals(correctlyFormattedTransaction, transaction.formatTransactionForBankStatement()); + } +}