Repository navigation
Expand file tree
/
Copy pathTransaction.java
More file actions
98 lines (83 loc) · 2.75 KB
/
Copy pathTransaction.java
File metadata and controls
98 lines (83 loc) · 2.75 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.time.LocalDate;
import java.util.UUID;
public class Transaction {
private String id;
private String username;
private String isbn;
private String type; // "Borrow" or "Return"
private LocalDate borrowDate;
private LocalDate returnDate;
public Transaction(String username, String isbn, String type) {
this.id = UUID.randomUUID().toString().substring(0, 8); // Generate a unique ID
this.username = username;
this.isbn = isbn;
this.type = type;
this.borrowDate = LocalDate.now();
this.returnDate = null; // Set to null initially for borrow transactions
}
public Transaction(String id, String username, String isbn, String type, LocalDate borrowDate) {
this.id = id;
this.username = username;
this.isbn = isbn;
this.type = type;
this.borrowDate = borrowDate;
this.returnDate = null;
}
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getIsbn() {
return isbn;
}
public String getType() {
return type;
}
public LocalDate getBorrowDate() {
return borrowDate;
}
public LocalDate getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDate returnDate) {
this.returnDate = returnDate;
}
public boolean isOverdue() {
if (type.equals("Borrow") && returnDate == null) {
// Typically books are due in 14 days
return LocalDate.now().isAfter(borrowDate.plusDays(14));
}
return false;
}
public int calculateFine() {
if (isOverdue()) {
// Calculate days overdue
long daysOverdue = java.time.temporal.ChronoUnit.DAYS.between(
borrowDate.plusDays(14), LocalDate.now());
// Fine is $0.50 per day overdue
return (int) (daysOverdue * 0.5);
}
return 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Transaction [id=").append(id)
.append(", username=").append(username)
.append(", isbn=").append(isbn)
.append(", type=").append(type)
.append(", borrowDate=").append(borrowDate);
if (returnDate != null) {
sb.append(", returnDate=").append(returnDate);
} else if (type.equals("Borrow")) {
sb.append(", status=").append(isOverdue() ? "Overdue" : "Active");
if (isOverdue()) {
sb.append(", fine=$").append(calculateFine());
}
}
sb.append("]");
return sb.toString();
}
}