Skip to content

Commit 0fc4226

Browse files
feat: added currency converter, AuditLog unfinished
1 parent 532fccd commit 0fc4226

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
lines changed

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

Whitespace-only changes.

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

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

33
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
4+
import com.codedifferently.lesson17.bank.exceptions.UnsupportedCurrencyException;
5+
46
import java.util.HashMap;
57
import java.util.Map;
68
import java.util.Set;
@@ -44,10 +46,12 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4446
*
4547
* @param accountNumber The account number.
4648
* @param amount The amount to deposit.
49+
* @throws UnsupportedCurrencyException
4750
*/
48-
public void depositFunds(String accountNumber, double amount) {
51+
public void depositFunds(String accountNumber, double amount, String currencyType) throws UnsupportedCurrencyException {
4952
CheckingAccount account = getAccountOrThrow(accountNumber);
50-
account.deposit(amount);
53+
double convertedAmount = CurrencyConverter.convert(amount, currencyType, "USD");
54+
account.deposit(convertedAmount);
5155
}
5256

5357
/**

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, doubl
1717
isActive = true;
1818
}
1919

20-
21-
2220
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Map;
4+
5+
import com.codedifferently.lesson17.bank.exceptions.UnsupportedCurrencyException;
6+
7+
public class CurrencyConverter {
8+
private static final Map<String, Double> exchangeRates = Map.of(
9+
"USD", 1.0,
10+
"EUR", 0.9,
11+
"GBP", 0.8,
12+
"JPY", 110.0
13+
);
14+
15+
public static double convert(double amount, String fromCurrency, String toCurrency) throws UnsupportedCurrencyException {
16+
if (!exchangeRates.containsKey(fromCurrency) || !exchangeRates.containsKey(toCurrency)) {
17+
throw new UnsupportedCurrencyException("Unsupported currency type.");
18+
}
19+
double amountInUSD = amount / exchangeRates.get(fromCurrency);
20+
return amountInUSD * exchangeRates.get(toCurrency);
21+
}
22+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Customer {
1010
private final UUID id;
1111
private final String name;
1212
private final Set<CheckingAccount> accounts = new HashSet<>();
13-
private boolean isBusiness;
13+
private boolean isBusiness = false;
1414

1515
/**
1616
* Creates a new customer.
@@ -24,6 +24,11 @@ public Customer(UUID id, String name, boolean isBusiness) {
2424
this.isBusiness = isBusiness;
2525
}
2626

27+
public Customer(UUID id, String name) {
28+
this.id = id;
29+
this.name = name;
30+
}
31+
2732
/**
2833
* Gets the ID of the customer.
2934
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public MoneyOrder(double amount, CheckingAccount account) {
1919
}
2020
this.amount = amount;
2121
this.account = account;
22+
23+
account.withdraw(amount);
2224
}
2325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codedifferently.lesson17.bank.exceptions;
2+
3+
public class UnsupportedCurrencyException extends Exception {
4+
public UnsupportedCurrencyException() {}
5+
6+
public UnsupportedCurrencyException(String message) {
7+
super(message);
8+
}
9+
}

0 commit comments

Comments
 (0)