-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrarian.java
More file actions
202 lines (177 loc) · 8.19 KB
/
librarian.java
File metadata and controls
202 lines (177 loc) · 8.19 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
import java.sql.*;
import java.util.Scanner;
public class librarian {
private static final String DB_URL = "jdbc:mysql://localhost:3306/library";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "Aditya1218";
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in);
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement()) {
createTables(stmt);
int choice;
boolean result = check(conn, scanner);
do {
if (result) {
displayMenu();
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook(conn, scanner);
break;
case 2:
removeBook(conn,scanner);
break;
case 3:
listMembers(conn);
break;
case 4:
listBooks(conn,scanner);
break;
case 0:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} else {
System.out.println("Incorrect Username or Password");
choice = -1; // Set choice to -1 to exit the loop if authentication fails
}
}while (choice > 0);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void displayMenu() {
System.out.println("Library Management System");
System.out.println("1. Add Book");
System.out.println("2. Remove Book");
System.out.println("3. List Members");
System.out.println("4. List Books");
System.out.println("0. Exit");
}
private static void createTables(Statement stmt) throws SQLException {
// Create tables if they don't exist
stmt.execute("CREATE DATABASE IF NOT EXISTS library");
stmt.execute("USE library");
stmt.execute("CREATE TABLE IF NOT EXISTS ADMIN (id INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(255),PASSWORD VARCHAR(225), PHNO VARCHAR(10))");
stmt.execute("CREATE TABLE IF NOT EXISTS books (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) primary key, author VARCHAR(255),copies int)");
stmt.execute("CREATE TABLE IF NOT EXISTS members (id INT AUTO_INCREMENT PRIMARY KEY, StudentName VARCHAR(50), EnNo INT UNIQUE, Password VARCHAR(10) UNIQUE)");
stmt.execute("CREATE TABLE IF NOT EXISTS transactions (id INT AUTO_INCREMENT PRIMARY KEY, book_id INT, member_id INT, borrowed_date DATE, returned_date DATE)");
}
private static boolean check(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter your username = ");
String username = scanner.next();
System.out.println("Enter your password =");
String password = scanner.next();
PreparedStatement ps = conn.prepareStatement("SELECT * FROM ADMIN WHERE NAME = ? AND PASSWORD = ?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
return rs.next(); // Return true if the result set has at least one row, indicating a match.
}
private static void addBook(Connection conn, Scanner scanner) throws SQLException {
System.out.print("Enter book title: ");
String title = scanner.next();
System.out.print("Enter book author: ");
String author = scanner.next();
System.out.print("Enter book copies: ");
int copies = scanner.nextInt();
System.out.print("Enter SCHOOL: ");
String SCHOOL = scanner.next();
String tableName;
if ("SOLS".equals(SCHOOL)) {
tableName = "SOLS";
} else if ("SOET".equals(SCHOOL)) {
tableName = "SOET";
} else if ("SOL".equals(SCHOOL)) {
tableName = "SOL";
} else if ("SOM".equals(SCHOOL)) {
tableName = "SOM";
}
else if ("soet".equals(SCHOOL)) {
tableName = "SOET";
}else if ("sol".equals(SCHOOL)) {
tableName = "SOL";
}else if ("som".equals(SCHOOL)) {
tableName = "SOM";
}
else if ("sols".equals(SCHOOL)) {
tableName = "SOM";
}
else {
System.out.print("Invalid category");
return; // Exit the method if the category is invalid
}
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + tableName + " (title, author, COPIES) VALUES (?, ?, ? ) " +
"ON DUPLICATE KEY UPDATE COPIES = COPIES + VALUES(COPIES)");
ps.setString(1, title);
ps.setString(2, author);
ps.setInt(3, copies);
ps.executeUpdate();
System.out.println("Book added successfully.");
}
private static void removeBook(Connection conn, Scanner scanner) {
try {
System.out.print("Enter book title to remove: ");
String titleToRemove = scanner.next();
System.out.print("Enter the author of the book: ");
String author = scanner.next();
System.out.print("Enter SCHOOL: ");
String school = scanner.next();
String tableName;
if ("SOLS".equals(school)) {
tableName = "SOLS";
} else if ("SOET".equals(school)) {
tableName = "SOET";
} else if ("SOL".equals(school)) {
tableName = "SOL";
} else if ("SOM".equals(school)) {
tableName = "SOM";
} else {
System.out.print("Invalid category");
return; // Exit the method if the category is invalid
}
// Use a prepared statement to remove the book by title
String deleteQuery = "DELETE FROM " + tableName + " WHERE title = ? AND author = ?";
try (PreparedStatement ps = conn.prepareStatement(deleteQuery)) {
ps.setString(1, titleToRemove);
ps.setString(2, author);
int rowsAffected = ps.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Book removed successfully.");
} else {
System.out.println("Book with title '" + titleToRemove + "' not found in " + tableName);
}
}
} catch (SQLException e) {
e.printStackTrace(); // Handle the exception according to your application's needs
}
}
private static void listBooks(Connection conn , Scanner scanner) throws SQLException {
System.out.println("List of Books:");
System.out.println("Enter the SCHOOL");
String SCHOOL = scanner.next();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM "+ SCHOOL);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
int copies = rs.getInt("copies");
System.out.println(id + ". "+ title+" || "+ author +" || "+ copies +" || ");
}
}
private static void listMembers(Connection conn) throws SQLException {
System.out.println("List of Members:");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM members");
while (rs.next()) {
int id = rs.getInt("EnNo");
String Studentname = rs.getString("Studentname");
System.out.println(id + " || " + Studentname);
}
}
}