diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java new file mode 100644 index 000000000..dff552ce7 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java @@ -0,0 +1,130 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; + +public abstract class BankAccount { + private final Set owners; + private final String accountNumber; + protected double balance; + protected boolean isActive; + + /** + * Creates a new account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public BankAccount(String accountNumber, Set 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 getOwners() { + return owners; + } + + /** + * 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"); + } + if (balance < amount) { + throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); + } + balance -= amount; + } + + /** + * Gets the balance of the account. + * + * @return The balance of the account. + */ + public double getBalance() { + return balance; + } + + /** Closes the account. */ + public void closeAccount() throws IllegalStateException { + if (balance > 0) { + throw new IllegalStateException("Cannot close account with a positive balance"); + } + isActive = false; + } + + /** + * 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 BankAccount other) { + return accountNumber.equals(other.accountNumber); + } + return false; + } + + @Override + public String toString() { + return getClass().getSimpleName() + + "{" + + "accountNumber='" + + accountNumber + + '\'' + + ", balance=" + + balance + + ", isActive=" + + isActive + + '}'; + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java index 8cbcd3cc0..00a5b4440 100644 --- a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java @@ -10,14 +10,14 @@ public class BankAtm { private final Map customerById = new HashMap<>(); - private final Map accountByNumber = new HashMap<>(); + private final Map accountByNumber = new HashMap<>(); /** - * Adds a checking account to the bank. + * Adds an account to the bank. * * @param account The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(BankAccount account) { accountByNumber.put(account.getAccountNumber(), account); account .getOwners() @@ -33,7 +33,7 @@ public void addAccount(CheckingAccount account) { * @param customerId The ID of the customer. * @return The unique set of accounts owned by the customer. */ - public Set findAccountsByCustomerId(UUID customerId) { + public Set findAccountsByCustomerId(UUID customerId) { return customerById.containsKey(customerId) ? customerById.get(customerId).getAccounts() : Set.of(); @@ -46,7 +46,7 @@ public Set findAccountsByCustomerId(UUID customerId) { * @param amount The amount to deposit. */ public void depositFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); + BankAccount account = getAccountOrThrow(accountNumber); account.deposit(amount); } @@ -55,9 +55,10 @@ public void depositFunds(String accountNumber, double amount) { * * @param accountNumber The account number. * @param check The check to deposit. + * @throws Exception */ - public void depositFunds(String accountNumber, Check check) { - CheckingAccount account = getAccountOrThrow(accountNumber); + public void depositFunds(String accountNumber, Check check) throws Exception { + BankAccount account = getAccountOrThrow(accountNumber); check.depositFunds(account); } @@ -66,9 +67,10 @@ public void depositFunds(String accountNumber, Check check) { * * @param accountNumber * @param amount + * @throws Exception */ - public void withdrawFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); + public void withdrawFunds(String accountNumber, double amount) throws Exception { + BankAccount account = getAccountOrThrow(accountNumber); account.withdraw(amount); } @@ -78,8 +80,8 @@ public void withdrawFunds(String accountNumber, double amount) { * @param accountNumber The account number. * @return The account. */ - private CheckingAccount getAccountOrThrow(String accountNumber) { - CheckingAccount account = accountByNumber.get(accountNumber); + private BankAccount getAccountOrThrow(String accountNumber) { + BankAccount account = accountByNumber.get(accountNumber); if (account == null || account.isClosed()) { throw new AccountNotFoundException("Account not found"); } diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java new file mode 100644 index 000000000..1d40a2ded --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,23 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException; +import java.util.Set; + +/** + * Creates a business account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ +public class BusinessCheckingAccount extends CheckingAccount { + public BusinessCheckingAccount( + String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + + boolean hasBusinessOwner = owners.stream().anyMatch(Customer::isBusiness); + if (!hasBusinessOwner) { + throw new InvalidBusinessAccountException("At least one owner must be a business."); + } + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Check.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Check.java index 061fa4a5c..1eb106d33 100644 --- a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Check.java +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Check.java @@ -44,8 +44,9 @@ public void voidCheck() { * Deposits the check into an account. * * @param toAccount The account to deposit the check into. + * @throws Exception */ - public void depositFunds(CheckingAccount toAccount) { + public void depositFunds(BankAccount toAccount) throws Exception { if (isVoided) { throw new CheckVoidedException("Check is voided"); } diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java index 5d8aeb74d..c3502f240 100644 --- a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java @@ -1,15 +1,9 @@ package com.codedifferently.lesson17.bank; -import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; import java.util.Set; /** Represents a checking account. */ -public class CheckingAccount { - - private final Set owners; - private final String accountNumber; - private double balance; - private boolean isActive; +public class CheckingAccount extends BankAccount { /** * Creates a new checking account. @@ -19,113 +13,6 @@ public class CheckingAccount { * @param initialBalance The initial balance of the account. */ public CheckingAccount(String accountNumber, Set 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 getOwners() { - return owners; - } - - /** - * 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"); - } - if (balance < amount) { - throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); - } - balance -= amount; - } - - /** - * Gets the balance of the account. - * - * @return The balance of the account. - */ - public double getBalance() { - return balance; - } - - /** Closes the account. */ - public void closeAccount() throws IllegalStateException { - if (balance > 0) { - throw new IllegalStateException("Cannot close account with a positive balance"); - } - isActive = false; - } - - /** - * 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 CheckingAccount other) { - return accountNumber.equals(other.accountNumber); - } - return false; - } - - @Override - public String toString() { - return "CheckingAccount{" - + "accountNumber='" - + accountNumber - + '\'' - + ", balance=" - + balance - + ", isActive=" - + isActive - + '}'; + super(accountNumber, owners, initialBalance); } } diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java index af0847134..c9bbc4710 100644 --- a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java @@ -9,7 +9,8 @@ public class Customer { private final UUID id; private final String name; - private final Set accounts = new HashSet<>(); + private final Set accounts = new HashSet<>(); + private final boolean isBusiness; /** * Creates a new customer. @@ -17,9 +18,10 @@ public class Customer { * @param id The ID of the customer. * @param name The name of the customer. */ - public Customer(UUID id, String name) { + public Customer(UUID id, String name, boolean isBusiness) { this.id = id; this.name = name; + this.isBusiness = isBusiness; } /** @@ -41,11 +43,20 @@ public String getName() { } /** - * Adds a checking account to the customer. + * Checks if this is a business + * + * @return True if this is a business, otherwise false. + */ + public boolean isBusiness() { + return isBusiness; + } + + /** + * Adds an account to the customer. * * @param account The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(BankAccount account) { accounts.add(account); } @@ -54,7 +65,7 @@ public void addAccount(CheckingAccount account) { * * @return The unique set of accounts owned by the customer. */ - public Set getAccounts() { + public Set getAccounts() { return accounts; } diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java new file mode 100644 index 000000000..fad92791d --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,18 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +/** Represents a savings account. */ +public class SavingsAccount extends BankAccount { + + /** + * 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 owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java new file mode 100644 index 000000000..e9524ed49 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson17.bank.exceptions; + +public class InvalidBusinessAccountException extends RuntimeException { + public InvalidBusinessAccountException(String message) { + super(message); + } +} diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java index fa4a913a2..35285557d 100644 --- a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java @@ -2,9 +2,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException; +import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException; import java.util.Set; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; @@ -13,89 +15,137 @@ class BankAtmTest { private BankAtm classUnderTest; - private CheckingAccount account1; - private CheckingAccount account2; + private CheckingAccount checkingAccount1; + private CheckingAccount checkingAccount2; + private SavingsAccount savingsAccount1; + private SavingsAccount savingsAccount2; private Customer customer1; private Customer customer2; @BeforeEach void setUp() { classUnderTest = new BankAtm(); - customer1 = new Customer(UUID.randomUUID(), "John Doe"); - customer2 = new Customer(UUID.randomUUID(), "Jane Smith"); - account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0); - account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0); - customer1.addAccount(account1); - customer1.addAccount(account2); - customer2.addAccount(account2); - classUnderTest.addAccount(account1); - classUnderTest.addAccount(account2); + customer1 = new Customer(UUID.randomUUID(), "John Doe", false); + customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false); + checkingAccount1 = new CheckingAccount("12345", Set.of(customer1), 100.0); + checkingAccount2 = new CheckingAccount("54321", Set.of(customer1, customer2), 200.0); + savingsAccount1 = new SavingsAccount("56789", Set.of(customer1), 100.0); + savingsAccount2 = new SavingsAccount("98765", Set.of(customer1, customer2), 200.0); + customer1.addAccount(checkingAccount1); + customer1.addAccount(checkingAccount2); + customer2.addAccount(checkingAccount1); + customer2.addAccount(checkingAccount2); + customer1.addAccount(savingsAccount1); + customer1.addAccount(savingsAccount2); + customer2.addAccount(savingsAccount1); + customer2.addAccount(savingsAccount2); + classUnderTest.addAccount(checkingAccount1); + classUnderTest.addAccount(checkingAccount2); + classUnderTest.addAccount(savingsAccount1); + classUnderTest.addAccount(savingsAccount2); } @Test void testAddAccount() { // Arrange - Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson"); - CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0); - customer3.addAccount(account3); + Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false); + CheckingAccount checkingAccount3 = new CheckingAccount("555555555", Set.of(customer3), 300.0); + SavingsAccount savingsAccount3 = new SavingsAccount("666666666", Set.of(customer3), 300.0); + customer3.addAccount(checkingAccount3); + customer3.addAccount(savingsAccount3); // Act - classUnderTest.addAccount(account3); + classUnderTest.addAccount(checkingAccount3); + classUnderTest.addAccount(savingsAccount3); // Assert - Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); - assertThat(accounts).containsOnly(account3); + Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); + assertThat(accounts).contains(checkingAccount3); + assertThat(accounts).contains(savingsAccount3); + } + + @Test + void testBusinessCheckingAccount_WithBusinessOwner_Succeeds() { + // Arrange + Customer businessCustomer = new Customer(UUID.randomUUID(), "Business LLC", true); + Customer individualCustomer = new Customer(UUID.randomUUID(), "Regular Joe", false); + Set owners = Set.of(businessCustomer, individualCustomer); + + // Act + BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("BUS123", owners, 1000.0); + + // Assert + assertThat(businessAccount.getBalance()).isEqualTo(1000.0); + assertThat(businessAccount.getOwners()).contains(businessCustomer); + } + + @Test + void testBusinessCheckingAccount_WithoutBusinessOwner_ThrowsException() { + // Arrange + Customer c1 = new Customer(UUID.randomUUID(), "Person One", false); + Customer c2 = new Customer(UUID.randomUUID(), "Person Two", false); + Set owners = Set.of(c1, c2); + + // Act & Assert + assertThatThrownBy(() -> new BusinessCheckingAccount("BUS999", owners, 500.0)) + .isInstanceOf(InvalidBusinessAccountException.class) + .hasMessage("At least one owner must be a business."); } @Test void testFindAccountsByCustomerId() { // Act - Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); + Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); // Assert - assertThat(accounts).containsOnly(account1, account2); + assertThat(accounts).contains(checkingAccount1, checkingAccount2); + assertThat(accounts).contains(savingsAccount1, savingsAccount2); } @Test void testDepositFunds() { // Act - classUnderTest.depositFunds(account1.getAccountNumber(), 50.0); + classUnderTest.depositFunds(checkingAccount1.getAccountNumber(), 50.0); + classUnderTest.depositFunds(savingsAccount1.getAccountNumber(), 50.0); // Assert - assertThat(account1.getBalance()).isEqualTo(150.0); + assertThat(checkingAccount1.getBalance()).isEqualTo(150.0); + assertThat(savingsAccount1.getBalance()).isEqualTo(150.0); } @Test - void testDepositFunds_Check() { + void testDepositFunds_Check() throws Exception { // Arrange - Check check = new Check("987654321", 100.0, account1); + Check check = new Check("12345", 100.0, checkingAccount1); // Act - classUnderTest.depositFunds("987654321", check); + classUnderTest.depositFunds("54321", check); // Assert - assertThat(account1.getBalance()).isEqualTo(0); - assertThat(account2.getBalance()).isEqualTo(300.0); + assertThat(checkingAccount1.getBalance()).isEqualTo(0); + assertThat(checkingAccount2.getBalance()).isEqualTo(300.0); } @Test - void testDepositFunds_DoesntDepositCheckTwice() { - Check check = new Check("987654321", 100.0, account1); + void testDepositFunds_DoesntDepositCheckTwice() throws Exception { + Check check = new Check("987654321", 100.0, checkingAccount1); - classUnderTest.depositFunds("987654321", check); + classUnderTest.depositFunds("54321", check); assertThatExceptionOfType(CheckVoidedException.class) - .isThrownBy(() -> classUnderTest.depositFunds("987654321", check)) + .isThrownBy(() -> classUnderTest.depositFunds("54321", check)) .withMessage("Check is voided"); } @Test - void testWithdrawFunds() { + void testWithdrawFunds() throws Exception { // Act - classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0); + classUnderTest.withdrawFunds(checkingAccount2.getAccountNumber(), 50.0); + classUnderTest.withdrawFunds(savingsAccount2.getAccountNumber(), 50.0); // Assert - assertThat(account2.getBalance()).isEqualTo(150.0); + assertThat(checkingAccount2.getBalance()).isEqualTo(150.0); + assertThat(savingsAccount2.getBalance()).isEqualTo(150.0); } @Test diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java index 6b62d39ba..ccad7effc 100644 --- a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java @@ -21,7 +21,7 @@ void setUp() { } @Test - void testDepositFunds() { + void testDepositFunds() throws Exception { // Act classUnderTest.depositFunds(account2); diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java index f155d8e5b..2ca05f63c 100644 --- a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java @@ -20,8 +20,8 @@ class CheckingAccountTest { @BeforeEach void setUp() { owners = new HashSet<>(); - owners.add(new Customer(UUID.randomUUID(), "John Doe")); - owners.add(new Customer(UUID.randomUUID(), "Jane Smith")); + owners.add(new Customer(UUID.randomUUID(), "John Doe", false)); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false)); classUnderTest = new CheckingAccount("123456789", owners, 100.0); }