feat: add advanced multithreading banking system skeleton

This commit is contained in:
2026-06-03 16:48:39 +03:30
parent 1b38f250e5
commit ac35c817ee
7 changed files with 250 additions and 0 deletions
@@ -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."
);
}
}
@@ -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();
}
}
@@ -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;
}
}
@@ -0,0 +1,7 @@
package dev.banking.model;
public enum TransactionType {
DEPOSIT,
WITHDRAW,
TRANSFER
}
@@ -0,0 +1,23 @@
package dev.banking.monitor;
import dev.banking.model.BankAccount;
import java.util.Collection;
public class LiveMonitor {
public void update(
Collection<BankAccount> accounts
) {
for (BankAccount account : accounts) {
System.out.printf(
"Account %d -> %d%n",
account.getAccountId(),
account.getBalance()
);
}
}
}
@@ -0,0 +1,44 @@
package dev.banking.processor;
import dev.banking.model.*;
import java.util.Map;
public class TransactionProcessor {
private final Map<Integer, BankAccount> accounts;
public TransactionProcessor(
Map<Integer, BankAccount> 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()
);
}
}
}
}
@@ -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<Transaction> transactions
) {
for (Transaction tx : transactions) {
executor.submit(() -> {
processor.process(tx);
});
}
}
}