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..d4ba80eed --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java @@ -0,0 +1,73 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; + +/** + * Checks if the account is closed + * + * @returns true if the account is closed, otherwise false + */ +public abstract class BankAccount { + private String accountNumber; + protected double balance; + private Set owners; + private boolean isClosed; + + public BankAccount(String accountNumber, Set owners, double initialBalance) { + this.accountNumber = accountNumber; + this.owners = owners; + this.balance = initialBalance; + } + + public boolean isActive() { + return isClosed; + } + + /** + * Gets the owners of the account. + * + * @return The owners of the account. + */ + public Set getOwners() { + return owners; + } + + /** + * Gets the account number. + * + * @return The account number. + */ + public String getAccountNumber() { + return accountNumber; + } + + /** + * Withdraws funds from the account. + * + * @param amount + * @throws InsufficientFundsException + */ + public void withdraw(double amount) throws InsufficientFundsException { + 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; + } + + /** + * Deposits funds into the account. + * + * @param amount The amount to deposit. + */ + public void deposit(double amount) throws IllegalStateException { + + if (amount <= 0) { + throw new IllegalArgumentException("Deposit amount must be positive"); + } + balance += amount; + } +} 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..30a3a7aec 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 @@ -11,6 +11,7 @@ public class BankAtm { private final Map customerById = new HashMap<>(); private final Map accountByNumber = new HashMap<>(); + private final Map savingsAccountByNumber = new HashMap<>(); /** * Adds a checking account to the bank. @@ -27,13 +28,28 @@ public void addAccount(CheckingAccount account) { }); } + /** + * Adds a savings account to the bank. + * + * @param account The account to add. + */ + public void addAccount(SavingsAccount account) { + savingsAccountByNumber.put(account.getAccountNumber(), account); + account + .getOwners() + .forEach( + owner -> { + customerById.put(owner.getId(), owner); + }); + } + /** * Finds all accounts owned by a customer. * * @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(); 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..031ff21ea --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,19 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +/** Represents a business checking account. */ +public class BusinessCheckingAccount extends BankAccount { + + /** + * Creates a new business checking account. + * + * @param accountNumber The account 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); + } +} 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..673f1240d 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 @@ -79,4 +79,8 @@ public String toString() { + account.getAccountNumber() + '}'; } + + public Object getAmount() { + throw new UnsupportedOperationException("Unimplemented method 'getAmount'"); + } } 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..ad26f20f6 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 @@ -4,9 +4,8 @@ import java.util.Set; /** Represents a checking account. */ -public class CheckingAccount { +public class CheckingAccount extends BankAccount { - private final Set owners; private final String accountNumber; private double balance; private boolean isActive; @@ -19,62 +18,10 @@ public class CheckingAccount { * @param initialBalance The initial balance of the account. */ public CheckingAccount(String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, 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; + this.isActive = true; } /** @@ -103,6 +50,18 @@ public boolean isClosed() { return !isActive; } + @Override + public void withdraw(double amount) throws InsufficientFundsException { + super.withdraw(amount); + balance -= amount; + } + + @Override + public void deposit(double amount) { + super.deposit(amount); + balance += amount; + } + @Override public int hashCode() { return accountNumber.hashCode(); 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..19ea6e361 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,7 @@ public class Customer { private final UUID id; private final String name; - private final Set accounts = new HashSet<>(); + private final Set accounts = new HashSet<>(); /** * Creates a new customer. @@ -43,10 +43,10 @@ public String getName() { /** * Adds a checking account to the customer. * - * @param account The account to add. + * @param account3 The account to add. */ - public void addAccount(CheckingAccount account) { - accounts.add(account); + public void addAccount(CheckingAccount account3) { + accounts.add(account3); } /** @@ -54,8 +54,13 @@ public void addAccount(CheckingAccount account) { * * @return The unique set of accounts owned by the customer. */ - public Set getAccounts() { - return accounts; + public Set getAccounts() { + return new HashSet<>(accounts); + } + + /** check if is business account */ + public boolean isBusinessAccount() { + return accounts.stream().anyMatch(BusinessCheckingAccount.class::isInstance); } @Override @@ -65,10 +70,14 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj instanceof Customer other) { - return id.equals(other.id); + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; } - return false; + Customer other = (Customer) obj; + return id.equals(other.id) && name.equals(other.name); } @Override 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..b5d2e917f --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,25 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +/** Represents a savings account that doesn't allow checks. */ +public class SavingsAccount extends BankAccount { + private boolean isActive = true; + + public SavingsAccount(String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } + + protected boolean isClosed() { + throw new IllegalStateException("Cannot check if account is closed"); + } + + @Override + public boolean isActive() { + return true; + } + + public void closeAccount() { + throw new IllegalStateException("Cannot close account with a positive balance"); + } +} diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAccountTest.java new file mode 100644 index 000000000..0466133a5 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAccountTest.java @@ -0,0 +1,48 @@ +package com.codedifferently.lesson17.bank; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.Test; + +public class BankAccountTest { + @Test + void testIsActive() { + // Arrange + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe")); + BankAccount account = new CheckingAccount("123456789", owners, 100.0); + // Act + boolean isActive = account.isActive(); + // Assert + assertThat(!isActive).isTrue(); + } + + @Test + void testGetOwners() { + // Act + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe")); + BankAccount account = new CheckingAccount("123456789", owners, 100.0); + // Assert + assertEquals(owners, account.getOwners()); + } + + @Test + void testWithdrawFunds() { + // Arrange + double initialBalance = 100.0; + double withdrawAmount = 50.0; + double expectedBalance = initialBalance - withdrawAmount; + + // Act + BankAccount account = new CheckingAccount("123456789", null, initialBalance); + account.withdraw(withdrawAmount); + + // Assert + assertThat(account.balance).isEqualTo(expectedBalance); + } +} 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..325f23450 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 000000000..d33baa666 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java @@ -0,0 +1,3 @@ +package com.codedifferently.lesson17.bank; + +public class BusinessCheckingAccountTest {} 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..f8670c5a9 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java @@ -0,0 +1,65 @@ +package com.codedifferently.lesson17.bank; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class SavingsAccountTest { + + private SavingsAccount classUnderTest; + private Set owners; + private double initialBalance = 100.0; + private String accountNumber = "123456789"; + + @BeforeEach + void setUp() { + owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe")); + classUnderTest = new SavingsAccount(accountNumber, owners, initialBalance); + } + + @Test + void testIsClosed() { + // Arrange + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "Ezra Nyabuti")); + SavingsAccount account = new SavingsAccount(accountNumber, owners, initialBalance); + + // Act + IllegalStateException exception = + assertThrows(IllegalStateException.class, () -> account.isClosed()); + + // Assert + assertEquals( + "Cannot check if account is closed", exception.getMessage(), "Exception message mismatch"); + } + + @Test + void testIsActive() { + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "Ezra Nyabuti")); + SavingsAccount account = new SavingsAccount(accountNumber, owners, initialBalance); + // ssert + assertTrue(account.isActive(), "Account should be active"); + } + + @Test + void testCloseAccount() { + // Arrange + Set owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "Ezra Nyabuti")); + + // Act + SavingsAccount account = new SavingsAccount(accountNumber, owners, initialBalance); + IllegalStateException exception = + assertThrows(IllegalStateException.class, () -> account.closeAccount()); + // Assert + assertEquals("Cannot close account with a positive balance", exception.getMessage()); + } +}