-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnBook.java
More file actions
130 lines (95 loc) · 4.05 KB
/
Copy pathReturnBook.java
File metadata and controls
130 lines (95 loc) · 4.05 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
125
126
127
128
129
130
package LMS.com;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class ReturnBook {
private Scanner sc = new Scanner(System.in);
// Insert Return record
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO return_book
(return_id, issue_id, member_id, book_id, return_date)
VALUES (?, ?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Return ID: ");
int returnId = sc.nextInt();
System.out.print("Enter Issue ID: ");
int issueId = sc.nextInt();
System.out.print("Enter Member ID: ");
int memberId = sc.nextInt();
System.out.print("Enter Book ID: ");
int bookId = sc.nextInt();
System.out.print("Enter Return Date (YYYY-MM-DD): ");
String returnDate = sc.next();
prestmt.setInt(1, returnId);
prestmt.setInt(2, issueId);
prestmt.setInt(3, memberId);
prestmt.setInt(4, bookId);
prestmt.setString(5, returnDate);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Book Returned Successfully!");
} else {
System.out.println("❌ Return Failed!");
}
}
}
// Delete Return record
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM return_book WHERE return_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Return ID to Delete: ");
int returnId = sc.nextInt();
prestmt.setInt(1, returnId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Return Record Deleted Successfully!");
} else {
System.out.println("❌ Return ID Not Found!");
}
}
}
// Update Return record
void update(Connection connection) throws SQLException {
String query = "UPDATE return_book SET return_date = ? WHERE return_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Return ID to Update: ");
int returnId = sc.nextInt();
System.out.print("Enter New Return Date (YYYY-MM-DD): ");
String returnDate = sc.next();
prestmt.setString(1, returnDate);
prestmt.setInt(2, returnId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Return Record Updated Successfully!");
} else {
System.out.println("❌ Return ID Not Found!");
}
}
}
// Get all Return records
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM return_book";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 🔄 Returned Books =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Return ID: " + rs.getInt("return_id") +
" | Issue ID: " + rs.getInt("issue_id") +
" | Member ID: " + rs.getInt("member_id") +
" | Book ID: " + rs.getInt("book_id") +
" | Return Date: " + rs.getDate("return_date")
);
}
if (!found) {
System.out.println("No return records found.");
}
}
}
}