-
Notifications
You must be signed in to change notification settings - Fork 23
feat: adds Savings Account and Business Checking Account and Tests #530
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
3 commits
Select commit
Hold shift + click to select a range
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
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
public final class BusinessCheckingAccount extends CheckingAccount { | ||
|
||
/** | ||
* Represents a business checking account. | ||
* | ||
* <p>It is a subclass of BankAccount and inherits its properties and methods. | ||
*/ | ||
public BusinessCheckingAccount( | ||
String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
if (!hasBusinessOwner(owners)) { | ||
throw new IllegalArgumentException( | ||
"A BusinessCheckingAccount must have at least one business owner."); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the owners of the account include at least one business customer. | ||
* | ||
* @param owners The owners of the account. | ||
* @return true if there is a business owner, false otherwise. | ||
*/ | ||
private boolean hasBusinessOwner(Set<Customer> owners) { | ||
return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...on_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCustomer.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,10 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.UUID; | ||
|
||
public class BusinessCustomer extends Customer { | ||
|
||
public BusinessCustomer(UUID id, String name) { | ||
super(id, name); | ||
} | ||
} |
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
115 changes: 115 additions & 0 deletions
115
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,115 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
import java.util.Set; | ||
|
||
public class SavingsAccount { | ||
private final Set<Customer> owners; | ||
private final String accountNumber; | ||
private double balance; | ||
private boolean isActive; | ||
|
||
/** | ||
* Represents a savings account. | ||
* | ||
* <p>It is a subclass of BankAccount and inherits its properties and methods. | ||
*/ | ||
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
this.accountNumber = accountNumber; | ||
this.owners = owners; | ||
this.balance = initialBalance; | ||
isActive = true; | ||
} | ||
|
||
/** | ||
* Gets the account number. | ||
* | ||
* @return The account number. | ||
*/ | ||
public String getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
/** | ||
* Gets the owners of the account. | ||
* | ||
* @return The owners of the account. | ||
*/ | ||
public Set<Customer> getOwners() { | ||
return owners; | ||
} | ||
|
||
/** | ||
* Gets the balance of the account. | ||
* | ||
* @return The balance of the account. | ||
*/ | ||
public double getBalance() { | ||
return balance; | ||
} | ||
|
||
/** | ||
* Deposits funds into the account. | ||
* | ||
* @param amount The amount to deposit. | ||
*/ | ||
public void deposit(double amount) throws IllegalStateException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot deposit to a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalArgumentException("Deposit amount must be positive"); | ||
} | ||
balance += amount; | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount | ||
* @throws InsufficientFundsException | ||
*/ | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot withdraw from a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalStateException("Withdrawal amount must be positive"); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the account is closed. | ||
* | ||
* @return True if the account is closed, otherwise false. | ||
*/ | ||
public boolean isClosed() { | ||
return !isActive; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return accountNumber.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof SavingsAccount other) { | ||
return accountNumber.equals(other.accountNumber); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CheckingAccount{" | ||
+ "accountNumber='" | ||
+ accountNumber | ||
+ '\'' | ||
+ ", balance=" | ||
+ balance | ||
+ ", isActive=" | ||
+ isActive | ||
+ '}'; | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...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,52 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BusinessCheckingAccountTest { | ||
@Test | ||
public void testAddBusinessCheckingAccountWithBusinessOwner() { | ||
|
||
BankAtm atm = new BankAtm(); | ||
|
||
UUID customerId = UUID.randomUUID(); | ||
|
||
BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1"); | ||
Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1"); | ||
|
||
Set<Customer> owners = new HashSet<>(); | ||
owners.add(businessCustomer); | ||
owners.add(personalCustomer); | ||
|
||
BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("12345", owners, 2000.00); | ||
atm.addAccount(businessAccount); | ||
assertNotNull((atm.findAccountsByCustomerId(customerId))); | ||
} | ||
|
||
@Test | ||
public void testAddBusinessCheckingAccountWithoutBusinessOwner() { | ||
|
||
BankAtm atm = new BankAtm(); | ||
|
||
Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1"); | ||
Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2"); | ||
|
||
Set<Customer> owners = new HashSet<>(); | ||
owners.add(personalCustomer1); | ||
owners.add(personalCustomer2); | ||
|
||
IllegalArgumentException exception = | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> { | ||
BusinessCheckingAccount businessAccount = | ||
new BusinessCheckingAccount("12345", owners, 2000.00); | ||
atm.addAccount(businessAccount); | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
..._17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.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,27 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SavingsAccountTest { | ||
@Test | ||
void testSavingsAccountCreation() { | ||
Set<Customer> owners = Set.of(new Customer(null, "John Doe")); | ||
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); | ||
|
||
assertEquals("123456789", savingsAccount.getAccountNumber()); | ||
assertEquals(owners, savingsAccount.getOwners()); | ||
assertEquals(1000.0, savingsAccount.getBalance()); | ||
} | ||
|
||
@Test | ||
void testDepositFunds() { | ||
Set<Customer> owners = Set.of(new Customer(null, "John Doe")); | ||
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); | ||
|
||
savingsAccount.deposit(500.0); | ||
assertEquals(1500.0, savingsAccount.getBalance()); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.