-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
211 lines (157 loc) · 6.76 KB
/
Library.java
File metadata and controls
211 lines (157 loc) · 6.76 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.sql.*;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Library{
public String login(String userId,String password){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("SELECT * FROM users WHERE user_id=? AND password=?");
ps.setString(1,userId);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
if(rs.next()){
return rs.getString("role");
}
}catch(Exception e){
System.out.println("Login failed");
}
return null;
}
public void registerUser(User user){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("INSERT INTO users VALUES (?, ?, ?, ?)");
ps.setString(1,user.getUserId());
ps.setString(2,user.getName());
ps.setString(3,user.getPassword());
ps.setString(4,user.getRole());
ps.executeUpdate();
System.out.println("User registered successfully");
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void addBook(Book book){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("INSERT INTO books VALUES (?, ?, ?, ?)");
ps.setString(1,book.getBookId());
ps.setString(2,book.getTitle());
ps.setString(3,book.getAuthor());
ps.setBoolean(4,false);
ps.executeUpdate();
System.out.println("Book added successfully");
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void issueBook(String bookId,String userId){
try{
Connection con=DBConnection.getConnection();
PreparedStatement check=con.prepareStatement("SELECT is_issued FROM books WHERE book_id=?");
check.setString(1,bookId);
ResultSet rs=check.executeQuery();
if(!rs.next()){
System.out.println("Book not found");
return;
}
if(rs.getBoolean("is_issued")){
System.out.println("Book already issued");
return;
}
LocalDate today=LocalDate.now();
LocalDate due=today.plusDays(7);
PreparedStatement ps1=con.prepareStatement("UPDATE books SET is_issued=true WHERE book_id=?");
ps1.setString(1,bookId);
ps1.executeUpdate();
PreparedStatement ps2=con.prepareStatement("INSERT INTO transactions(book_id,user_id,issue_date,due_date) VALUES (?, ?, ?, ?)");
ps2.setString(1,bookId);
ps2.setString(2,userId);
ps2.setDate(3,Date.valueOf(today));
ps2.setDate(4,Date.valueOf(due));
ps2.executeUpdate();
System.out.println("Book issued. Due date: "+due);
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void returnBook(String bookId){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("SELECT due_date FROM transactions WHERE book_id=? AND return_date IS NULL");
ps.setString(1,bookId);
ResultSet rs=ps.executeQuery();
if(rs.next()){
LocalDate due=rs.getDate("due_date").toLocalDate();
LocalDate today=LocalDate.now();
long delay=ChronoUnit.DAYS.between(due,today);
if(delay>0){
System.out.println("Late by "+delay+" days. Fine: ₹"+(delay*10));
}
PreparedStatement ps1=con.prepareStatement("UPDATE books SET is_issued=false WHERE book_id=?");
ps1.setString(1,bookId);
ps1.executeUpdate();
PreparedStatement ps2=con.prepareStatement("UPDATE transactions SET return_date=? WHERE book_id=? AND return_date IS NULL");
ps2.setDate(1,Date.valueOf(today));
ps2.setString(2,bookId);
ps2.executeUpdate();
System.out.println("Book returned");
}else{
System.out.println("No active transaction");
}
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void showBooks(){
try{
Connection con=DBConnection.getConnection();
ResultSet rs=con.createStatement().executeQuery("SELECT * FROM books");
while(rs.next()){
System.out.println(rs.getString("book_id")+" | "+rs.getString("title")+" | "+rs.getString("author")+" | Issued: "+rs.getBoolean("is_issued"));
}
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void searchBook(String keyword){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("SELECT * FROM books WHERE title LIKE ?");
ps.setString(1,"%"+keyword+"%");
ResultSet rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("title")+" | "+rs.getString("author"));
}
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
public void showTransactions(String userId){
try{
Connection con=DBConnection.getConnection();
PreparedStatement ps=con.prepareStatement("SELECT * FROM transactions WHERE user_id=?");
ps.setString(1,userId);
ResultSet rs=ps.executeQuery();
while(rs.next()){
String bookId=rs.getString("book_id");
LocalDate issue=rs.getDate("issue_date").toLocalDate();
LocalDate due=rs.getDate("due_date").toLocalDate();
Date returnDate=rs.getDate("return_date");
int fine=0;
if(returnDate!=null){
LocalDate returned=returnDate.toLocalDate();
long delay=ChronoUnit.DAYS.between(due,returned);
if(delay>0){
fine=(int)delay*10;
}
System.out.println("Book: "+bookId+" | Issued: "+issue+" | Due: "+due+" | Returned: "+returned+" | Fine: Rs."+fine);
}else{
System.out.println("Book: "+bookId+" | Issued: "+issue+" | Due: "+due+" | Returned: Not yet");
}
}
}catch(Exception e){
System.out.println("Error: "+e.getMessage());
}
}
}