-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.java
More file actions
42 lines (34 loc) · 1.36 KB
/
Transaction.java
File metadata and controls
42 lines (34 loc) · 1.36 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
package cse.school.codejam;
import java.time.LocalDateTime;
public class Transaction {
public enum TransactionType { DEPOSIT, WITHDRAW, TRANSFER }
private TransactionType type;
private String fromAccountNumber;
private String toAccountNumber;
private double amount;
private final LocalDateTime timestamp = LocalDateTime.now();
public Transaction setType(TransactionType type) {
this.type = type; return this;
}
public Transaction setFromAccountNumber(String from) {
this.fromAccountNumber = from; return this;
}
public Transaction setToAccountNumber(String to) {
this.toAccountNumber = to; return this;
}
public Transaction setAmount(double amt) {
if (amt <= 0) throw new IllegalArgumentException("Amount must be positive.");
this.amount = amt; return this;
}
public String getTransactionDetails() {
String msg = "";
if (type == TransactionType.DEPOSIT) {
msg = "Deposit of " + amount + " to " + toAccountNumber;
} else if (type == TransactionType.WITHDRAW) {
msg = "Withdrawal of " + amount + " from " + fromAccountNumber;
} else if (type == TransactionType.TRANSFER) {
msg = "Transfer of " + amount + " from " + fromAccountNumber + " to " + toAccountNumber;
}
return msg + " on " + timestamp;
}
}