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..6ee8d3ea5 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,8 @@ public class BankAtm { private final Map customerById = new HashMap<>(); - private final Map accountByNumber = new HashMap<>(); + final Map checkingAccountsByNumber = new HashMap<>(); + public Object accountByNumber; /** * Adds a checking account to the bank. @@ -18,7 +19,7 @@ public class BankAtm { * @param account The account to add. */ public void addAccount(CheckingAccount account) { - accountByNumber.put(account.getAccountNumber(), account); + checkingAccountsByNumber.put(account.getAccountNumber(), account); account .getOwners() .forEach( @@ -79,10 +80,10 @@ public void withdrawFunds(String accountNumber, double amount) { * @return The account. */ private CheckingAccount getAccountOrThrow(String accountNumber) { - CheckingAccount account = accountByNumber.get(accountNumber); - if (account == null || account.isClosed()) { + CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber); + if (checkingAccount == null || checkingAccount.isClosed()) { throw new AccountNotFoundException("Account not found"); } - return account; + return checkingAccount; } } 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..48a959134 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,30 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +public final class BusinessCheckingAccount extends CheckingAccount { + + /** + * Represents a business checking account. + * + *

It is a subclass of BankAccount and inherits its properties and methods. + */ + public BusinessCheckingAccount( + String accountNumber, Set 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 owners) { + return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer); + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCustomer.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCustomer.java new file mode 100644 index 000000000..e7498adc3 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCustomer.java @@ -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); + } +} 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..c908b7bf0 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 @@ -5,19 +5,11 @@ /** Represents a checking account. */ public class CheckingAccount { - - private final Set owners; + private static Set owners; private final String accountNumber; private double balance; private boolean isActive; - /** - * Creates a new checking account. - * - * @param accountNumber The account number. - * @param owners The owners of the account. - * @param initialBalance The initial balance of the account. - */ public CheckingAccount(String accountNumber, Set owners, double initialBalance) { this.accountNumber = accountNumber; this.owners = owners; @@ -43,6 +35,23 @@ public Set getOwners() { return owners; } + /** + * 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 positive balance"); + } + isActive = false; + } + /** * Deposits funds into the account. * @@ -71,29 +80,12 @@ public void withdraw(double amount) throws InsufficientFundsException { if (amount <= 0) { throw new IllegalStateException("Withdrawal amount must be positive"); } - if (balance < amount) { + if (amount > balance) { 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. * 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..b5619420d --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,115 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; + +public class SavingsAccount { + private final Set owners; + private final String accountNumber; + private double balance; + private boolean isActive; + + /** + * Represents a savings account. + * + *

It is a subclass of BankAccount and inherits its properties and methods. + */ + public SavingsAccount(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; + } + + /** + * Gets the balance of the account. + * + * @return The balance of the account. + */ + public double getBalance() { + return balance; + } + + /** + * 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"); + } + } + + /** + * 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 SavingsAccount other) { + return accountNumber.equals(other.accountNumber); + } + return false; + } + + @Override + public String toString() { + return "CheckingAccount{" + + "accountNumber='" + + accountNumber + + '\'' + + ", balance=" + + balance + + ", 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..03e531c38 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 @@ -27,6 +27,7 @@ void setUp() { account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0); customer1.addAccount(account1); customer1.addAccount(account2); + customer2.addAccount(account1); customer2.addAccount(account2); classUnderTest.addAccount(account1); classUnderTest.addAccount(account2); @@ -74,7 +75,7 @@ void testDepositFunds_Check() { classUnderTest.depositFunds("987654321", check); // Assert - assertThat(account1.getBalance()).isEqualTo(0); + assertThat(account1.getBalance()).isEqualTo(0.0); assertThat(account2.getBalance()).isEqualTo(300.0); } 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 000000000..a298db58e --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java @@ -0,0 +1,52 @@ +package com.codedifferently.lesson17.bank; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.Test; + +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 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 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); + }); + } +} 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..d0dc7764b --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java @@ -0,0 +1,27 @@ +package com.codedifferently.lesson17.bank; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Set; +import org.junit.jupiter.api.Test; + +public class SavingsAccountTest { + @Test + void testSavingsAccountCreation() { + Set 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 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()); + } +}