feat(model): introduce transaction hierarchy with deposit, withdraw, and transfer types
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package dev.banking.model;
|
||||
|
||||
/**
|
||||
* Represents a deposit operation.
|
||||
*/
|
||||
public final class DepositTransaction extends Transaction {
|
||||
|
||||
private final int accountId;
|
||||
|
||||
public DepositTransaction(
|
||||
int accountId,
|
||||
int amount
|
||||
) {
|
||||
super(amount);
|
||||
|
||||
if (accountId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid account id."
|
||||
);
|
||||
}
|
||||
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DepositTransaction{" +
|
||||
"accountId=" + accountId +
|
||||
", amount=" + getAmount() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,25 @@
|
||||
package dev.banking.model;
|
||||
|
||||
public class Transaction {
|
||||
|
||||
private final TransactionType type;
|
||||
|
||||
private final int sourceAccountId;
|
||||
private final int targetAccountId;
|
||||
/**
|
||||
* Base class for all transaction types.
|
||||
*
|
||||
* Transactions are immutable and thread-safe.
|
||||
*/
|
||||
public abstract class Transaction {
|
||||
|
||||
private final int amount;
|
||||
|
||||
public Transaction(
|
||||
TransactionType type,
|
||||
int sourceAccountId,
|
||||
int targetAccountId,
|
||||
int amount
|
||||
) {
|
||||
this.type = type;
|
||||
this.sourceAccountId = sourceAccountId;
|
||||
this.targetAccountId = targetAccountId;
|
||||
protected Transaction(int amount) {
|
||||
|
||||
if (amount <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Transaction amount must be positive."
|
||||
);
|
||||
}
|
||||
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public TransactionType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getSourceAccountId() {
|
||||
return sourceAccountId;
|
||||
}
|
||||
|
||||
public int getTargetAccountId() {
|
||||
return targetAccountId;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package dev.banking.model;
|
||||
|
||||
/**
|
||||
* Represents a transfer operation between two accounts.
|
||||
*/
|
||||
public final class TransferTransaction
|
||||
extends Transaction {
|
||||
|
||||
private final int sourceAccountId;
|
||||
private final int targetAccountId;
|
||||
|
||||
public TransferTransaction(
|
||||
int sourceAccountId,
|
||||
int targetAccountId,
|
||||
int amount
|
||||
) {
|
||||
super(amount);
|
||||
|
||||
if (sourceAccountId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid source account id."
|
||||
);
|
||||
}
|
||||
|
||||
if (targetAccountId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid target account id."
|
||||
);
|
||||
}
|
||||
|
||||
if (sourceAccountId == targetAccountId) {
|
||||
throw new IllegalArgumentException(
|
||||
"Source and target accounts must be different."
|
||||
);
|
||||
}
|
||||
|
||||
this.sourceAccountId = sourceAccountId;
|
||||
this.targetAccountId = targetAccountId;
|
||||
}
|
||||
|
||||
public int getSourceAccountId() {
|
||||
return sourceAccountId;
|
||||
}
|
||||
|
||||
public int getTargetAccountId() {
|
||||
return targetAccountId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TransferTransaction{" +
|
||||
"sourceAccountId=" + sourceAccountId +
|
||||
", targetAccountId=" + targetAccountId +
|
||||
", amount=" + getAmount() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package dev.banking.model;
|
||||
|
||||
/**
|
||||
* Represents a withdrawal operation.
|
||||
*/
|
||||
public final class WithdrawTransaction extends Transaction {
|
||||
|
||||
private final int accountId;
|
||||
|
||||
public WithdrawTransaction(
|
||||
int accountId,
|
||||
int amount
|
||||
) {
|
||||
super(amount);
|
||||
|
||||
if (accountId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid account id."
|
||||
);
|
||||
}
|
||||
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WithdrawTransaction{" +
|
||||
"accountId=" + accountId +
|
||||
", amount=" + getAmount() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user