From e8d9365a76f42e90ecbf659a3b631c87d37295a2 Mon Sep 17 00:00:00 2001 From: shyye Date: Tue, 27 Aug 2024 10:24:51 +0200 Subject: [PATCH 01/22] Add domain model --- .../java/com/booleanuk/core/domain-model.md | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/main/java/com/booleanuk/core/domain-model.md 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..95e687efd --- /dev/null +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -0,0 +1,108 @@ +# Domain Model + + +## Table +| Classes | Variables | Methods | Scenario | Output | +|----------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|-------------------------| +| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | +| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| | `-Branch branch` | `+getBranch()` | | | +| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | +| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | +| | `-Map transactions` | | | | +| | | `-calculateBalance()` | | Double | +| | | `+getBalance()` | | | +| | | `+deposit(Double sum)` | | void / throw exception | +| | | `+withdraw(Double sum)` | | void / throw exception | +| | | `+printTransctionHistory()` | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| enum `Branch` | `BRANCH_1, BRANCH_2, BRANCH_3` | | | | +| | | | | | +| `Transaction(Double amount, TransactionTYpe type)` | `-String id` | `+getId()` | | String | +| | `-LocalDate transactionDate` | `+getTransactionDate()` | | String | +| | `-TransactionType type` | `+getTransactionType()` | | TransactionType | +| | `-Double currentBalance` | `+getCurrentBalance()` | Get the balance the account had when this transaction was made. | Double | +| | | `+setCurrentBalance()` | | | +| | | | | | +| enum `TransactionType` | `DEBIT, CREDIT` | | | | +| | | | | | +| abstract or interface `IdGenerator` | | | | | +| `IdGeneratorRequest` extends `IdGenerator` | | | | | +| `IdGeneratorAccountNumber` extends `IdGenerator` | | | | | +| `IdGeneratorTransaction` extends `IdGenerator` | | | | | + +## 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. +``` From 6cdb69aa171d17122b1a472ad69ab5b803c2ba9d Mon Sep 17 00:00:00 2001 From: shyye Date: Tue, 27 Aug 2024 11:27:06 +0200 Subject: [PATCH 02/22] Add Test and code for IdGenerator, update domain model --- .../java/com/booleanuk/core/domain-model.md | 90 ++++++++++--------- .../core/idgenerator/IdGenerator.java | 38 ++++++++ .../booleanuk/core/idgenerator/IdPrefix.java | 13 +++ .../com/booleanuk/core/IdGeneratorTest.java | 28 ++++++ 4 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java create mode 100644 src/main/java/com/booleanuk/core/idgenerator/IdPrefix.java create mode 100644 src/test/java/com/booleanuk/core/IdGeneratorTest.java diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index 95e687efd..4f1890ac4 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -2,50 +2,52 @@ ## Table -| Classes | Variables | Methods | Scenario | Output | -|----------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|-------------------------| -| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | -| | `-Double overdraftSum` | `+getOverdraftSum()` | | | -| | | `+approve()` | Update account with facility to make overdrafts. | | -| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | -| | | | | | -| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | -| | `-Branch branch` | `+getBranch()` | | | -| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | -| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | -| | `-Map transactions` | | | | -| | | `-calculateBalance()` | | Double | -| | | `+getBalance()` | | | -| | | `+deposit(Double sum)` | | void / throw exception | -| | | `+withdraw(Double sum)` | | void / throw exception | -| | | `+printTransctionHistory()` | | | -| `CurrentAccount` extends `Account` | | | | | -| | | | | | -| `SavingAccount` extends `Account` | `-Double interestRate` | | | | -| | | | | | -| enum `Branch` | `BRANCH_1, BRANCH_2, BRANCH_3` | | | | -| | | | | | -| `Transaction(Double amount, TransactionTYpe type)` | `-String id` | `+getId()` | | String | -| | `-LocalDate transactionDate` | `+getTransactionDate()` | | String | -| | `-TransactionType type` | `+getTransactionType()` | | TransactionType | -| | `-Double currentBalance` | `+getCurrentBalance()` | Get the balance the account had when this transaction was made. | Double | -| | | `+setCurrentBalance()` | | | -| | | | | | -| enum `TransactionType` | `DEBIT, CREDIT` | | | | -| | | | | | -| abstract or interface `IdGenerator` | | | | | -| `IdGeneratorRequest` extends `IdGenerator` | | | | | -| `IdGeneratorAccountNumber` extends `IdGenerator` | | | | | -| `IdGeneratorTransaction` extends `IdGenerator` | | | | | +| Classes | Variables | Methods | Scenario | Output | +|----------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| +| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | +| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| | `-Branch branch` | `+getBranch()` | | | +| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | +| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | +| | `-Map transactions` | | | | +| | | `-calculateBalance()` | | Double | +| | | `+getBalance()` | | | +| | | `+deposit(Double sum)` | | void / throw exception | +| | | `+withdraw(Double sum)` | | void / throw exception | +| | | `+printTransctionHistory()` | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| enum `Branch` | `BRANCH_1, BRANCH_2, BRANCH_3` | | | | +| | | | | | +| `Transaction(Double amount, TransactionTYpe type)` | `-String id` | `+getId()` | | String | +| | `-LocalDate transactionDate` | `+getTransactionDate()` | | String | +| | `-TransactionType type` | `+getTransactionType()` | | TransactionType | +| | `-Double currentBalance` | `+getCurrentBalance()` | Get the balance the account had when this transaction was made. | Double | +| | | `+setCurrentBalance()` | | | +| | | | | | +| 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 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..15b726ce5 --- /dev/null +++ b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java @@ -0,0 +1,38 @@ +package com.booleanuk.core.idgenerator; + +public class IdGenerator { + + // Resource: https://www.freecodecamp.org/news/static-variables-in-java/ + private static int currentAC = 0; + private static int currentRE = 0; + private static int currentTR = 0; + + private final IdPrefix prefix; + private String id; + + public IdGenerator(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); + } + } + + private void setId(int currentNumber) { + this.id = prefix + "_" + currentNumber; + } + + public String getId() { + 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/test/java/com/booleanuk/core/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/IdGeneratorTest.java new file mode 100644 index 000000000..c6c9cb82c --- /dev/null +++ b/src/test/java/com/booleanuk/core/IdGeneratorTest.java @@ -0,0 +1,28 @@ +package com.booleanuk.core; + +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class IdGeneratorTest { + + @Test + public void generateMultipleUniqueIds() { + Assertions.assertEquals("AC_1", new IdGenerator(IdPrefix.AC).getId()); + Assertions.assertEquals("AC_2", new IdGenerator(IdPrefix.AC).getId()); + Assertions.assertEquals("AC_3", new IdGenerator(IdPrefix.AC).getId()); + + Assertions.assertEquals("RE_1", new IdGenerator(IdPrefix.RE).getId()); + Assertions.assertEquals("RE_2", new IdGenerator(IdPrefix.RE).getId()); + Assertions.assertEquals("RE_3", new IdGenerator(IdPrefix.RE).getId()); + + Assertions.assertEquals("TR_1", new IdGenerator(IdPrefix.TR).getId()); + Assertions.assertEquals("TR_2", new IdGenerator(IdPrefix.TR).getId()); + Assertions.assertEquals("TR_3", new IdGenerator(IdPrefix.TR).getId()); + + Assertions.assertEquals("AC_4", new IdGenerator(IdPrefix.AC).getId()); + Assertions.assertEquals("RE_4", new IdGenerator(IdPrefix.RE).getId()); + Assertions.assertEquals("TR_4", new IdGenerator(IdPrefix.TR).getId()); + } +} From cf40de64b4211be8ebbef0582ebaac13128ad62d Mon Sep 17 00:00:00 2001 From: shyye Date: Tue, 27 Aug 2024 17:40:05 +0200 Subject: [PATCH 03/22] Add Test and code for Transaction, update domain model --- .../java/com/booleanuk/core/domain-model.md | 92 +++++++++---------- .../core/transactons/Transaction.java | 43 +++++++++ .../core/transactons/TransactionType.java | 6 ++ .../{ => idgenerator}/IdGeneratorTest.java | 4 +- .../core/transactions/TransactionTest.java | 37 ++++++++ 5 files changed, 133 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/transactons/Transaction.java create mode 100644 src/main/java/com/booleanuk/core/transactons/TransactionType.java rename src/test/java/com/booleanuk/core/{ => idgenerator}/IdGeneratorTest.java (89%) create mode 100644 src/test/java/com/booleanuk/core/transactions/TransactionTest.java diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index 4f1890ac4..aff5e2bed 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -2,52 +2,52 @@ ## Table -| Classes | Variables | Methods | Scenario | Output | -|----------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| -| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | -| | `-Double overdraftSum` | `+getOverdraftSum()` | | | -| | | `+approve()` | Update account with facility to make overdrafts. | | -| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | -| | | | | | -| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | -| | `-Branch branch` | `+getBranch()` | | | -| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | -| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | -| | `-Map transactions` | | | | -| | | `-calculateBalance()` | | Double | -| | | `+getBalance()` | | | -| | | `+deposit(Double sum)` | | void / throw exception | -| | | `+withdraw(Double sum)` | | void / throw exception | -| | | `+printTransctionHistory()` | | | -| `CurrentAccount` extends `Account` | | | | | -| | | | | | -| `SavingAccount` extends `Account` | `-Double interestRate` | | | | -| | | | | | -| enum `Branch` | `BRANCH_1, BRANCH_2, BRANCH_3` | | | | -| | | | | | -| `Transaction(Double amount, TransactionTYpe type)` | `-String id` | `+getId()` | | String | -| | `-LocalDate transactionDate` | `+getTransactionDate()` | | String | -| | `-TransactionType type` | `+getTransactionType()` | | TransactionType | -| | `-Double currentBalance` | `+getCurrentBalance()` | Get the balance the account had when this transaction was made. | Double | -| | | `+setCurrentBalance()` | | | -| | | | | | -| 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 | +| Classes | Variables | Methods | Scenario | Output | +|--------------------------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| +| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | +| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| | `-Branch branch` | `+getBranch()` | | | +| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | +| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | +| | `-Map transactions` | | | | +| | | `-calculateBalance()` | | Double | +| | | `+getBalance()` | | | +| | | `+deposit(Double sum)` | | void / throw exception | +| | | `+withdraw(Double sum)` | | void / throw exception | +| | | `+printTransctionHistory()` | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| 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 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..d2e84d1fb --- /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(Double amount, TransactionType type, Double currentBalance) { + this.id = new IdGenerator(IdPrefix.TR).getId(); + 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/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java similarity index 89% rename from src/test/java/com/booleanuk/core/IdGeneratorTest.java rename to src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java index c6c9cb82c..5b6f731d1 100644 --- a/src/test/java/com/booleanuk/core/IdGeneratorTest.java +++ b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java @@ -1,7 +1,5 @@ -package com.booleanuk.core; +package com.booleanuk.core.idgenerator; -import com.booleanuk.core.idgenerator.IdGenerator; -import com.booleanuk.core.idgenerator.IdPrefix; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; 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..b53634fa7 --- /dev/null +++ b/src/test/java/com/booleanuk/core/transactions/TransactionTest.java @@ -0,0 +1,37 @@ +package com.booleanuk.core.transactions; + +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 javax.swing.text.DateFormatter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class TransactionTest { + + @Test + public void createTransactionReturnsCorrectId() { + Assertions.assertEquals("TR_1", new Transaction(2000.0, TransactionType.CREDIT, 3000.0).getId()); + Assertions.assertEquals("TR_2", new Transaction(2000.0, TransactionType.CREDIT, 3000.0).getId()); + } + + @Test + public void getDateAndTime() { + LocalDateTime localDateTime = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm"); + + Assertions.assertEquals( + localDateTime.format(formatter), + new Transaction(10.0, TransactionType.CREDIT, 1000.0).getTransactionDate().format(formatter)); + } + + @Test + public void getAmountTypeAndCurrentBalance() { + Transaction transaction = new Transaction(2000.00, TransactionType.CREDIT, 3000.00); + Assertions.assertEquals(2000.00, transaction.getAmount()); + Assertions.assertEquals(TransactionType.CREDIT, transaction.getType()); + Assertions.assertEquals(3000.00, transaction.getCurrentBalance()); + } +} From 40a173d2d82bbb157713eab1eb86dd6d8dafcae2 Mon Sep 17 00:00:00 2001 From: shyye Date: Tue, 27 Aug 2024 18:21:17 +0200 Subject: [PATCH 04/22] Add Test and code for Account with CurrentAccount and SavingsAccount, update domain model --- .../com/booleanuk/core/accounts/Account.java | 35 +++++++++++++++++++ .../com/booleanuk/core/accounts/Branch.java | 7 ++++ .../core/accounts/CurrentAccount.java | 9 +++++ .../core/accounts/SavingsAccount.java | 9 +++++ .../java/com/booleanuk/core/domain-model.md | 5 +-- .../booleanuk/core/accounts/AccountTest.java | 31 ++++++++++++++++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/accounts/Account.java create mode 100644 src/main/java/com/booleanuk/core/accounts/Branch.java create mode 100644 src/main/java/com/booleanuk/core/accounts/CurrentAccount.java create mode 100644 src/main/java/com/booleanuk/core/accounts/SavingsAccount.java create mode 100644 src/test/java/com/booleanuk/core/accounts/AccountTest.java 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..ff02c295d --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/Account.java @@ -0,0 +1,35 @@ +package com.booleanuk.core.accounts; + +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; + +public abstract class Account { + + private final String accountNumber; + private Branch branch; + boolean isPossibleToOverdraft; + + public Account() { + this.accountNumber = new IdGenerator(IdPrefix.AC).getId(); + } + + 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; + } +} 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..a3259ff83 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java @@ -0,0 +1,9 @@ +package com.booleanuk.core.accounts; + +public class CurrentAccount extends Account{ + + public CurrentAccount() { + 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..0b9dac939 --- /dev/null +++ b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java @@ -0,0 +1,9 @@ +package com.booleanuk.core.accounts; + +public class SavingsAccount extends Account{ + + public SavingsAccount() { + this.setBranch(Branch.BRANCH_1); + this.setPossibleToOverdraft(false); + } +} diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index aff5e2bed..bea15e70e 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -18,10 +18,11 @@ | | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | | | `-Branch branch` | `+getBranch()` | | | -| | `-boolean isPossibleToOvedraft` | `+getOverdraftFacility()` | | boolean | -| | | `+setOverdraftFacility(boolean isPossibleToOvedraft)` | | | +| | `-boolean isPossibleToOvedraft` | `+isPossibleToOverdraft()` | | boolean | +| | | `+setPossibleToOverdraft(boolean isPossibleToOvedraft)` | | | | | `-Map transactions` | | | | | | | `-calculateBalance()` | | Double | | | | `+getBalance()` | | | 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..7fbdf0ccc --- /dev/null +++ b/src/test/java/com/booleanuk/core/accounts/AccountTest.java @@ -0,0 +1,31 @@ +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(); + Assertions.assertEquals("AC_1", currentAccount.getAccountNumber()); + Assertions.assertEquals(Branch.BRANCH_1, currentAccount.getBranch()); + + Account savingsAccount = new CurrentAccount(); + Assertions.assertEquals("AC_2", savingsAccount.getAccountNumber()); + Assertions.assertEquals(Branch.BRANCH_1, savingsAccount.getBranch()); + } + + @Test + public void checkOverdraftFacility() { + Account currentAccount = new CurrentAccount(); + Assertions.assertTrue(currentAccount.isPossibleToOverdraft()); + currentAccount.setPossibleToOverdraft(false); + Assertions.assertFalse(currentAccount.isPossibleToOverdraft()); + + Account savingsAccount = new SavingsAccount(); + Assertions.assertFalse(savingsAccount.isPossibleToOverdraft()); + savingsAccount.setPossibleToOverdraft(true); + Assertions.assertTrue(savingsAccount.isPossibleToOverdraft()); + } +} From 15ffc41ce42f0ef8978c5185639979a429383a86 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 06:46:59 +0200 Subject: [PATCH 05/22] Add Test and code for withdraw() and deposit() --- .../com/booleanuk/core/accounts/Account.java | 64 ++++++++++++- .../java/com/booleanuk/core/domain-model.md | 93 ++++++++++--------- .../booleanuk/core/accounts/AccountTest.java | 29 ++++++ 3 files changed, 139 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/booleanuk/core/accounts/Account.java b/src/main/java/com/booleanuk/core/accounts/Account.java index ff02c295d..80dc244c6 100644 --- a/src/main/java/com/booleanuk/core/accounts/Account.java +++ b/src/main/java/com/booleanuk/core/accounts/Account.java @@ -2,15 +2,22 @@ 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.HashMap; +import java.util.Map; public abstract class Account { private final String accountNumber; private Branch branch; - boolean isPossibleToOverdraft; + private boolean isPossibleToOverdraft; + private Map transactions; public Account() { this.accountNumber = new IdGenerator(IdPrefix.AC).getId(); + this.transactions = new HashMap<>(); } public String getAccountNumber() { @@ -32,4 +39,59 @@ public boolean 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( + 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( + amount, + TransactionType.CREDIT, + currentBalance + ); + this.addTransaction(transaction); + } + + 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(); + } } diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index bea15e70e..53eea70a2 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -2,53 +2,54 @@ ## Table -| Classes | Variables | Methods | Scenario | Output | -|--------------------------------------------------------------------|--------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| -| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | -| | `-Double overdraftSum` | `+getOverdraftSum()` | | | -| | | `+approve()` | Update account with facility to make overdrafts. | | -| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | -| | | | | | -| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | -| | `-Branch branch` | `+getBranch()` | | | -| | `-boolean isPossibleToOvedraft` | `+isPossibleToOverdraft()` | | boolean | +| Classes | Variables | Methods | Scenario | Output | +|--------------------------------------------------------------------|--------------------------------------------------------|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| +| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | +| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| | `-Branch branch` | `+getBranch()` | | | +| | `-boolean isPossibleToOvedraft` | `+isPossibleToOverdraft()` | | boolean | | | | `+setPossibleToOverdraft(boolean isPossibleToOvedraft)` | | | -| | `-Map transactions` | | | | -| | | `-calculateBalance()` | | Double | -| | | `+getBalance()` | | | -| | | `+deposit(Double sum)` | | void / throw exception | -| | | `+withdraw(Double sum)` | | void / throw exception | -| | | `+printTransctionHistory()` | | | -| `CurrentAccount` extends `Account` | | | | | -| | | | | | -| `SavingAccount` extends `Account` | `-Double interestRate` | | | | -| | | | | | -| 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 | +| | `-Map transactions` | | | | +| | | `-addTransaction(Transaction transaction)` | | | +| | | `-calculateBalance()` | | Double | +| | | `+getBalance()` | | | +| | | `+deposit(Double sum)` | | void / throw exception | +| | | `+withdraw(Double sum)` | | void / throw exception | +| | | `+printTransctionHistory()` | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| 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 diff --git a/src/test/java/com/booleanuk/core/accounts/AccountTest.java b/src/test/java/com/booleanuk/core/accounts/AccountTest.java index 7fbdf0ccc..370dfd903 100644 --- a/src/test/java/com/booleanuk/core/accounts/AccountTest.java +++ b/src/test/java/com/booleanuk/core/accounts/AccountTest.java @@ -28,4 +28,33 @@ public void checkOverdraftFacility() { savingsAccount.setPossibleToOverdraft(true); Assertions.assertTrue(savingsAccount.isPossibleToOverdraft()); } + + @Test + public void depositMoney() { + Account currentAccount = new CurrentAccount(); + currentAccount.deposit(1000.00); + Assertions.assertEquals(1000.00, currentAccount.getBalance()); + + Account savingtAccount = new SavingsAccount(); + savingtAccount.deposit(1000.00); + Assertions.assertEquals(1000.00, savingtAccount.getBalance()); + } + + @Test + public void withdrawMoney() { + Account currentAccount = new CurrentAccount(); + currentAccount.withdraw(1000.00); + Assertions.assertEquals(-1000.00, currentAccount.getBalance()); + + Account savingtAccount = new SavingsAccount(); + // 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)); + } } From 1281c1f4597176e6835de35b2116685803cbeb16 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 06:53:22 +0200 Subject: [PATCH 06/22] Update domain model for Account --- .../com/booleanuk/core/accounts/Account.java | 2 + .../java/com/booleanuk/core/domain-model.md | 98 ++++++++++--------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/booleanuk/core/accounts/Account.java b/src/main/java/com/booleanuk/core/accounts/Account.java index 80dc244c6..01c7e804e 100644 --- a/src/main/java/com/booleanuk/core/accounts/Account.java +++ b/src/main/java/com/booleanuk/core/accounts/Account.java @@ -94,4 +94,6 @@ protected Double calculateBalance() { public Double getBalance() { return calculateBalance(); } + + // TODO: printTransactionHistory } diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index 53eea70a2..dba060611 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -2,54 +2,56 @@ ## Table -| Classes | Variables | Methods | Scenario | Output | -|--------------------------------------------------------------------|--------------------------------------------------------|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| -| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | -| | `-Double overdraftSum` | `+getOverdraftSum()` | | | -| | | `+approve()` | Update account with facility to make overdrafts. | | -| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | -| | | | | | -| `Customer(String personalNumber)` | `-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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | -| | `-Branch branch` | `+getBranch()` | | | -| | `-boolean isPossibleToOvedraft` | `+isPossibleToOverdraft()` | | boolean | -| | | `+setPossibleToOverdraft(boolean isPossibleToOvedraft)` | | | -| | `-Map transactions` | | | | -| | | `-addTransaction(Transaction transaction)` | | | -| | | `-calculateBalance()` | | Double | -| | | `+getBalance()` | | | -| | | `+deposit(Double sum)` | | void / throw exception | -| | | `+withdraw(Double sum)` | | void / throw exception | -| | | `+printTransctionHistory()` | | | -| `CurrentAccount` extends `Account` | | | | | -| | | | | | -| `SavingAccount` extends `Account` | `-Double interestRate` | | | | -| | | | | | -| 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 | +| Classes | Variables | Methods | Scenario | Output | +|--------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| +| `Bank` | `-Map requests` | `-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 accountNumber` | `+getAccountNumber()` | | String | +| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | | `+approve()` | Update account with facility to make overdrafts. | | +| | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | +| | | | | | +| `Customer(String personalNumber)` | `-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` | `-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()` | | | +| | | `+printTransctionHistory()` TODO: not implemented | | | +| `CurrentAccount` extends `Account` | | | | | +| | | | | | +| `SavingAccount` extends `Account` | `-Double interestRate` | | | | +| | | | | | +| 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 From 7a0d6f300fcd499b542e07f8dfb47696154249fc Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 06:55:28 +0200 Subject: [PATCH 07/22] Add interestRate variable to SavingsAccount --- src/main/java/com/booleanuk/core/accounts/SavingsAccount.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java index 0b9dac939..430d481df 100644 --- a/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java +++ b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java @@ -2,8 +2,11 @@ public class SavingsAccount extends Account{ + private Double interestRate; + public SavingsAccount() { this.setBranch(Branch.BRANCH_1); this.setPossibleToOverdraft(false); + this.interestRate = 0.12; } } From 8043aad0796f8dda6a13d8a19b282bd324c7e41a Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 07:19:11 +0200 Subject: [PATCH 08/22] Add Test and Code for Request, overdrafts, update domain model --- src/main/java/com/booleanuk/core/Request.java | 34 +++++++++++++++++++ .../java/com/booleanuk/core/domain-model.md | 6 ++-- .../java/com/booleanuk/core/RequestTest.java | 30 ++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/Request.java create mode 100644 src/test/java/com/booleanuk/core/RequestTest.java diff --git a/src/main/java/com/booleanuk/core/Request.java b/src/main/java/com/booleanuk/core/Request.java new file mode 100644 index 000000000..aa0b0f3f3 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Request.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.idgenerator.IdGenerator; +import com.booleanuk.core.idgenerator.IdPrefix; + +public class Request { + + private final String id; + private final Account account; + private Double overdraftSum; + + public Request(Account account, Double overdraftSum) { + this.id = new IdGenerator(IdPrefix.RE).getId(); + 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/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index dba060611..9cee84361 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -9,12 +9,12 @@ | | | `+makeRequest(Request request)` | Make a request as a user. | Request object / null | | | | | | | | `Request(Account account, Double overdraftSum)` | `-String id` | `+getId()` | | String | -| | `-String accountNumber` | `+getAccountNumber()` | | String | -| | `-Double overdraftSum` | `+getOverdraftSum()` | | | +| | `-String account` | | | String | +| | `-Double overdraftSum` | | | | | | | `+approve()` | Update account with facility to make overdrafts. | | | | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | | | | | | | -| `Customer(String personalNumber)` | `-String personalNumber` | | | | +| `Customer(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 | diff --git a/src/test/java/com/booleanuk/core/RequestTest.java b/src/test/java/com/booleanuk/core/RequestTest.java new file mode 100644 index 000000000..26d451850 --- /dev/null +++ b/src/test/java/com/booleanuk/core/RequestTest.java @@ -0,0 +1,30 @@ +package com.booleanuk.core; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.SavingsAccount; +import com.booleanuk.core.transactons.Transaction; +import com.booleanuk.core.transactons.TransactionType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class RequestTest { + + @Test + public void createRequest() { + Account account = new SavingsAccount(); + Double overdraftSum = 1000.0; + Assertions.assertEquals("RE_1", new Request(account, overdraftSum).getId()); + Assertions.assertEquals("RE_2", new Request(account, overdraftSum).getId()); + } + + @Test + public void changeOverdraftFacility() { + Account account = new SavingsAccount(); + Assertions.assertFalse(account.isPossibleToOverdraft()); + + Request request = new Request(account, 1000.00); + request.approve(); + + Assertions.assertTrue(account.isPossibleToOverdraft()); + } +} From ac498d9e8768d359e02a5a0b99ee9e1c5e023fd7 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 07:41:30 +0200 Subject: [PATCH 09/22] Add Test and Code for Bank, makeRequest, update domain model --- src/main/java/com/booleanuk/core/Bank.java | 29 +++++++++++++++++++ .../java/com/booleanuk/core/domain-model.md | 3 +- .../java/com/booleanuk/core/BankTest.java | 23 +++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/booleanuk/core/Bank.java create mode 100644 src/test/java/com/booleanuk/core/BankTest.java 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..1bf14eca0 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bank.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Bank { + + private Map requests; + + public Bank() { + this.requests = new HashMap<>(); + } + + public Map getAllRequests() { + return this.requests; + } + + private void addRequest(Request request) { + this.requests.put(request.getId(), request); + } + + public void makeRequest(Request request) { + this.addRequest(request); + } + + private void removeRequest(String requestId) { + this.requests.remove(requestId); + } +} diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index 9cee84361..59c9b6e7e 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -4,7 +4,8 @@ ## Table | Classes | Variables | Methods | Scenario | Output | |--------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| -| `Bank` | `-Map requests` | `-addRequest(Request request)` | Validate, add to the list requests if possible. | void / throw exception | +| `Bank` | `-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 | | | | | | | diff --git a/src/test/java/com/booleanuk/core/BankTest.java b/src/test/java/com/booleanuk/core/BankTest.java new file mode 100644 index 000000000..e548612dd --- /dev/null +++ b/src/test/java/com/booleanuk/core/BankTest.java @@ -0,0 +1,23 @@ +package com.booleanuk.core; + +import com.booleanuk.core.accounts.Account; +import com.booleanuk.core.accounts.CurrentAccount; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BankTest { + + @Test + public void makeRequest() { + Bank bank = new Bank(); + + Account account = new CurrentAccount(); + Request request = new Request(account, 100.00); + + bank.makeRequest(request); + Assertions.assertEquals(1, bank.getAllRequests().size()); + } + + // TODO: if time, test remove() or add wrapper function called delete + +} From 5768989fd0f53dc0facc52fcb944f3b7fe1ef0d3 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 13:07:35 +0200 Subject: [PATCH 10/22] Change IdGenerator Test and Code --- .../core/idgenerator/IdGenerator.java | 25 +++++++--- .../core/idgenerator/IdGeneratorTest.java | 50 ++++++++++++++----- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java index 15b726ce5..f0de66fad 100644 --- a/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java +++ b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java @@ -7,10 +7,25 @@ public class IdGenerator { private static int currentRE = 0; private static int currentTR = 0; - private final IdPrefix prefix; + private IdPrefix prefix; private String id; - public IdGenerator(IdPrefix prefix) { + 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? @@ -26,13 +41,7 @@ public IdGenerator(IdPrefix prefix) { currentTR++; setId(currentTR); } - } - - private void setId(int currentNumber) { - this.id = prefix + "_" + currentNumber; - } - public String getId() { return this.id; } } diff --git a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java index 5b6f731d1..8f66252ce 100644 --- a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java +++ b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java @@ -1,26 +1,52 @@ 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 org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Map; + public class IdGeneratorTest { @Test public void generateMultipleUniqueIds() { - Assertions.assertEquals("AC_1", new IdGenerator(IdPrefix.AC).getId()); - Assertions.assertEquals("AC_2", new IdGenerator(IdPrefix.AC).getId()); - Assertions.assertEquals("AC_3", new IdGenerator(IdPrefix.AC).getId()); + 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()); + } + + @Test + public void generateMultilhjjljpleUniqueIds() { + Bank bank = new Bank(); + + User customer1 = bank.createUser("P_#1"); + bank.createUserAccount(customer1, AccountType.CURRENT); + bank.createUserAccount(customer1, AccountType.SAVINGS); - Assertions.assertEquals("RE_1", new IdGenerator(IdPrefix.RE).getId()); - Assertions.assertEquals("RE_2", new IdGenerator(IdPrefix.RE).getId()); - Assertions.assertEquals("RE_3", new IdGenerator(IdPrefix.RE).getId()); + Assertions.assertEquals("AC_1", customer1.getAllAccounts().get(0).getAccountNumber()); + Assertions.assertEquals("AC_2", customer1.getAllAccounts().get(1).getAccountNumber()); - Assertions.assertEquals("TR_1", new IdGenerator(IdPrefix.TR).getId()); - Assertions.assertEquals("TR_2", new IdGenerator(IdPrefix.TR).getId()); - Assertions.assertEquals("TR_3", new IdGenerator(IdPrefix.TR).getId()); + User customer2 = bank.createUser("P_#1"); + bank.createUserAccount(customer2, AccountType.CURRENT); + bank.createUserAccount(customer2, AccountType.SAVINGS); - Assertions.assertEquals("AC_4", new IdGenerator(IdPrefix.AC).getId()); - Assertions.assertEquals("RE_4", new IdGenerator(IdPrefix.RE).getId()); - Assertions.assertEquals("TR_4", new IdGenerator(IdPrefix.TR).getId()); + Assertions.assertEquals("AC_3", customer2.getAllAccounts().get(0).getAccountNumber()); + Assertions.assertEquals("AC_4", customer2.getAllAccounts().get(1).getAccountNumber()); } } From e9a37a8c0fe0ebe1b719cd227d78526c046d8d52 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 13:10:22 +0200 Subject: [PATCH 11/22] Remove unused imports --- .../java/com/booleanuk/core/idgenerator/IdGeneratorTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java index 8f66252ce..63ff99795 100644 --- a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java +++ b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java @@ -7,9 +7,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.Map; - public class IdGeneratorTest { @Test @@ -32,7 +29,7 @@ public void generateMultipleUniqueIds() { } @Test - public void generateMultilhjjljpleUniqueIds() { + public void generateMultipleUniqueIdsForAnotherBankeShouldHaveOwnIds() { Bank bank = new Bank(); User customer1 = bank.createUser("P_#1"); From c871a1417185b45c6285bc96a050ba0beb19c813 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:14:56 +0200 Subject: [PATCH 12/22] Redo Test and Code for Transactions/Accounts --- .../com/booleanuk/core/accounts/Account.java | 25 +++++++-- .../core/transactons/Transaction.java | 5 +- .../booleanuk/core/{ => bank}/BankTest.java | 11 ++-- .../core/transactions/TransactionTest.java | 54 +++++++++++++------ 4 files changed, 67 insertions(+), 28 deletions(-) rename src/test/java/com/booleanuk/core/{ => bank}/BankTest.java (55%) diff --git a/src/main/java/com/booleanuk/core/accounts/Account.java b/src/main/java/com/booleanuk/core/accounts/Account.java index 01c7e804e..d89077482 100644 --- a/src/main/java/com/booleanuk/core/accounts/Account.java +++ b/src/main/java/com/booleanuk/core/accounts/Account.java @@ -5,18 +5,19 @@ import com.booleanuk.core.transactons.Transaction; import com.booleanuk.core.transactons.TransactionType; -import java.util.HashMap; -import java.util.Map; +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() { - this.accountNumber = new IdGenerator(IdPrefix.AC).getId(); + public Account(String accountNumber) { + this.idGenerator = new IdGenerator(); + this.accountNumber = accountNumber; this.transactions = new HashMap<>(); } @@ -46,6 +47,7 @@ protected void addTransaction(Transaction transaction) { public void deposit(Double amount) { Transaction transaction = new Transaction( + this.idGenerator.createId(IdPrefix.TR), amount, TransactionType.DEBIT, calculateBalance() @@ -72,6 +74,7 @@ public void withdraw(Double amount) { } Transaction transaction = new Transaction( + this.idGenerator.createId(IdPrefix.TR), amount, TransactionType.CREDIT, currentBalance @@ -79,6 +82,13 @@ public void withdraw(Double amount) { 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()) { @@ -96,4 +106,11 @@ public Double getBalance() { } // 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/transactons/Transaction.java b/src/main/java/com/booleanuk/core/transactons/Transaction.java index d2e84d1fb..25af961fe 100644 --- a/src/main/java/com/booleanuk/core/transactons/Transaction.java +++ b/src/main/java/com/booleanuk/core/transactons/Transaction.java @@ -13,8 +13,9 @@ public class Transaction { private TransactionType type; private Double currentBalance; - public Transaction(Double amount, TransactionType type, Double currentBalance) { - this.id = new IdGenerator(IdPrefix.TR).getId(); + public Transaction(String id, Double amount, TransactionType type, Double currentBalance) { +// this.id = new IdGenerator(IdPrefix.TR).getId(); + this.id = id; this.transactionDate = LocalDateTime.now(); // TODO: Format later this.amount = amount; this.type = type; diff --git a/src/test/java/com/booleanuk/core/BankTest.java b/src/test/java/com/booleanuk/core/bank/BankTest.java similarity index 55% rename from src/test/java/com/booleanuk/core/BankTest.java rename to src/test/java/com/booleanuk/core/bank/BankTest.java index e548612dd..266995672 100644 --- a/src/test/java/com/booleanuk/core/BankTest.java +++ b/src/test/java/com/booleanuk/core/bank/BankTest.java @@ -1,7 +1,7 @@ -package com.booleanuk.core; +package com.booleanuk.core.bank; import com.booleanuk.core.accounts.Account; -import com.booleanuk.core.accounts.CurrentAccount; +import com.booleanuk.core.accounts.AccountType; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -10,11 +10,12 @@ public class BankTest { @Test public void makeRequest() { Bank bank = new Bank(); + User customer = bank.createUser("P_#1"); - Account account = new CurrentAccount(); - Request request = new Request(account, 100.00); + bank.createUserAccount(customer, AccountType.CURRENT); + Account account = customer.getAllAccounts().get(0); - bank.makeRequest(request); + bank.makeRequest(account, 100.00); Assertions.assertEquals(1, bank.getAllRequests().size()); } diff --git a/src/test/java/com/booleanuk/core/transactions/TransactionTest.java b/src/test/java/com/booleanuk/core/transactions/TransactionTest.java index b53634fa7..0ae687e51 100644 --- a/src/test/java/com/booleanuk/core/transactions/TransactionTest.java +++ b/src/test/java/com/booleanuk/core/transactions/TransactionTest.java @@ -1,37 +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 javax.swing.text.DateFormatter; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; +import java.util.ArrayList; public class TransactionTest { @Test public void createTransactionReturnsCorrectId() { - Assertions.assertEquals("TR_1", new Transaction(2000.0, TransactionType.CREDIT, 3000.0).getId()); - Assertions.assertEquals("TR_2", new Transaction(2000.0, TransactionType.CREDIT, 3000.0).getId()); - } + Bank bank = new Bank(); + User customer1 = bank.createUser("P_#2"); + bank.createUserAccount(customer1, AccountType.CURRENT); + Account account = customer1.getAllAccounts().get(0); - @Test - public void getDateAndTime() { - LocalDateTime localDateTime = LocalDateTime.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm"); + account.deposit(1000.0); + account.withdraw(200.00); + account.withdraw(200.00); - Assertions.assertEquals( - localDateTime.format(formatter), - new Transaction(10.0, TransactionType.CREDIT, 1000.0).getTransactionDate().format(formatter)); + 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() { - Transaction transaction = new Transaction(2000.00, TransactionType.CREDIT, 3000.00); - Assertions.assertEquals(2000.00, transaction.getAmount()); - Assertions.assertEquals(TransactionType.CREDIT, transaction.getType()); - Assertions.assertEquals(3000.00, transaction.getCurrentBalance()); + 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()); } } From 69125d3f4d7646e4dcba3be5ade0b5eb2200bf80 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:16:33 +0200 Subject: [PATCH 13/22] Redo Test and Code for Transactions/Accounts --- .../booleanuk/core/accounts/AccountTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/booleanuk/core/accounts/AccountTest.java b/src/test/java/com/booleanuk/core/accounts/AccountTest.java index 370dfd903..596f4e1a2 100644 --- a/src/test/java/com/booleanuk/core/accounts/AccountTest.java +++ b/src/test/java/com/booleanuk/core/accounts/AccountTest.java @@ -7,23 +7,23 @@ public class AccountTest { @Test public void createCurrentAccount() { - Account currentAccount = new CurrentAccount(); - Assertions.assertEquals("AC_1", currentAccount.getAccountNumber()); + Account currentAccount = new CurrentAccount("test_1"); + Assertions.assertEquals("test_1", currentAccount.getAccountNumber()); Assertions.assertEquals(Branch.BRANCH_1, currentAccount.getBranch()); - Account savingsAccount = new CurrentAccount(); - Assertions.assertEquals("AC_2", savingsAccount.getAccountNumber()); + 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(); + Account currentAccount = new CurrentAccount("test_1"); Assertions.assertTrue(currentAccount.isPossibleToOverdraft()); currentAccount.setPossibleToOverdraft(false); Assertions.assertFalse(currentAccount.isPossibleToOverdraft()); - Account savingsAccount = new SavingsAccount(); + Account savingsAccount = new SavingsAccount("test_2"); Assertions.assertFalse(savingsAccount.isPossibleToOverdraft()); savingsAccount.setPossibleToOverdraft(true); Assertions.assertTrue(savingsAccount.isPossibleToOverdraft()); @@ -31,22 +31,22 @@ public void checkOverdraftFacility() { @Test public void depositMoney() { - Account currentAccount = new CurrentAccount(); + Account currentAccount = new CurrentAccount("test_1"); currentAccount.deposit(1000.00); Assertions.assertEquals(1000.00, currentAccount.getBalance()); - Account savingtAccount = new SavingsAccount(); + Account savingtAccount = new SavingsAccount("test_2"); savingtAccount.deposit(1000.00); Assertions.assertEquals(1000.00, savingtAccount.getBalance()); } @Test public void withdrawMoney() { - Account currentAccount = new CurrentAccount(); + Account currentAccount = new CurrentAccount("test_1"); currentAccount.withdraw(1000.00); Assertions.assertEquals(-1000.00, currentAccount.getBalance()); - Account savingtAccount = new SavingsAccount(); + Account savingtAccount = new SavingsAccount("test_2"); // Inner validate function should throw error Exception e = Assertions.assertThrows( Exception.class, From a1fd76810be2797cefa18590546249fabe71dfea Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:16:47 +0200 Subject: [PATCH 14/22] Redo Test and Code for Transactions/Accounts --- src/main/java/com/booleanuk/core/accounts/AccountType.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/com/booleanuk/core/accounts/AccountType.java 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 +} From ff7a64205481a0335b67a7a420e9675096d9e49c Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:23:07 +0200 Subject: [PATCH 15/22] Redo Test and Code for Request/Bank --- .../java/com/booleanuk/core/bank/Bank.java | 70 +++++++++++++++++++ .../booleanuk/core/{ => bank}/Request.java | 8 +-- .../com/booleanuk/core/bank/RequestTest.java | 39 +++++++++++ 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/bank/Bank.java rename src/main/java/com/booleanuk/core/{ => bank}/Request.java (72%) create mode 100644 src/test/java/com/booleanuk/core/bank/RequestTest.java 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..bc662f5cc --- /dev/null +++ b/src/main/java/com/booleanuk/core/bank/Bank.java @@ -0,0 +1,70 @@ +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 accounts; + private Map requests; + + public Bank() { + this.customers = new HashMap<>(); + this.accounts = new HashMap<>(); // TODO: do I need this? I just use the ID + 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/Request.java b/src/main/java/com/booleanuk/core/bank/Request.java similarity index 72% rename from src/main/java/com/booleanuk/core/Request.java rename to src/main/java/com/booleanuk/core/bank/Request.java index aa0b0f3f3..770d5db32 100644 --- a/src/main/java/com/booleanuk/core/Request.java +++ b/src/main/java/com/booleanuk/core/bank/Request.java @@ -1,8 +1,6 @@ -package com.booleanuk.core; +package com.booleanuk.core.bank; import com.booleanuk.core.accounts.Account; -import com.booleanuk.core.idgenerator.IdGenerator; -import com.booleanuk.core.idgenerator.IdPrefix; public class Request { @@ -10,8 +8,8 @@ public class Request { private final Account account; private Double overdraftSum; - public Request(Account account, Double overdraftSum) { - this.id = new IdGenerator(IdPrefix.RE).getId(); + public Request(String id, Account account, Double overdraftSum) { + this.id = id; this.account = account; this.overdraftSum = overdraftSum; } 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()); + } +} From bdadbda5e698a7879ef133dd48d6beb3f5971040 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:26:07 +0200 Subject: [PATCH 16/22] Redo Test and Code for Request/Bank --- src/main/java/com/booleanuk/core/Bank.java | 29 ---------------------- 1 file changed, 29 deletions(-) delete mode 100644 src/main/java/com/booleanuk/core/Bank.java diff --git a/src/main/java/com/booleanuk/core/Bank.java b/src/main/java/com/booleanuk/core/Bank.java deleted file mode 100644 index 1bf14eca0..000000000 --- a/src/main/java/com/booleanuk/core/Bank.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.booleanuk.core; - -import java.util.HashMap; -import java.util.Map; - -public class Bank { - - private Map requests; - - public Bank() { - this.requests = new HashMap<>(); - } - - public Map getAllRequests() { - return this.requests; - } - - private void addRequest(Request request) { - this.requests.put(request.getId(), request); - } - - public void makeRequest(Request request) { - this.addRequest(request); - } - - private void removeRequest(String requestId) { - this.requests.remove(requestId); - } -} From fb617901c90dcf089b07743ee8377580404e6a43 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:26:13 +0200 Subject: [PATCH 17/22] Redo Test and Code for Request/Bank --- .../java/com/booleanuk/core/RequestTest.java | 30 ------------------- 1 file changed, 30 deletions(-) delete mode 100644 src/test/java/com/booleanuk/core/RequestTest.java diff --git a/src/test/java/com/booleanuk/core/RequestTest.java b/src/test/java/com/booleanuk/core/RequestTest.java deleted file mode 100644 index 26d451850..000000000 --- a/src/test/java/com/booleanuk/core/RequestTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.booleanuk.core; - -import com.booleanuk.core.accounts.Account; -import com.booleanuk.core.accounts.SavingsAccount; -import com.booleanuk.core.transactons.Transaction; -import com.booleanuk.core.transactons.TransactionType; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class RequestTest { - - @Test - public void createRequest() { - Account account = new SavingsAccount(); - Double overdraftSum = 1000.0; - Assertions.assertEquals("RE_1", new Request(account, overdraftSum).getId()); - Assertions.assertEquals("RE_2", new Request(account, overdraftSum).getId()); - } - - @Test - public void changeOverdraftFacility() { - Account account = new SavingsAccount(); - Assertions.assertFalse(account.isPossibleToOverdraft()); - - Request request = new Request(account, 1000.00); - request.approve(); - - Assertions.assertTrue(account.isPossibleToOverdraft()); - } -} From ec563fcd6349242de55681d8c2523d767813b586 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:26:41 +0200 Subject: [PATCH 18/22] Redo Test and Code for Customer/User renamed --- .../java/com/booleanuk/core/bank/User.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/com/booleanuk/core/bank/User.java 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..71fb7741b --- /dev/null +++ b/src/main/java/com/booleanuk/core/bank/User.java @@ -0,0 +1,29 @@ +package com.booleanuk.core.bank; + +import com.booleanuk.core.accounts.Account; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class User { + + // TODO: Not safe to store personal numbers ike this, should be a uniwueId 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() { + return new ArrayList<>(accounts.values()); + } +} From 0d9e315b54f5811d2362c3b0f7d60c02db0b85b0 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:27:30 +0200 Subject: [PATCH 19/22] Redo Test and Code for Customer/User renamed --- src/main/java/com/booleanuk/core/bank/User.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/bank/User.java b/src/main/java/com/booleanuk/core/bank/User.java index 71fb7741b..77e9c52d4 100644 --- a/src/main/java/com/booleanuk/core/bank/User.java +++ b/src/main/java/com/booleanuk/core/bank/User.java @@ -8,7 +8,7 @@ public class User { - // TODO: Not safe to store personal numbers ike this, should be a uniwueId instead? + // 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; From a6cca4a6c6313e7a475531dc03b76bdb923a5491 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:27:41 +0200 Subject: [PATCH 20/22] Redo Test and Code for Accounts --- .../java/com/booleanuk/core/accounts/CurrentAccount.java | 6 +++++- .../java/com/booleanuk/core/accounts/SavingsAccount.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java b/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java index a3259ff83..dd2b5c732 100644 --- a/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java +++ b/src/main/java/com/booleanuk/core/accounts/CurrentAccount.java @@ -2,7 +2,11 @@ public class CurrentAccount extends Account{ - public CurrentAccount() { + // 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 index 430d481df..e4b224f62 100644 --- a/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java +++ b/src/main/java/com/booleanuk/core/accounts/SavingsAccount.java @@ -4,7 +4,11 @@ public class SavingsAccount extends Account{ private Double interestRate; - public SavingsAccount() { + // 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; From 1e763eeb9b985955e2be0d4362a3e98c1f585ea4 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:43:05 +0200 Subject: [PATCH 21/22] Update readme and remove comments --- src/main/java/com/booleanuk/core/bank/Bank.java | 2 -- src/main/java/com/booleanuk/core/domain-model.md | 15 +++++++++++---- .../booleanuk/core/transactons/Transaction.java | 1 - 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/booleanuk/core/bank/Bank.java b/src/main/java/com/booleanuk/core/bank/Bank.java index bc662f5cc..0be1ec68e 100644 --- a/src/main/java/com/booleanuk/core/bank/Bank.java +++ b/src/main/java/com/booleanuk/core/bank/Bank.java @@ -16,12 +16,10 @@ public class Bank { private IdGenerator idGenerator; private Map customers; - private Map accounts; private Map requests; public Bank() { this.customers = new HashMap<>(); - this.accounts = new HashMap<>(); // TODO: do I need this? I just use the ID this.requests = new HashMap<>(); this.idGenerator = new IdGenerator(); } diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md index 59c9b6e7e..b95a17898 100644 --- a/src/main/java/com/booleanuk/core/domain-model.md +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -4,7 +4,10 @@ ## Table | Classes | Variables | Methods | Scenario | Output | |--------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|------------------------| -| `Bank` | `-Map requests` | `+getAllRequests()` | Get all requests to be handled. | Map | +| `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 | @@ -15,28 +18,32 @@ | | | `+approve()` | Update account with facility to make overdrafts. | | | | | `+reject()` | Reject request, it should not be possible to make overdrafts. | | | | | | | | -| `Customer(String personalNumber)` TODO: not implemented | `-String personalNumber` | | | | +| `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` | `-String accountNumber` | `+getAccountNumber()` | Auto-generate account number. | | +| 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)` | | | +| | | `#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 | diff --git a/src/main/java/com/booleanuk/core/transactons/Transaction.java b/src/main/java/com/booleanuk/core/transactons/Transaction.java index 25af961fe..189c1dbce 100644 --- a/src/main/java/com/booleanuk/core/transactons/Transaction.java +++ b/src/main/java/com/booleanuk/core/transactons/Transaction.java @@ -14,7 +14,6 @@ public class Transaction { private Double currentBalance; public Transaction(String id, Double amount, TransactionType type, Double currentBalance) { -// this.id = new IdGenerator(IdPrefix.TR).getId(); this.id = id; this.transactionDate = LocalDateTime.now(); // TODO: Format later this.amount = amount; From d3b821c5e9226dfbffe63fdf17c9962fd875b288 Mon Sep 17 00:00:00 2001 From: shyye Date: Wed, 28 Aug 2024 14:59:33 +0200 Subject: [PATCH 22/22] Fix id issues --- src/main/java/com/booleanuk/core/bank/User.java | 6 +++++- .../com/booleanuk/core/idgenerator/IdGenerator.java | 6 +++--- .../booleanuk/core/idgenerator/IdGeneratorTest.java | 12 ++++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/bank/User.java b/src/main/java/com/booleanuk/core/bank/User.java index 77e9c52d4..066f55369 100644 --- a/src/main/java/com/booleanuk/core/bank/User.java +++ b/src/main/java/com/booleanuk/core/bank/User.java @@ -3,6 +3,7 @@ import com.booleanuk.core.accounts.Account; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -24,6 +25,9 @@ protected void addAccount(Account account) { } public ArrayList getAllAccounts() { - return new ArrayList<>(accounts.values()); + ArrayList list = new ArrayList<>(accounts.values()); +// Collections.reverse(list); + + return list; } } diff --git a/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java index f0de66fad..0f46e5931 100644 --- a/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java +++ b/src/main/java/com/booleanuk/core/idgenerator/IdGenerator.java @@ -3,9 +3,9 @@ public class IdGenerator { // Resource: https://www.freecodecamp.org/news/static-variables-in-java/ - private static int currentAC = 0; - private static int currentRE = 0; - private static int currentTR = 0; + private int currentAC = 0; + private int currentRE = 0; + private int currentTR = 0; private IdPrefix prefix; private String id; diff --git a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java index 63ff99795..67dda44a2 100644 --- a/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java +++ b/src/test/java/com/booleanuk/core/idgenerator/IdGeneratorTest.java @@ -4,9 +4,12 @@ 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 @@ -17,8 +20,13 @@ public void generateMultipleUniqueIds() { 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()); + 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);