Skip to content

Commit 1f7acc3

Browse files
Evan PhilakhongEvan Philakhong
authored andcommitted
feat: immplements SavingsAccount class
1 parent a28e5c8 commit 1f7acc3

File tree

6 files changed

+78
-41
lines changed

6 files changed

+78
-41
lines changed

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

Lines changed: 16 additions & 11 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();
@@ -43,22 +43,27 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4343
* Deposits funds into an account.
4444
*
4545
* @param accountNumber The account number.
46-
* @param amount The amount to deposit.
46+
* @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

5353
/**
5454
* Deposits funds into an account using a check.
5555
*
5656
* @param accountNumber The account number.
57-
* @param check The check to deposit.
57+
* @param check The check to deposit.
5858
*/
5959
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
61-
check.depositFunds(account);
60+
Account account = getAccountOrThrow(accountNumber);
61+
62+
if (account instanceof CheckingAccount checkingAccount) {
63+
check.depositFunds(checkingAccount);
64+
} else if (account instanceof SavingsAccount) {
65+
throw new IllegalStateException("Savings Accounts can't accept checks");
66+
}
6267
}
6368

6469
/**
@@ -68,7 +73,7 @@ public void depositFunds(String accountNumber, Check check) {
6873
* @param amount
6974
*/
7075
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
76+
Account account = getAccountOrThrow(accountNumber);
7277
account.withdraw(amount);
7378
}
7479

@@ -78,8 +83,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7883
* @param accountNumber The account number.
7984
* @return The account.
8085
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
86+
private Account getAccountOrThrow(String accountNumber) {
87+
Account account = accountByNumber.get(accountNumber);
8388
if (account == null || account.isClosed()) {
8489
throw new AccountNotFoundException("Account not found");
8590
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ 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
/**
1414
* Creates a new check.
1515
*
1616
* @param checkNumber The check number.
17-
* @param amount The amount of the check.
18-
* @param account The account the check is drawn on.
17+
* @param amount The amount of the check.
18+
* @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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import lombok.Getter;
4-
53
import java.util.Set;
64

75
/** Represents a checking account. */
8-
@Getter
96
public class CheckingAccount extends Account {
107

118
/**

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,35 @@
44
import java.util.Set;
55
import java.util.UUID;
66

7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
@Getter
11+
@Setter
712
/** Represents a customer of the bank. */
813
public class Customer {
914

1015
private final UUID id;
1116
private final String name;
12-
private final Set<CheckingAccount> accounts = new HashSet<>();
17+
private final Set<Account> accounts = new HashSet<>();
1318

1419
/**
1520
* Creates a new customer.
1621
*
17-
* @param id The ID of the customer.
22+
* @param id The ID of the customer.
1823
* @param name The name of the customer.
1924
*/
2025
public Customer(UUID id, String name) {
2126
this.id = id;
2227
this.name = name;
2328
}
2429

25-
/**
26-
* Gets the ID of the customer.
27-
*
28-
* @return The ID of the customer.
29-
*/
30-
public UUID getId() {
31-
return id;
32-
}
33-
34-
/**
35-
* Gets the name of the customer.
36-
*
37-
* @return The name of the customer.
38-
*/
39-
public String getName() {
40-
return name;
41-
}
42-
4330
/**
4431
* Adds a checking account to the customer.
4532
*
4633
* @param account The account to add.
4734
*/
48-
public void addAccount(CheckingAccount account) {
35+
public void addAccount(Account account) {
4936
accounts.add(account);
5037
}
5138

@@ -54,7 +41,7 @@ public void addAccount(CheckingAccount account) {
5441
*
5542
* @return The unique set of accounts owned by the customer.
5643
*/
57-
public Set<CheckingAccount> getAccounts() {
44+
public Set<Account> getAccounts() {
5845
return accounts;
5946
}
6047

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/** Represents a savings account. */
6+
public class SavingsAccount extends Account {
7+
8+
/**
9+
* Creates a new sacings account.
10+
*
11+
* @param accountNumber The account number.
12+
* @param owners The ownders of the account.
13+
* @param initalBalance The inital balance of the account.
14+
*/
15+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initalBalance) {
16+
super(accountNumber, owners, initalBalance);
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "SavingsAccount{"
22+
+ "accountNumber='"
23+
+ getAccountNumber()
24+
+ '\''
25+
+ ", balance="
26+
+ getBalance()
27+
+ ", isActive="
28+
+ isActive()
29+
+ '}';
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import java.util.UUID;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
class CheckingAccountTest {
16+
17+
}

0 commit comments

Comments
 (0)