-
Notifications
You must be signed in to change notification settings - Fork 23
feat: try to add savingAccount to bankATMLesson 17 #525
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
Changes from 16 commits
fc92fbb
e42056b
d7ca3b3
42cbca7
c57408a
af8eef3
7b24560
d3239c0
41cb92f
70d28f7
1dde77b
74fb0ca
957c931
636c494
f76c59d
1e6771f
9c6bf25
4ade1fc
133525f
98052f7
ba031fa
3f8cffa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
|
||
public class BusinessAccount extends CheckingAccount { | ||
|
||
/** | ||
* Creates a new business account. | ||
* | ||
* @param accountNumber The account number. | ||
* @param owners The owners of the account. | ||
* @param initialBalance The initial balance of the account. | ||
*/ | ||
public BusinessAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
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. Need to check that one of the owners is a business, right? |
||
super(accountNumber, owners, initialBalance); | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount The amount to withdraw. | ||
* @throws InsufficientFundsException If there are insufficient funds in the account. | ||
*/ | ||
@Override | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
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. Don't even need to override the superclass method since it does the same thing. |
||
if (amount > getBalance()) { | ||
throw new InsufficientFundsException("Insufficient funds in the account."); | ||
} | ||
super.withdraw(amount); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,27 @@ public class Check { | |
private final double amount; | ||
private final CheckingAccount account; | ||
private boolean isVoided = false; | ||
private final SavingsAccount savings; | ||
|
||
/** | ||
* Creates a new check. | ||
* | ||
* @param checkNumber The check number. | ||
* @param amount The amount of the check. | ||
* @param account The account the check is drawn on. | ||
* @param account The account the check is drawn on. Because account is represents checking it could also represent savings.so if | ||
* we call saving account saving we should be able to throw the exception on the check. | ||
*/ | ||
public Check(String checkNumber, double amount, CheckingAccount account) { | ||
if (amount < 0) { | ||
public Check(String checkNumber, double amount, CheckingAccount account, SavingsAccount savings) { | ||
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. Why did you need to change the constructor? You can only write checks against checking accounts. |
||
if (checkNumber == null || checkNumber.isEmpty()) { | ||
throw new IllegalArgumentException("Check number cannot be null or empty"); | ||
} | ||
if (amount < 0 ) { | ||
throw new IllegalArgumentException("Check amount must be positive"); | ||
} | ||
this.checkNumber = checkNumber; | ||
this.amount = amount; | ||
this.account = account; | ||
this.savings = null; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
// Removed redundant or incorrect import | ||
|
||
|
||
/** Represents a savings account, which does not support check writing. */ | ||
public class SavingsAccount extends CheckingAccount { | ||
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. This doesn't make sense to me. How is a savings account a kind of checking account? |
||
|
||
/** | ||
* Creates a new savings account. | ||
* | ||
* @param accountNumber The account number. | ||
* @param owners The owners of the account. | ||
* @param initialBalance The initial balance of the account. | ||
*/ | ||
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount The amount to withdraw. | ||
* @throws InsufficientFundsException If there are insufficient funds in the account. | ||
*/ | ||
@Override | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (amount > getBalance()) { | ||
throw new InsufficientFundsException("Insufficient funds in the account."); | ||
} | ||
// Use a protected setter or a method in parent class if balance is private | ||
super.withdraw(amount); // Use super if parent class has the logic | ||
} | ||
|
||
/** | ||
* Disallows writing checks from a savings account. | ||
* | ||
* @param check The amount to write on the check. | ||
* @throws UnsupportedOperationException Always, since savings accounts don't support checks. | ||
*/ | ||
public void writeCheck(Check check) { | ||
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. Why even have this method if you can't write checks account savings accounts? |
||
throw new UnsupportedOperationException("Savings accounts can't write checks."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
public class SavingAccount { | ||
|
||
} |
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. Must be still WIP so I'll ignore for now. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.codedifferently.lesson17.bank.SavingsAccount; | ||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
|
||
public class SavingsAccountTest { | ||
|
||
|
||
private SavingsAccount classUnderTest; | ||
private Set<Customer> owners; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
owners = new HashSet<>(); | ||
owners.add(new Customer(UUID.randomUUID(), "John Doe")); | ||
owners.add(new Customer(UUID.randomUUID(), "Jane Smith")); | ||
classUnderTest = new SavingsAccount("123456789", owners, 100.0); | ||
} | ||
|
||
@Test | ||
void getAccountNumber() { | ||
assertEquals("123456789", classUnderTest.getAccountNumber()); | ||
} | ||
|
||
@Test | ||
void getOwners() { | ||
assertEquals(owners, classUnderTest.getOwners()); | ||
} | ||
|
||
@Test | ||
void deposit() { | ||
classUnderTest.deposit(50.0); | ||
assertEquals(150.0, classUnderTest.getBalance()); | ||
} | ||
|
||
@Test | ||
void deposit_withNegativeAmount() { | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> classUnderTest.deposit(-50.0)); | ||
} | ||
|
||
@Test | ||
void withdraw() { | ||
classUnderTest.withdraw(50.0); | ||
assertEquals(50.0, classUnderTest.getBalance()); | ||
} | ||
|
||
@Test | ||
void withdraw_withNegativeAmount() { | ||
assertThatExceptionOfType(IllegalStateException.class) | ||
.isThrownBy(() -> classUnderTest.withdraw(-50.0)) | ||
.withMessage("Withdrawal amount must be positive"); | ||
} | ||
|
||
@Test | ||
void withdraw_withInsufficientBalance() { | ||
assertThatExceptionOfType(InsufficientFundsException.class) | ||
.isThrownBy(() -> classUnderTest.withdraw(150.0)) | ||
.withMessage("Account does not have enough funds for withdrawal"); | ||
} | ||
|
||
@Test | ||
void getBalance() { | ||
assertEquals(100.0, classUnderTest.getBalance()); | ||
} | ||
|
||
@Test | ||
void closeAccount_withPositiveBalance() { | ||
assertThatExceptionOfType(IllegalStateException.class) | ||
.isThrownBy(() -> classUnderTest.closeAccount()); | ||
} | ||
|
||
@Test | ||
void isClosed() { | ||
assertFalse(classUnderTest.isClosed()); | ||
classUnderTest.withdraw(100); | ||
classUnderTest.closeAccount(); | ||
assertTrue(classUnderTest.isClosed()); | ||
} | ||
|
||
@Test | ||
void equals() { | ||
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0); | ||
assertEquals(classUnderTest, otherAccount); | ||
} | ||
|
||
@Test | ||
void hashCodeTest() { | ||
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0); | ||
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode()); | ||
} | ||
|
||
@Test | ||
void toStringTest() { | ||
String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}"; | ||
assertEquals(expected, classUnderTest.toString()); | ||
} | ||
|
||
@Test | ||
void writeCheck() { | ||
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. Code formatting looks wrong, please fix. |
||
// Arrange | ||
Check check = new Check("987654321", 50.0, classUnderTest, classUnderTest); | ||
|
||
// Act | ||
classUnderTest.writeCheck(check); | ||
|
||
// Assert | ||
assertEquals(50.0, classUnderTest.getBalance()); | ||
assertTrue(check.getIsVoided()); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
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.
Prolly should be called
BusinessCheckingAccount
for more clarity.