feat: implement TransactionProcessor with type-based dispatch for all transaction operations

This commit is contained in:
2026-06-05 19:44:28 +03:30
parent 8cb8c11260
commit 43a53bdabf
3 changed files with 41 additions and 17 deletions
@@ -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()
);
}
}
}