-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin_Dash.java
More file actions
481 lines (396 loc) · 18.7 KB
/
Admin_Dash.java
File metadata and controls
481 lines (396 loc) · 18.7 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package Rent_Rover;
import Rent_Rover.DataBases.DB;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Admin_Dash extends JFrame {
private JPanel mainPanel;
private JLabel dashboardLabel;
private JLabel customersLabel;
private JLabel carsLabel;
private JLabel bookingsLabel;
private JLabel RR_logo, RentRover, RRR, pfp, adminName, adminRole;
private JPanel bottomPanel, topPanel;
private JScrollPane scrollPane;
public Admin_Dash() {
initComponents();
loadDashboard();
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 800);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Top gradient panel
topPanel = new GradientPanel(new Color(0, 128, 0), new Color(144, 238, 144), GradientPanel.Direction.HORIZONTAL);
topPanel.setPreferredSize(new Dimension(0, 30));
add(topPanel, BorderLayout.NORTH);
// Bottom navigation panel
bottomPanel = new JPanel();
bottomPanel.setBackground(Color.WHITE);
bottomPanel.setPreferredSize(new Dimension(200, 0));
bottomPanel.setLayout(new GridLayout(4, 1, 0, 20));
dashboardLabel = createNavLabel("DASHBOARD");
customersLabel = createNavLabel("CUSTOMERS");
carsLabel = createNavLabel("CARS");
bookingsLabel = createNavLabel("BOOKINGS");
bottomPanel.add(dashboardLabel);
bottomPanel.add(customersLabel);
bottomPanel.add(carsLabel);
bottomPanel.add(bookingsLabel);
add(bottomPanel, BorderLayout.WEST);
// -------- ADMIN PANEL --------
JPanel adminPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
adminPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
adminPanel.setBackground(Color.WHITE);
adminPanel.setOpaque(true);
adminPanel.setPreferredSize(new Dimension(180, 100)); // match RR panel height
JLabel pfp = new JLabel();
ImageIcon profileImg = new ImageIcon("C:\\Users\\97798\\Desktop\\ProjectImages\\gengar.png");
Image imgProfile = profileImg.getImage().getScaledInstance(60, 60, Image.SCALE_SMOOTH);
pfp.setIcon(new ImageIcon(imgProfile));
// Text panel for admin name and role
JPanel adminTextPanel = new JPanel();
adminTextPanel.setLayout(new BoxLayout(adminTextPanel, BoxLayout.Y_AXIS));
adminTextPanel.setOpaque(false);
JLabel adminName = new JLabel("John Cena");
adminName.setFont(new Font("Perpetua", Font.BOLD, 16));
adminName.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel adminRole = new JLabel("ADMIN");
adminRole.setFont(new Font("Perpetua", Font.BOLD, 14));
adminRole.setAlignmentX(Component.LEFT_ALIGNMENT);
adminTextPanel.add(adminName);
adminTextPanel.add(Box.createVerticalStrut(3));
adminTextPanel.add(adminRole);
// Add logo and text panel to admin panel
adminPanel.add(pfp);
adminPanel.add(adminTextPanel);
// -------- RR PANEL --------
JPanel rrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
rrPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
rrPanel.setBackground(Color.WHITE);
rrPanel.setOpaque(true);
rrPanel.setPreferredSize(new Dimension(250, 100));
JLabel RR_logo = new JLabel();
ImageIcon logoImg = new ImageIcon("C:\\Users\\97798\\Desktop\\ProjectImages\\RR logo.png");
Image imgLogo = logoImg.getImage().getScaledInstance(60, 60, Image.SCALE_SMOOTH);
RR_logo.setIcon(new ImageIcon(imgLogo));
JPanel rrTextPanel = new JPanel();
rrTextPanel.setLayout(new BoxLayout(rrTextPanel, BoxLayout.Y_AXIS));
rrTextPanel.setOpaque(false);
JLabel RentRover = new JLabel("RentRover");
RentRover.setFont(new Font("Perpetua", Font.BOLD, 20));
RentRover.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel RRR = new JLabel("Rent, Ride, Repeat");
RRR.setFont(new Font("SansSerif", Font.ITALIC, 14));
RRR.setAlignmentX(Component.LEFT_ALIGNMENT);
rrTextPanel.add(RentRover);
rrTextPanel.add(Box.createVerticalStrut(3));
rrTextPanel.add(RRR);
rrPanel.add(RR_logo);
rrPanel.add(rrTextPanel);
// -------- TOP CONTAINER --------
JPanel topContainer = new JPanel(new BorderLayout());
topContainer.setOpaque(false);
topContainer.add(adminPanel, BorderLayout.WEST);
topContainer.add(rrPanel, BorderLayout.EAST);
// -------- ADD TO FRAME --------
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setOpaque(true);
topPanel.setBackground(Color.WHITE);
topPanel.setPreferredSize(new Dimension(0, 110));
topPanel.add(topContainer, BorderLayout.CENTER);
add(topPanel, BorderLayout.NORTH);
// Main scrollable panel
mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
scrollPane = new JScrollPane(mainPanel);
add(scrollPane, BorderLayout.CENTER);
// Navigation listeners
dashboardLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { loadDashboard(); }
});
customersLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { loadCustomers(); }
});
carsLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { loadCars(); }
});
bookingsLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { loadBookings(); }
});
}
private JLabel createNavLabel(String text) {
JLabel lbl = new JLabel(text, SwingConstants.CENTER);
lbl.setFont(new Font("SansSerif", Font.BOLD, 24));
lbl.setBorder(new LineBorder(Color.BLACK));
lbl.setOpaque(true);
lbl.setBackground(Color.WHITE);
lbl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return lbl;
}
private ImageIcon scaleImage(byte[] bytes, int w, int h) {
ImageIcon raw = new ImageIcon(bytes);
Image scaled = raw.getImage().getScaledInstance(w, h, Image.SCALE_SMOOTH);
return new ImageIcon(scaled);
}
// ---------- LOAD DASHBOARD ----------
private void loadDashboard() {
mainPanel.removeAll();
try (Connection con = DB.getConnection()) {
Statement stmt = con.createStatement();
int customers = 0, cars = 0, bookings = 0;
ResultSet rs1 = stmt.executeQuery("SELECT COUNT(*) FROM customer");
if (rs1.next()) customers = rs1.getInt(1);
ResultSet rs2 = stmt.executeQuery("SELECT COUNT(DISTINCT brand, model, username) FROM bookings");
if (rs2.next()) cars = rs2.getInt(1);
ResultSet rs3 = stmt.executeQuery("SELECT COUNT(*) FROM bookings");
if (rs3.next()) bookings = rs3.getInt(1);
mainPanel.setLayout(new GridLayout(1, 3, 50, 50));
mainPanel.add(createDashboardCard("Customers", customers, "C:\\Users\\97798\\Desktop\\ProjectImages\\customer.png"));
mainPanel.add(createDashboardCard("Cars", cars, "C:\\Users\\97798\\Desktop\\ProjectImages\\car.png"));
mainPanel.add(createDashboardCard("Bookings", bookings, "C:\\Users\\97798\\Desktop\\ProjectImages\\booking.png"));
} catch (SQLException e) {
e.printStackTrace();
}
mainPanel.revalidate();
mainPanel.repaint();
}
private JPanel createDashboardCard(String title, int count, String iconPath) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(250, 150));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel lblIcon = new JLabel();
lblIcon.setAlignmentX(Component.CENTER_ALIGNMENT);
ImageIcon icon = new ImageIcon(iconPath);
Image img = icon.getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH);
lblIcon.setIcon(new ImageIcon(img));
JLabel lblCount = new JLabel(String.valueOf(count));
lblCount.setFont(new Font("SansSerif", Font.BOLD, 36));
lblCount.setAlignmentX(Component.CENTER_ALIGNMENT);
lblCount.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
JLabel lblTitle = new JLabel(title);
lblTitle.setFont(new Font("SansSerif", Font.BOLD, 20));
lblTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
lblTitle.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
panel.add(Box.createVerticalGlue());
panel.add(lblIcon);
panel.add(lblCount);
panel.add(lblTitle);
panel.add(Box.createVerticalGlue());
return panel;
}
// ---------- LOAD CUSTOMERS ----------
private void loadCustomers() {
mainPanel.removeAll();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
try (Connection con = DB.getConnection()) {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM customer");
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String email = rs.getString("email");
int balance = rs.getInt("balance");
Blob imgBlob = rs.getBlob("profile_picture");
ImageIcon img = null;
if (imgBlob != null) img = scaleImage(imgBlob.getBytes(1, (int) imgBlob.length()), 100, 100);
mainPanel.add(createCustomerCard(id, username, email, balance, img));
}
} catch (SQLException e) { e.printStackTrace(); }
mainPanel.revalidate();
mainPanel.repaint();
}
private JPanel createCustomerCard(int id, String username, String email, int balance, ImageIcon img) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(200, 250));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
panel.setLayout(new BorderLayout());
JLabel lblImg = new JLabel();
lblImg.setHorizontalAlignment(SwingConstants.CENTER);
if (img != null) lblImg.setIcon(img);
JLabel lblUser = new JLabel(username, SwingConstants.CENTER);
JLabel lblEmail = new JLabel(email, SwingConstants.CENTER);
JLabel lblBalance = new JLabel("Balance: " + balance, SwingConstants.CENTER);
JButton deleteBtn = new JButton("Delete");
deleteBtn.addActionListener(e -> {
int option = JOptionPane.showConfirmDialog(this, "Delete user and all their bookings?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) deleteCustomer(id);
});
JPanel infoPanel = new JPanel(new GridLayout(4, 1));
infoPanel.add(lblUser);
infoPanel.add(lblEmail);
infoPanel.add(lblBalance);
infoPanel.add(deleteBtn);
panel.add(lblImg, BorderLayout.CENTER);
panel.add(infoPanel, BorderLayout.SOUTH);
return panel;
}
private void deleteCustomer(int id) {
try (Connection con = DB.getConnection()) {
PreparedStatement ps = con.prepareStatement("DELETE FROM bookings WHERE username=(SELECT username FROM customer WHERE id=?)");
ps.setInt(1, id);
ps.executeUpdate();
ps = con.prepareStatement("DELETE FROM customer WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
loadCustomers();
} catch (SQLException e) {
e.printStackTrace();
}
}
// ---------- LOAD CARS ----------
private void loadCars() {
mainPanel.removeAll();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
try (Connection con = DB.getConnection()) {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DISTINCT brand, model, price_per_day, username FROM bookings");
while (rs.next()) {
String brand = rs.getString("brand");
String model = rs.getString("model");
int price = rs.getInt("price_per_day");
String username = rs.getString("username");
mainPanel.add(createCarCard(brand, model, price, username));
mainPanel.add(Box.createVerticalStrut(15));
}
} catch (SQLException e) {
e.printStackTrace();
}
mainPanel.revalidate();
mainPanel.repaint();
}
private JPanel createCarCard(String brand, String model, int price, String username) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(650, 160));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
panel.setLayout(new BorderLayout(15, 0));
// LEFT IMAGE
JLabel lblImg = new JLabel();
lblImg.setPreferredSize(new Dimension(180, 140));
try (Connection con = DB.getConnection()) {
PreparedStatement ps = con.prepareStatement(
"SELECT vehicle_image FROM bookings WHERE brand=? AND model=? LIMIT 1"
);
ps.setString(1, brand);
ps.setString(2, model);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
byte[] imgBytes = rs.getBytes("vehicle_image");
lblImg.setIcon(scaleImage(imgBytes, 180, 140));
}
} catch (Exception ex) {
ex.printStackTrace();
}
lblImg.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblImg, BorderLayout.WEST);
// RIGHT PANEL
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.setOpaque(false);
right.setBorder(BorderFactory.createEmptyBorder(12, 10, 10, 10));
JLabel info = new JLabel(
"<html><b style='font-size:18px'>" + brand + " " + model + "</b>" +
"<br><br>Owner: " + username +
"<br>Price/day: " + price + "</html>"
);
right.add(info);
// DELETE BUTTON
JButton deleteBtn = new JButton("Delete");
deleteBtn.setAlignmentX(Component.LEFT_ALIGNMENT);
deleteBtn.addActionListener(e -> {
int option = JOptionPane.showConfirmDialog(
this,
"Delete this car booking?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION)
deleteCar(username, brand, model);
});
right.add(Box.createVerticalStrut(10));
right.add(deleteBtn);
panel.add(right, BorderLayout.CENTER);
return panel;
}
private void deleteCar(String username, String brand, String model) {
try (Connection con = DB.getConnection()) {
PreparedStatement ps = con.prepareStatement("DELETE FROM bookings WHERE username=? AND brand=? AND model=? LIMIT 1");
ps.setString(1, username);
ps.setString(2, brand);
ps.setString(3, model);
ps.executeUpdate();
loadCars();
} catch (SQLException e) {
e.printStackTrace();
}
}
// ---------- LOAD BOOKINGS ----------
private void loadBookings() {
mainPanel.removeAll();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
try (Connection con = DB.getConnection()) {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT booking_id, username, brand, model, booked_by FROM bookings ORDER BY booking_id DESC");
while (rs.next()) {
int id = rs.getInt("booking_id");
String username = rs.getString("username");
String brand = rs.getString("brand");
String model = rs.getString("model");
String bookedBy = rs.getString("booked_by");
mainPanel.add(createBookingCard(id, username, brand, model, bookedBy));
mainPanel.add(Box.createVerticalStrut(15));
}
} catch (SQLException e) {
e.printStackTrace();
}
mainPanel.revalidate();
mainPanel.repaint();
}
private JPanel createBookingCard(int id, String username, String brand, String model, String bookedBy) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(650, 160));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
panel.setLayout(new BorderLayout(15, 0));
// LEFT IMAGE
JLabel lblImg = new JLabel();
lblImg.setPreferredSize(new Dimension(180, 140));
try (Connection con = DB.getConnection()) {
PreparedStatement ps = con.prepareStatement("SELECT vehicle_image FROM bookings WHERE booking_id=? LIMIT 1");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
byte[] imgBytes = rs.getBytes("vehicle_image");
lblImg.setIcon(scaleImage(imgBytes, 180, 140));
}
} catch (Exception ex) {
ex.printStackTrace();
}
lblImg.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblImg, BorderLayout.WEST);
// RIGHT PANEL
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.setOpaque(false);
right.setBorder(BorderFactory.createEmptyBorder(12, 10, 10, 10));
JLabel info = new JLabel(
"<html><b style='font-size:18px'>Booking Details</b>" +
"<br><br>Car: " + brand + " " + model +
"<br>User: " + username +
"<br>Booked By: " + bookedBy + "</html>"
);
right.add(info);
panel.add(right, BorderLayout.CENTER);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Admin_Dash().setVisible(true));
}
}