-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.java
More file actions
211 lines (182 loc) · 8.02 KB
/
lib.java
File metadata and controls
211 lines (182 loc) · 8.02 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.util.Scanner;
public class lib {
private static String[] usernames = {"BMLadmin1", "BMLadmin2"};
private static String[] passwords = {"password1", "password2"};
private static String[] MEMBERS = {"ADITYA", "MOHNIT","BHARGAVI","PAYAL"};
private static String[] ENROLL_NO = {"220568", "220670","220671","220672"};
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(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();
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;
}
}while (choice > 0);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static boolean check(Scanner scanner) {
System.out.println("Enter your username = ");
String username = scanner.next();
System.out.println("Enter your password =");
String password = scanner.next();
for (int i = 0; i < usernames.length; i++) {
if (usernames[i].equals(username) && passwords[i].equals(password)) {
return true;
}
}
return false;
}
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 {
stmt.execute("USE library");
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,bookname VARCHAR(255), ENROLL INT, borrowed_date DATE, returned_date DATE)");
}
private static void addBook(Connection conn, Scanner scanner) throws SQLException {
System.out.print("Enter book title: ");
String bookTitle = scanner.nextLine();
bookTitle = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
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;
}
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + tableName + " (title, author, COPIES) VALUES (?, ?, ? ) " +
"ON DUPLICATE KEY UPDATE COPIES = COPIES + VALUES(COPIES)");
ps.setString(1, bookTitle);
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.nextLine();
titleToRemove = scanner.nextLine();
System.out.print("Enter the author of the book: ");
String author = scanner.nextLine();
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 ("som".equals(school)) {
tableName = "SOM";
} else if ("soet".equals(school)) {
tableName = "SOET";
} else if ("sol".equals(school)) {
tableName = "SOL";
} else if ("sols".equals(school)) {
tableName = "SOLS";
} else {
System.out.print("Invalid category");
return;
}
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();
}
}
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() {
System.out.println("List of Members:");
for (int i = 0; i < MEMBERS.length; i++)
{
System.out.println("name: " + MEMBERS[i] + " || enrollment no:" + ENROLL_NO[i]);
}
}
}