-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
68 lines (55 loc) · 1.73 KB
/
Copy pathTransaction.java
File metadata and controls
68 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package model.entity;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import model.enums.TransactionType;
public class Transaction {
// 1. Fields (private, final everywhere)
private final TransactionType type;
private final double amount;
private final int transactionId;
private static int nextId = 1;
private final LocalDateTime timestamp;
private final double balanceAfter;
private final String description;
// 2. Constructor with validation
public Transaction(TransactionType type, double amount, double balanceAfter, String description)
{
if (type == null) throw new IllegalArgumentException("Transaction type required");
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive: " + amount);
if (description == null || description.trim().isEmpty()) {
throw new IllegalArgumentException("Description required");
}
this.transactionId = Transaction.nextId++;
this.timestamp = LocalDateTime.now();
this.type = type;
this.amount = amount;
this.balanceAfter = balanceAfter;
this.description = description.trim();
}
// 3. Business Getters
public int getTransactionId() {
return transactionId;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public TransactionType getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalanceAfter() {
return balanceAfter;
}
public String getDescription() {
return description;
}
// 4. Professional toString() - Table formatted
@Override
public String toString() {
return String.format("[%s] ID:%-3d %8s ₹%10.2f | Bal:₹%10.2f | %s",
timestamp.format(DateTimeFormatter.ofPattern("dd-MM HH:mm")),
transactionId, type, amount, balanceAfter, description);
}
}