Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions backend/FinancialAccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ class FinancialAccountManager {
DB_USER = config.getDatabase().getDbUser();
DB_PASSWORD = config.getDatabase().getDbPassword();
API_KEY = config.getApi().getPaymentGatewayKey();
logger.info("user Info" + DB_USER + ", " + API_KEY);
logger.info("user Info" + DB_USER);

} catch (IOException e) {
e.printStackTrace();
logger.warning("Error loading configuration", e);
}
}

public static void main(String[] args) {
logger.info("Starting banking operations");
System.out.println("Connecting to DB with user: " + DB_USER);
performBankingOperations();
}

public static void performBankingOperations() {
Map<String, Double> accountBalances = fetchBalancesFromDB();

logger.info("Fetching account balances");
System.out.println("Account Balances:");
accountBalances.forEach((name, balance) ->
System.out.printf("%s: $%.2f%n", name, balance)
);
// I don't think we should print all the balances, comment out this part for now in case there's other purpose.
// Map<String, Double> accountBalances = fetchBalancesFromDB();
//
// logger.info("Fetching account balances");
// System.out.println("Account Balances:");
// accountBalances.forEach((name, balance) ->
// System.out.printf("%s: $%.2f%n", name, balance)
// );

processTransaction("Alice", "Bob", 200.25);
}
Expand All @@ -51,27 +51,34 @@ private static Map<String, Double> fetchBalancesFromDB() {
return new HashMap<>();
}

private static void updateBalancesInDB(Map<String, Double> balances) {
// Simulate database update
// If balance becomes negative, throw exception and roll back transaction
}

public static void processTransaction(String from, String to, double amount) {
try {
Map<String, Double> balances = fetchBalancesFromDB();
Map<String, Double> newBalances = new HashMap<>();

if (!balances.containsKey(from) || !balances.containsKey(to)) {
logger.warning("Invalid transaction: Account not found for " + from + " or " + to);
logger.warning("Invalid transaction: Account not found", Pair.of("from", from), Pair.of("to", to));
throw new Exception("Invalid transaction: Account not found for " + from + " or " + to);
}

if (balances.get(from) < amount) {
logger.warning("Transaction failed: Insufficient funds in account: " + from);
logger.warning("Transaction failed: Insufficient funds", Pair.of("from", from));
throw new Exception("Insufficient funds in account: " + from);
}

balances.put(from, balances.get(from) - amount);
balances.put(to, balances.get(to) + amount);
newBalances.put(from, 0 - amount);
newBalances.put(to, amount);

updateBalancesInDB(newBalances);

logger.info("Transaction successful: " + from + " sent $" + amount + " to " + to);
System.out.printf("Transaction successful: %s sent $%.2f to %s%n", from, amount, to);
} catch (Exception e) {
e.printStackTrace();
logger.warning("Transaction failed", e, Pair.of("from", from), Pair.of("to", to));
}
}
}
4 changes: 2 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ database:
dbPort: "{{secretsmanager:/prod/db/port}}"

api:
paymentGatewayKey: "Fg7H2yLdM8VpQz3N6RbXtPWaK4CsJ9T5"
smsProviderKey: "X1Y2Z3A4B5C6D7E8F9G0H1I2J3K4L5M6"
paymentGatewayKey: "{{secretsmanager:/prod/api/paymentGatewayKey}}"
smsProviderKey: "{{secretsmanager:/prod/api/smsProviderKey}}"
internalServiceToken: "{{secretsmanager:/prod/api/internalService}}"
analyticsIntegrationKey: "{{ssm:/prod/api/analytics}}"

Expand Down