-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
126 lines (111 loc) · 4.2 KB
/
Transaction.java
File metadata and controls
126 lines (111 loc) · 4.2 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.sql.*;
import java.time.LocalDate;
public class Transaction {
private int transactionId;
private User user;
private Book book;
private LocalDate borrowDate;
private LocalDate returnDate;
private double fine;
public Transaction(int transactionId, User user, Book book) {
this.transactionId = transactionId;
this.user = user;
this.book = book;
this.borrowDate = LocalDate.now();
this.fine = 0.0;
}
public Transaction(int transactionId, User user, Book book, LocalDate borrowDate) {
this(transactionId, user, book);
this.borrowDate = borrowDate;
}
// Getters
public int getTransactionId() { return transactionId; }
public User getUser() { return user; }
public Book getBook() { return book; }
public LocalDate getBorrowDate() { return borrowDate; }
public LocalDate getReturnDate() { return returnDate; }
public double getFine() { return fine; }
// Database operations
public void save() {
try {
String sql = "INSERT INTO Transactions (transactionId, userId, bookIsbn, borrowDate, returnDate, fine) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement ps = Library.db.getConnection().prepareStatement(sql);
ps.setInt(1, transactionId);
ps.setInt(2, user.getId());
ps.setString(3, book.getIsbn());
ps.setDate(4, java.sql.Date.valueOf(borrowDate));
ps.setDate(5, returnDate != null ? java.sql.Date.valueOf(returnDate) : null);
ps.setDouble(6, fine);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void returnBook() {
this.returnDate = LocalDate.now();
calculateFine();
updateTransaction();
}
private void calculateFine() {
if (returnDate != null) {
long daysOverdue = returnDate.toEpochDay() - borrowDate.toEpochDay() - 14;
if (daysOverdue > 0) {
this.fine = daysOverdue;
}
}
}
private void updateTransaction() {
try {
String sql = "UPDATE Transactions SET returnDate = ?, fine = ? WHERE transactionId = ?";
PreparedStatement ps = Library.db.getConnection().prepareStatement(sql);
ps.setDate(1, java.sql.Date.valueOf(returnDate));
ps.setDouble(2, fine);
ps.setInt(3, transactionId);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Transaction getTransaction(int userId, String bookIsbn) {
try {
String sql = "SELECT * FROM Transactions WHERE userId = ? AND bookIsbn = ? AND returnDate IS NULL";
PreparedStatement ps = Library.db.getConnection().prepareStatement(sql);
ps.setInt(1, userId);
ps.setString(2, bookIsbn);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
User user = User.login(rs.getString("username"), rs.getString("password"));
Book book = Book.getBook(bookIsbn);
Transaction transaction = new Transaction(
rs.getInt("transactionId"),
user,
book,
rs.getDate("borrowDate").toLocalDate()
);
transaction.fine = rs.getDouble("fine");
if (rs.getDate("returnDate") != null) {
transaction.returnDate = rs.getDate("returnDate").toLocalDate();
}
ps.close();
return transaction;
}
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
return "Transaction{" +
"transaction Id='" + transactionId + '\'' +
", User=" + user.getName() +
", Book=" + book.getTitle() +
", Borrow Date=" + borrowDate +
", Return Date=" + returnDate +
", Fine=" + fine +
'}';
}
}