Skip to content

Commit 5c07b2d

Browse files
feat: adds 2 new features for lesson_17 bankATM
1 parent 1c93014 commit 5c07b2d

File tree

10 files changed

+894
-10
lines changed

10 files changed

+894
-10
lines changed

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

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

3-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
43
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.UUID;
87

9-
/** Represents a bank ATM. */
8+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9+
10+
/**
11+
* Represents a bank ATM system that manages customer accounts and transactions.
12+
*
13+
* <p>This ATM supports various types of checking accounts including standard
14+
* {@link CheckingAccount} and specialized {@link BusinessCheckingAccount} instances.
15+
* The system uses polymorphism to treat all account types uniformly, enabling
16+
* seamless operations regardless of the specific account implementation.
17+
*
18+
* <p>Supported operations include:
19+
* <ul>
20+
* <li>Adding accounts to the system</li>
21+
* <li>Finding accounts by customer ID</li>
22+
* <li>Depositing funds (cash or check) with multi-currency support</li>
23+
* <li>Withdrawing funds</li>
24+
* </ul>
25+
*
26+
* <p>The ATM supports deposits in multiple currencies including USD, EUR, GBP, JPY,
27+
* CAD, AUD, CHF, and CNY. All currency conversions are handled automatically using
28+
* the integrated {@link CurrencyConverter}, with all account balances maintained in USD.
29+
*
30+
* <p>The ATM maintains internal mappings of customers and accounts for efficient
31+
* lookup operations and enforces business rules such as account existence validation.
32+
*
33+
* @see CheckingAccount
34+
* @see BusinessCheckingAccount
35+
* @see Customer
36+
* @see BusinessCustomer
37+
* @see Currency
38+
* @see CurrencyConverter
39+
* @since 1.0
40+
*/
1041
public class BankAtm {
1142

1243
private final Map<UUID, Customer> customerById = new HashMap<>();
1344
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
45+
private final CurrencyConverter currencyConverter;
46+
47+
/**
48+
* Creates a new BankAtm with default currency conversion support.
49+
*
50+
* <p>Initializes the ATM system with an empty set of accounts and customers,
51+
* and sets up currency conversion capabilities for multi-currency deposits.
52+
*/
53+
public BankAtm() {
54+
this.currencyConverter = new CurrencyConverter();
55+
}
1456

1557
/**
16-
* Adds a checking account to the bank.
58+
* Adds a checking account to the ATM system.
59+
*
60+
* <p>This method accepts any type of checking account, including standard
61+
* {@link CheckingAccount} and {@link BusinessCheckingAccount} instances.
62+
* The account and all its owners are registered in the system for lookup operations.
63+
*
64+
* <p>After adding an account, customers can perform transactions using the
65+
* account number, and the account can be found using any owner's customer ID.
1766
*
18-
* @param account The account to add.
67+
* @param account The checking account to add (including business accounts)
68+
* @throws NullPointerException if the account is null
69+
*
70+
* @see BusinessCheckingAccount
71+
* @see #findAccountsByCustomerId(UUID)
1972
*/
2073
public void addAccount(CheckingAccount account) {
2174
accountByNumber.put(account.getAccountNumber(), account);
@@ -40,14 +93,31 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4093
}
4194

4295
/**
43-
* Deposits funds into an account.
96+
* Deposits funds into an account with automatic currency conversion to USD.
97+
*
98+
* <p>This method accepts deposits in various currencies and automatically
99+
* converts the amount to USD using current exchange rates before depositing
100+
* into the account. All account balances are maintained in USD.
101+
*
102+
* <p>Supported currencies include USD, EUR, GBP, JPY, CAD, AUD, CHF, and CNY.
103+
* Exchange rates are managed by the internal {@link CurrencyConverter}.
104+
* For USD deposits, the amount is deposited directly without conversion overhead.
44105
*
45-
* @param accountNumber The account number.
46-
* @param amount The amount to deposit.
106+
* @param accountNumber The account number to deposit funds into
107+
* @param amount The amount to deposit in the specified currency
108+
* @param currency The currency of the deposit amount
109+
* @throws AccountNotFoundException if the account is not found or is closed
110+
* @throws IllegalArgumentException if the amount is negative
111+
* @throws IllegalArgumentException if the currency is null
112+
* @throws UnsupportedOperationException if the currency is not supported
113+
*
114+
* @see Currency
115+
* @see CurrencyConverter
47116
*/
48-
public void depositFunds(String accountNumber, double amount) {
117+
public void depositFunds(String accountNumber, double amount, Currency currency) {
49118
CheckingAccount account = getAccountOrThrow(accountNumber);
50-
account.deposit(amount);
119+
double usdAmount = currencyConverter.convertToUsd(amount, currency);
120+
account.deposit(usdAmount);
51121
}
52122

53123
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* Represents a business checking account that extends the standard checking account functionality.
7+
*
8+
* <p>A business checking account enforces that at least one owner must be a business customer
9+
* ({@link BusinessCustomer}). This validation occurs during account creation and will throw
10+
* an {@link IllegalArgumentException} if no business owners are present.
11+
*
12+
* <p>All standard checking account operations are inherited from {@link CheckingAccount},
13+
* including deposits, withdrawals, balance inquiries, and account closure functionality.
14+
*
15+
* <p>Business accounts can be seamlessly integrated with the {@link BankAtm} system
16+
* through polymorphism, requiring no changes to existing ATM operations.
17+
*
18+
* @see CheckingAccount
19+
* @see BusinessCustomer
20+
* @see BankAtm
21+
* @since 1.0
22+
*/
23+
public class BusinessCheckingAccount extends CheckingAccount {
24+
25+
/**
26+
* Creates a new business checking account with validation for business ownership.
27+
*
28+
* <p>This constructor validates that at least one owner in the provided set is a
29+
* {@link BusinessCustomer}. If no business customers are found among the owners,
30+
* an {@link IllegalArgumentException} is thrown.
31+
*
32+
* <p>The account inherits all functionality from {@link CheckingAccount} including
33+
* deposit/withdrawal operations, balance management, and account closure capabilities.
34+
*
35+
* @param accountNumber The unique account number for this business account
36+
* @param owners The set of account owners, must contain at least one {@link BusinessCustomer}
37+
* @param initialBalance The initial balance of the account (must be non-negative)
38+
* @throws IllegalArgumentException if the owners set is empty or contains no business customers
39+
* @throws IllegalArgumentException if the account number is null or empty
40+
* @throws IllegalArgumentException if the initial balance is negative
41+
*
42+
* @see BusinessCustomer
43+
* @see CheckingAccount#CheckingAccount(String, Set, double)
44+
*/
45+
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
46+
super(accountNumber, owners, initialBalance);
47+
48+
// Validate that at least one owner is a business customer
49+
boolean hasBusinessOwner = owners.stream()
50+
.anyMatch(owner -> owner instanceof BusinessCustomer);
51+
52+
if (!hasBusinessOwner) {
53+
throw new IllegalArgumentException("Business checking account requires at least one business owner");
54+
}
55+
}
56+
57+
/**
58+
* Returns a string representation of this business checking account.
59+
*
60+
* <p>The string includes the account number, current balance, and active status,
61+
* formatted specifically for business accounts to distinguish them from regular
62+
* checking accounts in logging and debugging scenarios.
63+
*
64+
* @return A formatted string containing account details
65+
*/
66+
@Override
67+
public String toString() {
68+
return "BusinessCheckingAccount{"
69+
+ "accountNumber='"
70+
+ getAccountNumber()
71+
+ '\''
72+
+ ", balance="
73+
+ getBalance()
74+
+ ", isActive="
75+
+ !isClosed()
76+
+ '}';
77+
}
78+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.UUID;
4+
5+
/** Represents a business customer of the bank. */
6+
public class BusinessCustomer extends Customer {
7+
8+
private final String businessName;
9+
private final String taxId;
10+
11+
/**
12+
* Creates a new business customer.
13+
*
14+
* @param id The ID of the business customer.
15+
* @param businessName The name of the business.
16+
* @param taxId The tax identification number of the business.
17+
*/
18+
public BusinessCustomer(UUID id, String businessName, String taxId) {
19+
super(id, businessName);
20+
this.businessName = businessName;
21+
this.taxId = taxId;
22+
}
23+
24+
/**
25+
* Gets the business name.
26+
*
27+
* @return The business name.
28+
*/
29+
public String getBusinessName() {
30+
return businessName;
31+
}
32+
33+
/**
34+
* Gets the tax identification number.
35+
*
36+
* @return The tax identification number.
37+
*/
38+
public String getTaxId() {
39+
return taxId;
40+
}
41+
42+
/**
43+
* Determines if this customer is a business.
44+
*
45+
* @return True, since this is a business customer.
46+
*/
47+
public boolean isBusiness() {
48+
return true;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "BusinessCustomer{"
54+
+ "id=" + getId()
55+
+ ", businessName='" + businessName + '\''
56+
+ ", taxId='" + taxId + '\''
57+
+ '}';
58+
}
59+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/**
4+
* Enumeration of supported currencies for banking transactions.
5+
*
6+
* <p>This enum defines the various currencies that can be used for deposits
7+
* and other banking operations. All currency amounts are converted to USD
8+
* for internal account balance management.
9+
*
10+
* @see CurrencyConverter
11+
* @since 1.0
12+
*/
13+
public enum Currency {
14+
/** United States Dollar (base currency) */
15+
USD,
16+
17+
/** Euro */
18+
EUR,
19+
20+
/** British Pound Sterling */
21+
GBP,
22+
23+
/** Japanese Yen */
24+
JPY,
25+
26+
/** Canadian Dollar */
27+
CAD,
28+
29+
/** Australian Dollar */
30+
AUD,
31+
32+
/** Swiss Franc */
33+
CHF,
34+
35+
/** Chinese Yuan */
36+
CNY
37+
}

0 commit comments

Comments
 (0)