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 00000000..92e67b7b --- /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 8cbcd3cc..ce0c5572 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. * * @param account The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(Account 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); + Account account = getAccountOrThrow(accountNumber); account.deposit(amount); } @@ -57,8 +57,13 @@ 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 CheckingAccount checkingAccount) { + check.depositFunds(checkingAccount); + } else if (account instanceof SavingsAccount) { + throw new IllegalStateException("Savings Accounts can't accept checks"); + } } /** @@ -68,7 +73,7 @@ public void depositFunds(String accountNumber, Check check) { * @param amount */ public void withdrawFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); + Account account = getAccountOrThrow(accountNumber); account.withdraw(amount); } @@ -78,8 +83,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 00000000..5b8a3d68 --- /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 061fa4a5..ff611598 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 5d8aeb74..4adf04f7 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,19 @@ 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 + + getAccountNumber() + '\'' + ", balance=" - + 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 af084713..e3ff7c5a 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,41 +3,43 @@ import java.util.HashSet; import java.util.Set; import java.util.UUID; +import lombok.Getter; +import lombok.Setter; +@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. + * Creates an new individual customer. * - * @return The name of the customer. + * @param id The ID of the customer. + * @param name The name of the customer. + * @param type The type of custimer (individual or business) */ - public String getName() { - return name; + public Customer(UUID id, String name) { + this.id = id; + this.name = name; + this.type = CustomerType.INDIVIDUAL; } /** @@ -45,7 +47,7 @@ public String getName() { * * @param account The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(Account account) { accounts.add(account); } @@ -54,10 +56,19 @@ public void addAccount(CheckingAccount account) { * * @return The unique set of accounts owned by the customer. */ - public Set getAccounts() { + public Set getAccounts() { return accounts; } + /** + * Checks if the owner is a business. + * + * @return True if the customer is a business, false otherwise. + */ + public boolean isBusiness() { + return this.type == CustomerType.BUSINESS; + } + @Override public int hashCode() { return id.hashCode(); diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CustomerType.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CustomerType.java new file mode 100644 index 00000000..30f05a33 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/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/SavingsAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java new file mode 100644 index 00000000..411cf060 --- /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 fa4a913a..9f08e317 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 @@ -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/BusinessCheckingAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java new file mode 100644 index 00000000..e88be2ed --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java @@ -0,0 +1,21 @@ +package com.codedifferently.lesson17.bank; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.Test; + +public class BusinessCheckingAccountTest { + + @Test + void validateBusinessOwner_withBusinessOwner() { + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "Elon Musk", CustomerType.BUSINESS)); + owners.add(new Customer(UUID.randomUUID(), "Elon Musk", CustomerType.BUSINESS)); + + BusinessCheckingAccount account = new BusinessCheckingAccount("123456789", owners, 1000.0); + assertThat(account).isNotNull(); + } +} diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccountTest.java new file mode 100644 index 00000000..501a2080 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccountTest.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(), "Yuji Nishida")); + owners.add(new Customer(UUID.randomUUID(), "Masahiro Sekita")); + + 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()); + } +}