diff --git a/src/main/java/dev/banking/model/DepositTransaction.java b/src/main/java/dev/banking/model/DepositTransaction.java new file mode 100644 index 0000000..f4798d2 --- /dev/null +++ b/src/main/java/dev/banking/model/DepositTransaction.java @@ -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() + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/model/Transaction.java b/src/main/java/dev/banking/model/Transaction.java index 8ea1c37..174785b 100644 --- a/src/main/java/dev/banking/model/Transaction.java +++ b/src/main/java/dev/banking/model/Transaction.java @@ -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; } diff --git a/src/main/java/dev/banking/model/TransferTransaction.java b/src/main/java/dev/banking/model/TransferTransaction.java new file mode 100644 index 0000000..89d3a57 --- /dev/null +++ b/src/main/java/dev/banking/model/TransferTransaction.java @@ -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() + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/dev/banking/model/WithdrawTransaction.java b/src/main/java/dev/banking/model/WithdrawTransaction.java new file mode 100644 index 0000000..ba1ebd2 --- /dev/null +++ b/src/main/java/dev/banking/model/WithdrawTransaction.java @@ -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() + + '}'; + } +} \ No newline at end of file