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;
|
package dev.banking.model;
|
||||||
|
|
||||||
public class Transaction {
|
/**
|
||||||
|
* Base class for all transaction types.
|
||||||
private final TransactionType type;
|
*
|
||||||
|
* Transactions are immutable and thread-safe.
|
||||||
private final int sourceAccountId;
|
*/
|
||||||
private final int targetAccountId;
|
public abstract class Transaction {
|
||||||
|
|
||||||
private final int amount;
|
private final int amount;
|
||||||
|
|
||||||
public Transaction(
|
protected Transaction(int amount) {
|
||||||
TransactionType type,
|
|
||||||
int sourceAccountId,
|
if (amount <= 0) {
|
||||||
int targetAccountId,
|
throw new IllegalArgumentException(
|
||||||
int amount
|
"Transaction amount must be positive."
|
||||||
) {
|
);
|
||||||
this.type = type;
|
}
|
||||||
this.sourceAccountId = sourceAccountId;
|
|
||||||
this.targetAccountId = targetAccountId;
|
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TransactionType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSourceAccountId() {
|
|
||||||
return sourceAccountId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTargetAccountId() {
|
|
||||||
return targetAccountId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAmount() {
|
public int getAmount() {
|
||||||
return amount;
|
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