-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpeningPage.java
More file actions
311 lines (275 loc) · 13.4 KB
/
Copy pathOpeningPage.java
File metadata and controls
311 lines (275 loc) · 13.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.sql.*;
public class OpeningPage extends JFrame {
private BufferedImage bg;
private JPanel rolePanel;
public OpeningPage() {
System.out.println("OpeningPage constructor started");
// Initialize database early
try {
initializeDatabase();
System.out.println("Database initialized successfully");
} catch (Exception e) {
System.out.println("Database initialization failed: " + e.getMessage());
e.printStackTrace();
}
setTitle("Library Management System");
setSize(1000, 650);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try { bg = ImageIO.read(new File("00.jpg")); }
catch (Exception e) { bg = null; }
JPanel bgPanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null)
g.drawImage(bg.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH),0,0,null);
else {
g.setColor(new Color(40,60,90));
g.fillRect(0,0,getWidth(),getHeight());
}
}
};
bgPanel.setLayout(new GridBagLayout());
add(bgPanel);
JPanel card = new JPanel(new GridBagLayout());
card.setBackground(new Color(255,255,255,220));
card.setPreferredSize(new Dimension(760,330));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
JLabel title = new JLabel("Library Management System");
title.setFont(new Font("Segoe UI", Font.BOLD, 42));
title.setForeground(Color.GREEN.darker());
c.gridy = 0;
card.add(title, c);
JButton enter = new JButton("ENTER");
enter.setFont(new Font("Segoe UI", Font.BOLD, 26));
enter.setPreferredSize(new Dimension(220,60));
c.gridy = 1;
card.add(enter, c);
JButton arrow = new JButton("^");
arrow.setFont(new Font("Segoe UI", Font.BOLD, 20));
arrow.setPreferredSize(new Dimension(60,35));
c.gridy = 2;
card.add(arrow, c);
// Role buttons (hidden)
rolePanel = new JPanel(new GridLayout(1,3,15,15));
rolePanel.setOpaque(false);
rolePanel.setVisible(false);
JButton admin = new JButton("Admin");
JButton librarian = new JButton("Librarian");
JButton user = new JButton("User");
Font rf = new Font("Segoe UI", Font.BOLD, 16);
admin.setFont(rf);
librarian.setFont(rf);
user.setFont(rf);
rolePanel.add(admin);
rolePanel.add(librarian);
rolePanel.add(user);
c.gridy = 3;
card.add(rolePanel, c);
bgPanel.add(card);
// ENTER -> LoginPage
enter.addActionListener(e -> {
dispose();
new LoginPage();
});
// ^ -> show/hide dashboard roles
arrow.addActionListener(e ->
rolePanel.setVisible(!rolePanel.isVisible())
);
admin.addActionListener(e -> openDashboard("admin"));
librarian.addActionListener(e -> openDashboard("librarian"));
user.addActionListener(e -> openDashboard("user"));
setVisible(true);
}
private void openDashboard(String role) {
dispose();
new DashboardPage(role, true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(OpeningPage::new);
}
// Database initialization methods
private void initializeDatabase() {
System.out.println("Initializing database...");
try { Class.forName("org.sqlite.JDBC"); System.out.println("JDBC driver loaded."); } catch (Exception e) { System.out.println("Failed to load JDBC: " + e.getMessage()); }
createBooksTableIfNotExists();
createUsersTableIfNotExists();
createLibrariansTableIfNotExists();
createIssuesTableIfNotExists();
insertSampleBooks();
}
private void createBooksTableIfNotExists() {
String sql = "CREATE TABLE IF NOT EXISTS books (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"isbn TEXT NOT NULL," +
"title TEXT NOT NULL," +
"author TEXT NOT NULL," +
"category TEXT," +
"publisher TEXT," +
"publish_year TEXT," +
"edition TEXT," +
"quantity INTEGER DEFAULT 0," +
"available INTEGER DEFAULT 0" +
");";
try (Connection c = getConnection(); Statement s = c.createStatement()) {
s.execute(sql);
} catch (SQLException ex) { ex.printStackTrace(); }
}
private void createUsersTableIfNotExists() {
String sql = "CREATE TABLE IF NOT EXISTS users (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT NOT NULL," +
"username TEXT NOT NULL UNIQUE," +
"password TEXT NOT NULL," +
"email TEXT," +
"phone TEXT," +
"role TEXT NOT NULL" +
");";
try (Connection c = getConnection(); Statement s = c.createStatement()) {
s.execute(sql);
// Insert default users if empty
insertDefaultUsersIfEmpty(c);
} catch (SQLException ex) { ex.printStackTrace(); }
}
private void createLibrariansTableIfNotExists() {
String sql = "CREATE TABLE IF NOT EXISTS librarians (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT NOT NULL," +
"username TEXT NOT NULL UNIQUE," +
"password TEXT NOT NULL," +
"email TEXT," +
"phone TEXT," +
"photo_path TEXT" +
");";
try (Connection c = getConnection(); Statement s = c.createStatement()) {
s.execute(sql);
insertDefaultLibrariansIfEmpty(c);
} catch (SQLException ex) { ex.printStackTrace(); }
}
private void createIssuesTableIfNotExists() {
String sql = "CREATE TABLE IF NOT EXISTS issues (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"book_id INTEGER NOT NULL," +
"book_title TEXT NOT NULL," +
"user_id INTEGER NOT NULL," +
"user_name TEXT NOT NULL," +
"issue_date TEXT NOT NULL," +
"due_date TEXT NOT NULL," +
"return_date TEXT," +
"late_days INTEGER DEFAULT 0," +
"issued_by TEXT," +
"remarks TEXT," +
"FOREIGN KEY (book_id) REFERENCES books(id)," +
"FOREIGN KEY (user_id) REFERENCES users(id)" +
");";
try (Connection c = getConnection(); Statement s = c.createStatement()) {
s.execute(sql);
} catch (SQLException ex) { ex.printStackTrace(); }
}
private void insertSampleBooks() {
try (Connection c = getConnection()) {
// Insert sample books, ignore if already exist
insertBook(c, "978-0-123456-78-9", "Java Programming", "John Doe", "Programming", "Tech Books Inc", "2020", "1st Edition", 5, 5);
insertBook(c, "978-0-987654-32-1", "Data Structures", "Jane Smith", "Computer Science", "Academic Press", "2019", "2nd Edition", 3, 3);
insertBook(c, "978-0-111111-11-1", "Database Management", "Bob Johnson", "Databases", "DB Publishers", "2021", "1st Edition", 4, 4);
insertBook(c, "978-0-222222-22-2", "Algorithms", "Alice Brown", "Computer Science", "Algo Press", "2018", "3rd Edition", 2, 2);
insertBook(c, "978-0-333333-33-3", "Web Development", "Charlie Wilson", "Web", "Web Books Ltd", "2022", "1st Edition", 6, 6);
insertBook(c, "978-0-444444-44-4", "Machine Learning", "David Lee", "AI", "AI Books Co", "2023", "1st Edition", 3, 3);
insertBook(c, "978-0-555555-55-5", "History of Computing", "Eva Green", "History", "History Press", "2017", "2nd Edition", 4, 4);
insertBook(c, "978-0-666666-66-6", "Python Basics", "Frank White", "Programming", "Code Publishers", "2021", "Revised Edition", 5, 5);
// Insert 100 more books programmatically
String[] categories = {"Fiction", "Non-Fiction", "Science", "Mathematics", "History", "Technology", "Biography", "Children", "Other"};
String[] authors = {"Author A", "Author B", "Author C", "Author D", "Author E", "Author F", "Author G", "Author H", "Author I", "Author J"};
String[] publishers = {"Publisher X", "Publisher Y", "Publisher Z", "Publisher W", "Publisher V"};
for (int i = 9; i <= 108; i++) {
String isbn = "978-0-" + String.format("%06d", i) + "-" + String.format("%02d", i % 100) + "-" + String.format("%01d", i % 10);
String title = "Book " + i;
String author = authors[i % authors.length];
String category = categories[i % categories.length];
String publisher = publishers[i % publishers.length];
String year = String.valueOf(2000 + (i % 25));
String edition = (i % 3 + 1) + "st Edition";
int quantity = 2 + (i % 5);
int available = quantity;
insertBook(c, isbn, title, author, category, publisher, year, edition, quantity, available);
}
System.out.println("Sample books ensured in database.");
} catch (SQLException ex) { ex.printStackTrace(); }
}
private boolean insertBook(Connection c, String isbn, String title, String author, String category, String publisher, String publishYear, String edition, int quantity, int available) {
String sql = "INSERT OR IGNORE INTO books (isbn, title, author, category, publisher, publish_year, edition, quantity, available) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, isbn);
ps.setString(2, title);
ps.setString(3, author);
ps.setString(4, category);
ps.setString(5, publisher);
ps.setString(6, publishYear);
ps.setString(7, edition);
ps.setInt(8, quantity);
ps.setInt(9, available);
ps.executeUpdate();
return true;
} catch (SQLException ex) { ex.printStackTrace(); return false; }
}
private void insertDefaultUsersIfEmpty(Connection c) throws SQLException {
String countSql = "SELECT COUNT(*) FROM users;";
try (Statement s = c.createStatement(); ResultSet rs = s.executeQuery(countSql)) {
if (rs.next() && rs.getInt(1) == 0) {
String sql = "INSERT INTO users (name, username, password, email, phone, role) VALUES (?, ?, ?, ?, ?, ?);";
try (PreparedStatement ps = c.prepareStatement(sql)) {
// Admin
ps.setString(1, "Admin User");
ps.setString(2, "admin");
ps.setString(3, "admin123");
ps.setString(4, "admin@library.com");
ps.setString(5, "1234567890");
ps.setString(6, "admin");
ps.executeUpdate();
// Librarian
ps.setString(1, "Librarian User");
ps.setString(2, "librarian");
ps.setString(3, "lib123");
ps.setString(4, "lib@library.com");
ps.setString(5, "0987654321");
ps.setString(6, "librarian");
ps.executeUpdate();
// User
ps.setString(1, "Regular User");
ps.setString(2, "user");
ps.setString(3, "user123");
ps.setString(4, "user@library.com");
ps.setString(5, "1122334455");
ps.setString(6, "user");
ps.executeUpdate();
}
}
}
}
private void insertDefaultLibrariansIfEmpty(Connection c) throws SQLException {
String countSql = "SELECT COUNT(*) FROM librarians;";
try (Statement s = c.createStatement(); ResultSet rs = s.executeQuery(countSql)) {
if (rs.next() && rs.getInt(1) == 0) {
String sql = "INSERT INTO librarians (name, username, password, email, phone, photo_path) VALUES (?, ?, ?, ?, ?, ?);";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, "John Librarian");
ps.setString(2, "johnlib");
ps.setString(3, "john123");
ps.setString(4, "john@lib.com");
ps.setString(5, "1112223333");
ps.setString(6, "");
ps.executeUpdate();
}
}
}
}
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:sqlite:library.db");
}
}