diff --git a/src/main/domain-model.md b/src/main/domain-model.md new file mode 100644 index 000000000..9d0333a2a --- /dev/null +++ b/src/main/domain-model.md @@ -0,0 +1,50 @@ +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. + +| Classes | Variables | Method | Scenario | Output | +|------------|-----------------|----------------------------------------------------------------------|--------------------------------------------------|-------------------------------------------------| +| Customer | Account.balance | Account Customer.createAccount(String type) | If type is valid and account created | Account | +| Account | | | if type is invalid | null | +| | | | if account already created | null | +| | | | | | +| | Account.balance | Float Customer.withdrawFunds(Float amount, Account account) | if amount is less than balance | the amount left i account | +| | | | if amount is more than balance | -1.00f | +| | Account.balance | Float Customer.depositFunds(Float amount, Account account) | Parameters are valid | the amount in account | +| | | | parameters are not valid | null | +| | | | | | +| | transactions | String Customer.generateBankStatement(StringType) | if the account has transactions | return the bank statement | +| | | | if no transactions | return " " | +| | | | | | +| | | Float Customer.getBalanceHardWay(String Type) | account has transactions | return the balance | +| | | | if transactions is empty | -1.00f | +| | | | | | +| | branch | boolean Customer.setBranch(String branch) | if place is a place | true | +| | | | if place is " " | false | +| | | | | | +| | | boolean Customer.overdraft(Float amount ) | if amount is more than 200 over bal and approved | false | +| | | | if it is less than 200 over bal | true | +| | | | if the amount is less than bal | false | +| | | | | | +| | | boolean Customer.createRequests(Float amount) | if amount is overdraft and is under 400 | List created and return true | +| | | | if overdraft is over 400 | nothing happens return true | +| | | | if i write something else | | +| | | | | | +| | | boolean Customer.approveRequest( Float request, boolean approved) | if approved is true | hashmap is updated and deleted when set through | +| | | | if approved is false | request is declined and entry is deleted. | +| | | | if request found | false | +| | | | if request not found but approved is true | false | +| | | | | | +| | | | | | diff --git a/src/main/img.png b/src/main/img.png new file mode 100644 index 000000000..7dfb65497 Binary files /dev/null and b/src/main/img.png differ diff --git a/src/main/java/com/booleanuk/core/Account.java b/src/main/java/com/booleanuk/core/Account.java new file mode 100644 index 000000000..2f755a6fb --- /dev/null +++ b/src/main/java/com/booleanuk/core/Account.java @@ -0,0 +1,83 @@ +package com.booleanuk.core; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Objects; + +public abstract class Account { + protected Float balance; + protected ArrayList transactions; + protected ArrayList requests; + + + + + public Account(ArrayList transactions) { + this.balance = 0.00f; + this.transactions = transactions; + this.requests = new ArrayList(); + } + + public Float findAmount(Float amount){ + for (Float item : requests){ + if (Objects.equals(item, amount)) { + return item; + } + } + return -999.40f; + } + + public boolean addRequest(Float amount){ + requests.add(amount); + return true; + } + + public void removeRequest(Float amount){ + requests.remove(amount); + } + + + public Float addAmount(Float amount){ + balance+= amount; + transactions.add(new Transaction(LocalDate.now(),amount, balance, "+")); + + return balance; + } + + public Float getBalance() { + return balance; + } + + public Float withdrawAmount(Float amount){ + balance-= amount; + transactions.add(new Transaction(LocalDate.now(),amount, balance, "-")); + + return balance; + } + public boolean generateBankStatement(){ + String statement = " "; + for (Transaction transaction : transactions) { + statement += transaction.getInfo() + "\n"; + } + System.out.println(statement); + return true; + } + + + public Float getBalanceHardWay(){ + Float bal = 0.00f; + for (Transaction transaction : transactions) { + if (transaction.getType()){ + bal += transaction.getAmount(); + } + else { + bal -= transaction.getAmount(); + + } + } + return bal; + } + + + + } diff --git a/src/main/java/com/booleanuk/core/Current.java b/src/main/java/com/booleanuk/core/Current.java new file mode 100644 index 000000000..421dd973b --- /dev/null +++ b/src/main/java/com/booleanuk/core/Current.java @@ -0,0 +1,13 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Current extends Account{ + + public Current(ArrayList transactions) { + super(transactions); + } + + + +} diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java new file mode 100644 index 000000000..db5a58fa8 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -0,0 +1,141 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Customer { + Savings savings; + Current current; + enum Branch { + OSLO, BERGEN, HARDANGER + } + Customer.Branch branch; + + + public Customer() { + this.branch = Branch.BERGEN; + } + + public Boolean setBranch(Customer.Branch branch) { + this.branch = branch; + return true; + } + public boolean overdraft(Float amount){ + if (amount < current.getBalance()+200){ + //må legge til at dette er ekstraordinert`? + current.withdrawAmount(amount); + return true; + } + return false; + } + + public boolean createRequests(Float amount){ + if (amount < current.getBalance()+400.00f){ + //må legge til at dette er ekstraordinert`? + + return current.addRequest(amount); + + } + + return false; + } + + public boolean approveRequests(Float requests, boolean approved){ + if (current.findAmount(requests) != -999.40f) { + if (approved) { + current.withdrawAmount(requests); + return true; + } + current.removeRequest(requests); + } + + return false; + } + + //Should I have this as boolean or as object as return? when I tried with a object it did not pass the test. + public boolean createAccount(String type){ + + if (type.equalsIgnoreCase("SAVINGS")){ + + savings = new Savings(new ArrayList()); + return true; + } + if (type.equalsIgnoreCase("CURRENT")){ + + current = new Current(new ArrayList()); + return true; + } + + return false; + } + public Float withdrawFunds(Float amount, String type){ + if (type.equalsIgnoreCase("SAVINGS")){ + if (amount <= savings.getBalance()) + return savings.withdrawAmount(amount); + } + if (type.equalsIgnoreCase("CURRENT")){ + if (amount <= current.getBalance()) + return current.withdrawAmount(amount); + + } + return -1.00f; + } + //it does not work with giving account as a parameter? I need to do it with the variables for customer? + public Float depositFunds(Float amount, String type){ + + if (type.equalsIgnoreCase("SAVINGS")){ + return savings.addAmount(amount); + } + if (type.equalsIgnoreCase("CURRENT")){ + return current.addAmount(amount); + + } + + + return -1.00f; + } + //I am not sure how to test it wihout true or false + + public boolean generateBankStatement(String type){ + + if (type.equalsIgnoreCase("SAVINGS")) { + + if (savings.transactions.size() > 0){ + System.out.println("\n ---- BANK STATEMENT ---- \n\tFor Savings account"); + + return savings.generateBankStatement(); + } + } + if (type.equalsIgnoreCase("CURRENT")){ + if (current.transactions.size()>0) { + System.out.println("\n ---- BANK STATEMENT ---- \n\tFor Current account"); + + return current.generateBankStatement(); + } + + } + + return false; + } + public Float getBalanceHardWay(String type){ + + if (type.equalsIgnoreCase("SAVINGS")) { + + if (savings.transactions.size() > 0){ + + return savings.getBalanceHardWay(); + } + } + if (type.equalsIgnoreCase("CURRENT")){ + if (current.transactions.size()>0) { + System.out.println("\n ---- BANK STATEMENT ---- \n\tFor Current account"); + + return current.getBalanceHardWay(); + } + + } + return -1.00f; + + } +} + + diff --git a/src/main/java/com/booleanuk/core/Savings.java b/src/main/java/com/booleanuk/core/Savings.java new file mode 100644 index 000000000..cf7f97387 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Savings.java @@ -0,0 +1,12 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Savings extends Account { + + public Savings( ArrayList transactions) { + super(transactions); + } + + +} diff --git a/src/main/java/com/booleanuk/core/Transaction.java b/src/main/java/com/booleanuk/core/Transaction.java new file mode 100644 index 000000000..07d4c9b6a --- /dev/null +++ b/src/main/java/com/booleanuk/core/Transaction.java @@ -0,0 +1,36 @@ +package com.booleanuk.core; + +import java.time.LocalDate; +import java.util.Objects; + +public class Transaction { + private final LocalDate date; + private final Float amount; + private final Float balance; + private final String type; + + public Transaction(LocalDate date, Float amount, Float balance, String type) { + this.date = date; + this.amount = amount; + this.balance = balance; + this.type = type; + } + + public String getInfo(){ + return "Date: " + date.toString() + "\t Amount: " + type+amount.toString() + "\t balance after " + balance.toString(); + } + + public Float getAmount() { + return amount; + } + + public boolean getType() { + if (Objects.equals(type, "-")){ + return false; + } + else if (Objects.equals(type, "+")) + return true; + + return false; + } +} diff --git a/src/test/java/com/booleanuk/core/ExtensionTest.java b/src/test/java/com/booleanuk/core/ExtensionTest.java new file mode 100644 index 000000000..ad54fc15a --- /dev/null +++ b/src/test/java/com/booleanuk/core/ExtensionTest.java @@ -0,0 +1,124 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static com.booleanuk.core.Customer.Branch.*; + +public class ExtensionTest { + @Test + public void getBalHardWayTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + Assertions.assertEquals(false, customer.generateBankStatement("current") ); + + + //add more deposits, how do I test it? should I just do booleans? + customer.depositFunds(10000.00f, "savings"); + //Assertions.assertEquals(" ", customer.generateBankStatement(current) ); + + customer.depositFunds(10000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + Assertions.assertEquals(true, customer.generateBankStatement("savings") ); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(125000.00f, "savings"); + customer.withdrawFunds(150000.00f, "savings"); + + Assertions.assertEquals(true, customer.generateBankStatement("savings") ); + Assertions.assertEquals(55000.00f, customer.getBalanceHardWay("savings") ); + + customer.depositFunds(10000.00f, "current"); + customer.depositFunds(100000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.depositFunds(100000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.withdrawFunds(60000.00f, "current"); + + + + Assertions.assertEquals(true, customer.generateBankStatement("current") ); + Assertions.assertEquals(0.00f, customer.getBalanceHardWay("current") ); + + + + } + + @Test + public void setBranchTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + Assertions.assertEquals(true, customer.setBranch(OSLO) ); + Assertions.assertEquals(true, customer.setBranch(HARDANGER) ); + Assertions.assertEquals(true, customer.setBranch(BERGEN) ); + + + } + + @Test + public void overdraftTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + + customer.depositFunds(1000.00f, "current"); + Assertions.assertEquals(true, customer.overdraft(1100.00f) ); + + Assertions.assertEquals(true, customer.overdraft(50.00f) ); + + Assertions.assertEquals(false, customer.overdraft(100.00f) ); + customer.depositFunds(1000.00f, "savings"); + Assertions.assertEquals(false, customer.overdraft(900.00f) ); + System.out.println("overdraft----"); + customer.generateBankStatement("current"); + + + + } + @Test + public void approveRequests(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + customer.depositFunds(1000.00f, "current"); + //try this + Assertions.assertEquals(true, customer.createRequests(1300.00f) ); + Assertions.assertEquals(false, customer.approveRequests(1300.00f,false) ); + Assertions.assertEquals(true, customer.createRequests(1300.00f) ); + Assertions.assertEquals(true, customer.approveRequests(1300.00f,true) ); + + customer.depositFunds(1300.00f, "current"); + + Assertions.assertEquals(true, customer.createRequests(1300.00f) ); + + Assertions.assertEquals(false, customer.approveRequests(1340.00f,true) ); + Assertions.assertEquals(false, customer.approveRequests(1300.00f,false) ); + Assertions.assertEquals(true, customer.approveRequests(1300.00f,true) ); + System.out.println("overdraft----"); + customer.generateBankStatement("current"); + } + + +} diff --git a/src/test/java/com/booleanuk/core/coreTest.java b/src/test/java/com/booleanuk/core/coreTest.java new file mode 100644 index 000000000..a90dedc34 --- /dev/null +++ b/src/test/java/com/booleanuk/core/coreTest.java @@ -0,0 +1,124 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class coreTest { + + @Test + public void createAccountTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + + Assertions.assertEquals(true, customer.createAccount("current")); + + Assertions.assertEquals(true, customer.createAccount("saVings")); + + Assertions.assertEquals(false, customer.createAccount("savngs")); + Assertions.assertEquals(false, customer.createAccount("curngs")); + + } + @Test + public void depositTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + Assertions.assertEquals(2500.00f, customer.depositFunds(2500.00f, "savings")); + Assertions.assertEquals(5000.00f, customer.depositFunds(2500.00f, "savings")); + + Assertions.assertEquals(2500.00f, customer.depositFunds(2500.00f, "current")); + Assertions.assertEquals(5000.00f, customer.depositFunds(2500.00f, "current")); + + Assertions.assertEquals(7150.00f, customer.depositFunds(2150.00f, "savings")); + + //not valid + Assertions.assertEquals(-1.00f, customer.depositFunds(2150.00f, "sds")); + + + } + @Test + public void withdrawTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + customer.depositFunds(10000.00f, "savings"); + customer.depositFunds(15000.00f, "current"); + + + + Assertions.assertEquals(2500.00f, customer.withdrawFunds(7500.00f, "savings") ); + Assertions.assertEquals(2000.00f, customer.withdrawFunds(500.00f, "savings") ); + + + Assertions.assertEquals(10000.00f, customer.withdrawFunds(5000.00f, "current") ); + + Assertions.assertEquals(500.00f, customer.withdrawFunds(9500.00f, "current") ); + + //dont work + Assertions.assertEquals(-1.00f, customer.withdrawFunds(7500.00f, "savings") ); + Assertions.assertEquals(-1.00f, customer.withdrawFunds(2500.00f, "savings") ); + Assertions.assertEquals(-1.00f, customer.withdrawFunds(501.00f, "current") ); + + + Assertions.assertEquals(00.00f, customer.withdrawFunds(500.00f, "current") ); + + + } + + @Test + public void bankStatementTest(){ + Customer customer = new Customer(); + ArrayList transactions = new ArrayList(); + Current current = new Current( transactions ); + Savings savings = new Savings(transactions ); + customer.createAccount("current"); + customer.createAccount("saVings"); + + Assertions.assertEquals(false, customer.generateBankStatement("current") ); + + + //add more deposits, how do I test it? should I just do booleans? + customer.depositFunds(10000.00f, "savings"); + //Assertions.assertEquals(" ", customer.generateBankStatement(current) ); + + customer.depositFunds(10000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + Assertions.assertEquals(true, customer.generateBankStatement("savings") ); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(15000.00f, "savings"); + customer.depositFunds(125000.00f, "savings"); + customer.withdrawFunds(150000.00f, "savings"); + + Assertions.assertEquals(true, customer.generateBankStatement("savings") ); + + customer.depositFunds(10000.00f, "current"); + customer.depositFunds(100000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.depositFunds(100000.00f, "current"); + customer.withdrawFunds(50000.00f, "current"); + customer.withdrawFunds(60000.00f, "current"); + + + + Assertions.assertEquals(true, customer.generateBankStatement("current") ); + + + } + + + + +}