From 8ab9a7c49cafbf0a5dc2fba201c6ed77e35f657b Mon Sep 17 00:00:00 2001 From: Meiko-S22 Date: Mon, 14 Apr 2025 18:06:43 +0000 Subject: [PATCH 1/3] feat: adds Savings Account and Business Checking Account and Tests --- .../lesson17/bank/BankAtm.java | 23 ++++--- .../bank/BusinessCheckingAccount.java | 32 ++++++++++ .../lesson17/bank/BusinessCustomer.java | 10 ++++ .../lesson17/bank/CheckingAccount.java | 7 ++- .../lesson17/bank/SavingsAccount.java | 25 ++++++++ .../lesson17/BusinessCheckingAccountTest.java | 60 +++++++++++++++++++ .../lesson17/bank/SavingsAccountTest.java | 40 +++++++++++++ 7 files changed, 186 insertions(+), 11 deletions(-) create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCustomer.java create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java create mode 100644 lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java create mode 100644 lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java 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..2f05bfada 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 @@ -1,16 +1,19 @@ package com.codedifferently.lesson17.bank; -import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.UUID; +import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; + /** Represents a bank ATM. */ 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 +21,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( @@ -27,6 +30,8 @@ public void addAccount(CheckingAccount account) { }); } + + /** * Finds all accounts owned by a customer. * @@ -79,10 +84,12 @@ 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()) { - throw new AccountNotFoundException("Account not found"); + CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber); + if (checkingAccount != null && !checkingAccount.isClosed()) { + throw new AccountNotFoundException("Account not foudn"); + } + return checkingAccount; } - return account; + } -} + 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..bc4b0c39d --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,32 @@ +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 CheckingAccount 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..958cf1b3e --- /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); + } +} \ No newline at end of file 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..b7e56ac60 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,12 +1,13 @@ package com.codedifferently.lesson17.bank; -import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; import java.util.Set; +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; + /** 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; @@ -20,7 +21,7 @@ public class CheckingAccount { */ public CheckingAccount(String accountNumber, Set owners, double initialBalance) { this.accountNumber = accountNumber; - this.owners = owners; + CheckingAccount.owners = owners; this.balance = initialBalance; isActive = true; } 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..f93217144 --- /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; + + + + +public class SavingsAccount extends CheckingAccount { + + /** + * Represents a savings account. + * + *

It is a subclass of CheckingAccount and inherits its properties and methods. + */ + + + +public SavingsAccount (String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } + + public void depositFunds(Check check) { + throw new UnsupportedOperationException("Cannot deposit check into a savings account"); +} +} diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java new file mode 100644 index 000000000..2d47ae84c --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java @@ -0,0 +1,60 @@ +package com.codedifferently.lesson17; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +import com.codedifferently.lesson17.bank.BankAtm; +import com.codedifferently.lesson17.bank.BusinessCheckingAccount; +import com.codedifferently.lesson17.bank.BusinessCustomer; +import com.codedifferently.lesson17.bank.Customer; +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..aba17b753 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java @@ -0,0 +1,40 @@ +package com.codedifferently.lesson17.bank; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.jupiter.api.Assertions.assertEquals; +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()); + } + + @Test + void testDepositCheck() { + Set owners = Set.of(new Customer(null, "John Doe")); + SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); + Check check = new Check(null, 200.0, savingsAccount); + + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> savingsAccount.depositFunds(check)) + .withMessage("Cannot deposit check into a savings account"); + } +} From 89a467da3ee7b67da9f7e1fcc592e62ac4d5c4c6 Mon Sep 17 00:00:00 2001 From: Meiko-S22 Date: Wed, 16 Apr 2025 13:33:02 +0000 Subject: [PATCH 2/3] feat: implement BusinessCheckingAccount and SavingsAccount wth asscoiated tests --- .../lesson17/bank/BankAtm.java | 13 +-- .../bank/BusinessCheckingAccount.java | 36 +++--- .../lesson17/bank/BusinessCustomer.java | 2 +- .../lesson17/bank/CheckingAccount.java | 49 ++++---- .../lesson17/bank/SavingsAccount.java | 110 ++++++++++++++++-- .../lesson17/bank/BankAtmTest.java | 3 +- .../BusinessCheckingAccountTest.java | 28 ++--- .../lesson17/bank/SavingsAccountTest.java | 44 +++---- 8 files changed, 170 insertions(+), 115 deletions(-) rename lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/{ => bank}/BusinessCheckingAccountTest.java (82%) 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 2f05bfada..854555fe8 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 @@ -13,7 +13,6 @@ public class BankAtm { private final Map customerById = new HashMap<>(); final Map checkingAccountsByNumber = new HashMap<>(); public Object accountByNumber; - /** * Adds a checking account to the bank. @@ -30,8 +29,6 @@ public void addAccount(CheckingAccount account) { }); } - - /** * Finds all accounts owned by a customer. * @@ -85,11 +82,9 @@ public void withdrawFunds(String accountNumber, double amount) { */ private CheckingAccount getAccountOrThrow(String accountNumber) { CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber); - if (checkingAccount != null && !checkingAccount.isClosed()) { - throw new AccountNotFoundException("Account not foudn"); - } - return checkingAccount; + if (checkingAccount == null || checkingAccount.isClosed()) { + throw new AccountNotFoundException("Account not found"); } - + 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 index bc4b0c39d..48a959134 100644 --- 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 @@ -4,29 +4,27 @@ public final class BusinessCheckingAccount extends CheckingAccount { - /** - * Represents a business checking account. - * - *

It is a subclass of CheckingAccount and inherits its properties and methods. - */ - public BusinessCheckingAccount(String accountNumber, Set owners, double initialBalance) { - super(accountNumber, owners, initialBalance); - if (!hasBusinessOwner(owners)) { - throw new IllegalArgumentException( + /** + * 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) { + /** + * 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 index 958cf1b3e..e7498adc3 100644 --- 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 @@ -7,4 +7,4 @@ public class BusinessCustomer extends Customer { public BusinessCustomer(UUID id, String name) { super(id, name); } -} \ No newline at end of file +} 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 b7e56ac60..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 @@ -1,27 +1,18 @@ package com.codedifferently.lesson17.bank; -import java.util.Set; - import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; /** Represents a checking account. */ public class CheckingAccount { - 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; - CheckingAccount.owners = owners; + this.owners = owners; this.balance = initialBalance; isActive = true; } @@ -44,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. * @@ -72,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 index f93217144..b5619420d 100644 --- 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 @@ -1,25 +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; + } -public class SavingsAccount extends CheckingAccount { - /** - * Represents a savings account. + * 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. * - *

It is a subclass of CheckingAccount and inherits its properties and methods. + * @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; + } -public SavingsAccount (String accountNumber, Set owners, double initialBalance) { - super(accountNumber, owners, initialBalance); + @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 + + '}'; } - - public void depositFunds(Check check) { - throw new UnsupportedOperationException("Cannot deposit check into a savings account"); -} } 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/BusinessCheckingAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java similarity index 82% rename from lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java rename to lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java index 2d47ae84c..a298db58e 100644 --- a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/BusinessCheckingAccountTest.java +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BusinessCheckingAccountTest.java @@ -1,20 +1,17 @@ -package com.codedifferently.lesson17; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; +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; -import com.codedifferently.lesson17.bank.BankAtm; -import com.codedifferently.lesson17.bank.BusinessCheckingAccount; -import com.codedifferently.lesson17.bank.BusinessCustomer; -import com.codedifferently.lesson17.bank.Customer; public class BusinessCheckingAccountTest { - @Test + @Test public void testAddBusinessCheckingAccountWithBusinessOwner() { - + BankAtm atm = new BankAtm(); UUID customerId = UUID.randomUUID(); @@ -22,12 +19,10 @@ public void testAddBusinessCheckingAccountWithBusinessOwner() { 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))); @@ -35,26 +30,23 @@ public void testAddBusinessCheckingAccountWithBusinessOwner() { @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( + 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 index aba17b753..f24ff3cc3 100644 --- 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 @@ -1,40 +1,28 @@ package com.codedifferently.lesson17.bank; + import java.util.Set; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.jupiter.api.Assertions.assertEquals; 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 testSavingsAccountCreation() { + Set owners = Set.of(new Customer(null, "John Doe")); + SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); - @Test - void testDepositFunds() { - 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()); + } - savingsAccount.deposit(500.0); - assertEquals(1500.0, savingsAccount.getBalance()); - } + @Test + void testDepositFunds() { + Set owners = Set.of(new Customer(null, "John Doe")); + SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); - @Test - void testDepositCheck() { - Set owners = Set.of(new Customer(null, "John Doe")); - SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0); - Check check = new Check(null, 200.0, savingsAccount); + savingsAccount.deposit(500.0); + assertEquals(1500.0, savingsAccount.getBalance()); + } - assertThatExceptionOfType(UnsupportedOperationException.class) - .isThrownBy(() -> savingsAccount.depositFunds(check)) - .withMessage("Cannot deposit check into a savings account"); - } } From 81ad1d5cede744274dfab6163aa096e53fd19f03 Mon Sep 17 00:00:00 2001 From: Meiko-S22 Date: Wed, 16 Apr 2025 13:34:48 +0000 Subject: [PATCH 3/3] fix:formatting of code --- .../main/java/com/codedifferently/lesson17/bank/BankAtm.java | 3 +-- .../codedifferently/lesson17/bank/SavingsAccountTest.java | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) 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 854555fe8..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 @@ -1,12 +1,11 @@ package com.codedifferently.lesson17.bank; +import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.UUID; -import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; - /** Represents a bank ATM. */ public class BankAtm { 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 index f24ff3cc3..d0dc7764b 100644 --- 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 @@ -1,8 +1,8 @@ package com.codedifferently.lesson17.bank; -import java.util.Set; - import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Set; import org.junit.jupiter.api.Test; public class SavingsAccountTest { @@ -24,5 +24,4 @@ void testDepositFunds() { savingsAccount.deposit(500.0); assertEquals(1500.0, savingsAccount.getBalance()); } - }