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
86 changes: 86 additions & 0 deletions src/main/java/com/booleanuk/core/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class Account {

private final ArrayList<Transaction> transactions;
private final int accountNumber;
private StringBuilder stringBuilder = new StringBuilder();
private int prefix;
private double limit;
private boolean overDraftRequested;
private double requestedLimit;


public Account(int prefix, int accountNumber) {
this.accountNumber = accountNumber;
this.transactions = new ArrayList<>();
this.stringBuilder.insert(0, String.format("%21s || %10s || %8s || %8s\n", "Date", "Withdrawal", "Deposit", "Balance"));
this.prefix = prefix;
this.limit = 0.0;
this.requestedLimit = 0.0;
this.overDraftRequested = false;
}

public void makeTransaction(double amount) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using a transactiontype enum to figure out whether to do a deposit or withdrawal. It does not seem right to have a makeTransaction method also generate a statement. Consider having a getBalance method and call that in a new function that is only responsible for generating statements

Transaction transaction = new Transaction(amount);
this.transactions.add(transaction);
//It might seem a bit odd to already start generating the statement here, but the only alternative I can think of
//is that Transaction gets to know about the balance of the account despite it just being a record class.
//Looping over the transaction list after every transaction has been completed only gives the final balance, not the balance after each transaction
this.stringBuilder.append(String.format(transaction + "%8s\n", getBalance()));

}

public StringBuilder generateStatement() {
return this.stringBuilder;
}

public ArrayList<Transaction> getTransactions() {
return this.transactions;
}

public double getBalance() {
double total = 0.0;
for (Transaction transaction: this.transactions) {
if (transaction.getType().equals(Transaction.Type.DEPOSIT)) {
total += transaction.getAmount();
} else {
if (total - transaction.getAmount() > this.limit) {
total -= transaction.getAmount();
}
}
}
return total;
}

public int getAccountNumber() {
return this.accountNumber;
}

public int getPrefix() {
return this.prefix;
}

public double getLimit() {
return this.limit;
}

public void requestOverdraft(double amount) {
this.overDraftRequested = true;
this.requestedLimit = amount;
}

public void changeLimit() {
this.limit -= this.requestedLimit;
this.overDraftRequested = false;
this.requestedLimit = 0.0;
}

public boolean getRequestStatus() {
return this.overDraftRequested;
}


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

import java.util.ArrayList;

public interface Branch {
enum Type {SAVINGS, CURRENT};
void handleOverdraftRequest(double amountInCurrency, Account account);
void createAndAddAccount(int accountNumber, Type type);


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

public class CurrentAccount extends Account {
private boolean overDraftRequested;
private double limit;
private double requestedLimit;

public CurrentAccount(int prefix, int accountNumber) {
super(prefix, accountNumber);
this.limit = 0.0;
this.requestedLimit = 0.0;
this.overDraftRequested = false;
}

}
43 changes: 43 additions & 0 deletions src/main/java/com/booleanuk/core/Extension-domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
I have no idea if it's a good idea to use interfaces for this, but I still decided to use interfaces for practice.

# Interface: Branch

| Members | Methods |
|-----------|----------------------------------------------------------------|
| enum Type | void createAndAddAccount((int accountNumber, Type accountType) |
| | void handleOverdraftRequest(double amount, Account account) |


# Class: NorwayBranch

| Implements interface | Members | Methods | Scenario | Result/Output |
|----------------------|------------------------------|---------------------------------------------------------------|----------------------------------------------------|-----------------------------------------------------------------------|
| Branch | ArrayList\<Account> accounts | void createAndAddAccount(int accountNumber, Type accountType) | | account gets created with norwayPrefix and added to the accounts-list |
| | int norwayPrefix | void handleOverdraftRequest(double amount, Account account) | account type == SavingsAccount | Deny (do nothing) |
| | | | account type == CurrentAccount, but amount > 1000 | Deny (do nothing) |
| | | | account type == CurrentAccount, and amount <= 1000 | Accept (update the lower limit) |



# Class: SwedenBranch

| Implements interface | Members | Methods | Scenario | Result/Output |
|----------------------|------------------------------|---------------------------------------------------------------|---------------------------------------------------|-----------------------------------------------------------------------|
| Branch | ArrayList\<Account> accounts | void createAndAddAccount(int accountNumber, Type accountType) | | account gets created with swedenPrefix and added to the accounts-list |
| | int swedenPrefix | void handleOverdraftRequest(double amount, Account account) | account type == SavingsAccount | Deny (do nothing) |
| | | | account type == CurrentAccount, but amount > 982 | Deny (do nothing) |
| | | | account type == CurrentAccount, and amount <= 982 | Accept (update the lower limit) |



# Class: UKBranch

| Implements interface | Members | Methods | Scenario | Result/Output |
|----------------------|------------------------------|---------------------------------------------------------------|--------------------------------------------------|-------------------------------------------------------------------|
| Branch | ArrayList\<Account> accounts | void createAndAddAccount(int accountNumber, Type accountType) | | account gets created with ukPrefix and added to the accounts-list |
| | int ukPrefix | void handleOverdraftRequest(double amount, Account account) | account type == SavingsAccount | Deny (do nothing) |
| | | | account type == CurrentAccount, but amount > 72 | Deny (do nothing) |
| | | | account type == CurrentAccount, and amount <= 72 | Accept (update the lower limit) |



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

import java.util.ArrayList;

public class NorwayBranch implements Branch {

private final ArrayList<Account> accounts = new ArrayList<>();
private final int prefix = 1;
private double overdraftLimit = 1000.0;


@Override
public void handleOverdraftRequest(double amountInKroner, Account account) {
if (account instanceof CurrentAccount && account.getRequestStatus() && amountInKroner <= this.overdraftLimit) {
account.changeLimit();
}
}

@Override
public void createAndAddAccount(int accountNumber, Type type) {
if (type.equals(Type.SAVINGS)) {
this.accounts.add(new SavingsAccount(this.prefix, accountNumber));
} else if (type.equals(Type.CURRENT)) {
this.accounts.add(new CurrentAccount(this.prefix, accountNumber));
} else {
System.out.println("Invalid account type!");
}
}

public ArrayList<Account> getAccounts() {
return this.accounts;
}
}
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(int prefix, int accountNumber) {
super(prefix, accountNumber);
}
//cant have overdraft
}
35 changes: 35 additions & 0 deletions src/main/java/com/booleanuk/core/SwedenBranch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.booleanuk.core;


import java.util.ArrayList;

public class SwedenBranch implements Branch {

private final ArrayList<Account> accounts = new ArrayList<>();
private final int prefix = 2;
private double overdraftLimit = 982.0;

@Override
public void handleOverdraftRequest(double amountInKroner, Account account) {
if (account instanceof CurrentAccount && account.getRequestStatus() && amountInKroner <= this.overdraftLimit) {
account.changeLimit();
}
}

@Override
public void createAndAddAccount(int accountNumber, Type type) {
if (type.equals(Type.SAVINGS)) {
this.accounts.add(new SavingsAccount(this.prefix, accountNumber));
} else if (type.equals(Type.CURRENT)) {
this.accounts.add(new CurrentAccount(this.prefix, accountNumber));
} else {
System.out.println("Invalid account type!");
}
}


public ArrayList<Account> getAccounts() {
return this.accounts;
}

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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;


public class Transaction {
private final String date; //needs time as well!!
private double amount;
enum Type {WITHDRAWAL, DEPOSIT};
private final Type type;
private final UUID uid;

public Transaction(double amount){
this.date = new SimpleDateFormat("yyyy.MM.dd HH:mm z").format(Calendar.getInstance().getTime());
this.amount = amount;
this.type = this.setType();
this.uid = UUID.randomUUID();
}

public String getDate() {
return date;
}

//Don't want other classes to be able to set the type so this is done internally

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

//This means that even if a negative transaction amount is passed in, the behaviour is still expected.
private Type setType() {
if (this.amount < 0.0) {
this.amount *= -1;
return Type.WITHDRAWAL;
} else {
return Type.DEPOSIT;
}
}

public double getAmount() {
return amount;
}

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

public UUID getUid() {
return this.uid;
}

@Override
public String toString() {
if (this.type.equals(Type.WITHDRAWAL)) {
return String.format("%21s || %10s || %8s || ", this.date, this.amount, " ");
} else {
return String.format("%21s || %10s || %8s || ", this.date, " ", this.amount);
}

}

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


import java.util.ArrayList;

public class UKBranch implements Branch {

private final ArrayList<Account> accounts = new ArrayList<>();
private final int prefix = 3;
private double overdraftLimit = 72.0;

@Override
public void handleOverdraftRequest(double amountInKroner, Account account) {
if (account instanceof CurrentAccount && account.getRequestStatus() && amountInKroner <= this.overdraftLimit) {
account.changeLimit();
}
}

@Override
public void createAndAddAccount(int accountNumber, Type type) {
if (type.equals(Type.SAVINGS)) {
this.accounts.add(new SavingsAccount(this.prefix, accountNumber));
} else if (type.equals(Type.CURRENT)) {
this.accounts.add(new CurrentAccount(this.prefix, accountNumber));
} else {
System.out.println("Invalid account type!");
}
}

public ArrayList<Account> getAccounts() {
return this.accounts;
}

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class AccountTest {

@Test
public void createAccountWithGivenNumber() {
//Check if an account is created correctly with default values with given number
Account accountWithGivenNumber = new Account(1, 12345678);
Assertions.assertEquals(12345678, accountWithGivenNumber.getAccountNumber());
Assertions.assertEquals(0.0, accountWithGivenNumber.getBalance());
Assertions.assertEquals(new ArrayList<Transaction>(), accountWithGivenNumber.getTransactions());
}

@Test
public void testMakeTransactionDeposit() {
Account account = new Account(1, 12345678);
account.makeTransaction(100.0);
Assertions.assertEquals(100.0, account.getBalance());
Assertions.assertEquals(Transaction.Type.DEPOSIT, account.getTransactions().getFirst().getType());
}

@Test
public void testMakeTransactionWithdrawal() {
Account account = new Account( 1, 12345678);
account.makeTransaction(500.0);
account.makeTransaction(-100.0);
Assertions.assertEquals(400.0, account.getBalance());
Assertions.assertEquals(Transaction.Type.WITHDRAWAL, account.getTransactions().get(1).getType());
}

@Test
public void testStatementGeneration() {
Account account = new Account( 1, 12345678);
account.makeTransaction(500.0);
account.makeTransaction(-100.0);
account.makeTransaction(150.0);
String date = new SimpleDateFormat("yyyy.MM.dd HH:mm z").format(Calendar.getInstance().getTime());
StringBuilder stringbuilder = new StringBuilder();
stringbuilder.append(String.format("%21s || %10s || %8s || %8s\n", "Date", "Withdrawal", "Deposit", "Balance"));
stringbuilder.append(String.format("%21s || %10s || %8s || %8s\n", date, " ", "500.0", "500.0"));
stringbuilder.append(String.format("%21s || %10s || %8s || %8s\n", date, "100.0", " ", "400.0"));
stringbuilder.append(String.format("%21s || %10s || %8s || %8s\n", date, " ", "150.0", "550.0"));
Assertions.assertEquals(0, stringbuilder.compareTo(account.generateStatement()));

}







}
Loading