Skip to content
Binary file added src/Class-diagram-core.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/class-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.
90 changes: 90 additions & 0 deletions src/domain-model.md

Large diffs are not rendered by default.

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

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

public abstract class Account {
private float balance;
private List<Transaction> transactions;
private static int nextId = 0;
private int accountId;

public Account(){
this.balance = 0;
this.transactions = new ArrayList<>();
this.accountId = nextId;
nextId++;
}

public boolean depositFunds(float amount) {
if (amount > 0){
this.balance += amount;
transactions.add(new Transaction(LocalDate.now(),this.balance, amount, "credit"));
return true;
} return false;
}

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

public boolean withdrawFunds(float amount) {
if (amount > 0 && amount <= this.balance){
this.balance -= amount;
transactions.add(new Transaction(LocalDate.now(),this.balance, amount, "debit"));
return true;
} return false;
}

public String generateStatement() {
String transactionStrings = "";
for(Transaction transaction : transactions){
transactionStrings += transaction.generateStatement();
}
return transactionStrings;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/core/Current.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.core;

public class Current extends Account {

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

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

public class Customer {
private List<Account> accounts;

public Customer(){
this.accounts = new ArrayList<>();
}

public boolean createAccount(String type) {
String accountType = type.toUpperCase();
if(accountType.equals("SAVINGS")){
accounts.add(new Savings());
return true;
} else if (accountType.equals("CURRENT")) {
accounts.add(new Current());
return true;
}
return false;
}


public boolean depositFunds(int accountId, float amount) {
if (!accounts.isEmpty() && amount > 0) {
for (Account account : accounts) {
if (account.getId() == accountId) {
return account.depositFunds(amount);
}
}
}
return false;
}

public boolean withdrawFunds(int accountId, float amount) {
if (!accounts.isEmpty() && amount > 0) {
for (Account account : accounts) {
if (account.getId() == accountId) {
return account.withdrawFunds(amount);
}
}
}
return false;
}

public String generateStatement(int accountId) {
String transactions = String.format(" %-15s || %-15s || %-15s || %s " , "date", "credit", "debit", "balance");
if(!accounts.isEmpty()){
for(Account account: accounts){
if (account.getId() == accountId){
transactions += account.generateStatement();
}
}
} return transactions;

}

public List<Account> getAccounts(){
return accounts;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/core/Savings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.core;

public class Savings extends Account{

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

import java.time.LocalDate;

public class Transaction {
private LocalDate date;
private float balance;
private float amount;
private String type;

public Transaction(LocalDate now, float balance, float amount, String type) {
this.date = now;
this.balance = balance;
this.amount = amount;
this.type = type;
}

public String generateStatement() {
String transaction = "";
if (this.type.equals("credit")) {
transaction += "\n" + (String.format(" %-15s || %-15s || %-15s || %s " , String.valueOf(this.date) , this.amount, " ", this.balance ));
} else {
transaction += "\n" + (String.format(" %-15s || %-15s || %-15s || %s " , String.valueOf(this.date) , " ", this.amount, this.balance )); }
return transaction;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/booleanuk/extension/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.booleanuk.extension;

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

public abstract class Account {
private float balance;
private List<Transaction> transactions;
private static int nextId = 0;
private int accountId;
private String branch;
private boolean canOverdraft;

public Account(){
this.balance = 0;
this.transactions = new ArrayList<>();
this.accountId = nextId++;
this.canOverdraft = false;
}

public boolean depositFunds(float amount) {
if (amount > 0){
this.balance += amount;
transactions.add(new Transaction(LocalDate.now(),this.balance, amount, "credit"));
return true;
} return false;
}

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

public boolean withdrawFunds(float amount) {
if ((amount > 0 && amount <= this.balance) || (amount > 0 && canOverdraft)){
this.balance -= amount;
transactions.add(new Transaction(LocalDate.now(),this.balance, amount, "debit"));
return true;
} return false;
}

public String generateStatement() {
String transactionStrings = "";
for(Transaction transaction : transactions){
transactionStrings += transaction.generateStatement();
}
return transactionStrings;
}

public float getBalance(){
float sum = 0;
if (!transactions.isEmpty()){
for(Transaction transaction: transactions){
if(transaction.getType().equals("credit")){
sum += transaction.getAmount();
} else sum -= transaction.getAmount();
}
} return sum;
}

void withdrawFundsOverdraft(Manager manager, float amount) {
if (manager == null ){
throw new SecurityException("Only managers can change basket capacity");
}
this.balance -= amount;
transactions.add(new Transaction(LocalDate.now(),this.balance, amount, "debit"));
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/extension/Current.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.extension;

public class Current extends Account {

}
75 changes: 75 additions & 0 deletions src/main/java/com/booleanuk/extension/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.booleanuk.extension;

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

public class Customer {
private List<Account> accounts;

public Customer(){
this.accounts = new ArrayList<>();
}

public boolean createAccount(String type) {
String accountType = type.toUpperCase();
if(accountType.equals("SAVINGS")){
accounts.add(new Savings());
return true;
} else if (accountType.equals("CURRENT")) {
accounts.add(new Current());
return true;
}
return false;
}


public boolean depositFunds(int accountId, float amount) {
if (!accounts.isEmpty() && amount > 0) {
for (Account account : accounts) {
if (account.getId() == accountId) {
return account.depositFunds(amount);
}
}
}
return false;
}

public boolean withdrawFunds(int accountId, float amount) {
if (!accounts.isEmpty() && amount > 0) {
for (Account account : accounts) {
if (account.getId() == accountId) {
return account.withdrawFunds(amount);
}
}
}
return false;
}

public String generateStatement(int accountId) {
String transactions = String.format(" %-15s || %-15s || %-15s || %s " , "date", "credit", "debit", "balance");
if(!accounts.isEmpty()){
for(Account account: accounts){
if (account.getId() == accountId){
transactions += account.generateStatement();
}
}
} return transactions;

}

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


public Boolean requestOverdraft(Manager manager, int accountId, float amount) {
if(!accounts.isEmpty()){
for(Account account: accounts){
if (account.getId() == accountId){
return manager.requestOverdraft(account,amount);
}
}
} return false;

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


public class Manager {

public Boolean requestOverdraft(Account account, float amount) {
if(account instanceof Savings){
return false;
}
if (account.getBalance() - amount <= -500){
return false;
} account.withdrawFundsOverdraft(this, amount);
return true;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/extension/Savings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.extension;

public class Savings extends Account {

}
34 changes: 34 additions & 0 deletions src/main/java/com/booleanuk/extension/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.booleanuk.extension;

import java.time.LocalDate;

public class Transaction {
private LocalDate date;
private float balance;
private float amount;
private String type;

public Transaction(LocalDate now, float balance, float amount, String type) {
this.date = now;
this.balance = balance;
this.amount = amount;
this.type = type;
}

public String generateStatement() {
String transaction = "";
if (this.type.equals("credit")) {
transaction += "\n" + (String.format(" %-15s || %-15s || %-15s || %s " , String.valueOf(this.date) , this.amount, " ", this.balance ));
} else {
transaction += "\n" + (String.format(" %-15s || %-15s || %-15s || %s " , String.valueOf(this.date) , " ", this.amount, this.balance )); }
return transaction;
}

public String getType() {
return this.type;
}

public float getAmount() {
return this.amount;
}
}
Loading
Loading