Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/COREEXERCISE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Bank exercise

### Core excercise

**User Stories**

```
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.
```
**Domain Models**

| Classes | Members | Methods | Scenario | Outputs |
|------------|-------------------|-------------------|----------|---------|
| `Customer` | `properties` | `getters/setters` | | |
| | `Account account` | `createAccount` | | |

| Classes | Members | Methods | Scenario | Outputs |
|-----------|-----------------------------------|---------------------------|-------------------------------|---------|
| `Account` | `List<Transactions> transactions` | `generateBankStatement()` | If there are any transactions | String |
| | | | If there are no transactions | String |

| Classes | Members | Methods | Scenario | Outputs |
|-----------|---------------------|-------------|------------------------------|---------|
| `Account` | `BigDecimal amount` | `deposit()` | If amount is a valid int | String |
| | | | If amount is not a valid int | String |

| Classes | Members | Methods | Scenario | Outputs |
|-----------|---------------------|--------------|-----------------------------------|---------|
| `Account` | `BigDecimal amount` | `withdraw()` | If amount is a valid int | String |
| | | | If amount is a valid int | String |
| | | | If amount is greater than balance | String |


| Classes | Members | Methods | Scenario | Outputs |
|---------------|--------------|--------------------|----------|---------|
| `Transaction` | `properties` | `getters/setters` | | |
Binary file added src/Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions src/EXTENSION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Bank exercise

### Core excercise

**User Stories**

```
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.

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.
```
**Domain Models**

| Classes | Members | Methods | Scenario | Outputs |
|------------|-------------------|----------------------|-------------------|---------|
| `Customer` | `properties` | `getters/setters` | | |
| | `Account account` | `createAccount` | | |
| | `Account account` | `requestOverdraft()` | if it is accepted | true |
| | | | if it is rejected | |


| Classes | Members | Methods | Scenario | Outputs |
|-----------|--------------------------------------|-----------------------|-------------------|---------|
| `Manager` | `properties` | `getters/setters` | | |
| | `Customer customer, Account account` | `decideOnOverdraft()` | if it is accepted | true |
| | | | if it is rejected | false |

| Classes | Members | Methods | Scenario | Outputs |
|-----------|-----------------------------------|---------------------------|------------------------------|---------|
| `Account` | `List<Transactions> transactions` | `generateBankStatement()` | If there are any transactions | String |
| | | | If there are no transactions | String |
| | `double amount` | `deposit()` | If amount is a valid int | String |
| | | | If amount is not a valid int | String |
| | `List <Transaction>` | `calculateBalance()` | if there are no transactions | String |
| | | | if there are transactions | String |


| Classes | Members | Methods | Scenario | Outputs |
|-----------|-----------------|--------------|--------------------------------------------------------------------|---------|
| `Account` | `double amount` | `withdraw()` | If amount is a valid int | String |
| | | | If amount is a valid int | String |
| | | | If amount is greater than balance and overdraft has been accepted | String |
| | | | if account is greater than balance and overdraft has been rejected | String |


| Classes | Members | Methods | Scenario | Outputs |
|---------------|--------------|--------------------|----------|---------|
| `Transaction` | `properties` | `getters/setters` | | |


| Classes | Members | Methods | Scenario | Outputs |
|----------|-------------------|-------------------|----------|---------|
| `Branch` | `properties` | `getters/setters` | | |

Binary file added src/diagram-extension.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/main/java/com/booleanuk/core/dto/TransactionResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.booleanuk.core.dto;

import java.math.BigDecimal;

public class TransactionResult {
private boolean success;
private String message;

public TransactionResult(boolean success, String message) {
this.success = success;
this.message = message;
}

public boolean isSuccess() { return success; }

public String getMessage() { return message; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core.exceptions;

public class InsufficientFundsException extends Exception{
public InsufficientFundsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core.exceptions;

public class InvalidAmountException extends Exception {
public InvalidAmountException(String message) {
super(message);
}
}
114 changes: 114 additions & 0 deletions src/main/java/com/booleanuk/core/models/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.booleanuk.core.models;

import com.booleanuk.core.dto.TransactionResult;
import com.booleanuk.core.exceptions.InsufficientFundsException;
import com.booleanuk.core.exceptions.InvalidAmountException;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Account {
private Customer customer;
private int id;
private List<Transaction> transactions;
private int transactionCount = 1;
private boolean canBeOverdrafted = false;
private Branch branch;
private AccountType accountType;

public Account(Customer customer, int id, AccountType accountType) {
this.customer = customer;
this.id = id;
this.accountType = accountType;
this.transactions = new ArrayList<>();
}
public Account(Customer customer, int id, Branch branch, AccountType accountType) {
this.customer = customer;
this.id = id;
this.branch = branch;
this.accountType = accountType;
this.transactions = new ArrayList<>();
}
public Account () {}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}

public TransactionResult deposit(BigDecimal transAmount){
if (transAmount.compareTo(BigDecimal.ZERO) >= 0) {
return new TransactionResult(false, "Amount must be more than 0");
}

Transaction trans = new Transaction(this.transactionCount, LocalDate.now(), transAmount, this, TransactionType.DEPOSIT);
this.transactionCount++;
this.transactions.add(trans);
return new TransactionResult(true, "Deposit successful");


}
public TransactionResult withdraw(BigDecimal transAmount){

if (transAmount.compareTo(BigDecimal.ZERO) <=0) {
throw new InvalidAmountException("Amount must be more than 0");
}
if (calculateBalance().compareTo(transAmount) <0 && !canBeOverdrafted) {
throw new InsufficientFundsException("Insufficient funds. Balance is at: " +calculateBalance().toString());
}

Transaction trans = new Transaction(this.transactionCount, LocalDate.now(), transAmount,this, TransactionType.WITHDRAWAL);
this.transactionCount++;
this.transactions.add(trans);
return new TransactionResult(true, transAmount.toString()+ " have been withdrawn from you account");
}
public StringBuilder generateBankStatements() {

StringBuilder returnString = new StringBuilder("date\t\t||\tcredit\t||\tdebit\t||\tbalance\n");
BigDecimal currentBalance = new BigDecimal("0");

for (Transaction trans : transactions) {
currentBalance = currentBalance.add(trans.getTransAmount());
if (trans.getType() == TransactionType.WITHDRAWAL)
returnString.append(trans.getDate().toString() + "\t||\t" + trans.getTransAmount().toString()+"\t\t||\t\t||\t"+currentBalance+"\n");

if (trans.getTransAmount().compareTo(BigDecimal.ZERO) >= 0)
returnString.append(trans.getDate().toString() + "\t||\t\t\t||\t" + trans.getTransAmount()+"\t||\t"+currentBalance+"\n");


}
return returnString;
}
public boolean requestOverdraft() {
this.canBeOverdrafted = branch.getManager().decideOnOverdraft(this);
return this.canBeOverdrafted;
}
public BigDecimal calculateBalance() {
BigDecimal totalBalance = new BigDecimal("0");
for (Transaction trans : transactions){
totalBalance = totalBalance.add(trans.getTransAmount());
}
return totalBalance;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/core/models/AccountsType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.core.models;

public enum AccountType {
SAVINGS, CURRENT
}
41 changes: 41 additions & 0 deletions src/main/java/com/booleanuk/core/models/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.booleanuk.core.models;

import java.util.ArrayList;
import java.util.List;

public class Branch {
private String name;
private Manager manager;
private List<Account> accounts;

public Branch(String name, Manager manager) {
this.name = name;
this.manager = manager;
this.accounts = new ArrayList<>();
}
public Branch() {}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Manager getManager() {
return manager;
}

public void setManager(Manager manager) {
this.manager = manager;
}

public List<Account> getAccounts() {
return accounts;
}

public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/booleanuk/core/models/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.booleanuk.core.models;

import java.util.ArrayList;
import java.util.List;

public class Customer extends User {

private List<Account> accounts;

public Customer(String name, int id) {
super(name, id);
this.accounts = new ArrayList<>();
}

public Customer() {}

public String createAccount( Account account) {
this.accounts.add(account);
return "Account "+account.getId()+" was added to " + super.getName();
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/booleanuk/core/models/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.booleanuk.core.models;

public class Manager extends User {
private Branch branch;

public Manager(String name, int id, Branch branch) {
super(name, id);
this.branch = branch;
}

public Manager() {
}
public boolean decideOnOverdraft(Account a) {
if (a.getTransactions().size()>3) {
return true;
} else
return false;
}
}
Loading
Loading