diff --git a/gleek-k3O-mjFL9gaOsrPhyzn1vQ.png b/gleek-k3O-mjFL9gaOsrPhyzn1vQ.png new file mode 100644 index 000000000..972d241cd Binary files /dev/null and b/gleek-k3O-mjFL9gaOsrPhyzn1vQ.png differ 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..55be40ad9 --- /dev/null +++ b/src/main/java/com/booleanuk/core/BankAccount.java @@ -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 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(); + } + + +} diff --git a/src/main/java/com/booleanuk/core/Branch.java b/src/main/java/com/booleanuk/core/Branch.java new file mode 100644 index 000000000..e660c5445 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Branch.java @@ -0,0 +1,19 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Branch { + private String name; + private ArrayList accounts; + + public Branch(String name){ + accounts = new ArrayList<>(); + this.name = name; + } + + public Boolean addAccount(BankAccount account){ + accounts.add(account); + + return accounts.contains(account); + } +} 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..1279aa170 --- /dev/null +++ b/src/main/java/com/booleanuk/core/CurrentAccount.java @@ -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); + } + +} 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..aa1585626 --- /dev/null +++ b/src/main/java/com/booleanuk/core/SavingsAccount.java @@ -0,0 +1,9 @@ +package com.booleanuk.core; + +public class SavingsAccount extends BankAccount{ + + public SavingsAccount(Branch branch) { + super(branch); + } + +} 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..65d88979d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Transaction.java @@ -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"; + } +} diff --git a/src/test/java/com/booleanuk/core/BranchTest.java b/src/test/java/com/booleanuk/core/BranchTest.java new file mode 100644 index 000000000..cd9491ffe --- /dev/null +++ b/src/test/java/com/booleanuk/core/BranchTest.java @@ -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)); + } +} diff --git a/src/test/java/com/booleanuk/core/CurrentAccountTest.java b/src/test/java/com/booleanuk/core/CurrentAccountTest.java new file mode 100644 index 000000000..213d6236f --- /dev/null +++ b/src/test/java/com/booleanuk/core/CurrentAccountTest.java @@ -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()); + + } +} diff --git a/src/test/java/com/booleanuk/core/SavingsAccountTest.java b/src/test/java/com/booleanuk/core/SavingsAccountTest.java new file mode 100644 index 000000000..1d89ee68f --- /dev/null +++ b/src/test/java/com/booleanuk/core/SavingsAccountTest.java @@ -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); + + } +}