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 final int accountId;
|
||||||
private long balance;
|
private long balance;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Students may introduce additional fields
|
||||||
|
* if required by their synchronization strategy.
|
||||||
|
*/
|
||||||
|
|
||||||
public BankAccount(int accountId, long initialBalance) {
|
public BankAccount(int accountId, long initialBalance) {
|
||||||
this.accountId = accountId;
|
this.accountId = accountId;
|
||||||
this.balance = initialBalance;
|
this.balance = initialBalance;
|
||||||
|
|||||||
@@ -16,29 +16,36 @@ public class TransactionProcessor {
|
|||||||
|
|
||||||
public void process(Transaction tx) {
|
public void process(Transaction tx) {
|
||||||
|
|
||||||
BankAccount source =
|
if (tx instanceof DepositTransaction deposit) {
|
||||||
accounts.get(tx.getSourceAccountId());
|
|
||||||
|
|
||||||
switch (tx.getType()) {
|
BankAccount account = accounts.get(deposit.getAccountId());
|
||||||
|
|
||||||
case DEPOSIT -> {
|
account.deposit(deposit.getAmount());
|
||||||
source.deposit(tx.getAmount());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case WITHDRAW -> {
|
else if (tx instanceof WithdrawTransaction withdraw) {
|
||||||
source.withdraw(tx.getAmount());
|
|
||||||
|
BankAccount account = accounts.get(withdraw.getAccountId());
|
||||||
|
|
||||||
|
account.withdraw(withdraw.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
case TRANSFER -> {
|
else if (tx instanceof TransferTransaction transfer) {
|
||||||
|
|
||||||
BankAccount target =
|
BankAccount source = accounts.get(transfer.getSourceAccountId());
|
||||||
accounts.get(tx.getTargetAccountId());
|
|
||||||
|
BankAccount target = accounts.get(transfer.getTargetAccountId());
|
||||||
|
|
||||||
source.transfer(
|
source.transfer(
|
||||||
target,
|
target,
|
||||||
tx.getAmount()
|
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.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
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 {
|
public class BankingSystem {
|
||||||
|
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
|
|||||||
Reference in New Issue
Block a user