From fc92fbb550e0ba6565a07c14a5b250475999eed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTezz03=E2=80=9D?= <“mmbradley@gmail.com”> Date: Tue, 25 Mar 2025 20:33:39 +0000 Subject: [PATCH 1/6] feat:mbLesson_09 --- .../dataprovider/montezBProviderProvider.java | 25 +++++ .../src/main/resources/data/montezb.json | 92 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java create mode 100644 lesson_09/types/types_app/src/main/resources/data/montezb.json diff --git a/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java b/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java new file mode 100644 index 00000000..266650d4 --- /dev/null +++ b/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java @@ -0,0 +1,25 @@ +package com.codedifferently.lesson9.dataprovider; + +import java.util.Map; + +import org.springframework.stereotype.Service; + +@Service +public class montezBProviderProvider extends DataProvider { + public String getProviderName() { + return "montezBProvider"; + } + + public Map getColumnTypeByName() { + return Map.of( + "column1", Boolean.class, + "column2", Float.class, + "column3", Double.class, + "column4", Short.class, + "column5", Integer.class, + "column6", String.class, + "column7", Long.class); + } +} + + diff --git a/lesson_09/types/types_app/src/main/resources/data/montezb.json b/lesson_09/types/types_app/src/main/resources/data/montezb.json new file mode 100644 index 00000000..1d8cbf9a --- /dev/null +++ b/lesson_09/types/types_app/src/main/resources/data/montezb.json @@ -0,0 +1,92 @@ +[ + { + "column1": "false", + "column2": "2.0165133E38", + "column3": "2.7989049813330837E307", + "column4": "416", + "column5": "435833166", + "column6": "3cuw2", + "column7": "3530240046379697664" + }, + { + "column1": "true", + "column2": "1.460264E37", + "column3": "2.9225361274287315E307", + "column4": "17516", + "column5": "260615671", + "column6": "8j52s4aq6d", + "column7": "65341810874192888" + }, + { + "column1": "true", + "column2": "2.776658E38", + "column3": "2.0826943783702183E307", + "column4": "31533", + "column5": "161572734", + "column6": "s8yavhue1", + "column7": "4903929290600323072" + }, + { + "column1": "true", + "column2": "1.3027212E38", + "column3": "1.1914573950682302E308", + "column4": "29377", + "column5": "451167147", + "column6": "2at5p9weyni", + "column7": "1756929147146946048" + }, + { + "column1": "true", + "column2": "1.6506819E38", + "column3": "3.7690907310588337E307", + "column4": "23676", + "column5": "405162496", + "column6": "migsey8lk", + "column7": "5227682372169227264" + }, + { + "column1": "true", + "column2": "6.687427E37", + "column3": "1.7507811122323619E308", + "column4": "763", + "column5": "385548066", + "column6": "ni6d58js04u", + "column7": "814327769818061056" + }, + { + "column1": "false", + "column2": "2.6281565E38", + "column3": "3.3046191712251996E307", + "column4": "29441", + "column5": "497419740", + "column6": "7ypumj83a", + "column7": "6639371891920120832" + }, + { + "column1": "false", + "column2": "9.816115E37", + "column3": "1.3824553928097414E308", + "column4": "3537", + "column5": "753736722", + "column6": "68aglu4", + "column7": "1905184695820989696" + }, + { + "column1": "false", + "column2": "2.674275E38", + "column3": "1.562287548934654E308", + "column4": "25482", + "column5": "2121817019", + "column6": "2plvf", + "column7": "8721696529208318976" + }, + { + "column1": "true", + "column2": "7.985794E37", + "column3": "1.377992454751497E308", + "column4": "29060", + "column5": "1516947099", + "column6": "c4685r9", + "column7": "5859838884205409280" + } +] \ No newline at end of file From c57408a27f530a49c41e68e9e8fa1f979efef61e Mon Sep 17 00:00:00 2001 From: Tezz03 <105926982+Tezz03@users.noreply.github.com> Date: Fri, 28 Mar 2025 11:05:16 -0400 Subject: [PATCH 2/6] Delete lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java --- .../dataprovider/montezBProviderProvider.java | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java diff --git a/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java b/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java deleted file mode 100644 index 266650d4..00000000 --- a/lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/montezBProviderProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.codedifferently.lesson9.dataprovider; - -import java.util.Map; - -import org.springframework.stereotype.Service; - -@Service -public class montezBProviderProvider extends DataProvider { - public String getProviderName() { - return "montezBProvider"; - } - - public Map getColumnTypeByName() { - return Map.of( - "column1", Boolean.class, - "column2", Float.class, - "column3", Double.class, - "column4", Short.class, - "column5", Integer.class, - "column6", String.class, - "column7", Long.class); - } -} - - From af8eef31ed00826ff3c0769c2d4651a28a43233e Mon Sep 17 00:00:00 2001 From: Tezz03 <105926982+Tezz03@users.noreply.github.com> Date: Fri, 28 Mar 2025 11:06:34 -0400 Subject: [PATCH 3/6] Delete lesson_09/types/types_app/src/main/resources/data/montezb.json --- .../src/main/resources/data/montezb.json | 92 ------------------- 1 file changed, 92 deletions(-) delete mode 100644 lesson_09/types/types_app/src/main/resources/data/montezb.json diff --git a/lesson_09/types/types_app/src/main/resources/data/montezb.json b/lesson_09/types/types_app/src/main/resources/data/montezb.json deleted file mode 100644 index 1d8cbf9a..00000000 --- a/lesson_09/types/types_app/src/main/resources/data/montezb.json +++ /dev/null @@ -1,92 +0,0 @@ -[ - { - "column1": "false", - "column2": "2.0165133E38", - "column3": "2.7989049813330837E307", - "column4": "416", - "column5": "435833166", - "column6": "3cuw2", - "column7": "3530240046379697664" - }, - { - "column1": "true", - "column2": "1.460264E37", - "column3": "2.9225361274287315E307", - "column4": "17516", - "column5": "260615671", - "column6": "8j52s4aq6d", - "column7": "65341810874192888" - }, - { - "column1": "true", - "column2": "2.776658E38", - "column3": "2.0826943783702183E307", - "column4": "31533", - "column5": "161572734", - "column6": "s8yavhue1", - "column7": "4903929290600323072" - }, - { - "column1": "true", - "column2": "1.3027212E38", - "column3": "1.1914573950682302E308", - "column4": "29377", - "column5": "451167147", - "column6": "2at5p9weyni", - "column7": "1756929147146946048" - }, - { - "column1": "true", - "column2": "1.6506819E38", - "column3": "3.7690907310588337E307", - "column4": "23676", - "column5": "405162496", - "column6": "migsey8lk", - "column7": "5227682372169227264" - }, - { - "column1": "true", - "column2": "6.687427E37", - "column3": "1.7507811122323619E308", - "column4": "763", - "column5": "385548066", - "column6": "ni6d58js04u", - "column7": "814327769818061056" - }, - { - "column1": "false", - "column2": "2.6281565E38", - "column3": "3.3046191712251996E307", - "column4": "29441", - "column5": "497419740", - "column6": "7ypumj83a", - "column7": "6639371891920120832" - }, - { - "column1": "false", - "column2": "9.816115E37", - "column3": "1.3824553928097414E308", - "column4": "3537", - "column5": "753736722", - "column6": "68aglu4", - "column7": "1905184695820989696" - }, - { - "column1": "false", - "column2": "2.674275E38", - "column3": "1.562287548934654E308", - "column4": "25482", - "column5": "2121817019", - "column6": "2plvf", - "column7": "8721696529208318976" - }, - { - "column1": "true", - "column2": "7.985794E37", - "column3": "1.377992454751497E308", - "column4": "29060", - "column5": "1516947099", - "column6": "c4685r9", - "column7": "5859838884205409280" - } -] \ No newline at end of file From 1e6771f54f9cc3c5b33830d943be292be8c30eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTezz03=E2=80=9D?= <“mmbradley@gmail.com”> Date: Mon, 14 Apr 2025 16:03:32 +0000 Subject: [PATCH 4/6] Feat: Need more time --- .../lesson17/bank/BankAtm.java | 24 +++- .../lesson17/bank/BusinessAccount.java | 34 +++++ .../codedifferently/lesson17/bank/Check.java | 12 +- .../lesson17/bank/CheckingAccount.java | 3 +- .../lesson17/bank/SavingsAccount.java | 47 +++++++ .../lesson17/bank/SavingAccount.java | 5 + .../lesson17/bank/SavingsAccountTest.java | 127 ++++++++++++++++++ 7 files changed, 242 insertions(+), 10 deletions(-) create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessAccount.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/bank/SavingAccount.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 8cbcd3cc..539d7626 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 java.util.stream.Collectors; + +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<>(); + private final Map accountByType = new HashMap<>(); /** * Adds a checking account to the bank. @@ -18,6 +21,9 @@ public class BankAtm { * @param account The account to add. */ public void addAccount(CheckingAccount account) { + if (account == null) { + throw new IllegalArgumentException("Account cannot be null"); + } accountByNumber.put(account.getAccountNumber(), account); account .getOwners() @@ -33,9 +39,9 @@ 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() + ? customerById.get(customerId).getAccounts().stream().collect(Collectors.toSet()) : Set.of(); } @@ -46,9 +52,15 @@ public Set findAccountsByCustomerId(UUID customerId) { * @param amount The amount to deposit. */ public void depositFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); - account.deposit(amount); - } + Account account = getAccountOrThrow(accountNumber); + + if (account instanceof CheckingAccount) { + ((CheckingAccount) account).deposit(amount); + } else if (account instanceof BusinessAccount) { + ((BusinessAccount) account).deposit(amount); + } else { + throw new IllegalArgumentException("Unsupported account type"); + } /** * Deposits funds into an account using a check. diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessAccount.java new file mode 100644 index 00000000..013e355c --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessAccount.java @@ -0,0 +1,34 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; + +public class BusinessAccount extends CheckingAccount { + + /** + * Creates a new business account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public BusinessAccount(String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } + + /** + * Withdraws funds from the account. + * + * @param amount The amount to withdraw. + * @throws InsufficientFundsException If there are insufficient funds in the account. + */ + @Override + public void withdraw(double amount) throws InsufficientFundsException { + if (amount > getBalance()) { + throw new InsufficientFundsException("Insufficient funds in the account."); + } + super.withdraw(amount); + } + +} 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..998e817e 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 @@ -9,21 +9,27 @@ public class Check { private final double amount; private final CheckingAccount account; private boolean isVoided = false; + private final SavingsAccount savings; /** * Creates a new check. * * @param checkNumber The check number. * @param amount The amount of the check. - * @param account The account the check is drawn on. + * @param account The account the check is drawn on. Because account is represents checking it could also represent savings.so if + * we call saving account saving we should be able to throw the exception on the check. */ - public Check(String checkNumber, double amount, CheckingAccount account) { - if (amount < 0) { + public Check(String checkNumber, double amount, CheckingAccount account, SavingsAccount savings) { + if (checkNumber == null || checkNumber.isEmpty()) { + throw new IllegalArgumentException("Check number cannot be null or empty"); + } + if (amount < 0 ) { throw new IllegalArgumentException("Check amount must be positive"); } this.checkNumber = checkNumber; this.amount = amount; this.account = account; + this.savings = null; } /** 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..3e701978 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,8 +1,9 @@ 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 { 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..c500cb8d --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,47 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +// Removed redundant or incorrect import + + +/** Represents a savings account, which does not support check writing. */ +public class SavingsAccount extends CheckingAccount { + + /** + * Creates a new savings account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public SavingsAccount(String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } + + /** + * Withdraws funds from the account. + * + * @param amount The amount to withdraw. + * @throws InsufficientFundsException If there are insufficient funds in the account. + */ + @Override + public void withdraw(double amount) throws InsufficientFundsException { + if (amount > getBalance()) { + throw new InsufficientFundsException("Insufficient funds in the account."); + } + // Use a protected setter or a method in parent class if balance is private + super.withdraw(amount); // Use super if parent class has the logic + } + + /** + * Disallows writing checks from a savings account. + * + * @param check The amount to write on the check. + * @throws UnsupportedOperationException Always, since savings accounts don't support checks. + */ + public void writeCheck(Check check) { + throw new UnsupportedOperationException("Savings accounts can't write checks."); + } +} diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccount.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccount.java new file mode 100644 index 00000000..dd780f94 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingAccount.java @@ -0,0 +1,5 @@ +package com.codedifferently.lesson17.bank; + +public class SavingAccount { + +} 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 00000000..6d1112e7 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java @@ -0,0 +1,127 @@ +package com.codedifferently.lesson17.bank; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +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 org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.codedifferently.lesson17.bank.SavingsAccount; +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; + +public class SavingsAccountTest { + + + private SavingsAccount classUnderTest; + private Set owners; + + @BeforeEach + void setUp() { + owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe")); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith")); + classUnderTest = new SavingsAccount("123456789", owners, 100.0); + } + + @Test + void getAccountNumber() { + assertEquals("123456789", classUnderTest.getAccountNumber()); + } + + @Test + void getOwners() { + assertEquals(owners, classUnderTest.getOwners()); + } + + @Test + void deposit() { + classUnderTest.deposit(50.0); + assertEquals(150.0, classUnderTest.getBalance()); + } + + @Test + void deposit_withNegativeAmount() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> classUnderTest.deposit(-50.0)); + } + + @Test + void withdraw() { + classUnderTest.withdraw(50.0); + assertEquals(50.0, classUnderTest.getBalance()); + } + + @Test + void withdraw_withNegativeAmount() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.withdraw(-50.0)) + .withMessage("Withdrawal amount must be positive"); + } + + @Test + void withdraw_withInsufficientBalance() { + assertThatExceptionOfType(InsufficientFundsException.class) + .isThrownBy(() -> classUnderTest.withdraw(150.0)) + .withMessage("Account does not have enough funds for withdrawal"); + } + + @Test + void getBalance() { + assertEquals(100.0, classUnderTest.getBalance()); + } + + @Test + void closeAccount_withPositiveBalance() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.closeAccount()); + } + + @Test + void isClosed() { + assertFalse(classUnderTest.isClosed()); + classUnderTest.withdraw(100); + classUnderTest.closeAccount(); + assertTrue(classUnderTest.isClosed()); + } + + @Test + void equals() { + SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0); + assertEquals(classUnderTest, otherAccount); + } + + @Test + void hashCodeTest() { + SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0); + assertEquals(classUnderTest.hashCode(), otherAccount.hashCode()); + } + + @Test + void toStringTest() { + String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}"; + assertEquals(expected, classUnderTest.toString()); + } + + @Test + void writeCheck() { + // Arrange + Check check = new Check("987654321", 50.0, classUnderTest, classUnderTest); + + // Act + classUnderTest.writeCheck(check); + + // Assert + assertEquals(50.0, classUnderTest.getBalance()); + assertTrue(check.getIsVoided()); + + } +} + + + + From 05b9e0af7186844560318b83e47e8dd3269c981d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTezz03=E2=80=9D?= <“mmbradley@gmail.com”> Date: Wed, 16 Apr 2025 18:02:34 +0000 Subject: [PATCH 5/6] feat: NewPR --- .../com/codedifferently/lesson17/bank/Check.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) 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 998e817e..ab4a2c6d 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 @@ -2,34 +2,27 @@ import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException; -/** Represents a check. */ public class Check { private final String checkNumber; private final double amount; private final CheckingAccount account; private boolean isVoided = false; - private final SavingsAccount savings; /** * Creates a new check. * * @param checkNumber The check number. * @param amount The amount of the check. - * @param account The account the check is drawn on. Because account is represents checking it could also represent savings.so if - * we call saving account saving we should be able to throw the exception on the check. + * @param account The account the check is drawn on. */ - public Check(String checkNumber, double amount, CheckingAccount account, SavingsAccount savings) { - if (checkNumber == null || checkNumber.isEmpty()) { - throw new IllegalArgumentException("Check number cannot be null or empty"); - } - if (amount < 0 ) { + public Check(String checkNumber, double amount, CheckingAccount account) { + if (amount < 0) { throw new IllegalArgumentException("Check amount must be positive"); } this.checkNumber = checkNumber; this.amount = amount; this.account = account; - this.savings = null; } /** @@ -50,8 +43,9 @@ public void voidCheck() { * Deposits the check into an account. * * @param toAccount The account to deposit the check into. + * @throws Exception */ - public void depositFunds(CheckingAccount toAccount) { + public void depositFunds(BankAccount toAccount) throws Exception { if (isVoided) { throw new CheckVoidedException("Check is voided"); } From a0e1cd09ed1c9f81b0142f26b4d00352fb4017eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTezz03=E2=80=9D?= <“mmbradley@gmail.com”> Date: Wed, 16 Apr 2025 18:05:31 +0000 Subject: [PATCH 6/6] fix:update rest of files --- .../lesson17/bank/BankAccount.java | 132 ++++++++++++++++++ .../lesson17/bank/BankAtm.java | 39 ++---- .../bank/BusinessCheckingAccount.java | 23 +++ .../lesson17/bank/CheckingAccount.java | 96 +------------ .../lesson17/bank/Customer.java | 27 +++- .../lesson17/bank/SavingsAccount.java | 76 +++++----- .../InvalidBusinessAccountException.java | 7 + .../lesson17/bank/BankAtmTest.java | 125 ++++++++++------- .../lesson17/bank/CheckTest.java | 115 +++++++++------ .../lesson17/bank/CheckingAccountTest.java | 5 +- .../lesson17/bank/SavaingsAccountTest.java | 118 ++++++++++++++++ 11 files changed, 505 insertions(+), 258 deletions(-) create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java 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/exceptions/InvalidBusinessAccountException.java create mode 100644 lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavaingsAccountTest.java 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 00000000..126705e4 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java @@ -0,0 +1,132 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; + +/** Represents a checking account. */ +public class BankAccount { + + private final 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. + */ + // Constructor + public BankAccount(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 BankAccount other) { + return accountNumber.equals(other.accountNumber); + } + return false; + } + + @Override + public String toString() { + return "BankAccount{" + + "accountNumber='" + + accountNumber + + '\'' + + ", balance=" + + balance + + ", isActive=" + + isActive + + '}'; + } +} 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 539d7626..6fba37d6 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,29 +1,24 @@ 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 java.util.stream.Collectors; -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<>(); - private final Map accountByType = new HashMap<>(); + private final Map accountByNumber = new HashMap<>(); /** - * Adds a checking account to the bank. + * Adds an account to the bank. * * @param account The account to add. */ - public void addAccount(CheckingAccount account) { - if (account == null) { - throw new IllegalArgumentException("Account cannot be null"); - } + public void addAccount(BankAccount account) { accountByNumber.put(account.getAccountNumber(), account); account .getOwners() @@ -39,7 +34,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().stream().collect(Collectors.toSet()) : Set.of(); @@ -52,15 +47,9 @@ public Set findAccountsByCustomerId(UUID customerId) { * @param amount The amount to deposit. */ public void depositFunds(String accountNumber, double amount) { - Account account = getAccountOrThrow(accountNumber); - - if (account instanceof CheckingAccount) { - ((CheckingAccount) account).deposit(amount); - } else if (account instanceof BusinessAccount) { - ((BusinessAccount) account).deposit(amount); - } else { - throw new IllegalArgumentException("Unsupported account type"); - } + BankAccount account = getAccountOrThrow(accountNumber); + account.deposit(amount); + } /** * Deposits funds into an account using a check. @@ -68,8 +57,8 @@ public void depositFunds(String accountNumber, double amount) { * @param accountNumber The account number. * @param check The check to deposit. */ - public void depositFunds(String accountNumber, Check check) { - CheckingAccount account = getAccountOrThrow(accountNumber); + public void depositFunds(String accountNumber, Check check) throws Exception { + BankAccount account = getAccountOrThrow(accountNumber); check.depositFunds(account); } @@ -79,8 +68,8 @@ public void depositFunds(String accountNumber, Check check) { * @param accountNumber * @param amount */ - public void withdrawFunds(String accountNumber, double amount) { - CheckingAccount account = getAccountOrThrow(accountNumber); + public void withdrawFunds(String accountNumber, double amount) throws Exception { + BankAccount account = getAccountOrThrow(accountNumber); account.withdraw(amount); } @@ -90,8 +79,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 BankAccount getAccountOrThrow(String accountNumber) { + BankAccount 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..ba8f40c7 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,23 @@ +package com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException; +import java.util.Set; + +/** + * Creates a business account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ +public class BusinessCheckingAccount extends CheckingAccount { + public BusinessCheckingAccount( + String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + + boolean hasBusinessOwner = owners.stream().anyMatch(customer -> customer.isBusiness()); + if (!hasBusinessOwner) { + throw new InvalidBusinessAccountException("At least one owner must be a business."); + } + } +} 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 3e701978..a3141cae 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 @@ -2,10 +2,8 @@ import java.util.Set; -import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; - /** Represents a checking account. */ -public class CheckingAccount { +public class CheckingAccount extends BankAccount { private final Set owners; private final String accountNumber; @@ -20,103 +18,13 @@ 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; - } - - /** - * 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; - } - @Override public String toString() { return "CheckingAccount{" 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..e210009f 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 @@ -7,9 +7,14 @@ /** Represents a customer of the bank. */ public class Customer { + static boolean isBusinessStatic() { + throw new UnsupportedOperationException("Not supported yet."); + } + private final UUID id; private final String name; - private final Set accounts = new HashSet<>(); + private final Set accounts = new HashSet<>(); + private final boolean isBusiness; /** * Creates a new customer. @@ -17,9 +22,10 @@ public class Customer { * @param id The ID of the customer. * @param name The name of the customer. */ - public Customer(UUID id, String name) { + public Customer(UUID id, String name, boolean isBusiness) { this.id = id; this.name = name; + this.isBusiness = isBusiness; } /** @@ -41,11 +47,20 @@ public String getName() { } /** - * Adds a checking account to the customer. + * Adds a checking account to the customer. Checks if this is a business + * + * @return True if this is a business, otherwise false. + */ + public boolean isBusiness() { + return isBusiness; + } + + /** + * Adds an account to the customer. * - * @param account The account to add. + * @param checkingAccount2 The account to add. */ - public void addAccount(CheckingAccount account) { + public void addAccount(BankAccount account) { accounts.add(account); } @@ -54,7 +69,7 @@ public void addAccount(CheckingAccount account) { * * @return The unique set of accounts owned by the customer. */ - public Set getAccounts() { + public Set getAccounts() { return accounts; } 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 c500cb8d..71780205 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,47 +1,47 @@ package com.codedifferently.lesson17.bank; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; -// Removed redundant or incorrect import +/** Represents a saving account. */ +public class SavingsAccount extends BankAccount { + // private boolean isActive; + private final boolean isCheckCreationAllowed; + private static final Logger logger = LoggerFactory.getLogger(SavingsAccount.class); + /** + * Creates a new saving account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public SavingsAccount(String accountNumber, Set owners, double balance) { + super(accountNumber, owners, balance); + logger.info("Saving Account constructor accessed..."); + isCheckCreationAllowed = false; + } -/** Represents a savings account, which does not support check writing. */ -public class SavingsAccount extends CheckingAccount { + public boolean isActive() { + // Logic to determine if the account is active + return true; // Replace with actual logic + } - /** - * Creates a new savings account. - * - * @param accountNumber The account number. - * @param owners The owners of the account. - * @param initialBalance The initial balance of the account. - */ - public SavingsAccount(String accountNumber, Set owners, double initialBalance) { - super(accountNumber, owners, initialBalance); - } + @Override + public String toString() { + return "SavingsAccount{" + + "accountNumber='" + + getAccountNumber() + + '\'' + + ", balance=" + + getBalance() + + ", isActive=" + + isActive() + + '}'; + } - /** - * Withdraws funds from the account. - * - * @param amount The amount to withdraw. - * @throws InsufficientFundsException If there are insufficient funds in the account. - */ - @Override - public void withdraw(double amount) throws InsufficientFundsException { - if (amount > getBalance()) { - throw new InsufficientFundsException("Insufficient funds in the account."); - } - // Use a protected setter or a method in parent class if balance is private - super.withdraw(amount); // Use super if parent class has the logic - } - - /** - * Disallows writing checks from a savings account. - * - * @param check The amount to write on the check. - * @throws UnsupportedOperationException Always, since savings accounts don't support checks. - */ - public void writeCheck(Check check) { - throw new UnsupportedOperationException("Savings accounts can't write checks."); - } + public boolean isCheckCreationAllowed() { + return isCheckCreationAllowed; + } } diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java new file mode 100644 index 00000000..e9524ed4 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/exceptions/InvalidBusinessAccountException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson17.bank.exceptions; + +public class InvalidBusinessAccountException extends RuntimeException { + public InvalidBusinessAccountException(String message) { + super(message); + } +} 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..e1c8d709 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 @@ -2,9 +2,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; -import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException; +import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException; import java.util.Set; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; @@ -13,95 +14,123 @@ class BankAtmTest { private BankAtm classUnderTest; - private CheckingAccount account1; - private CheckingAccount account2; + private CheckingAccount checkingAccount1; + private CheckingAccount checkingAccount2; + private SavingsAccount savingsAccount1; + private SavingsAccount savingsAccount2; private Customer customer1; private Customer customer2; @BeforeEach void setUp() { classUnderTest = new BankAtm(); - customer1 = new Customer(UUID.randomUUID(), "John Doe"); - customer2 = new Customer(UUID.randomUUID(), "Jane Smith"); - account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0); - account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0); - customer1.addAccount(account1); - customer1.addAccount(account2); - customer2.addAccount(account2); - classUnderTest.addAccount(account1); - classUnderTest.addAccount(account2); + customer1 = new Customer(UUID.randomUUID(), "John Doe", false); + customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false); + + checkingAccount1 = new CheckingAccount("12345", Set.of(customer1), 100.0); + checkingAccount2 = new CheckingAccount("54321", Set.of(customer1, customer2), 200.0); + savingsAccount1 = new SavingsAccount("56789", Set.of(customer1), 100.0); + savingsAccount2 = new SavingsAccount("98765", Set.of(customer1, customer2), 200.0); + customer1.addAccount(checkingAccount1); + customer1.addAccount(checkingAccount2); + customer1.addAccount(savingsAccount1); + customer1.addAccount(savingsAccount2); + classUnderTest.addAccount(checkingAccount1); + classUnderTest.addAccount(checkingAccount2); + classUnderTest.addAccount(savingsAccount1); + classUnderTest.addAccount(savingsAccount2); } @Test void testAddAccount() { // Arrange - Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson"); - CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0); - customer3.addAccount(account3); + Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false); + CheckingAccount checkingAccount3 = new CheckingAccount("555555555", Set.of(customer3), 300.0); + SavingsAccount savingsAccount3 = new SavingsAccount("666666666", Set.of(customer3), 400.0); + customer3.addAccount(checkingAccount3); + customer3.addAccount(savingsAccount3); + classUnderTest.addAccount(checkingAccount3); + classUnderTest.addAccount(savingsAccount3); + Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); + assertThat(accounts).contains(checkingAccount3); + assertThat(accounts).contains(savingsAccount3); + } + + @Test + void testBusinessCheckingAccount_WithBusinessOwner_Succeeds() { + // Arrange + Customer businessCustomer = new Customer(UUID.randomUUID(), "Business LLC", true); + Customer individualCustomer = new Customer(UUID.randomUUID(), "Regular Joe", false); + Set owners = Set.of(businessCustomer, individualCustomer); // Act - classUnderTest.addAccount(account3); + BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("BUS123", owners, 1000.0); // Assert - Set accounts = classUnderTest.findAccountsByCustomerId(customer3.getId()); - assertThat(accounts).containsOnly(account3); + assertThat(businessAccount.getBalance()).isEqualTo(1000.0); + assertThat(businessAccount.getOwners()).contains(businessCustomer); } @Test - void testFindAccountsByCustomerId() { - // Act - Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); + void testBusinessCheckingAccount_WithoutBusinessOwner_ThrowsException() { + // Arrange + Customer c1 = new Customer(UUID.randomUUID(), "Person One", false); + Customer c2 = new Customer(UUID.randomUUID(), "Person Two", false); + Set owners = Set.of(c1, c2); - // Assert - assertThat(accounts).containsOnly(account1, account2); + // Act & Assert + assertThatThrownBy(() -> new BusinessCheckingAccount("BUS999", owners, 500.0)) + .isInstanceOf(InvalidBusinessAccountException.class) + .hasMessage("At least one owner must be a business."); + } + + @Test + void testFindAccountsByCustomerId() { + Set accounts = classUnderTest.findAccountsByCustomerId(customer1.getId()); + assertThat(accounts).contains(checkingAccount1, checkingAccount2); + assertThat(accounts).contains(savingsAccount1, savingsAccount2); } @Test void testDepositFunds() { // Act - classUnderTest.depositFunds(account1.getAccountNumber(), 50.0); - - // Assert - assertThat(account1.getBalance()).isEqualTo(150.0); + classUnderTest.depositFunds(checkingAccount1.getAccountNumber(), 50.0); + classUnderTest.depositFunds(savingsAccount1.getAccountNumber(), 50.0); + assertThat(checkingAccount1.getBalance()).isEqualTo(150.0); + assertThat(savingsAccount1.getBalance()).isEqualTo(150.0); } @Test - void testDepositFunds_Check() { + void testDepositFunds_Check() throws Exception { // Arrange - Check check = new Check("987654321", 100.0, account1); - - // Act - classUnderTest.depositFunds("987654321", check); - - // Assert - assertThat(account1.getBalance()).isEqualTo(0); - assertThat(account2.getBalance()).isEqualTo(300.0); + Check check = new Check("12345", 100.0, checkingAccount1); + classUnderTest.depositFunds("54321", check); + assertThat(checkingAccount1.getBalance()).isEqualTo(0); + assertThat(checkingAccount2.getBalance()).isEqualTo(300.0); } @Test - void testDepositFunds_DoesntDepositCheckTwice() { - Check check = new Check("987654321", 100.0, account1); - - classUnderTest.depositFunds("987654321", check); - - assertThatExceptionOfType(CheckVoidedException.class) - .isThrownBy(() -> classUnderTest.depositFunds("987654321", check)) - .withMessage("Check is voided"); + void testDepositFunds_DoesntDepositCheckTwice() throws Exception { + Check check = new Check("987654321", 100.0, checkingAccount1); + classUnderTest.depositFunds("54321", check); + assertThatThrownBy(() -> classUnderTest.depositFunds("54321", check)) + .hasMessage("Check is voided"); } @Test - void testWithdrawFunds() { + void testWithdrawFunds() throws Exception { // Act - classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0); + classUnderTest.withdrawFunds(checkingAccount2.getAccountNumber(), 50.0); + classUnderTest.withdrawFunds(savingsAccount2.getAccountNumber(), 50.0); // Assert - assertThat(account2.getBalance()).isEqualTo(150.0); + assertThat(checkingAccount2.getBalance()).isEqualTo(150.0); + assertThat(savingsAccount2.getBalance()).isEqualTo(150.0); } @Test void testWithdrawFunds_AccountNotFound() { String nonExistingAccountNumber = "999999999"; - // Act & Assert assertThatExceptionOfType(AccountNotFoundException.class) .isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0)) diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java index 6b62d39b..710e0154 100644 --- a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckTest.java @@ -1,78 +1,105 @@ package com.codedifferently.lesson17.bank; -import static org.assertj.core.api.Assertions.assertThat; 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.CheckVoidedException; +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 CheckTest { - - private CheckingAccount account1; - private CheckingAccount account2; - private Check classUnderTest; + private CheckingAccount classUnderTest; + private Set owners; @BeforeEach void setUp() { - account1 = new CheckingAccount("123456789", null, 100.0); - account2 = new CheckingAccount("987654321", null, 200.0); - classUnderTest = new Check("123456789", 50.0, account1); + owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe", false)); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false)); + classUnderTest = new CheckingAccount("123456789", owners, 100.0); } @Test - void testDepositFunds() { - // Act - classUnderTest.depositFunds(account2); + void getAccountNumber() { + assertEquals("123456789", classUnderTest.getAccountNumber()); + } - // Assert - assertThat(account1.getBalance()).isEqualTo(50.0); - assertThat(account2.getBalance()).isEqualTo(250.0); + @Test + void getOwners() { + assertEquals(owners, classUnderTest.getOwners()); } @Test - void testDepositFunds_CheckVoided() { - // Arrange - classUnderTest.voidCheck(); - - // Act & Assert - assertThatExceptionOfType(CheckVoidedException.class) - .isThrownBy(() -> classUnderTest.depositFunds(account2)) - .withMessage("Check is voided"); + void deposit() { + classUnderTest.deposit(50.0); + assertEquals(150.0, classUnderTest.getBalance()); } @Test - void testConstructor_CantCreateCheckWithNegativeAmount() { - // Act & Assert + void deposit_withNegativeAmount() { assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> new Check("123456789", -50.0, account1)) - .withMessage("Check amount must be positive"); + .isThrownBy(() -> classUnderTest.deposit(-50.0)); + } + + @Test + void withdraw() { + classUnderTest.withdraw(50.0); + assertEquals(50.0, classUnderTest.getBalance()); + } + + @Test + void withdraw_withNegativeAmount() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.withdraw(-50.0)) + .withMessage("Withdrawal amount must be positive"); + } + + @Test + void withdraw_withInsufficientBalance() { + assertThatExceptionOfType(InsufficientFundsException.class) + .isThrownBy(() -> classUnderTest.withdraw(150.0)) + .withMessage("Account does not have enough funds for withdrawal"); + } + + @Test + void getBalance() { + assertEquals(100.0, classUnderTest.getBalance()); } @Test - void testHashCode() { - // Arrange - Check otherCheck = new Check("123456789", 100.0, account1); + void closeAccount_withPositiveBalance() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.closeAccount()); + } - // Assert - assertThat(classUnderTest.hashCode()).isEqualTo(otherCheck.hashCode()); + @Test + void isClosed() { + assertFalse(classUnderTest.isClosed()); + classUnderTest.withdraw(100); + classUnderTest.closeAccount(); + assertTrue(classUnderTest.isClosed()); + } + + @Test + void equals() { + CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0); + assertEquals(classUnderTest, otherAccount); } @Test - void testEquals() { - // Arrange - Check otherCheck = new Check("123456789", 100.0, account1); - Check differentCheck = new Check("987654321", 100.0, account1); - - // Assert - assertThat(classUnderTest.equals(otherCheck)).isTrue(); - assertThat(classUnderTest.equals(differentCheck)).isFalse(); + void hashCodeTest() { + CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0); + assertEquals(classUnderTest.hashCode(), otherAccount.hashCode()); } @Test - void testToString() { - // Assert - assertThat(classUnderTest.toString()) - .isEqualTo("Check{checkNumber='123456789', amount=50.0, account=123456789}"); + void toStringTest() { + String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}"; + assertEquals(expected, classUnderTest.toString()); } } 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 f155d8e5..9e113775 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 @@ -13,15 +13,14 @@ import org.junit.jupiter.api.Test; class CheckingAccountTest { - private CheckingAccount classUnderTest; private Set owners; @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", false)); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false)); classUnderTest = new CheckingAccount("123456789", owners, 100.0); } diff --git a/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavaingsAccountTest.java b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavaingsAccountTest.java new file mode 100644 index 00000000..de5c2881 --- /dev/null +++ b/lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavaingsAccountTest.java @@ -0,0 +1,118 @@ +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 SavingAccountTest { + + private SavingsAccount classUnderTest; + private Set owners; + + @BeforeEach + void setUp() { + owners = new HashSet<>(); + owners.add(new Customer(UUID.randomUUID(), "John Doe", false)); + owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false)); + classUnderTest = new SavingsAccount("123456789", owners, 100.0); + } + + @Test + void getAccountNumber() { + assertEquals("123456789", classUnderTest.getAccountNumber()); + } + + @Test + void getOwners() { + assertEquals(owners, classUnderTest.getOwners()); + } + + @Test + void deposit() { + classUnderTest.deposit(50.0); + assertEquals(150.0, classUnderTest.getBalance()); + } + + @Test + void deposit_withNegativeAmount() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> classUnderTest.deposit(-50.0)); + } + + @Test + void withdraw() { + classUnderTest.withdraw(50.0); + assertEquals(50.0, classUnderTest.getBalance()); + } + + @Test + void withdraw_withNegativeAmount() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.withdraw(-50.0)) + .withMessage("Withdrawal amount must be positive"); + } + + @Test + void withdraw_withInsufficientBalance() { + assertThatExceptionOfType(InsufficientFundsException.class) + .isThrownBy(() -> classUnderTest.withdraw(150.0)) + .withMessage("Account does not have enough funds for withdrawal"); + } + + @Test + void getBalance() { + assertEquals(100.0, classUnderTest.getBalance()); + } + + @Test + void closeAccount_withPositiveBalance() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> classUnderTest.closeAccount()); + } + + @Test + void isClosed() { + assertFalse(classUnderTest.isClosed()); + classUnderTest.withdraw(100); + classUnderTest.closeAccount(); + assertTrue(classUnderTest.isClosed()); + } + + @Test + void equals() { + CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0); + assertEquals(classUnderTest, otherAccount); + } + + @Test + void hashCodeTest() { + SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0); + assertEquals(classUnderTest.hashCode(), otherAccount.hashCode()); + } + + @Test + void toStringTest() { + String expected = "SavingsAccount{accountNumber='123456789', balance=100.0, isActive=true}"; + assertEquals(expected, classUnderTest.toString()); + } + + @Test + public void testIsCheckCreationAllowed_WhenFalse() { + // Arrange: + + // Act: + boolean result = classUnderTest.isCheckCreationAllowed(); + + // Assert: + assertFalse( + result, "Check creation should not be allowed when isCheckCreationAllowed is false"); + } +}