From c41a6f5f8a7c700df9fa5bb1031ee2f9ec9f9cf6 Mon Sep 17 00:00:00 2001 From: Yitian Xue Date: Wed, 26 Feb 2025 16:42:49 -0500 Subject: [PATCH] fix --- backend/FinancialAccountManager.java | 39 ++++++++++++++++------------ config/config.yaml | 4 +-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/backend/FinancialAccountManager.java b/backend/FinancialAccountManager.java index e40448c..53e2da5 100644 --- a/backend/FinancialAccountManager.java +++ b/backend/FinancialAccountManager.java @@ -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 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 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); } @@ -51,27 +51,34 @@ private static Map fetchBalancesFromDB() { return new HashMap<>(); } + private static void updateBalancesInDB(Map 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 balances = fetchBalancesFromDB(); + Map 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)); } } } diff --git a/config/config.yaml b/config/config.yaml index 6a0d67e..27bfb49 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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}}"