File tree Expand file tree Collapse file tree 6 files changed +139
-2
lines changed
lesson_17/bank/bank_app/src
main/java/com/codedifferently/lesson17/bank
test/java/com/codedifferently/lesson17/bank Expand file tree Collapse file tree 6 files changed +139
-2
lines changed Original file line number Diff line number Diff line change 1+ package com .codedifferently .lesson17 .bank ;
2+
3+ import com .codedifferently .lesson17 .bank .exceptions .InsufficientFundsException ;
4+ import java .util .Set ;
5+
6+ /** Base class for all bank accounts. */
7+ public abstract class BankAccount {
8+ private String accountNumber ;
9+ private double balance ;
10+ private Set <Customer > owners ;
11+
12+ public BankAccount (String accountNumber , Set <Customer > owners , double initialBalance ) {
13+ this .accountNumber = accountNumber ;
14+ this .owners = owners ;
15+ this .balance = initialBalance ;
16+ }
17+
18+ public String getAccountNumber () {
19+ return accountNumber ;
20+ }
21+
22+ public double getBalance () {
23+ return balance ;
24+ }
25+
26+ public Set <Customer > getOwners () {
27+ return owners ;
28+ }
29+
30+ public void withdraw (double amount ) throws InsufficientFundsException {
31+ if (amount > balance ) {
32+ throw new InsufficientFundsException ("Insufficient funds" );
33+ }
34+ balance -= amount ;
35+ }
36+
37+ public void deposit (double amount ) {
38+ if (amount <= 0 ) {
39+ throw new IllegalArgumentException ("Deposit amount must be positive" );
40+ }
41+ balance += amount ;
42+ }
43+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ public class BankAtm {
1111
1212 private final Map <UUID , Customer > customerById = new HashMap <>();
1313 private final Map <String , CheckingAccount > accountByNumber = new HashMap <>();
14+ private final Map <String , SavingsAccount > savingsAccountByNumber = new HashMap <>();
1415
1516 /**
1617 * Adds a checking account to the bank.
@@ -27,6 +28,21 @@ public void addAccount(CheckingAccount account) {
2728 });
2829 }
2930
31+ /**
32+ * Adds a savings account to the bank.
33+ *
34+ * @param account The account to add.
35+ */
36+ public void addAccount (SavingsAccount account ) {
37+ savingsAccountByNumber .put (account .getAccountNumber (), account );
38+ account
39+ .getOwners ()
40+ .forEach (
41+ owner -> {
42+ customerById .put (owner .getId (), owner );
43+ });
44+ }
45+
3046 /**
3147 * Finds all accounts owned by a customer.
3248 *
Original file line number Diff line number Diff line change 1+ package com .codedifferently .lesson17 .bank ;
2+
3+ import java .util .Set ;
4+
5+ /** Represents a business checking account. */
6+ public class BusinessCheckingAccount extends CheckingAccount {
7+ public BusinessCheckingAccount (
8+ String accountNumber , Set <Customer > owners , double initialBalance ) {
9+ super (accountNumber , owners , initialBalance );
10+ }
11+ /**
12+ * Gets the account number.
13+ *
14+ * @return The account number.
15+ */
16+ }
Original file line number Diff line number Diff line change 11package com .codedifferently .lesson17 .bank ;
22
3- import com .codedifferently .lesson17 .bank .exceptions .InsufficientFundsException ;
43import java .util .Set ;
54
5+ import com .codedifferently .lesson17 .bank .exceptions .InsufficientFundsException ;
6+
67/** Represents a checking account. */
7- public class CheckingAccount {
8+ public class CheckingAccount extends BankAccount {
89
910 private final Set <Customer > owners ;
1011 private final String accountNumber ;
@@ -19,6 +20,7 @@ public class CheckingAccount {
1920 * @param initialBalance The initial balance of the account.
2021 */
2122 public CheckingAccount (String accountNumber , Set <Customer > owners , double initialBalance ) {
23+ super (accountNumber , owners , initialBalance );
2224 this .accountNumber = accountNumber ;
2325 this .owners = owners ;
2426 this .balance = initialBalance ;
Original file line number Diff line number Diff line change 1+ package com .codedifferently .lesson17 .bank ;
2+
3+ import java .util .Set ;
4+
5+ /** Represents a savings account. */
6+ public class SavingsAccount extends CheckingAccount {
7+
8+ public SavingsAccount (String accountNumber , Set <Customer > owners , double initialBalance ) {
9+ super (accountNumber , owners , initialBalance );
10+ }
11+
12+ /**
13+ * Gets the account number.
14+ *
15+ * @return The account number.
16+ */
17+ public void depositFunds (Check check ) {
18+ throw new UnsupportedOperationException ("Cannot deposit a check into a savings account" );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .codedifferently .lesson17 .bank ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+
6+ import java .util .Set ;
7+ import org .junit .jupiter .api .Test ;
8+
9+ public class SavingsAccountTest {
10+ @ Test
11+ public void testDepositFunds () {
12+ // Create a customer
13+ Customer customer = new Customer (null , "John Doe" );
14+
15+ // Create a savings account
16+ SavingsAccount savingsAccount = new SavingsAccount ("123456789" , Set .of (customer ), 1000.0 );
17+
18+ // Deposit funds into the account
19+ savingsAccount .deposit (500.0 );
20+
21+ // Assert that the balance is correct
22+ assertEquals (1500.0 , savingsAccount .getBalance ());
23+ }
24+
25+ @ Test
26+ public void testDepositCheck () {
27+ // Create a customer
28+ Customer customer = new Customer (null , "John Doe" );
29+
30+ // Create a savings account
31+ SavingsAccount savingsAccount = new SavingsAccount ("123456789" , Set .of (customer ), 1000.0 );
32+
33+ // Attempt to deposit a check into the account
34+ assertThrows (
35+ UnsupportedOperationException .class ,
36+ () -> {
37+ savingsAccount .depositFunds (new Check (null , 100.0 , savingsAccount ));
38+ });
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments