51 lines
1.2 KiB
Java
51 lines
1.2 KiB
Java
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) {
|
|
|
|
if (tx instanceof DepositTransaction deposit) {
|
|
|
|
BankAccount account = accounts.get(deposit.getAccountId());
|
|
|
|
account.deposit(deposit.getAmount());
|
|
}
|
|
|
|
else if (tx instanceof WithdrawTransaction withdraw) {
|
|
|
|
BankAccount account = accounts.get(withdraw.getAccountId());
|
|
|
|
account.withdraw(withdraw.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()
|
|
);
|
|
}
|
|
}
|
|
} |