feat: implement TransactionProcessor with type-based dispatch for all transaction operations
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user