-
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 1 commit
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
32 changes: 32 additions & 0 deletions
32
...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,32 @@ | ||
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 CheckingAccount 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); | ||
Meiko-S22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
} |
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
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; | ||
|
||
|
||
|
||
|
||
public class SavingsAccount extends CheckingAccount { | ||
Meiko-S22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Represents a savings account. | ||
* | ||
* <p>It is a subclass of CheckingAccount and inherits its properties and methods. | ||
*/ | ||
|
||
|
||
|
||
public SavingsAccount (String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
|
||
public void depositFunds(Check check) { | ||
throw new UnsupportedOperationException("Cannot deposit check into a savings account"); | ||
Meiko-S22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...bank/bank_app/src/test/java/com/codedifferently/lesson17/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,60 @@ | ||
package com.codedifferently.lesson17; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.codedifferently.lesson17.bank.BankAtm; | ||
import com.codedifferently.lesson17.bank.BusinessCheckingAccount; | ||
import com.codedifferently.lesson17.bank.BusinessCustomer; | ||
import com.codedifferently.lesson17.bank.Customer; | ||
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); | ||
}); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
..._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,40 @@ | ||
package com.codedifferently.lesson17.bank; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
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()); | ||
} | ||
|
||
@Test | ||
void testDepositCheck() { | ||
Meiko-S22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Set<Customer> owners = Set.of(new Customer(null, "John Doe")); | ||
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); | ||
Check check = new Check(null, 200.0, savingsAccount); | ||
|
||
assertThatExceptionOfType(UnsupportedOperationException.class) | ||
.isThrownBy(() -> savingsAccount.depositFunds(check)) | ||
.withMessage("Cannot deposit check into a savings account"); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.