Skip to content

Commit a48e204

Browse files
feat: implement Account interface for polymorphism
- Add Account interface with core banking operations - Update CheckingAccount to implement Account interface - Modify BankAtm to work with Account interface instead of CheckingAccount - Update Customer class to store Account objects - Update Check class to work with Account interface - Update tests to use Account interface - All existing functionality preserved with 27 tests passing
1 parent ccf8696 commit a48e204

File tree

6 files changed

+70
-17
lines changed

6 files changed

+70
-17
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Interface representing a bank account. */
7+
public interface Account {
8+
/**
9+
* Gets the account number.
10+
*
11+
* @return The account number.
12+
*/
13+
String getAccountNumber();
14+
15+
/**
16+
* Gets the owners of the account.
17+
*
18+
* @return The owners of the account.
19+
*/
20+
Set<Customer> getOwners();
21+
22+
/**
23+
* Deposits funds into the account.
24+
*
25+
* @param amount The amount to deposit.
26+
*/
27+
void deposit(double amount);
28+
29+
/**
30+
* Withdraws funds from the account.
31+
*
32+
* @param amount The amount to withdraw.
33+
* @throws InsufficientFundsException If insufficient funds.
34+
*/
35+
void withdraw(double amount) throws InsufficientFundsException;
36+
37+
/**
38+
* Gets the balance of the account.
39+
*
40+
* @return The balance of the account.
41+
*/
42+
double getBalance();
43+
44+
/** Closes the account. */
45+
void closeAccount();
46+
47+
/**
48+
* Checks if the account is closed.
49+
*
50+
* @return True if the account is closed, otherwise false.
51+
*/
52+
boolean isClosed();
53+
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
public class BankAtm {
1111

1212
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
13+
private final Map<String, Account> accountByNumber = new HashMap<>();
1414

1515
/**
1616
* Adds a checking account to the bank.
1717
*
1818
* @param account The account to add.
1919
*/
20-
public void addAccount(CheckingAccount account) {
20+
public void addAccount(Account account) {
2121
accountByNumber.put(account.getAccountNumber(), account);
2222
account
2323
.getOwners()
@@ -33,7 +33,7 @@ public void addAccount(CheckingAccount account) {
3333
* @param customerId The ID of the customer.
3434
* @return The unique set of accounts owned by the customer.
3535
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
36+
public Set<Account> findAccountsByCustomerId(UUID customerId) {
3737
return customerById.containsKey(customerId)
3838
? customerById.get(customerId).getAccounts()
3939
: Set.of();
@@ -46,7 +46,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4646
* @param amount The amount to deposit.
4747
*/
4848
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
49+
Account account = getAccountOrThrow(accountNumber);
5050
account.deposit(amount);
5151
}
5252

@@ -57,7 +57,7 @@ public void depositFunds(String accountNumber, double amount) {
5757
* @param check The check to deposit.
5858
*/
5959
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
60+
Account account = getAccountOrThrow(accountNumber);
6161
check.depositFunds(account);
6262
}
6363

@@ -68,7 +68,7 @@ public void depositFunds(String accountNumber, Check check) {
6868
* @param amount
6969
*/
7070
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
71+
Account account = getAccountOrThrow(accountNumber);
7272
account.withdraw(amount);
7373
}
7474

@@ -78,8 +78,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7878
* @param accountNumber The account number.
7979
* @return The account.
8080
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
81+
private Account getAccountOrThrow(String accountNumber) {
82+
Account account = accountByNumber.get(accountNumber);
8383
if (account == null || account.isClosed()) {
8484
throw new AccountNotFoundException("Account not found");
8585
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Check.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Check {
77

88
private final String checkNumber;
99
private final double amount;
10-
private final CheckingAccount account;
10+
private final Account account;
1111
private boolean isVoided = false;
1212

1313
/**
@@ -17,7 +17,7 @@ public class Check {
1717
* @param amount The amount of the check.
1818
* @param account The account the check is drawn on.
1919
*/
20-
public Check(String checkNumber, double amount, CheckingAccount account) {
20+
public Check(String checkNumber, double amount, Account account) {
2121
if (amount < 0) {
2222
throw new IllegalArgumentException("Check amount must be positive");
2323
}
@@ -45,7 +45,7 @@ public void voidCheck() {
4545
*
4646
* @param toAccount The account to deposit the check into.
4747
*/
48-
public void depositFunds(CheckingAccount toAccount) {
48+
public void depositFunds(Account toAccount) {
4949
if (isVoided) {
5050
throw new CheckVoidedException("Check is voided");
5151
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Set;
55

66
/** Represents a checking account. */
7-
public class CheckingAccount {
7+
public class CheckingAccount implements Account {
88

99
private final Set<Customer> owners;
1010
private final String accountNumber;

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Customer {
99

1010
private final UUID id;
1111
private final String name;
12-
private final Set<CheckingAccount> accounts = new HashSet<>();
12+
private final Set<Account> accounts = new HashSet<>();
1313

1414
/**
1515
* Creates a new customer.
@@ -45,7 +45,7 @@ public String getName() {
4545
*
4646
* @param account The account to add.
4747
*/
48-
public void addAccount(CheckingAccount account) {
48+
public void addAccount(Account account) {
4949
accounts.add(account);
5050
}
5151

@@ -54,7 +54,7 @@ public void addAccount(CheckingAccount account) {
5454
*
5555
* @return The unique set of accounts owned by the customer.
5656
*/
57-
public Set<CheckingAccount> getAccounts() {
57+
public Set<Account> getAccounts() {
5858
return accounts;
5959
}
6060

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ void testAddAccount() {
4343
classUnderTest.addAccount(account3);
4444

4545
// Assert
46-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
46+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
4747
assertThat(accounts).containsOnly(account3);
4848
}
4949

5050
@Test
5151
void testFindAccountsByCustomerId() {
5252
// Act
53-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
53+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5454

5555
// Assert
5656
assertThat(accounts).containsOnly(account1, account2);

0 commit comments

Comments
 (0)