Skip to content

Commit 4db6846

Browse files
committed
Should be don
1 parent d0c65b6 commit 4db6846

File tree

14 files changed

+1234
-10
lines changed

14 files changed

+1234
-10
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Set;
77
import java.util.UUID;
88

9-
/**
9+
/**
1010
* Represents a bank ATM with support for multiple account types and comprehensive audit logging.
1111
* Enhanced to follow SOLID principles.
1212
*/
@@ -55,7 +55,7 @@ private void addAccountInternal(Account account) {
5555
* @param customerId The ID of the customer.
5656
* @return The unique set of accounts owned by the customer.
5757
*/
58-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
58+
public Set<Account> findAccountsByCustomerId(UUID customerId) {
5959
return customerById.containsKey(customerId)
6060
? customerById.get(customerId).getAccounts()
6161
: Set.of();
@@ -94,7 +94,8 @@ public void depositFunds(String accountNumber, Check check) {
9494
auditLog.recordCredit(
9595
accountNumber,
9696
check.getAmount(),
97-
String.format("Check deposit from account %s", check.getSourceAccount().getAccountNumber()));
97+
String.format(
98+
"Check deposit from account %s", check.getSourceAccount().getAccountNumber()));
9899
}
99100

100101
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* Represents a business checking account that requires at least one business owner. This account
7+
* type extends CheckingAccount but adds business-specific validation.
8+
*/
9+
public class BusinessCheckingAccount extends CheckingAccount {
10+
11+
/**
12+
* Creates a new business checking account.
13+
*
14+
* @param accountNumber The account number.
15+
* @param owners The owners of the account.
16+
* @param initialBalance The initial balance of the account.
17+
* @throws IllegalArgumentException if no business owners are found.
18+
*/
19+
public BusinessCheckingAccount(
20+
String accountNumber, Set<Customer> owners, double initialBalance) {
21+
super(accountNumber, owners, initialBalance);
22+
validateBusinessOwnership(owners);
23+
}
24+
25+
/**
26+
* Validates that at least one owner is a business customer.
27+
*
28+
* @param owners The set of owners to validate.
29+
* @throws IllegalArgumentException if no business customers are found among the owners.
30+
*/
31+
private void validateBusinessOwnership(Set<Customer> owners) {
32+
boolean hasBusiness = owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer);
33+
if (!hasBusiness) {
34+
throw new IllegalArgumentException(
35+
"Business checking account requires at least one business customer as owner");
36+
}
37+
}
38+
39+
/**
40+
* Checks if this account has business ownership.
41+
*
42+
* @return True if at least one owner is a business customer.
43+
*/
44+
public boolean hasBusinessOwnership() {
45+
return getOwners().stream().anyMatch(owner -> owner instanceof BusinessCustomer);
46+
}
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.UUID;
4+
5+
/**
6+
* Represents a business customer of the bank. Business customers have additional properties and can
7+
* be owners of business checking accounts.
8+
*/
9+
public class BusinessCustomer extends Customer {
10+
11+
private final String businessLicense;
12+
private final String taxId;
13+
14+
/**
15+
* Creates a new business customer.
16+
*
17+
* @param id The ID of the customer.
18+
* @param name The name of the business.
19+
* @param businessLicense The business license number.
20+
* @param taxId The tax identification number.
21+
*/
22+
public BusinessCustomer(UUID id, String name, String businessLicense, String taxId) {
23+
super(id, name);
24+
this.businessLicense = businessLicense;
25+
this.taxId = taxId;
26+
}
27+
28+
/**
29+
* Gets the business license number.
30+
*
31+
* @return The business license number.
32+
*/
33+
public String getBusinessLicense() {
34+
return businessLicense;
35+
}
36+
37+
/**
38+
* Gets the tax identification number.
39+
*
40+
* @return The tax identification number.
41+
*/
42+
public String getTaxId() {
43+
return taxId;
44+
}
45+
46+
/**
47+
* Checks if this customer is a business.
48+
*
49+
* @return Always true for BusinessCustomer.
50+
*/
51+
public boolean isBusiness() {
52+
return true;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "BusinessCustomer{"
58+
+ "id="
59+
+ getId()
60+
+ ", name='"
61+
+ getName()
62+
+ '\''
63+
+ ", businessLicense='"
64+
+ businessLicense
65+
+ '\''
66+
+ ", taxId='"
67+
+ taxId
68+
+ '\''
69+
+ '}';
70+
}
71+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/**
4+
* Currency converter utility that handles conversion between different currencies and USD.
5+
* Encapsulates currency conversion logic following the Single Responsibility Principle.
6+
*/
7+
public class CurrencyConverter {
8+
9+
/**
10+
* Converts an amount from the specified currency to USD.
11+
*
12+
* @param amount The amount to convert.
13+
* @param fromCurrency The source currency type.
14+
* @return The amount converted to USD.
15+
* @throws IllegalArgumentException if amount is negative.
16+
*/
17+
public double convertToUSD(double amount, CurrencyType fromCurrency) {
18+
if (amount < 0) {
19+
throw new IllegalArgumentException("Amount cannot be negative");
20+
}
21+
22+
if (fromCurrency == CurrencyType.USD) {
23+
return amount;
24+
}
25+
26+
return amount * fromCurrency.getExchangeRateToUSD();
27+
}
28+
29+
/**
30+
* Converts an amount from USD to the specified currency.
31+
*
32+
* @param amountUSD The USD amount to convert.
33+
* @param toCurrency The target currency type.
34+
* @return The amount converted to the target currency.
35+
* @throws IllegalArgumentException if amount is negative.
36+
*/
37+
public double convertFromUSD(double amountUSD, CurrencyType toCurrency) {
38+
if (amountUSD < 0) {
39+
throw new IllegalArgumentException("Amount cannot be negative");
40+
}
41+
42+
if (toCurrency == CurrencyType.USD) {
43+
return amountUSD;
44+
}
45+
46+
return amountUSD / toCurrency.getExchangeRateToUSD();
47+
}
48+
49+
/**
50+
* Converts an amount between two currencies.
51+
*
52+
* @param amount The amount to convert.
53+
* @param fromCurrency The source currency.
54+
* @param toCurrency The target currency.
55+
* @return The converted amount.
56+
*/
57+
public double convert(double amount, CurrencyType fromCurrency, CurrencyType toCurrency) {
58+
if (fromCurrency == toCurrency) {
59+
return amount;
60+
}
61+
62+
double usdAmount = convertToUSD(amount, fromCurrency);
63+
return convertFromUSD(usdAmount, toCurrency);
64+
}
65+
66+
/**
67+
* Formats a currency amount with the appropriate currency symbol.
68+
*
69+
* @param amount The amount to format.
70+
* @param currency The currency type.
71+
* @return The formatted currency string.
72+
*/
73+
public String formatAmount(double amount, CurrencyType currency) {
74+
return String.format("%.2f %s", amount, currency.name());
75+
}
76+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/** Enumeration of supported currency types for banking operations. */
4+
public enum CurrencyType {
5+
USD("United States Dollar", 1.0),
6+
EUR("Euro", 1.1),
7+
GBP("British Pound", 1.25),
8+
CAD("Canadian Dollar", 0.75),
9+
JPY("Japanese Yen", 0.007),
10+
CNY("Chinese Yuan", 0.14),
11+
AUD("Australian Dollar", 0.67),
12+
CHF("Swiss Franc", 1.05),
13+
SEK("Swedish Krona", 0.095),
14+
NOK("Norwegian Krone", 0.092);
15+
16+
private final String fullName;
17+
private final double exchangeRateToUSD;
18+
19+
/**
20+
* Creates a currency type with its exchange rate to USD.
21+
*
22+
* @param fullName The full name of the currency.
23+
* @param exchangeRateToUSD The exchange rate to convert to USD.
24+
*/
25+
CurrencyType(String fullName, double exchangeRateToUSD) {
26+
this.fullName = fullName;
27+
this.exchangeRateToUSD = exchangeRateToUSD;
28+
}
29+
30+
/**
31+
* Gets the full name of the currency.
32+
*
33+
* @return The full name.
34+
*/
35+
public String getFullName() {
36+
return fullName;
37+
}
38+
39+
/**
40+
* Gets the exchange rate to USD.
41+
*
42+
* @return The exchange rate.
43+
*/
44+
public double getExchangeRateToUSD() {
45+
return exchangeRateToUSD;
46+
}
47+
}

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

Lines changed: 4 additions & 4 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<Account> accounts = new HashSet<>();
1313

1414
/**
1515
* Creates a new customer.
@@ -41,11 +41,11 @@ public String getName() {
4141
}
4242

4343
/**
44-
* Adds a checking account to the customer.
44+
* Adds an account to the customer.
4545
*
4646
* @param account The account to add.
4747
*/
48-
public void addAccount(CheckingAccount account) {
48+
public void addAccount(Account 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<Account> getAccounts() {
5858
return accounts;
5959
}
6060

0 commit comments

Comments
 (0)