diff --git a/src/main/java/dev/banking/DemoFactory.java b/src/main/java/dev/banking/DemoFactory.java new file mode 100644 index 0000000..3bb7f92 --- /dev/null +++ b/src/main/java/dev/banking/DemoFactory.java @@ -0,0 +1,55 @@ +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/model/BankAccount.java b/src/main/java/dev/banking/model/BankAccount.java new file mode 100644 index 0000000..fa83105 --- /dev/null +++ b/src/main/java/dev/banking/model/BankAccount.java @@ -0,0 +1,48 @@ +package dev.banking.model; + +public class BankAccount { + + private final int accountId; + private long balance; + + public BankAccount(int accountId, long initialBalance) { + this.accountId = accountId; + this.balance = initialBalance; + } + + public int getAccountId() { + return accountId; + } + + /* + * TODO + * Implement a thread-safe balance reader. + */ + public long getBalance() { + throw new UnsupportedOperationException(); + } + + /* + * TODO + * Implement a thread-safe deposit operation. + */ + public void deposit(int amount) { + throw new UnsupportedOperationException(); + } + + /* + * TODO + * Implement a thread-safe withdrawal operation. + */ + public void withdraw(int amount) { + throw new UnsupportedOperationException(); + } + + /* + * TODO + * Implement an atomic and deadlock-free transfer. + */ + public void transfer(BankAccount target, int amount) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/model/Transaction.java b/src/main/java/dev/banking/model/Transaction.java new file mode 100644 index 0000000..8ea1c37 --- /dev/null +++ b/src/main/java/dev/banking/model/Transaction.java @@ -0,0 +1,39 @@ +package dev.banking.model; + +public class Transaction { + + private final TransactionType type; + + private final int sourceAccountId; + private final int targetAccountId; + + private final int amount; + + public Transaction( + TransactionType type, + int sourceAccountId, + int targetAccountId, + int amount + ) { + this.type = type; + this.sourceAccountId = sourceAccountId; + this.targetAccountId = targetAccountId; + this.amount = amount; + } + + public TransactionType getType() { + return type; + } + + public int getSourceAccountId() { + return sourceAccountId; + } + + public int getTargetAccountId() { + return targetAccountId; + } + + public int getAmount() { + return amount; + } +} \ 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 new file mode 100644 index 0000000..7653758 --- /dev/null +++ b/src/main/java/dev/banking/model/TransactionType.java @@ -0,0 +1,7 @@ +package dev.banking.model; + +public enum TransactionType { + DEPOSIT, + WITHDRAW, + TRANSFER +} \ No newline at end of file diff --git a/src/main/java/dev/banking/monitor/LiveMonitor.java b/src/main/java/dev/banking/monitor/LiveMonitor.java new file mode 100644 index 0000000..a625199 --- /dev/null +++ b/src/main/java/dev/banking/monitor/LiveMonitor.java @@ -0,0 +1,23 @@ +package dev.banking.monitor; + +import dev.banking.model.BankAccount; + +import java.util.Collection; + +public class LiveMonitor { + + public void update( + Collection accounts + ) { + + for (BankAccount account : accounts) { + + System.out.printf( + "Account %d -> %d%n", + account.getAccountId(), + account.getBalance() + ); + + } + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/processor/TransactionProcessor.java b/src/main/java/dev/banking/processor/TransactionProcessor.java new file mode 100644 index 0000000..97ee0bb --- /dev/null +++ b/src/main/java/dev/banking/processor/TransactionProcessor.java @@ -0,0 +1,44 @@ +package dev.banking.processor; + +import dev.banking.model.*; + +import java.util.Map; + +public class TransactionProcessor { + + private final Map accounts; + + public TransactionProcessor( + Map accounts + ) { + this.accounts = accounts; + } + + public void process(Transaction tx) { + + BankAccount source = + accounts.get(tx.getSourceAccountId()); + + switch (tx.getType()) { + + case DEPOSIT -> { + source.deposit(tx.getAmount()); + } + + case WITHDRAW -> { + source.withdraw(tx.getAmount()); + } + + case TRANSFER -> { + + BankAccount target = + accounts.get(tx.getTargetAccountId()); + + source.transfer( + target, + tx.getAmount() + ); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/service/BankingSystem.java b/src/main/java/dev/banking/service/BankingSystem.java new file mode 100644 index 0000000..ec0101b --- /dev/null +++ b/src/main/java/dev/banking/service/BankingSystem.java @@ -0,0 +1,34 @@ +package dev.banking.service; + +import dev.banking.model.*; +import dev.banking.processor.TransactionProcessor; + +import java.util.List; +import java.util.concurrent.ExecutorService; + +public class BankingSystem { + + private final ExecutorService executor; + private final TransactionProcessor processor; + + public BankingSystem( + ExecutorService executor, + TransactionProcessor processor + ) { + this.executor = executor; + this.processor = processor; + } + + public void processTransactions( + List transactions + ) { + + for (Transaction tx : transactions) { + + executor.submit(() -> { + processor.process(tx); + }); + + } + } +} \ No newline at end of file