-
Notifications
You must be signed in to change notification settings - Fork 131
Magnus Krumbacher #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Magnus-droid
wants to merge
12
commits into
boolean-uk:main
Choose a base branch
from
Magnus-droid:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Magnus Krumbacher #120
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c658bf
Added class diagram and a working Transaction class + tests
Magnus-droid 7e07547
Created different account classes + test for account creation
Magnus-droid 701841d
added transaction tests
Magnus-droid 9bb21f6
Changed return type of GenerateStatement and added tests for it
Magnus-droid 208c821
Implemented generateStatement method, finished core!
Magnus-droid 7fa7b79
Fixed some whitespace bugs in TransactionTest
Magnus-droid c6016bd
Changed getBalance to calculate balance based on transaction history
Magnus-droid de4894d
Removed balance
Magnus-droid f3c11d2
Added domain model for branch and request extension
Magnus-droid 8c4259c
Added branch interface and Norway, Sweden and UK branch classes
Magnus-droid 5cd73f3
Finished branches and their tests
Magnus-droid b7869e5
Updated the Account class to match what is expected of it in the bran…
Magnus-droid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
43
src/main/java/com/booleanuk/core/Extension-domain-model.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | | ||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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