diff --git a/src/main/java/dev/banking/DemoApplication.java b/src/main/java/dev/banking/DemoApplication.java new file mode 100644 index 0000000..0d03147 --- /dev/null +++ b/src/main/java/dev/banking/DemoApplication.java @@ -0,0 +1,108 @@ +package dev.banking; + +import dev.banking.model.*; +import dev.banking.monitor.LiveMonitor; +import dev.banking.processor.TransactionProcessor; +import dev.banking.service.BankingSystem; + +import java.util.*; +import java.util.concurrent.*; + +/** + * DemoApplication is responsible for: + * - Building the system + * - Running the simulation + * - Managing lifecycle (threads, schedulers) + * + * This class replaces DemoFactory as a full runner. + */ +public final class DemoApplication { + + private DemoApplication() { + } + + public static void run() { + + System.out.println("Initializing Banking Simulation...\n"); + + /* + * 1. Create bank accounts + */ + BankAccount acc1 = new BankAccount(1, 1000); + BankAccount acc2 = new BankAccount(2, 2000); + BankAccount acc3 = new BankAccount(3, 1500); + + Map accounts = new HashMap<>(); + accounts.put(1, acc1); + accounts.put(2, acc2); + accounts.put(3, acc3); + + /* + * 2. Create transactions + */ + List transactions = List.of( + new DepositTransaction(1, 200), + new WithdrawTransaction(2, 300), + new TransferTransaction(1, 2, 150), + new TransferTransaction(2, 3, 400), + new DepositTransaction(3, 500), + new WithdrawTransaction(1, 100), + new TransferTransaction(3, 1, 250) + ); + + /* + * 3. Thread pool (workers) + */ + ExecutorService executor = Executors.newFixedThreadPool(4); + + /* + * 4. Processor + */ + TransactionProcessor processor = new TransactionProcessor(accounts); + + /* + * 5. Banking system + */ + BankingSystem bankingSystem = new BankingSystem(executor, processor); + + /* + * 6. Live monitor (UI simulation) + */ + LiveMonitor monitor = new LiveMonitor(); + + ScheduledExecutorService monitorExecutor = Executors.newSingleThreadScheduledExecutor(); + + monitorExecutor.scheduleAtFixedRate(() -> { + monitor.update(accounts.values()); + System.out.println("----------------------"); + }, 0, 1, TimeUnit.SECONDS); + + /* + * 7. Run simulation + */ + bankingSystem.processTransactions(transactions); + + /* + * 8. Shutdown / lifecycle management + */ + shutdown(executor, monitorExecutor); + + System.out.println("\nSimulation completed."); + } + + private static void shutdown( + ExecutorService executor, + ScheduledExecutorService monitorExecutor + ) { + try { + executor.shutdown(); + executor.awaitTermination(5, TimeUnit.SECONDS); + + monitorExecutor.shutdown(); + monitorExecutor.awaitTermination(5, TimeUnit.SECONDS); + + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/DemoFactory.java b/src/main/java/dev/banking/DemoFactory.java deleted file mode 100644 index 3bb7f92..0000000 --- a/src/main/java/dev/banking/DemoFactory.java +++ /dev/null @@ -1,55 +0,0 @@ -package dev.banking; - -import dev.banking.service.BankingSystem; - -/** - * Creates a ready-to-run demonstration environment. - * - * Students do not need to modify this class. - */ -public final class DemoFactory { - - private DemoFactory() { - } - - /** - * Builds a fully configured banking simulation system. - * - * This includes: - * - Bank accounts - * - Transaction stream generator / loader - * - ExecutorService (thread pool) - * - Transaction processor - * - Live monitoring system - * - * @return configured BankingSystem instance - */ - public static BankingSystem createDemoSystem() { - - /* - * TODO (Instructor Implementation Only) - * - * Example responsibilities: - * - * 1. Create bank accounts: - * - Account(1, 1000) - * - Account(2, 2000) - * - Account(3, 1500) - * - * 2. Create transaction list or stream - * - * 3. Initialize ExecutorService: - * Executors.newFixedThreadPool(n) - * - * 4. Create TransactionProcessor - * - * 5. Create BankingSystem and inject dependencies - * - * 6. Attach LiveMonitor (optional) - */ - - throw new UnsupportedOperationException( - "Instructor implementation required." - ); - } -} \ No newline at end of file diff --git a/src/main/java/dev/banking/Main.java b/src/main/java/dev/banking/Main.java index 136f962..40145b9 100644 --- a/src/main/java/dev/banking/Main.java +++ b/src/main/java/dev/banking/Main.java @@ -1,17 +1,11 @@ package dev.banking; -import dev.banking.service.BankingSystem; -import dev.banking.DemoFactory; - /** * Entry point of the application. * - * This class is provided for demonstration purposes only. - * * IMPORTANT: - * - Do NOT modify this file. - * - This file is NOT used during grading. - * - All grading is performed through the provided JUnit tests. + * - Do NOT modify this file in student assignment. + * - This is only a wrapper for running the demo. */ public final class Main { @@ -22,25 +16,19 @@ public final class Main { System.out.println("================================="); System.out.println(" Advanced Banking System"); - System.out.println("================================="); + System.out.println("=================================\n"); try { - BankingSystem bankingSystem = - DemoFactory.createDemoSystem(); - - System.out.println("System initialized..."); - System.out.println("Running simulation with concurrent workers...\n"); - -// bankingSystem.start(); + DemoApplication.run(); System.out.println("\n================================="); - System.out.println("Simulation completed successfully."); + System.out.println(" System finished successfully "); System.out.println("================================="); } catch (Exception e) { - System.err.println("\nUnexpected error occurred:"); + System.err.println("Unexpected error occurred:"); e.printStackTrace(); } } diff --git a/src/main/java/dev/banking/model/BankAccount.java b/src/main/java/dev/banking/model/BankAccount.java index caf0a6b..115a4e0 100644 --- a/src/main/java/dev/banking/model/BankAccount.java +++ b/src/main/java/dev/banking/model/BankAccount.java @@ -31,7 +31,7 @@ public class BankAccount { * TODO * Implement a thread-safe deposit operation. */ - public void deposit(int amount) { + public void deposit(long amount) { throw new UnsupportedOperationException(); } @@ -39,7 +39,7 @@ public class BankAccount { * TODO * Implement a thread-safe withdrawal operation. */ - public void withdraw(int amount) { + public void withdraw(long amount) { throw new UnsupportedOperationException(); } @@ -47,7 +47,7 @@ public class BankAccount { * TODO * Implement an atomic and deadlock-free transfer. */ - public void transfer(BankAccount target, int amount) { + public void transfer(BankAccount target, long amount) { throw new UnsupportedOperationException(); } } \ No newline at end of file diff --git a/src/main/java/dev/banking/model/TransactionType.java b/src/main/java/dev/banking/model/TransactionType.java deleted file mode 100644 index 7653758..0000000 --- a/src/main/java/dev/banking/model/TransactionType.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.banking.model; - -public enum TransactionType { - DEPOSIT, - WITHDRAW, - TRANSFER -} \ No newline at end of file