diff --git a/src/main/java/com/booleanuk/core/accounts/Account.java b/src/main/java/com/booleanuk/core/accounts/Account.java new file mode 100644 index 000000000..d89077482 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/Account.java @@ -0,0 +1,116 @@ +package com.booleanuk.core.accounts; + +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; +import com.booleanuk.core.transactons.Transaction; +import com.booleanuk.core.transactons.TransactionType; + +import java.util.*; + +public abstract class Account { + + private IdGenerator idGenerator; + private final String accountNumber; + private Branch branch; + private boolean isPossibleToOverdraft; + private Map transactions; + + public Account(String accountNumber) { + this.idGenerator = new IdGenerator(); + this.accountNumber = accountNumber; + this.transactions = new HashMap<>(); + } + + public String getAccountNumber() { + return this.accountNumber; + } + + public Branch getBranch() { + return branch; + } + + public void setBranch(Branch branch) { + this.branch = branch; + } + + public boolean isPossibleToOverdraft() { + return isPossibleToOverdraft; + } + + public void setPossibleToOverdraft(boolean possibleToOverdraft) { + isPossibleToOverdraft = possibleToOverdraft; + } + + protected void addTransaction(Transaction transaction) { + this.transactions.put(transaction.getId(), transaction); + } + + public void deposit(Double amount) { + Transaction transaction = new Transaction( + this.idGenerator.createId(IdPrefix.TR), + amount, + TransactionType.DEBIT, + calculateBalance() + ); + this.addTransaction(transaction); + } + + protected void validateOverdraft(Double currentBalance, Double withdrawAmount) throws Exception { + boolean isNegativeBalance = (currentBalance - withdrawAmount) < 0; + if (!this.isPossibleToOverdraft && isNegativeBalance) { + + // TODO: should make custom exception + + throw new Exception("Not possible to overdraft"); + } + } + + public void withdraw(Double amount) { + Double currentBalance = this.calculateBalance(); + try { + this.validateOverdraft(currentBalance, amount); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + Transaction transaction = new Transaction( + this.idGenerator.createId(IdPrefix.TR), + amount, + TransactionType.CREDIT, + currentBalance + ); + this.addTransaction(transaction); + } + + public ArrayList getAllTransactions() { + ArrayList list = new ArrayList<>(transactions.values()); + Collections.reverse(list); + + return list; + } + + protected Double calculateBalance() { + Double sum = 0.0; + for (Transaction transaction : transactions.values()) { + if (transaction.getType() == TransactionType.DEBIT) { + sum += transaction.getAmount(); + } else if (transaction.getType() == TransactionType.CREDIT) { + sum -= transaction.getAmount(); + } + } + return sum; + } + + public Double getBalance() { + return calculateBalance(); + } + + // TODO: printTransactionHistory + public void printTransactionHistory() { + System.out.println(); + System.out.printf("%s", "date", "||"); + for (Transaction transaction : transactions.values()) { + System.out.println(); + } + } +} diff --git a/src/main/java/com/booleanuk/core/accounts/AccountType.java b/src/main/java/com/booleanuk/core/accounts/AccountType.java new file mode 100644 index 000000000..cf8c130fb --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/AccountType.java @@ -0,0 +1,6 @@ +package com.booleanuk.core.accounts; + +public enum AccountType { + CURRENT, + SAVINGS +} diff --git a/src/main/java/com/booleanuk/core/accounts/Branch.java b/src/main/java/com/booleanuk/core/accounts/Branch.java new file mode 100644 index 000000000..d647c1469 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/Branch.java @@ -0,0 +1,7 @@ +package com.booleanuk.core.accounts; + +public enum Branch { + BRANCH_1, + BRANCH_2, + BRANCH_3 +} diff --git a/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java b/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java new file mode 100644 index 000000000..dd2b5c732 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java @@ -0,0 +1,13 @@ +package com.booleanuk.core.accounts; + +public class CurrentAccount extends Account{ + + // TODO: Could this constructor be protected? + // should not be able to create without a Bank + + public CurrentAccount(String accountNumber) { + super(accountNumber); + this.setBranch(Branch.BRANCH_1); + this.setPossibleToOverdraft(true); + } +} diff --git a/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java new file mode 100644 index 000000000..e4b224f62 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java @@ -0,0 +1,16 @@ +package com.booleanuk.core.accounts; + +public class SavingsAccount extends Account{ + + private Double interestRate; + + // TODO: Could this constructor be protected? + // should not be able to create without a Bank + + public SavingsAccount(String accountNumber) { + super(accountNumber); + this.setBranch(Branch.BRANCH_1); + this.setPossibleToOverdraft(false); + this.interestRate = 0.12; + } +} diff --git a/src/main/java/com/booleanuk/core/bank/Bank.java b/src/main/java/com/booleanuk/core/bank/Bank.java new file mode 100644 index 000000000..0be1ec68e --- /dev/null +++ b/src/main/java/com/booleanuk/core/bank/Bank.java @@ -0,0 +1,68 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.AccountType; +import com.booleanuk.core.accounts.CurrentAccount; +import com.booleanuk.core.accounts.SavingsAccount; +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Bank { + + private IdGenerator idGenerator; + private Map customers; + private Map requests; + + public Bank() { + this.customers = new HashMap<>(); + this.requests = new HashMap<>(); + this.idGenerator = new IdGenerator(); + } + + public User createUser(String personalNumber) { + User customer = new User(personalNumber); + this.customers.put(personalNumber, customer); + return customer; + } + + public void createUserAccount(User user, AccountType type) { + + String id = idGenerator.createId(IdPrefix.AC); + + if (type == AccountType.CURRENT) { + Account currentAccount = new CurrentAccount(id); + user.addAccount(currentAccount); + + } else if (type == AccountType.SAVINGS) { + Account savingsAccount = new SavingsAccount(id); + user.addAccount(savingsAccount); + } + } + + public ArrayList getAllRequests() { + ArrayList list = new ArrayList<>(requests.values()); + Collections.reverse(list); + + return list; + } + + private void addRequest(Request request) { + this.requests.put(request.getId(), request); + } + + public void makeRequest(Account account, Double overdraftSum) { + String id = this.idGenerator.createId(IdPrefix.RE); + + Request request = new Request(id, account, overdraftSum); + this.addRequest(request); + } + + private void removeRequest(String requestId) { + this.requests.remove(requestId); + } +} diff --git a/src/main/java/com/booleanuk/core/bank/Request.java b/src/main/java/com/booleanuk/core/bank/Request.java new file mode 100644 index 000000000..770d5db32 --- /dev/null +++ b/src/main/java/com/booleanuk/core/bank/Request.java @@ -0,0 +1,32 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; + +public class Request { + + private final String id; + private final Account account; + private Double overdraftSum; + + public Request(String id, Account account, Double overdraftSum) { + this.id = id; + this.account = account; + this.overdraftSum = overdraftSum; + } + + public String getId() { + return id; + } + + // TODO: If time, add some function so the user can + // validate if it's an reasonable amount of overdraft + // with the overdraft sum + + public void approve() { + this.account.setPossibleToOverdraft(true); + } + + public void reject() { + this.account.setPossibleToOverdraft(false); + } +} diff --git a/src/main/java/com/booleanuk/core/bank/User.java b/src/main/java/com/booleanuk/core/bank/User.java new file mode 100644 index 000000000..066f55369 --- /dev/null +++ b/src/main/java/com/booleanuk/core/bank/User.java @@ -0,0 +1,33 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class User { + + // TODO: Not safe to store personal numbers ike this, should be a uniqueId instead? + // It's personal number for now for simplicity + + private String personalNumber; + private Map accounts; + + public User(String personalNumber) { + this.personalNumber = personalNumber; + this.accounts = new HashMap<>(); + } + + protected void addAccount(Account account) { + this.accounts.put(account.getAccountNumber(), account); + } + + public ArrayList getAllAccounts() { + ArrayList list = new ArrayList<>(accounts.values()); +// Collections.reverse(list); + + return list; + } +} diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md new file mode 100644 index 000000000..b95a17898 --- /dev/null +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -0,0 +1,122 @@ +# Domain Model + + +## Table +| Classes | Variables | Methods | Scenario | Output | +|--------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| +| `Bank` | `-IdGenerator idGenerator` | | | | +| | `-Map customers` | `+createUser(String personalNumber)` | | | +| | | `+createUserAccount(User user, AccountType type)` | | | +| | `-Map requests` | `+getAllRequests()` | Get all requests to be handled. | Map | +| | | `-addRequest(Request request)` | Validate, add to the list requests if possible. | void / throw exception | +| | | `-removeRequest(String requestId)` | Remove request by providing id. | void / throw exception | +| | | `+makeRequest(Request request)` | Make a request as a user. | Request object / null | +| | | | | | +| `Request(Account account, Double overdraftSum)` | `-String id` | `+getId()` | | String | +| | `-String account` | | | String | +| | `-Double overdraftSum` | | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `User(String personalNumber)` TODO: not implemented | `-String personalNumber` | | | | +| | `-Map accounts` | | | | +| | | `+addAccount(Account newAcount)` | | void / throw exception | +| | | `+removeAccount(String accountNumber)` | If possible to remove account, provide user with confirmation question
before the account is fully removed. | void / throw exception | +| | | | | | +| abstract `Account` | `IdGenerator idGenerator` | | | | +| | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| | `-Branch branch` | `+getBranch()` | | | +| | | `+setBranch()` | | | +| | `-boolean isPossibleToOvedraft` | `+isPossibleToOverdraft()` | | boolean | +| | | `+setPossibleToOverdraft(boolean isPossibleToOvedraft)` | | | +| | `-Map transactions` | | | | +| | | `#addTransaction(Transaction transaction)` | | | +| | | `+deposit(Double sum)` | | void / throw exception | +| | | `#validateOverdraft(Double currentBalance, Double withdrawAmount)` | | | +| | | `+withdraw(Double sum)` | | void / throw exception | +| | | `#calculateBalance()` | | Double | +| | | `+getBalance()` | | | +| | | `getAllTransactions()` | | ArrayList | +| | | `+printTransctionHistory()` TODO: not implemented | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| enum `AccountType` | `CURRENT, SAVINGS` | | | | +| | | | | | +| enum `Branch` | `BRANCH_1, BRANCH_2, BRANCH_3` | | | | +| | | | | | +| `Transaction(Double amount, TransactionTYpe type, Double balance)` | `-String id` | `+getId()` | Get id. Id is created via IdGenerator. | String | +| | `-LocalDateTime transactionDate` | `+getTransactionDate()` | Get LocalDateTime, this is created when a Transaction is created. | String | +| | `-Double amount` | `+getAmount()` | | Double | +| | `-TransactionType type` | `+getTransactionType()` | Could be CREDIT or DEBIT. | TransactionType | +| | `-Double currentBalance` | `+getCurrentBalance()` | Get the balance the account had when this transaction was made. | Double | +| | | | | | +| enum `TransactionType` | `DEBIT, CREDIT` | | | | +| | | | | | +| `IdGenerator(IdPrefix prefix)` | `-static int currentAC` | | | | +| | `-static int currentRE` | | Generate different Id's based on provided prefix. | | +| | `-static int currentTR` | | | | +| | `-IdPrefix prefix` | | | | +| | `-String id` | `-setId(int currentNumber)` | Generate id by prefix and current id number. | | +| | | `+getId()` | | String | + +## Class Diagram + + +## User Stories - Core +``` +As a customer, +So I can safely store and use my money, +I want to create a current account. + +As a customer, +So I can save for a rainy day, +I want to create a savings account. + +As a customer, +So I can keep a record of my finances, +I want to generate bank statements with transaction dates, amounts, and balance at the time of transaction. + +As a customer, +So I can use my account, +I want to deposit and withdraw funds. +``` + +## Acceptance Criteria + +**Given** a client makes a deposit of 1000 on 10-01-2012 +**And** a deposit of 2000 on 13-01-2012 +**And** a withdrawal of 500 on 14-01-2012 +**When** she prints her bank statement +**Then** she would see: + +``` +date || credit || debit || balance +14/01/2012 || || 500.00 || 2500.00 +13/01/2012 || 2000.00 || || 3000.00 +10/01/2012 || 1000.00 || || 1000.00 +``` + +## User Stories - Extensions +``` +As an engineer, +So I don't need to keep track of state, +I want account balances to be calculated based on transaction history instead of stored in memory. + +As a bank manager, +So I can expand, +I want accounts to be associated with specific branches. + +As a customer, +So I have an emergency fund, +I want to be able to request an overdraft on my account. + +As a bank manager, +So I can safeguard our funds, +I want to approve or reject overdraft requests. + +As a customer, +So I can stay up to date, +I want statements to be sent as messages to my phone. +``` diff --git a/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java new file mode 100644 index 000000000..0f46e5931 --- /dev/null +++ b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java @@ -0,0 +1,47 @@ +package com.booleanuk.core.idgenerator; + +public class IdGenerator { + + // Resource: https://www.freecodecamp.org/news/static-variables-in-java/ + private int currentAC = 0; + private int currentRE = 0; + private int currentTR = 0; + + private IdPrefix prefix; + private String id; + + public IdGenerator() { + // Reset + currentAC = 0; + currentRE = 0; + currentTR = 0; + } + + private void setId(int currentNumber) { + this.id = prefix + "_" + currentNumber; + } + + public String getId() { + return this.id; + } + + public String createId(IdPrefix prefix) { + + // TODO: Duplication, could be improved? + + this.prefix = prefix; + + if (prefix == IdPrefix.AC) { + currentAC++; + setId(currentAC); + } else if (prefix == IdPrefix.RE) { + currentRE++; + setId(currentRE); + } else if (prefix == IdPrefix.TR) { + currentTR++; + setId(currentTR); + } + + return this.id; + } +} diff --git a/src/main/java/com/booleanuk/core/idgenerator/IdPrefix.java b/src/main/java/com/booleanuk/core/idgenerator/IdPrefix.java new file mode 100644 index 000000000..5f9cbdc21 --- /dev/null +++ b/src/main/java/com/booleanuk/core/idgenerator/IdPrefix.java @@ -0,0 +1,13 @@ +package com.booleanuk.core.idgenerator; + +/** + * Prefix for: + * Requests (RE), + * Accounts (AC), + * Transactions (TR) + */ +public enum IdPrefix { + RE, + AC, + TR, +} diff --git a/src/main/java/com/booleanuk/core/transactons/Transaction.java b/src/main/java/com/booleanuk/core/transactons/Transaction.java new file mode 100644 index 000000000..189c1dbce --- /dev/null +++ b/src/main/java/com/booleanuk/core/transactons/Transaction.java @@ -0,0 +1,43 @@ +package com.booleanuk.core.transactons; + +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; + +import java.time.LocalDateTime; + +public class Transaction { + + private String id; + private LocalDateTime transactionDate; + private Double amount; + private TransactionType type; + private Double currentBalance; + + public Transaction(String id, Double amount, TransactionType type, Double currentBalance) { + this.id = id; + this.transactionDate = LocalDateTime.now(); // TODO: Format later + this.amount = amount; + this.type = type; + this.currentBalance = currentBalance; + } + + public String getId() { + return id; + } + + public LocalDateTime getTransactionDate() { + return transactionDate; + } + + public Double getAmount() { + return amount; + } + + public TransactionType getType() { + return type; + } + + public Double getCurrentBalance() { + return currentBalance; + } +} diff --git a/src/main/java/com/booleanuk/core/transactons/TransactionType.java b/src/main/java/com/booleanuk/core/transactons/TransactionType.java new file mode 100644 index 000000000..3c5f100c1 --- /dev/null +++ b/src/main/java/com/booleanuk/core/transactons/TransactionType.java @@ -0,0 +1,6 @@ +package com.booleanuk.core.transactons; + +public enum TransactionType { + DEBIT, + CREDIT +} diff --git a/src/test/java/com/booleanuk/core/accounts/AccountTest.java b/src/test/java/com/booleanuk/core/accounts/AccountTest.java new file mode 100644 index 000000000..596f4e1a2 --- /dev/null +++ b/src/test/java/com/booleanuk/core/accounts/AccountTest.java @@ -0,0 +1,60 @@ +package com.booleanuk.core.accounts; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class AccountTest { + + @Test + public void createCurrentAccount() { + Account currentAccount = new CurrentAccount("test_1"); + Assertions.assertEquals("test_1", currentAccount.getAccountNumber()); + Assertions.assertEquals(Branch.BRANCH_1, currentAccount.getBranch()); + + Account savingsAccount = new CurrentAccount("test_2"); + Assertions.assertEquals("test_2", savingsAccount.getAccountNumber()); + Assertions.assertEquals(Branch.BRANCH_1, savingsAccount.getBranch()); + } + + @Test + public void checkOverdraftFacility() { + Account currentAccount = new CurrentAccount("test_1"); + Assertions.assertTrue(currentAccount.isPossibleToOverdraft()); + currentAccount.setPossibleToOverdraft(false); + Assertions.assertFalse(currentAccount.isPossibleToOverdraft()); + + Account savingsAccount = new SavingsAccount("test_2"); + Assertions.assertFalse(savingsAccount.isPossibleToOverdraft()); + savingsAccount.setPossibleToOverdraft(true); + Assertions.assertTrue(savingsAccount.isPossibleToOverdraft()); + } + + @Test + public void depositMoney() { + Account currentAccount = new CurrentAccount("test_1"); + currentAccount.deposit(1000.00); + Assertions.assertEquals(1000.00, currentAccount.getBalance()); + + Account savingtAccount = new SavingsAccount("test_2"); + savingtAccount.deposit(1000.00); + Assertions.assertEquals(1000.00, savingtAccount.getBalance()); + } + + @Test + public void withdrawMoney() { + Account currentAccount = new CurrentAccount("test_1"); + currentAccount.withdraw(1000.00); + Assertions.assertEquals(-1000.00, currentAccount.getBalance()); + + Account savingtAccount = new SavingsAccount("test_2"); + // Inner validate function should throw error + Exception e = Assertions.assertThrows( + Exception.class, + () -> { savingtAccount.validateOverdraft(savingtAccount.getBalance(), 1000.00); } + ); + Assertions.assertEquals("Not possible to overdraft", e.getMessage()); + + // Outer function .withdraw() should handle error + Assertions.assertDoesNotThrow(() -> savingtAccount.withdraw(1000.0)); + } +} diff --git a/src/test/java/com/booleanuk/core/bank/BankTest.java b/src/test/java/com/booleanuk/core/bank/BankTest.java new file mode 100644 index 000000000..266995672 --- /dev/null +++ b/src/test/java/com/booleanuk/core/bank/BankTest.java @@ -0,0 +1,24 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.AccountType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BankTest { + + @Test + public void makeRequest() { + Bank bank = new Bank(); + User customer = bank.createUser("P_#1"); + + bank.createUserAccount(customer, AccountType.CURRENT); + Account account = customer.getAllAccounts().get(0); + + bank.makeRequest(account, 100.00); + Assertions.assertEquals(1, bank.getAllRequests().size()); + } + + // TODO: if time, test remove() or add wrapper function called delete + +} diff --git a/src/test/java/com/booleanuk/core/bank/RequestTest.java b/src/test/java/com/booleanuk/core/bank/RequestTest.java new file mode 100644 index 000000000..b65c904a4 --- /dev/null +++ b/src/test/java/com/booleanuk/core/bank/RequestTest.java @@ -0,0 +1,39 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.AccountType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class RequestTest { + + @Test + public void createRequest() { + + // TODO: same as in BankTest, unsure about the right way to test + // when the structure is like this. + + Bank bank = new Bank(); + User customer = bank.createUser("P_#1"); + + bank.createUserAccount(customer, AccountType.CURRENT); + Account account = customer.getAllAccounts().get(0); + + bank.makeRequest(account, 100.00); + Assertions.assertEquals(1, bank.getAllRequests().size()); + } + + @Test + public void changeOverdraftFacility() { + Bank bank = new Bank(); + User customer = bank.createUser("P_#1"); + bank.createUserAccount(customer, AccountType.SAVINGS); + Account account = customer.getAllAccounts().get(0); + bank.makeRequest(account, 100.00); + + Request request = bank.getAllRequests().get(0); + Assertions.assertFalse(account.isPossibleToOverdraft()); + request.approve(); + Assertions.assertTrue(account.isPossibleToOverdraft()); + } +} diff --git a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java new file mode 100644 index 000000000..67dda44a2 --- /dev/null +++ b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java @@ -0,0 +1,57 @@ +package com.booleanuk.core.idgenerator; + +import com.booleanuk.core.bank.Bank; +import com.booleanuk.core.bank.User; +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.AccountType; +import com.booleanuk.core.transactons.Transaction; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class IdGeneratorTest { + + @Test + public void generateMultipleUniqueIds() { + Bank bank = new Bank(); + + User customer1 = bank.createUser("P_#1"); + bank.createUserAccount(customer1, AccountType.CURRENT); + bank.createUserAccount(customer1, AccountType.SAVINGS); + + ArrayList accountss1 = customer1.getAllAccounts(); + for (Account a : accountss1) { + System.out.println(a.getAccountNumber()); + } + + Assertions.assertEquals("AC_1", accountss1.get(0).getAccountNumber()); + Assertions.assertEquals("AC_2", accountss1.get(1).getAccountNumber()); + + User customer2 = bank.createUser("P_#1"); + bank.createUserAccount(customer2, AccountType.CURRENT); + bank.createUserAccount(customer2, AccountType.SAVINGS); + + Assertions.assertEquals("AC_3", customer2.getAllAccounts().get(0).getAccountNumber()); + Assertions.assertEquals("AC_4", customer2.getAllAccounts().get(1).getAccountNumber()); + } + + @Test + public void generateMultipleUniqueIdsForAnotherBankeShouldHaveOwnIds() { + Bank bank = new Bank(); + + User customer1 = bank.createUser("P_#1"); + bank.createUserAccount(customer1, AccountType.CURRENT); + bank.createUserAccount(customer1, AccountType.SAVINGS); + + Assertions.assertEquals("AC_1", customer1.getAllAccounts().get(0).getAccountNumber()); + Assertions.assertEquals("AC_2", customer1.getAllAccounts().get(1).getAccountNumber()); + + User customer2 = bank.createUser("P_#1"); + bank.createUserAccount(customer2, AccountType.CURRENT); + bank.createUserAccount(customer2, AccountType.SAVINGS); + + Assertions.assertEquals("AC_3", customer2.getAllAccounts().get(0).getAccountNumber()); + Assertions.assertEquals("AC_4", customer2.getAllAccounts().get(1).getAccountNumber()); + } +} diff --git a/src/test/java/com/booleanuk/core/transactions/TransactionTest.java b/src/test/java/com/booleanuk/core/transactions/TransactionTest.java new file mode 100644 index 000000000..0ae687e51 --- /dev/null +++ b/src/test/java/com/booleanuk/core/transactions/TransactionTest.java @@ -0,0 +1,57 @@ +package com.booleanuk.core.transactions; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.AccountType; +import com.booleanuk.core.bank.Bank; +import com.booleanuk.core.bank.User; +import com.booleanuk.core.transactons.Transaction; +import com.booleanuk.core.transactons.TransactionType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class TransactionTest { + + @Test + public void createTransactionReturnsCorrectId() { + Bank bank = new Bank(); + User customer1 = bank.createUser("P_#2"); + bank.createUserAccount(customer1, AccountType.CURRENT); + Account account = customer1.getAllAccounts().get(0); + + account.deposit(1000.0); + account.withdraw(200.00); + account.withdraw(200.00); + + ArrayList transactions = account.getAllTransactions(); + + Assertions.assertEquals("TR_1", transactions.get(0).getId()); + Assertions.assertEquals("TR_2", transactions.get(1).getId()); + Assertions.assertEquals("TR_3", transactions.get(2).getId()); + } + + @Test + public void getAmountTypeAndCurrentBalance() { + Bank bank = new Bank(); + User customer1 = bank.createUser("P_#2"); + bank.createUserAccount(customer1, AccountType.CURRENT); + Account account = customer1.getAllAccounts().get(0); + account.deposit(10000.00); + account.withdraw(5000.00); + + ArrayList transactions = account.getAllTransactions(); + Transaction transaction1 = transactions.get(0); + Transaction transaction2 = transactions.get(1); + + Assertions.assertEquals(10000.00, transaction1.getAmount()); + Assertions.assertEquals(TransactionType.DEBIT, transaction1.getType()); + Assertions.assertEquals(0.00, transaction1.getCurrentBalance()); + + Assertions.assertEquals(5000.00, transaction2.getAmount()); + Assertions.assertEquals(TransactionType.CREDIT, transaction2.getType()); + Assertions.assertEquals(10000.00, transaction2.getCurrentBalance()); + + Assertions.assertEquals(5000.00, account.getBalance()); + } +}