-
Notifications
You must be signed in to change notification settings - Fork 23
feat: implements bank account system with checking and savings account. #531
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
476f31c
feat: implements bank account system with checking and savings accoun…
enyabuti a02f3a8
updated my savings account test to use the new constructor
enyabuti bf71953
made changes to the following files:
enyabuti 0bb67d3
feat: adds BusinessCheckingAccount and BankAccount classes, update te…
enyabuti 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
73 changes: 73 additions & 0 deletions
73
lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java
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,73 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
import java.util.Set; | ||
|
||
/** | ||
* Checks if the account is closed | ||
* | ||
* @returns true if the account is closed, otherwise false | ||
*/ | ||
public abstract class BankAccount { | ||
private String accountNumber; | ||
protected double balance; | ||
private Set<Customer> owners; | ||
private boolean isClosed; | ||
|
||
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
this.accountNumber = accountNumber; | ||
this.owners = owners; | ||
this.balance = initialBalance; | ||
} | ||
|
||
public boolean isActive() { | ||
return isClosed; | ||
} | ||
|
||
/** | ||
* Gets the owners of the account. | ||
* | ||
* @return The owners of the account. | ||
*/ | ||
public Set<Customer> getOwners() { | ||
return owners; | ||
} | ||
|
||
/** | ||
* Gets the account number. | ||
* | ||
* @return The account number. | ||
*/ | ||
public String getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount | ||
* @throws InsufficientFundsException | ||
*/ | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (amount <= 0) { | ||
throw new IllegalStateException("Withdrawal amount must be positive"); | ||
} | ||
if (balance < amount) { | ||
throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); | ||
} | ||
balance -= amount; | ||
} | ||
|
||
/** | ||
* Deposits funds into the account. | ||
* | ||
* @param amount The amount to deposit. | ||
*/ | ||
public void deposit(double amount) throws IllegalStateException { | ||
|
||
if (amount <= 0) { | ||
throw new IllegalArgumentException("Deposit amount must be positive"); | ||
} | ||
balance += 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ public class BankAtm { | |
|
||
private final Map<UUID, Customer> customerById = new HashMap<>(); | ||
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>(); | ||
private final Map<String, SavingsAccount> savingsAccountByNumber = new HashMap<>(); | ||
|
||
/** | ||
* Adds a checking account to the bank. | ||
|
@@ -27,13 +28,28 @@ public void addAccount(CheckingAccount account) { | |
}); | ||
} | ||
|
||
/** | ||
* Adds a savings account to the bank. | ||
* | ||
* @param account The account to add. | ||
*/ | ||
public void addAccount(SavingsAccount account) { | ||
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. Nope, can't add a new method. |
||
savingsAccountByNumber.put(account.getAccountNumber(), account); | ||
account | ||
.getOwners() | ||
.forEach( | ||
owner -> { | ||
customerById.put(owner.getId(), owner); | ||
}); | ||
} | ||
|
||
/** | ||
* Finds all accounts owned by a customer. | ||
* | ||
* @param customerId The ID of the customer. | ||
* @return The unique set of accounts owned by the customer. | ||
*/ | ||
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) { | ||
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) { | ||
return customerById.containsKey(customerId) | ||
? customerById.get(customerId).getAccounts() | ||
: Set.of(); | ||
|
19 changes: 19 additions & 0 deletions
19
...ank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java
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,19 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
/** Represents a business checking account. */ | ||
public class BusinessCheckingAccount extends BankAccount { | ||
|
||
/** | ||
* Creates a new business checking account. | ||
* | ||
* @param accountNumber The account number. | ||
* @param owners The owners of the account. | ||
* @param initialBalance The initial balance of the account. | ||
*/ | ||
public BusinessCheckingAccount( | ||
String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
} |
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
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
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
25 changes: 25 additions & 0 deletions
25
lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java
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,25 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
/** Represents a savings account that doesn't allow checks. */ | ||
public class SavingsAccount extends BankAccount { | ||
private boolean isActive = true; | ||
|
||
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
|
||
protected boolean isClosed() { | ||
throw new IllegalStateException("Cannot check if account is closed"); | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
|
||
public void closeAccount() { | ||
throw new IllegalStateException("Cannot close account with a positive balance"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAccountTest.java
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.codedifferently.lesson17.bank; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BankAccountTest { | ||
@Test | ||
void testIsActive() { | ||
// Arrange | ||
Set<Customer> owners = new HashSet<>(); | ||
owners.add(new Customer(UUID.randomUUID(), "John Doe")); | ||
BankAccount account = new CheckingAccount("123456789", owners, 100.0); | ||
// Act | ||
boolean isActive = account.isActive(); | ||
// Assert | ||
assertThat(!isActive).isTrue(); | ||
} | ||
|
||
@Test | ||
void testGetOwners() { | ||
// Act | ||
Set<Customer> owners = new HashSet<>(); | ||
owners.add(new Customer(UUID.randomUUID(), "John Doe")); | ||
BankAccount account = new CheckingAccount("123456789", owners, 100.0); | ||
// Assert | ||
assertEquals(owners, account.getOwners()); | ||
} | ||
|
||
@Test | ||
void testWithdrawFunds() { | ||
// Arrange | ||
double initialBalance = 100.0; | ||
double withdrawAmount = 50.0; | ||
double expectedBalance = initialBalance - withdrawAmount; | ||
|
||
// Act | ||
BankAccount account = new CheckingAccount("123456789", null, initialBalance); | ||
account.withdraw(withdrawAmount); | ||
|
||
// Assert | ||
assertThat(account.balance).isEqualTo(expectedBalance); | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
...bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java
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,3 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
public class BusinessCheckingAccountTest {} |
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.
Still needed to do more work to integrate the
BankAccount
class with theBankAtm
implementation.