Skip to content

Commit 0bb67d3

Browse files
committed
feat: adds BusinessCheckingAccount and BankAccount classes, update tests and methods for account handling
1 parent bf71953 commit 0bb67d3

File tree

11 files changed

+221
-104
lines changed

11 files changed

+221
-104
lines changed
Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,59 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import java.util.Set;
4-
53
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
65

7-
/** Base class for all bank accounts. */
6+
/**
7+
* Checks if the account is closed
8+
*
9+
* @returns true if the account is closed, otherwise false
10+
*/
811
public abstract class BankAccount {
912
private String accountNumber;
10-
private double balance;
13+
protected double balance;
1114
private Set<Customer> owners;
15+
private boolean isClosed;
1216

1317
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
1418
this.accountNumber = accountNumber;
1519
this.owners = owners;
1620
this.balance = initialBalance;
1721
}
18-
/**
19-
* Gets the account number.
20-
*
21-
* @return The account number.
22-
*/
2322

23+
public boolean isActive() {
24+
return isClosed;
25+
}
26+
27+
/**
28+
* Gets the owners of the account.
29+
*
30+
* @return The owners of the account.
31+
*/
32+
public Set<Customer> getOwners() {
33+
return owners;
34+
}
35+
36+
/**
37+
* Gets the account number.
38+
*
39+
* @return The account number.
40+
*/
41+
public String getAccountNumber() {
42+
return accountNumber;
43+
}
44+
45+
/**
46+
* Withdraws funds from the account.
47+
*
48+
* @param amount
49+
* @throws InsufficientFundsException
50+
*/
2451
public void withdraw(double amount) throws InsufficientFundsException {
25-
if (amount > balance) {
26-
throw new InsufficientFundsException("Insufficient funds");
52+
if (amount <= 0) {
53+
throw new IllegalStateException("Withdrawal amount must be positive");
54+
}
55+
if (balance < amount) {
56+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
2757
}
2858
balance -= amount;
2959
}
@@ -34,13 +64,10 @@ public void withdraw(double amount) throws InsufficientFundsException {
3464
* @param amount The amount to deposit.
3565
*/
3666
public void deposit(double amount) throws IllegalStateException {
37-
if (isClosed()) {
38-
throw new IllegalStateException("Cannot deposit to a closed account");
39-
}
40-
if (amount <= 0) {
41-
throw new IllegalArgumentException("Deposit amount must be positive");
42-
}
43-
balance += amount;
44-
}
45-
protected abstract boolean isClosed();
67+
68+
if (amount <= 0) {
69+
throw new IllegalArgumentException("Deposit amount must be positive");
70+
}
71+
balance += amount;
72+
}
4673
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void addAccount(SavingsAccount account) {
4949
* @param customerId The ID of the customer.
5050
* @return The unique set of accounts owned by the customer.
5151
*/
52-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
52+
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
5353
return customerById.containsKey(customerId)
5454
? customerById.get(customerId).getAccounts()
5555
: Set.of();

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import java.util.Set;
44

55
/** Represents a business checking account. */
6-
public class BusinessCheckingAccount extends CheckingAccount {
6+
public class BusinessCheckingAccount extends BankAccount {
7+
8+
/**
9+
* Creates a new business checking account.
10+
*
11+
* @param accountNumber The account number.
12+
* @param owners The owners of the account.
13+
* @param initialBalance The initial balance of the account.
14+
*/
715
public BusinessCheckingAccount(
816
String accountNumber, Set<Customer> owners, double initialBalance) {
917
super(accountNumber, owners, initialBalance);
1018
}
11-
/**
12-
* Gets the account number.
13-
*
14-
* @return The account number.
15-
*/
1619
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ public String toString() {
7979
+ account.getAccountNumber()
8080
+ '}';
8181
}
82+
83+
public Object getAmount() {
84+
throw new UnsupportedOperationException("Unimplemented method 'getAmount'");
85+
}
8286
}

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

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import java.util.Set;
4-
53
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
65

76
/** Represents a checking account. */
87
public class CheckingAccount extends BankAccount {
98

10-
private final Set<Customer> owners;
119
private final String accountNumber;
1210
private double balance;
1311
private boolean isActive;
@@ -20,50 +18,10 @@ public class CheckingAccount extends BankAccount {
2018
* @param initialBalance The initial balance of the account.
2119
*/
2220
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
23-
super(accountNumber, owners, initialBalance);
21+
super(accountNumber, owners, initialBalance);
2422
this.accountNumber = accountNumber;
25-
this.owners = owners;
2623
this.balance = initialBalance;
27-
isActive = true;
28-
}
29-
30-
/**
31-
* Gets the account number.
32-
*
33-
* @return The account number.
34-
*/
35-
public String getAccountNumber() {
36-
return accountNumber;
37-
}
38-
39-
/**
40-
* Gets the owners of the account.
41-
*
42-
* @return The owners of the account.
43-
*/
44-
public Set<Customer> getOwners() {
45-
return owners;
46-
}
47-
48-
49-
50-
/**
51-
* Withdraws funds from the account.
52-
*
53-
* @param amount
54-
* @throws InsufficientFundsException
55-
*/
56-
public void withdraw(double amount) throws InsufficientFundsException {
57-
if (isClosed()) {
58-
throw new IllegalStateException("Cannot withdraw from a closed account");
59-
}
60-
if (amount <= 0) {
61-
throw new IllegalStateException("Withdrawal amount must be positive");
62-
}
63-
if (balance < amount) {
64-
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
65-
}
66-
balance -= amount;
24+
this.isActive = true;
6725
}
6826

6927
/**
@@ -92,6 +50,18 @@ public boolean isClosed() {
9250
return !isActive;
9351
}
9452

53+
@Override
54+
public void withdraw(double amount) throws InsufficientFundsException {
55+
super.withdraw(amount);
56+
balance -= amount;
57+
}
58+
59+
@Override
60+
public void deposit(double amount) {
61+
super.deposit(amount);
62+
balance += amount;
63+
}
64+
9565
@Override
9666
public int hashCode() {
9767
return accountNumber.hashCode();

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

Lines changed: 18 additions & 9 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.
@@ -43,19 +43,24 @@ public String getName() {
4343
/**
4444
* Adds a checking account to the customer.
4545
*
46-
* @param account The account to add.
46+
* @param account3 The account to add.
4747
*/
48-
public void addAccount(CheckingAccount account) {
49-
accounts.add(account);
48+
public void addAccount(CheckingAccount account3) {
49+
accounts.add(account3);
5050
}
5151

5252
/**
5353
* Gets the accounts owned by the customer.
5454
*
5555
* @return The unique set of accounts owned by the customer.
5656
*/
57-
public Set<CheckingAccount> getAccounts() {
58-
return accounts;
57+
public Set<BankAccount> getAccounts() {
58+
return new HashSet<>(accounts);
59+
}
60+
61+
/** check if is business account */
62+
public boolean isBusinessAccount() {
63+
return accounts.stream().anyMatch(BusinessCheckingAccount.class::isInstance);
5964
}
6065

6166
@Override
@@ -65,10 +70,14 @@ public int hashCode() {
6570

6671
@Override
6772
public boolean equals(Object obj) {
68-
if (obj instanceof Customer other) {
69-
return id.equals(other.id);
73+
if (this == obj) {
74+
return true;
75+
}
76+
if (obj == null || getClass() != obj.getClass()) {
77+
return false;
7078
}
71-
return false;
79+
Customer other = (Customer) obj;
80+
return id.equals(other.id) && name.equals(other.name);
7281
}
7382

7483
@Override
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
package com.codedifferently.lesson17.bank;
22

33
import java.util.Set;
4-
import java.util.UUID;
54

6-
/** Represents a savings account. */
5+
/** Represents a savings account that doesn't allow checks. */
76
public class SavingsAccount extends BankAccount {
7+
private boolean isActive = true;
88

99
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
1010
super(accountNumber, owners, initialBalance);
1111
}
1212

13-
/**
14-
* Gets the account number.
15-
*
16-
* @return The account number.
17-
*/
18-
public void depositFunds(Check check) {
19-
throw new UnsupportedOperationException("Cannot withdraw from savings account.");
20-
}
21-
22-
public String getAccountNumber() {
23-
// TODO Auto-generated method stub
24-
throw new UnsupportedOperationException("Unimplemented method 'getAccountNumber'");
13+
protected boolean isClosed() {
14+
throw new IllegalStateException("Cannot check if account is closed");
2515
}
2616

27-
public Map<UUID getOwners() {
28-
// TODO Auto-generated method stub
29-
throw new UnsupportedOperationException("Unimplemented method 'getOwners'");
17+
@Override
18+
public boolean isActive() {
19+
return true;
3020
}
3121

32-
@Override
33-
protected boolean isClosed() {
34-
// TODO Auto-generated method stub
35-
throw new UnsupportedOperationException("Unimplemented method 'isClosed'");
22+
public void closeAccount() {
23+
throw new IllegalStateException("Cannot close account with a positive balance");
3624
}
3725
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import java.util.UUID;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class BankAccountTest {
12+
@Test
13+
void testIsActive() {
14+
// Arrange
15+
Set<Customer> owners = new HashSet<>();
16+
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
17+
BankAccount account = new CheckingAccount("123456789", owners, 100.0);
18+
// Act
19+
boolean isActive = account.isActive();
20+
// Assert
21+
assertThat(!isActive).isTrue();
22+
}
23+
24+
@Test
25+
void testGetOwners() {
26+
// Act
27+
Set<Customer> owners = new HashSet<>();
28+
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
29+
BankAccount account = new CheckingAccount("123456789", owners, 100.0);
30+
// Assert
31+
assertEquals(owners, account.getOwners());
32+
}
33+
34+
@Test
35+
void testWithdrawFunds() {
36+
// Arrange
37+
double initialBalance = 100.0;
38+
double withdrawAmount = 50.0;
39+
double expectedBalance = initialBalance - withdrawAmount;
40+
41+
// Act
42+
BankAccount account = new CheckingAccount("123456789", null, initialBalance);
43+
account.withdraw(withdrawAmount);
44+
45+
// Assert
46+
assertThat(account.balance).isEqualTo(expectedBalance);
47+
}
48+
}

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<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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
public class BusinessCheckingAccountTest {}

0 commit comments

Comments
 (0)