-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
44 lines (39 loc) · 1.3 KB
/
Copy pathTransaction.java
File metadata and controls
44 lines (39 loc) · 1.3 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
package Phase3_ObjectOrientation.BankingApp;
import java.time.LocalDateTime;
/**
* Transaction - immutable log entry for a single account event.
* <p>
*
* Implemented as a Java RECORD (Java 16+):
* - All fields are final.
* - equals / hashCode / toString are generated.
* - The compiler also synthesises a canonical constructor and accessor
* methods type(), amount(), balanceAfter(), timestamp().
* <p>
*
* The nested `Type` enum makes the kind of event explicit and lets us
* use exhaustive `switch` statements in any caller code.
*/
public record Transaction(
Transaction.Type type,
double amount,
double balanceAfter,
LocalDateTime timestamp
) {
/** Compact constructor - validates and supplies a default timestamp. */
public Transaction {
if (amount < 0) {
throw new IllegalArgumentException("transaction amount must be >= 0");
}
if (timestamp == null) {
timestamp = LocalDateTime.now();
}
}
/** Convenience constructor - uses "now" for the timestamp. */
public Transaction(Type type, double amount, double balanceAfter) {
this(type, amount, balanceAfter, LocalDateTime.now());
}
public enum Type {
OPEN, DEPOSIT, WITHDRAW, INTEREST, FEE
}
}