-
Notifications
You must be signed in to change notification settings - Fork 131
Mattias Hedbom #124
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
Hedbom98
wants to merge
29
commits into
boolean-uk:main
Choose a base branch
from
Hedbom98: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
Mattias Hedbom #124
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a0b8d8c
Classdiagram for core-exercise completed. Also I have made all the cl…
a7f9f3c
Test made for method addAccount() in BankBranch-class.
6cc70a9
initial method for addAccount() in BankBranch-class completed.
74c5684
Added another Test for addAccount().
aa42c32
Created the Transaction-class and populated the ArrayList<Transaction…
4a997dc
Added getters and setters for the transaction-class. Changed the cons…
b3e982d
Changed the constructor for the BankAccount class, thus I needed to u…
51329e1
Test for makeDeposit() method completed.
83a3148
makeDeposit() initial method completed. Needed to do a little adjustm…
f36a7c5
makeDeposit() method completed.
e5b61f2
Initial test made for makeWithDraw() method.
f56e60f
Initial method makeWithDraw() completed.
3856d1f
added another test for makeWithDraw().
97f7cc5
method makeWithDraw() completed. Made a small error in the correspodi…
89309ea
Deleted the correctlyFormattedTransaction() from the classdiagram, th…
0b65ddc
Realised that I could add "newBalance" to Transaction class when inst…
6a68e19
Added a getter for the newBalance in the Transaction class. I also ma…
248708a
Completed formatTransactionForBankStatement() i Transaction
b766d93
Test made for generateBankStatements() in BankAccount
f2f7457
Method generateBankStatements() in BankAccount completed. Noticed tha…
eaa6a0a
Test for the first extension where im updating my getter for this.bal…
79800d5
Completed Method for the first extension where im updating my getter …
2ae6ec5
I already had a BankBranch class that have an Arraylist<BankAccount> …
f23d996
I already had a BankBranch class that have an Arraylist<BankAccount> …
8554277
Created test for extension 2
8bc4e5f
created initial method: belongsBankAccountToBankBranch(). I also chan…
cd10a14
renamed attribute bankID in BankAccount to branchID.
d318ba1
Completed belongsBankAccountToBankBranch() method for extension 2
3aee9d2
Added another test for belongsBankAccountToBankBranch() method for ex…
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
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,21 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class Bank { | ||
| private String name; | ||
| private Integer bankID; | ||
| private ArrayList<BankBranch> listOfBranches; | ||
|
|
||
| public Bank(String name, Integer bankID, ArrayList<BankBranch> listOfBranches){ | ||
| this.name = name; | ||
| this.bankID = bankID; | ||
| this.listOfBranches = listOfBranches; | ||
| } | ||
|
|
||
| public Bank(String name, Integer bankID){ | ||
| this.name = name; | ||
| this.bankID = bankID; | ||
| this.listOfBranches = new ArrayList<BankBranch>(); | ||
| } | ||
| } |
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,88 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class BankAccount { | ||
| private Integer uniqueBankAccountNumber; | ||
| private Double balance; | ||
| private ArrayList<Transaction> listOfTransactions; | ||
| private Integer branchID; | ||
|
|
||
| public BankAccount(Integer uniqueBankAccountNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){ | ||
| this.uniqueBankAccountNumber = uniqueBankAccountNumber; | ||
|
|
||
| Double totalBalance = 0D; | ||
| for(Transaction transaction : listOfTransactions){ | ||
| if(transaction.getTypeOfTransaction().equals("Withdraw")){ | ||
| totalBalance -= transaction.getAmount(); | ||
| } | ||
| else { | ||
| totalBalance += transaction.getAmount(); | ||
| } | ||
| } | ||
|
|
||
| this.balance = Math.round(totalBalance * 100.0) / 100.0; | ||
| this.listOfTransactions = listOfTransactions; | ||
| this.branchID = branchID; | ||
| } | ||
|
|
||
| public BankAccount(Integer uniqueBankAccountNumber, Integer branchID){ | ||
| this.uniqueBankAccountNumber = uniqueBankAccountNumber; | ||
| this.balance = 0D; | ||
| this.listOfTransactions = new ArrayList<Transaction>(); | ||
| this.branchID = branchID; | ||
| } | ||
|
|
||
| public Double getBalance() { | ||
| Double calculatedBalance = 0D; | ||
| for(Transaction transaction : listOfTransactions){ | ||
| if(transaction.getTypeOfTransaction().equals("Withdraw")){ | ||
| calculatedBalance -= transaction.getAmount(); | ||
| } | ||
| else { | ||
| calculatedBalance += transaction.getAmount(); | ||
| } | ||
| } | ||
| return calculatedBalance; | ||
| } | ||
|
|
||
| public Integer getUniqueBankNumber() { | ||
| return uniqueBankAccountNumber; | ||
| } | ||
|
|
||
| public ArrayList<Transaction> getListOfTransactions() { | ||
| return listOfTransactions; | ||
| } | ||
|
|
||
| public Integer getBranchID() { | ||
| return branchID; | ||
| } | ||
|
|
||
| public Double makeDeposit(Double amount, LocalDateTime dateTime){ | ||
| if((this.getBalance() + amount) > this.getBalance()){ | ||
| Transaction deposit = new Transaction(dateTime, amount, "Deposit", (this.getBalance() + amount)); | ||
| this.listOfTransactions.add(deposit); | ||
| this.balance += amount; | ||
| } | ||
| return this.balance; | ||
| } | ||
|
|
||
| public Double makeWithDraw(Double amount, LocalDateTime dateTime){ | ||
| if(amount > 0){ | ||
| Transaction withdraw = new Transaction(dateTime, amount, "Withdraw", (this.getBalance() - amount)); | ||
| this.listOfTransactions.add(withdraw); | ||
| this.balance -= amount; | ||
| } | ||
| return this.balance; | ||
| } | ||
|
|
||
| public String generateBankStatements(){ | ||
| String correctBankStatement = "Date || Credit || Debit || Balance\n"; | ||
| System.out.println(this.listOfTransactions.size()); | ||
| for(int i = (this.listOfTransactions.size() - 1); i >= 0; i--){ | ||
| correctBankStatement += this.listOfTransactions.get(i).formatTransactionForBankStatement() + "\n"; | ||
| } | ||
| return correctBankStatement; | ||
| } | ||
| } | ||
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,40 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class BankBranch { | ||
| private Integer branchID; | ||
| private String location; | ||
| private ArrayList<BankAccount> listOfBankAccounts; | ||
|
|
||
| public BankBranch(Integer branchID, String location, ArrayList<BankAccount> listOfBankAccounts){ | ||
| this.branchID = branchID; | ||
| this.location = location; | ||
| this.listOfBankAccounts = listOfBankAccounts; | ||
| } | ||
|
|
||
| public BankBranch(Integer branchID, String location){ | ||
| this.branchID = branchID; | ||
| this.location = location; | ||
| this.listOfBankAccounts = new ArrayList<BankAccount>(); | ||
| } | ||
|
|
||
| public boolean addAccount(BankAccount accountToAdd){ | ||
| for(BankAccount account : this.listOfBankAccounts){ | ||
| if(account.getUniqueBankNumber() == accountToAdd.getUniqueBankNumber()){ | ||
| return false; | ||
| } | ||
| } | ||
| this.listOfBankAccounts.add(accountToAdd); | ||
| return true; | ||
| } | ||
|
|
||
| public Boolean belongsBankAccountToBankBranch(BankAccount bankAccount){ | ||
| for(BankAccount account : this.listOfBankAccounts){ | ||
| if(bankAccount.getUniqueBankNumber() == account.getUniqueBankNumber()){ | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
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,18 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class CurrentAccount extends BankAccount{ | ||
| private Integer uniqueBankNumber; | ||
| private Double balance; | ||
| private ArrayList<Transaction> listOfTransactions; | ||
| private Integer branchID; | ||
|
|
||
| public CurrentAccount(Integer uniqueBankNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){ | ||
| super(uniqueBankNumber, listOfTransactions, branchID); | ||
| } | ||
|
|
||
| public CurrentAccount(Integer uniqueBankNumber, Integer branchID){ | ||
| super(uniqueBankNumber, branchID); | ||
| } | ||
| } |
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,18 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class SavingsAccount extends BankAccount{ | ||
| private Integer uniqueBankNumber; | ||
| private Double balance; | ||
| private ArrayList<Transaction> listOfTransactions; | ||
| private Integer branchID; | ||
|
|
||
| public SavingsAccount(Integer uniqueBankNumber, ArrayList<Transaction> listOfTransactions, Integer branchID){ | ||
| super(uniqueBankNumber, listOfTransactions, branchID); | ||
| } | ||
|
|
||
| public SavingsAccount(Integer uniqueBankNumber, Integer branchID){ | ||
| super(uniqueBankNumber, branchID); | ||
| } | ||
| } |
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,48 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class Transaction { | ||
| private LocalDateTime date; | ||
| private Double amount; | ||
| private String typeOfTransaction; | ||
| private Double newBalance; | ||
|
|
||
| public Transaction(LocalDateTime date, Double amount, String typeOfTransaction, Double newBalance){ | ||
| this.date = date; | ||
| this.amount = amount; | ||
| this.typeOfTransaction = typeOfTransaction; | ||
| this.newBalance = newBalance; | ||
| } | ||
|
|
||
| public LocalDateTime getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public Double getAmount() { | ||
| return amount; | ||
| } | ||
|
|
||
| public String getTypeOfTransaction() { | ||
| return typeOfTransaction; | ||
| } | ||
|
|
||
| public Double getNewBalance() { | ||
| return newBalance; | ||
| } | ||
|
|
||
| public String formatTransactionForBankStatement(){ | ||
| String formatedTransaction = ""; | ||
|
|
||
| formatedTransaction = this.getDate().toString() + " || "; | ||
| if(this.getTypeOfTransaction().equals("Withdraw")){ | ||
| formatedTransaction += " || " + this.getAmount() + " || " + this.getNewBalance(); | ||
| } else if (this.getTypeOfTransaction().equals("Deposit")) { | ||
| formatedTransaction += this.getAmount() + " || || " + this.getNewBalance(); | ||
| } | ||
|
|
||
| return formatedTransaction; | ||
| } | ||
| } |
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,88 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class BankAccountTest { | ||
| private ArrayList<Transaction> listOfTransactions = new ArrayList<>(){{ | ||
| LocalDateTime dateTime1 = LocalDateTime.of(2024, 9, 7, 13, 24, 10); | ||
| Transaction transaction1 = new Transaction(dateTime1, 200D, "Deposit", 200D); | ||
| add(transaction1); | ||
|
|
||
| LocalDateTime dateTime2 = LocalDateTime.of(2024, 11, 5, 10, 30, 8); | ||
| Transaction transaction2 = new Transaction(dateTime2, 700D, "Deposit", 900D); | ||
| add(transaction2); | ||
|
|
||
| LocalDateTime dateTime3 = LocalDateTime.of(2025, 1, 13, 13, 58, 10); | ||
| Transaction transaction3 = new Transaction(dateTime3, 500D, "Withdraw", 400D); | ||
| add(transaction3); | ||
| }}; | ||
|
|
||
| @Test | ||
| public void balanceIncreasesWhenDepositingValidAmount(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
| LocalDateTime dateTime = LocalDateTime.now(); | ||
| dateTime = dateTime.plusDays(4); | ||
|
|
||
| Assertions.assertEquals(600D, account.makeDeposit(200D, dateTime)); | ||
| } | ||
|
|
||
| @Test | ||
| public void balanceStaysTheSameWhenDepositingNonValidAmount(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
| LocalDateTime dateTime = LocalDateTime.now(); | ||
| dateTime = dateTime.plusDays(4); | ||
|
|
||
| Assertions.assertEquals(400D, account.makeDeposit(-200D, dateTime)); | ||
| } | ||
|
|
||
| @Test | ||
| public void balanceDecreasesWhenWithdrawingValidAmount(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
| LocalDateTime dateTime = LocalDateTime.now(); | ||
| dateTime = dateTime.plusDays(4); | ||
|
|
||
| Assertions.assertEquals(300D, account.makeWithDraw(100D, dateTime)); | ||
| } | ||
|
|
||
| @Test | ||
| public void balanceStaysTheSameWhenWithdrawingNonValidAmount(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
| LocalDateTime dateTime = LocalDateTime.now(); | ||
| dateTime = dateTime.plusDays(4); | ||
|
|
||
| Assertions.assertEquals(400D, account.makeWithDraw(-100D, dateTime)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void correctBankStatementsAreOutputed(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
| String correctBankStatement = | ||
| "Date || Credit || Debit || Balance\n" + | ||
| "2025-01-13T13:58:10 || || 500.0 || 400.0\n" + | ||
| "2024-11-05T10:30:08 || 700.0 || || 900.0\n" + | ||
| "2024-09-07T13:24:10 || 200.0 || || 200.0\n"; | ||
|
|
||
| Assertions.assertEquals(correctBankStatement, account.generateBankStatements()); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // EXTENSION 1 | ||
| // Im updating my getter (getBalance()) so it is calculated whenever its called. | ||
| @Test | ||
| public void getterCalculateCorrectly(){ | ||
| BankAccount account = new CurrentAccount(1, listOfTransactions, 1); | ||
|
|
||
| Assertions.assertEquals(400, account.getBalance()); | ||
| } | ||
|
|
||
| } |
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,56 @@ | ||
| package com.booleanuk.core; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class BankBranchTest { | ||
| private ArrayList<BankAccount> listOfBankAccounts = new ArrayList<>(){{ | ||
| BankAccount currentAccount = new CurrentAccount(1, 1); | ||
| add(currentAccount); | ||
|
|
||
| BankAccount savingsAccount = new SavingsAccount(2, 1); | ||
| add(savingsAccount); | ||
| }}; | ||
|
|
||
|
|
||
| @Test | ||
| public void bankAccountIdExistsAlready(){ | ||
| BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts); | ||
| BankAccount existingAccount = new CurrentAccount(1, 1); | ||
|
|
||
| Assertions.assertFalse(bankBranch.addAccount(existingAccount)); | ||
| } | ||
|
|
||
| @Test | ||
| public void bankAccountIdDontExists(){ | ||
| BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts); | ||
| BankAccount newAccount = new CurrentAccount(3, 1); | ||
|
|
||
| Assertions.assertTrue(bankBranch.addAccount(newAccount)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // EXTENSION 2 | ||
| @Test | ||
| public void bankAccountIsIncludedInBankBranchListOfBankAccounts(){ | ||
| BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts); | ||
| BankAccount newAccount = new CurrentAccount(3, 1); | ||
| bankBranch.addAccount(newAccount); | ||
|
|
||
| Assertions.assertTrue(bankBranch.belongsBankAccountToBankBranch(newAccount)); | ||
| } | ||
|
|
||
| @Test | ||
| public void bankAccountIsNotIncludedInBankBranchListOfBankAccounts(){ | ||
| BankBranch bankBranch = new BankBranch(1, "Gothenburg", listOfBankAccounts); | ||
| BankAccount newAccount = new CurrentAccount(3, 1); | ||
|
|
||
| Assertions.assertFalse(bankBranch.belongsBankAccountToBankBranch(newAccount)); | ||
| } | ||
|
|
||
|
|
||
| } |
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.
It would be better to use an enum here to get strong typing