diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/CustomerType.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/CustomerType.java new file mode 100644 index 000000000..30f05a33c --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/CustomerType.java @@ -0,0 +1,6 @@ +package com.codedifferently.lesson17.bank; + +public enum CustomerType { + INDIVIDUAL, + BUSINESS +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Account.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Account.java new file mode 100644 index 000000000..92e67b7bb --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Account.java @@ -0,0 +1,94 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; +import lombok.Getter; +import lombok.Setter; + +/** Abstract base class for all bank accounts. */ +@Getter +@Setter +public abstract class Account { + private final Set owners; + private final String accountNumber; + private double balance; + private boolean isActive; + + /** + * Creates a new account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initalBalance The inital balance of the account. + */ + public Account(String accountNumber, Set owners, double initalBalance) { + this.accountNumber = accountNumber; + this.owners = owners; + this.balance = initalBalance; + this.isActive = true; + } + + /** + * 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; + } + + /** 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.getAccountNumber()); + } + return false; + } +} 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..ff4750d9d 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,7 +10,7 @@ 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. @@ -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); + Account account = getAccountOrThrow(accountNumber); account.deposit(amount); } @@ -57,18 +57,22 @@ public void depositFunds(String accountNumber, double amount) { * @param check The check to deposit. */ public void depositFunds(String accountNumber, Check check) { - CheckingAccount account = getAccountOrThrow(accountNumber); - check.depositFunds(account); + Account account = getAccountOrThrow(accountNumber); + if (account instanceof Account) { + check.depositFunds((Account) account); + } else { + throw new UnsupportedOperationException("This account does not support check deposits."); + } } /** * Withdraws funds from an account. * - * @param accountNumber - * @param amount + * @param accountNumber The account number. + * @param amount The amount to withdraw. */ public void withdrawFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); + Account account = getAccountOrThrow(accountNumber); account.withdraw(amount); } @@ -78,8 +82,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 Account getAccountOrThrow(String accountNumber) { + Account 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..5b8a3d688 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,46 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +/** Represents a business checking account */ +public class BusinessCheckingAccount extends CheckingAccount { + + /** + * Creates a new business checking account + * + * @param accountNumber The accounnt number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public BusinessCheckingAccount( + String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } + + /** + * Checks if at least one owner is a business. + * + * @param owners The owers of the account. + * @throws IllegalArgumentEception If no business owner is found. + */ + private void validateBusinessOwner(Set owners) { + boolean hasBusinessOwner = owners.stream().anyMatch(Customer::isBusiness); + if (!hasBusinessOwner) { + throw new IllegalArgumentException( + "Business Checking Account must have at least one business owner"); + } + } + + @Override + public String toString() { + return "BusinessCheckingAccount{" + + "accountNumber='" + + getAccountNumber() + + '\'' + + ", balance=" + + getBalance() + + ", isActive=" + + isActive() + + '}'; + } +} 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..ff6115984 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 @@ -45,7 +45,7 @@ public void voidCheck() { * * @param toAccount The account to deposit the check into. */ - public void depositFunds(CheckingAccount toAccount) { + public void depositFunds(Account toAccount) { 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..058e2e95b 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 Account { /** * Creates a new checking account. @@ -19,113 +13,17 @@ 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; + super(accountNumber, owners, initialBalance); } @Override public String toString() { - return "CheckingAccount{" - + "accountNumber='" - + accountNumber - + '\'' - + ", balance=" - + balance + return "CheckingAccount{accountNumber='" + + getAccountNumber() + + "', balance=" + + getBalance() + ", isActive=" - + isActive + + isActive() + '}'; } } 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..8f98753e2 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 @@ -3,59 +3,56 @@ import java.util.HashSet; import java.util.Set; import java.util.UUID; +import lombok.Getter; +import lombok.Setter; -/** Represents a customer of the bank. */ +@Getter +@Setter +/** Represents a customer of the bank */ public class Customer { private final UUID id; private final String name; - private final Set accounts = new HashSet<>(); + private final CustomerType type; + private final Set accounts = new HashSet<>(); /** * Creates a new customer. * * @param id The ID of the customer. * @param name The name of the customer. + * @param type The type of customer (individual or business) */ - public Customer(UUID id, String name) { + public Customer(UUID id, String name, CustomerType type) { this.id = id; this.name = name; + this.type = type; } /** - * Gets the ID of the customer. - * - * @return The ID of the customer. - */ - public UUID getId() { - return id; - } - - /** - * Gets the name of the customer. - * - * @return The name of the customer. - */ - public String getName() { - return name; - } - - /** - * Adds a checking account to the customer. + * Creates a new individual customer. * * + * @param id The ID of the customer. + * @param name The name of the customer. + * @param type The type of customer (individual or business) / public Customer(UUID id, String + * name) { this.id = id; this.name = name; this.type = CustomerType. INDIVIDUAL; } + *

/** Adds a checking account to the customer. * @param account The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(Account account) { accounts.add(account); } /** * Gets the accounts owned by the customer. * - * @return The unique set of accounts owned by the customer. + * @return The set of accounts owned by the customer. / public Set getAccounts() { return + * accounts; } + *

/** Checks if the owner is a business. + * @return True if the customer is a business, false otherwise. */ - public Set getAccounts() { - return accounts; + public boolean isBusiness() { + return this.type == CustomerType.BUSINESS; } @Override @@ -73,6 +70,6 @@ public boolean equals(Object obj) { @Override public String toString() { - return "Customer{" + "id=" + id + ", name='" + name + '\'' + '}'; + return "Customer" + "id=" + id + ", name='" + name + '\'' + '}'; } } 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..411cf060d --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,31 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +/** Represents a savings account. */ +public class SavingsAccount extends Account { + + /** + * Creates a new sacings account. + * + * @param accountNumber The account number. + * @param owners The ownders of the account. + * @param initalBalance The inital balance of the account. + */ + public SavingsAccount(String accountNumber, Set owners, double initalBalance) { + super(accountNumber, owners, initalBalance); + } + + @Override + public String toString() { + return "SavingsAccount{" + + "accountNumber='" + + getAccountNumber() + + '\'' + + ", balance=" + + getBalance() + + ", isActive=" + + isActive() + + '}'; + } +} 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..1b76f12a2 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 @@ -21,8 +21,8 @@ class BankAtmTest { @BeforeEach void setUp() { classUnderTest = new BankAtm(); - customer1 = new Customer(UUID.randomUUID(), "John Doe"); - customer2 = new Customer(UUID.randomUUID(), "Jane Smith"); + customer1 = new Customer(UUID.randomUUID(), "John Doe", CustomerType.INDIVIDUAL); + customer2 = new Customer(UUID.randomUUID(), "Jane Smith", CustomerType.INDIVIDUAL); account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0); account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0); customer1.addAccount(account1); @@ -35,7 +35,7 @@ void setUp() { @Test void testAddAccount() { // Arrange - Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson"); + Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", CustomerType.INDIVIDUAL); CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0); customer3.addAccount(account3); @@ -43,14 +43,14 @@ void testAddAccount() { classUnderTest.addAccount(account3); // Assert - Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); + Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); assertThat(accounts).containsOnly(account3); } @Test void testFindAccountsByCustomerId() { // Act - Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); + Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); // Assert assertThat(accounts).containsOnly(account1, 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..3b6f00af4 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", CustomerType.INDIVIDUAL)); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith", CustomerType.INDIVIDUAL)); classUnderTest = new CheckingAccount("123456789", owners, 100.0); } diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java new file mode 100644 index 000000000..02b593be5 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java @@ -0,0 +1,67 @@ +package com.codedifferently.lesson17.bank; + +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 com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SavingsAccountTest { + + private SavingsAccount account; + private Set owners; + + @BeforeEach + void setUp() { + owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Snow", CustomerType.INDIVIDUAL)); + owners.add(new Customer(UUID.randomUUID(), "Jamie Lannister", CustomerType.INDIVIDUAL)); + + account = new SavingsAccount("123456789", owners, 100.0); + } + + @Test + void deposit() { + account.deposit(100.0); + assertEquals(200.0, account.getBalance()); + } + + @Test + void withdraw() { + account.withdraw(25.0); + assertEquals(75.0, account.getBalance()); + } + + @Test + void withdraw_withNegativeAmount() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> account.withdraw(-25.0)) + .withMessage("Withdrawal amount must be positive"); + } + + @Test + void withdraw_withInsufficientBalance() { + assertThatExceptionOfType(InsufficientFundsException.class) + .isThrownBy(() -> account.withdraw(200.0)) + .withMessage("Account does not have enough funds for withdrawal"); + } + + @Test + void closeAccount_withPositiveBalance() { + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> account.closeAccount()); + } + + @Test + void isClosed() { + assertFalse(account.isClosed()); + account.withdraw(100.0); + account.closeAccount(); + assertTrue(account.isClosed()); + } +}