diff --git a/domainModel.md b/domainModel.md new file mode 100644 index 000000000..b133f9869 --- /dev/null +++ b/domainModel.md @@ -0,0 +1,36 @@ + +| Classes | variables | Methods | Scenario | Outputs | +| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| BankAccount | String accountName, Branch branch,
ListFloat>> transactions | BankAccount(String accountName,
Branch branch) | if all is valid | creates account with no transactions and
registers the account to the given branch | +| | | | if branch is null | creates account but does not register to branch | +| | | withdraw(float amount) | if amount < 0 | returns not a valid transaction | +| | | | if ballance is < amount
and notallowToOverdraft | returns not enough balance | +| | | | otherwise | add transaction of withdraw | +| | | deposit(float amount) | if amount <= 0 | returns not a valid transaction | +| | | | otherwise | add deposit to transactions | +| | | getSatementsToSTring | | prints out nice looking statement data and history | +| | | requestOverdraft(Bankaccount account) | | request overdraft at given branch and returns value | +| | | getBalance() | | call getbalance(float transaction count) with negative value and returns value | +| | | getBalance(float transaction count) | transaction count < 0 | gives the full transaction hisory | +| | | | otherwise | gives transaction history till the given transaction count | +| | | | | | +| CheckingAccount | | super() | | extends BankAccount | +| SavingsAccount | | super() | | extends BankAccount | +| | | | | | +| Branch | String branchLocation,
String managerAccessKey,
List/ accounts,
List/ requestedOverdraft,
Lis/ approvedOverdraft, | Branch(STring branchLocation,
String fmanagerAccessKey) | | creates branch object with empty lists | +| | | addAccount | if account is already in accountsList | returns account is already part of branch | +| | | | if not | adds account to the branch | +| | | requestOverdraft(BAnkAccount) | if account is not part of branch | returns bank is not part of branch | +| | | | if account already requested overdraft | returns account already requested overdraft | +| | | | if account already is allowed to overdraft | returns account is allready approved for overdraft | +| | | | otherwise | adds account to list of requested overdraft
and returns overdraft has been approved | +| | | processOverdraft(BankAccount account, String accessKey, boolean accepts) | if accessKey is not the one of the manager | returns no authority to process overdrafts | +| | | | if account is not part of this branch | returns account is not part of this branch | +| | | | if accepts is false | removes account from requested list and returns overdraft was rejected | +| | | | if account did not request overdraft | returns no overdraft was requested | +| | | | otherwise | removes account from requested list and adds it tto the approved list and returns overdraft was approved | +| | | wasjapprovedOverdraft(bankaccount account) | | returns if bankaccount is in overdraft approved list | +| OsakaBranch | | super() | | | +| tokyoBranch | | super() | | | + + diff --git a/src/main/java/com/booleanuk/core/BankAccount.java b/src/main/java/com/booleanuk/core/BankAccount.java new file mode 100644 index 000000000..3f7418621 --- /dev/null +++ b/src/main/java/com/booleanuk/core/BankAccount.java @@ -0,0 +1,88 @@ +package com.booleanuk.core; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public abstract class BankAccount { + + private final String accountName; + private final Branch branch; + private List> transactions; + + public BankAccount(String accountName, Branch branch) { + this.accountName = accountName; + + this.transactions = new ArrayList<>(); + this.branch = branch; + if (branch != null) + branch.addAccount(this); + } + + public String withdraw(float amount) { + boolean overdraftAllowed = branch.wasApprovedOverdraft(this); + + if (amount <= 0) + return "Not a valid transaction"; + + if (getBalance() < amount && !overdraftAllowed) + return "Not enough balance"; + + + transactions.add(Map.entry(LocalDate.now(), - amount)); + return "Withdraw completed"; + } + + public String deposit(float amount) { + if (amount <= 0) + return "Not a valid transaction"; + + transactions.add(Map.entry(LocalDate.now(),amount)); + return "Amount successfully deposited"; + } + + public List> getStatements() { + return transactions; + } + + public String getStatementsToString() { + StringBuilder sb = new StringBuilder(); + sb.append("\ndate \t\t | | transactions \t\t | | balance\n"); + for (int i = 0; i < transactions.size(); i++) { + var transaction = transactions.get(i); + sb.append(transaction.getKey() + + "\t\t | | " + + transaction.getValue() + + "\t\t | | " + + getBalance(i +1) + + + "\n"); + + } + return sb.toString(); + } + + public String requestOverdraft() { + return branch.requestOverdraft(this); + } + + public float getBalance() { + return getBalance(-1); + } + + public float getBalance(int transactionCount) { + + if (transactionCount < 0) { + transactionCount = transactions.size(); + } + float balance = 0; + for (int i = 0; i < transactionCount; i++) { + var transaction = transactions.get(i); + balance += transaction.getValue(); + } + return balance; + } + +} diff --git a/src/main/java/com/booleanuk/core/Branch.java b/src/main/java/com/booleanuk/core/Branch.java new file mode 100644 index 000000000..f3f161b65 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Branch.java @@ -0,0 +1,67 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public abstract class Branch { + + private String branchLocation; + private String managerAccessKey; + private List accounts; + private List requestedOverdraft; + private List approvedOverdraft; + + Branch(String branchLocation, String managerAccessKey1) { + this.branchLocation = branchLocation; + this.managerAccessKey = managerAccessKey1; + this.accounts = new ArrayList<>(); + this.requestedOverdraft = new ArrayList<>(); + this.approvedOverdraft = new ArrayList<>(); + } + + public String addAccount(BankAccount account) { + if (accounts.contains(account)) + return "Account already part of this branch"; + + accounts.add(account); + return "Account successfully registered at this branch"; + } + + public String requestOverdraft(BankAccount account) { + if (!accounts.contains(account)) + return "Not part of this branch"; + + if(requestedOverdraft.contains(account)) + return "Already requested overdraft"; + + if (approvedOverdraft.contains(account)) + return "Overdraft already approved"; + + requestedOverdraft.add(account); + return "Overdraft has been requested"; + } + + public String processOverdraft(BankAccount account, String accessKey, Boolean approve) { + if (!managerAccessKey.equals(accessKey)) + return "No authority to approve overdrafts"; + + if (!accounts.contains(account)) + return "Not part of this branch"; + + + if(requestedOverdraft.contains(account) && approve) { + requestedOverdraft.remove(account); + approvedOverdraft.add(account); + return "Overdraft approved"; + } + + if (!approve) + return "Overdraft was rejected"; + + return "No overdraft was requested"; + } + + public boolean wasApprovedOverdraft(BankAccount account) { + return approvedOverdraft.contains(account); + } +} diff --git a/src/main/java/com/booleanuk/core/CheckingAccount.java b/src/main/java/com/booleanuk/core/CheckingAccount.java new file mode 100644 index 000000000..18d7ee704 --- /dev/null +++ b/src/main/java/com/booleanuk/core/CheckingAccount.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class CheckingAccount extends BankAccount{ + public CheckingAccount(String accountName, Branch branch) { + super(accountName, branch); + } +} diff --git a/src/main/java/com/booleanuk/core/OsakaBranch.java b/src/main/java/com/booleanuk/core/OsakaBranch.java new file mode 100644 index 000000000..0d163933f --- /dev/null +++ b/src/main/java/com/booleanuk/core/OsakaBranch.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class OsakaBranch extends Branch{ + public OsakaBranch() { + super("大阪", "42"); + } +} diff --git a/src/main/java/com/booleanuk/core/SavingsAccount.java b/src/main/java/com/booleanuk/core/SavingsAccount.java new file mode 100644 index 000000000..38b03c56f --- /dev/null +++ b/src/main/java/com/booleanuk/core/SavingsAccount.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class SavingsAccount extends BankAccount{ + public SavingsAccount(String accountName, Branch branch) { + super(accountName, branch); + } +} diff --git a/src/main/java/com/booleanuk/core/TokyoBranch.java b/src/main/java/com/booleanuk/core/TokyoBranch.java new file mode 100644 index 000000000..7c668bf1f --- /dev/null +++ b/src/main/java/com/booleanuk/core/TokyoBranch.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class TokyoBranch extends Branch{ + public TokyoBranch() { + super("東京", "454"); + } +} diff --git a/src/test/java/com/booleanuk/core/BankAccountTest.java b/src/test/java/com/booleanuk/core/BankAccountTest.java new file mode 100644 index 000000000..ded7a5393 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BankAccountTest.java @@ -0,0 +1,85 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BankAccountTest { + + private static BankAccount account; + private static Branch branch; + + @BeforeEach + void beforeEach() { + branch = new OsakaBranch(); + account = new SavingsAccount("Sakamoto Ryouma", branch); + } + + + @Test + void withdraw() { + Assertions.assertEquals("Not enough balance", account.withdraw(500f)); + account.deposit(500f); + Assertions.assertEquals("Withdraw completed", account.withdraw(200)); + Assertions.assertEquals("Not enough balance", account.withdraw(400)); + Assertions.assertEquals("Not a valid transaction", account.withdraw(-500f)); + + account.requestOverdraft(); + branch.processOverdraft(account, "42", true); + Assertions.assertEquals("Withdraw completed", account.withdraw(400)); + + + } + + @Test + void deposit() { + Assertions.assertEquals("Amount successfully deposited", account.deposit(42f)); + Assertions.assertEquals("Not a valid transaction", account.deposit(-420f)); + } + + @Test + void getStatements() { + Assertions.assertEquals(0, account.getStatements().size()); + account.deposit(500); + account.withdraw(500); + Assertions.assertEquals(2, account.getStatements().size()); + } +// This test was to see the string personally +// @Test +// void getStatementsToString() { +// account.deposit(500); +// account.deposit(52.8f); +// account.withdraw(442.27f); +// +// account.requestOverdraft(); +// branch.processOverdraft(account, "42", true); +// account.withdraw(1000); +// Assertions.assertEquals("", account.getStatementsToString()); +// +// } + + + @Test + void requestOverdraft() { + Assertions.assertEquals("Overdraft has been requested", account.requestOverdraft()); + Assertions.assertEquals("Already requested overdraft", account.requestOverdraft()); + branch.processOverdraft(account, "42", true); + Assertions.assertEquals("Overdraft already approved", account.requestOverdraft()); + } + + @Test + void getBalance() { + Assertions.assertEquals(0, account.getBalance()); + account.deposit(500); + account.deposit(52.8f); + account.withdraw(442.27f); + Assertions.assertEquals(110.53, account.getBalance(), 0.001); + Assertions.assertEquals(552.8f, account.getBalance(2), 0.001); + + account.requestOverdraft(); + branch.processOverdraft(account, "42", true); + account.withdraw(1000); + Assertions.assertEquals(-889.47, account.getBalance(), 0.001); + } + +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/BranchTest.java b/src/test/java/com/booleanuk/core/BranchTest.java new file mode 100644 index 000000000..77d25ea2c --- /dev/null +++ b/src/test/java/com/booleanuk/core/BranchTest.java @@ -0,0 +1,56 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BranchTest { + Branch branch; + BankAccount account; + BankAccount emptyBranch; + + @BeforeEach + void setUp() { + branch = new TokyoBranch(); + account = new SavingsAccount("埼玉", branch); + emptyBranch = new CheckingAccount("", null); + } + + @Test + void addAccount() { + Assertions.assertEquals("Account already part of this branch", branch.addAccount(account)); + + BankAccount emptyBranch = new CheckingAccount("", null); + Assertions.assertEquals("Account successfully registered at this branch", + branch.addAccount(emptyBranch)); + } + + @Test + void requestOverdraft() { + Assertions.assertEquals("Overdraft has been requested",branch.requestOverdraft(account)); + Assertions.assertEquals("Not part of this branch", branch.requestOverdraft(emptyBranch)); + Assertions.assertEquals("Already requested overdraft",branch.requestOverdraft(account)); + branch.processOverdraft(account, "454", true); + Assertions.assertEquals("Overdraft already approved",branch.requestOverdraft(account)); + + } + + @Test + void processOverdraft() { + Assertions.assertEquals("No authority to approve overdrafts", branch.processOverdraft(account, "", true)); + Assertions.assertEquals("Not part of this branch", branch.processOverdraft(emptyBranch, "454", true)); + Assertions.assertEquals("No overdraft was requested", branch.processOverdraft(account, "454", true)); + branch.requestOverdraft(account); + Assertions.assertEquals("Overdraft was rejected", branch.processOverdraft(account, "454", false)); + + Assertions.assertEquals("Overdraft approved", branch.processOverdraft(account, "454", true)); + } + + @Test + void wasApprovedOverdraft() { + Assertions.assertFalse(branch.wasApprovedOverdraft(account)); + branch.requestOverdraft(account); + branch.processOverdraft(account, "454", true); + Assertions.assertTrue(branch.wasApprovedOverdraft(account)); + } +} \ No newline at end of file