Skip to content

Commit 108b1d9

Browse files
committed
chore:altered code to be more efficient
1 parent 46beb05 commit 108b1d9

File tree

7 files changed

+26
-26
lines changed

7 files changed

+26
-26
lines changed

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Account.java renamed to lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
import java.util.Set;
55

66
/** Represents a account. */
7-
public class Account {
7+
abstract class BankAccount {
88

9-
private final Set<Customer> owners;
10-
private final String accountNumber;
9+
protected final Set<Customer> owners;
10+
protected final String accountNumber;
1111
protected double balance;
1212
protected boolean isActive;
1313

1414
/**
15-
* Creates a new account.
15+
* Creates a new Bank account.
1616
*
1717
* @param accountNumber The account number.
1818
* @param owners The owners of the account.
1919
* @param initialBalance The initial balance of the account.
2020
*/
21-
public Account(String accountNumber, Set<Customer> owners, double initialBalance) {
21+
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
2222
this.accountNumber = accountNumber;
2323
this.owners = owners;
2424
this.balance = initialBalance;
@@ -110,7 +110,7 @@ public int hashCode() {
110110

111111
@Override
112112
public boolean equals(Object obj) {
113-
if (obj instanceof Account other) {
113+
if (obj instanceof BankAccount other) {
114114
return accountNumber.equals(other.accountNumber);
115115
}
116116
return false;

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, BankAccount> 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(BankAccount 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<BankAccount> 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+
BankAccount 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+
BankAccount 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+
BankAccount 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 BankAccount getAccountOrThrow(String accountNumber) {
82+
BankAccount 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 BankAccount 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, BankAccount 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(BankAccount 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
@@ -2,7 +2,7 @@
22

33
import java.util.Set;
44

5-
public class CheckingAccount extends Account {
5+
public class CheckingAccount extends BankAccount {
66

77
/**
88
* Creates a new saving account.

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<BankAccount> 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(BankAccount 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<BankAccount> getAccounts() {
5858
return accounts;
5959
}
6060

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

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

55
/** Represents a savings account. */
6-
public class SavingAccount extends Account {
6+
public class SavingAccount extends BankAccount {
77

88
/**
99
* Creates a new savings account.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BankAtmTest {
1414

1515
private BankAtm classUnderTest;
1616
private CheckingAccount account1;
17-
private CheckingAccount account2;
17+
private SavingAccount account2;
1818
private Customer customer1;
1919
private Customer customer2;
2020

@@ -24,7 +24,7 @@ void setUp() {
2424
customer1 = new Customer(UUID.randomUUID(), "John Doe");
2525
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
2626
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
27-
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
27+
account2 = new SavingAccount("987654321", Set.of(customer1, customer2), 200.0);
2828
customer1.addAccount(account1);
2929
customer1.addAccount(account2);
3030
customer2.addAccount(account2);
@@ -43,14 +43,14 @@ void testAddAccount() {
4343
classUnderTest.addAccount(account3);
4444

4545
// Assert
46-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
46+
Set<BankAccount> 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<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5454

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

0 commit comments

Comments
 (0)