feat: implement TransactionProcessor with type-based dispatch for all transaction operations
This commit is contained in:
@@ -5,6 +5,11 @@ public class BankAccount {
|
||||
private final int accountId;
|
||||
private long balance;
|
||||
|
||||
/*
|
||||
* Students may introduce additional fields
|
||||
* if required by their synchronization strategy.
|
||||
*/
|
||||
|
||||
public BankAccount(int accountId, long initialBalance) {
|
||||
this.accountId = accountId;
|
||||
this.balance = initialBalance;
|
||||
|
||||
@@ -16,29 +16,36 @@ public class TransactionProcessor {
|
||||
|
||||
public void process(Transaction tx) {
|
||||
|
||||
BankAccount source =
|
||||
accounts.get(tx.getSourceAccountId());
|
||||
if (tx instanceof DepositTransaction deposit) {
|
||||
|
||||
switch (tx.getType()) {
|
||||
BankAccount account = accounts.get(deposit.getAccountId());
|
||||
|
||||
case DEPOSIT -> {
|
||||
source.deposit(tx.getAmount());
|
||||
}
|
||||
account.deposit(deposit.getAmount());
|
||||
}
|
||||
|
||||
case WITHDRAW -> {
|
||||
source.withdraw(tx.getAmount());
|
||||
}
|
||||
else if (tx instanceof WithdrawTransaction withdraw) {
|
||||
|
||||
case TRANSFER -> {
|
||||
BankAccount account = accounts.get(withdraw.getAccountId());
|
||||
|
||||
BankAccount target =
|
||||
accounts.get(tx.getTargetAccountId());
|
||||
account.withdraw(withdraw.getAmount());
|
||||
}
|
||||
|
||||
source.transfer(
|
||||
target,
|
||||
tx.getAmount()
|
||||
);
|
||||
}
|
||||
else if (tx instanceof TransferTransaction transfer) {
|
||||
|
||||
BankAccount source = accounts.get(transfer.getSourceAccountId());
|
||||
|
||||
BankAccount target = accounts.get(transfer.getTargetAccountId());
|
||||
|
||||
source.transfer(
|
||||
target,
|
||||
transfer.getAmount()
|
||||
);
|
||||
}
|
||||
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"Unknown transaction type: " + tx.getClass()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,18 @@ import dev.banking.processor.TransactionProcessor;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
/**
|
||||
* Dispatches a list of transactions to a shared ExecutorService
|
||||
* for concurrent (asynchronous) processing.
|
||||
*
|
||||
* Each transaction is submitted as an independent task and may
|
||||
* be executed in parallel depending on thread availability.
|
||||
*
|
||||
* No ordering guarantees are provided between transactions.
|
||||
*
|
||||
* Lifecycle management of the ExecutorService (creation,
|
||||
* shutdown, termination) is handled outside this class.
|
||||
*/
|
||||
public class BankingSystem {
|
||||
|
||||
private final ExecutorService executor;
|
||||
|
||||
Reference in New Issue
Block a user