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
99 changes: 99 additions & 0 deletions src/main/java/com/booleanuk/core/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.booleanuk.core;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class Account {
private final String branch;
private final Manager manager;
private final HashMap<String, List<Integer>> transactions;
private final int id;

public Account(String branch, int id, Manager manager){
this.branch = branch;
this.manager = manager;
this.id = id;
this.transactions = new HashMap<>();
}

public int getId(){
return this.id;
}

public double getBalance(){
double balance = 0.0;
for (Map.Entry<String, List<Integer>> entry : transactions.entrySet()) {
List<Integer> value = entry.getValue();
for (int trans : value) {
balance += trans;
}
}
return balance;
}

public String getBranch(){
return this.branch;
}

public HashMap<String, List<Integer>> getTransactions(){
return this.transactions;
}

public boolean deposit(int amount){
if (amount < 0) { return false; }
// create transaction
String date = currentDate();
if (!transactions.containsKey(date)){
transactions.put(date, new ArrayList<>(){{add(amount);}});
} else {
transactions.get(date).add(amount);
}
return true;
}

public boolean withdraw(int amount){
if (!manager.approveDraft(amount, getBalance()) || getBalance()-amount < 0 && this instanceof SavingsAccount) {
return false;
}
// create transaction
String date = currentDate();
if (!transactions.containsKey(date)){
transactions.put(date, new ArrayList<>(){{add(-amount);}});
} else {
transactions.get(date).add(-amount);
}
return true;
}

public String currentDate(){
LocalDateTime time = LocalDateTime.now();
String day = String.valueOf(time.getDayOfMonth());
String month = String.valueOf(time.getMonthValue());
String year = String.valueOf(time.getYear());
return day + "/" + month + "/" + year;
}

public String generateStatement(){
StringBuilder res = new StringBuilder();
String start = "|" + "Date " + " |" + "Deposit" + " |" + "Withdraw" +" |" + "Balance" + " |\n";
res.append(start);
double total = 0;
for (Map.Entry<String, List<Integer>> entry : transactions.entrySet()) {
String date = entry.getKey();
List<Integer> value = entry.getValue();
for (int trans : value) {
total += trans;
String curr;
if (trans > 0) {
curr = "|" + date + " |" + trans + " | |" + total + " |\n";
} else {
curr = "|" + date + " |" + " |" + trans + " |" + total + " |\n";
}
res.append(curr);
}
}
return res.toString();
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/booleanuk/core/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.booleanuk.core;
import java.util.ArrayList;
import java.util.List;

public class Bank {
private final String name;
private final List<Account> allAccounts;
private int idIncrementer;
Manager manager;

public Bank(String name, Manager manager){
this.name = name;
this.allAccounts = new ArrayList<>();
this.idIncrementer = 1;
this.manager = manager;
}

public String getName(){
return this.name;
}

public List<Account> getAllAccounts(){
return this.allAccounts;
}

public Account createCurrentAccount(Customer customer){
Account newAccount = new CurrentAccount(customer.getBranch(), idIncrementer, manager);
allAccounts.add(newAccount);
this.idIncrementer++;
return newAccount;
}

public Account createSavingsAccount(Customer customer){
Account newAccount = new SavingsAccount(customer.getBranch(), idIncrementer, manager);
allAccounts.add(newAccount);
this.idIncrementer++;
return newAccount;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core;

public class CurrentAccount extends Account {

public CurrentAccount(String branch, int id, Manager manager){
super(branch, id, manager);
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/booleanuk/core/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.booleanuk.core;

public class Customer {
private final Bank bank;
private final String name;
private final String branch;
private SavingsAccount savingsAccount = null;
private CurrentAccount currentAccount = null;

public Customer(Bank bank, String name, String branch){
this.bank = bank;
this.name = name;
this.branch = branch;
}

public String getName(){
return this.name;
}

public String getBranch(){
return this.branch;
}

public SavingsAccount getSavingsAccount(){
return this.savingsAccount;
}

public CurrentAccount getCurrentAccount(){
return this.currentAccount;
}

public boolean createCurrentAccount(){
if (currentAccount != null){ return false; }

Account newAccount = this.bank.createCurrentAccount(this);
this.currentAccount = (CurrentAccount) newAccount;
return currentAccount != null;
}

public boolean createSavingsAccount(){
if (savingsAccount != null) { return false; }

Account newAccount = this.bank.createSavingsAccount(this);
this.savingsAccount = (SavingsAccount) newAccount;
return savingsAccount != null;
}

public boolean deposit(Account account, int amount){
return account.deposit(amount);
}

public boolean withdraw(Account account, int amount){
return account.withdraw(amount);
}

public String generateStatement(Account account){
return account.generateStatement();
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/booleanuk/core/DomainModel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

| Class | Method | Scenario | Output |
|----------|---------------------------------------------|---------------------------------------------|--------|
| Customer | createCurrentAccount() | If current account already exists | false |
| | | If no current account exists | true |
| | | | |
| Customer | createSavingsAccount() | If savings account already exists | false |
| | | If no savings account exists | true |
| | | | |
| Customer | generateBankStatements(Account account)) | If account does not exist | null |
| | | If account exists | String |
| | | | |
| Customer | deposit(Account account, int amount) | If amount < 0 | false |
| | | If amount is > 0 | true |
| | | | |
| Customer | withdraw(Account account, int amount) | If amount is larger than balance | false |
| | | If amount is less or equal to balance | true |
| | | | |
| Bank | createCurrentAccount(Customer customer) | If customer has a current account | false |
| | | If customer does not have a current account | true |
| | | | |
| Bank | createSavingsAccount(Customer customer) | If customer has a savings account | false |
| | | If customer does not have a savings account | true |
| | | | |
| Bank | generateStatement(Account account) | If account does not exist | null |
| | | If account exists | String |
| | | | |
| Account | deposit(int amount) | If amount is < 0 | false |
| | | If amount is > 0 | true |
| | | | |
| Account | withdraw(int amount) | If amount is larger than balance | false |
| | | If amount is less or equal to balance | true |
| | | | |
| Account | generateStatement() | If list of statements is empty | null |
| | | If list of statements is not empty | String |
| | | | |
| Manager | approveDraft(double amount, double currBal) | If amount is too large | false |
| | | If amount is within the negative limit | true |
| | | | |
15 changes: 15 additions & 0 deletions src/main/java/com/booleanuk/core/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.booleanuk.core;

public class Manager {
String name;
double draftLimit;

public Manager(String name, double draftLimit){
this.name = name;
this.draftLimit = draftLimit;
}

public boolean approveDraft(double amount, double currentBalance){
return (currentBalance-amount) > draftLimit;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core;

public class SavingsAccount extends Account{

public SavingsAccount(String branch, int id, Manager manager){
super(branch, id, manager);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading