Repository navigation
Expand file tree
/
Copy pathUpdateRoom.java
More file actions
123 lines (106 loc) · 4.27 KB
/
Copy pathUpdateRoom.java
File metadata and controls
123 lines (106 loc) · 4.27 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
package Hotel.Management.System;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import java.util.Timer;
import java.util.TimerTask;
public class UpdateRoom extends JFrame {
JTable table;
DefaultTableModel model;
UpdateRoom() {
setTitle("Room Status Dashboard");
setLayout(null);
JLabel heading = new JLabel("Room Status Dashboard");
heading.setFont(new Font("Tahoma", Font.BOLD, 22));
heading.setBounds(350, 20, 400, 30);
add(heading);
// Table setup
String[] columns = {"Room Number", "Status", "Customer ID"};
model = new DefaultTableModel(columns, 0);
table = new JTable(model);
JScrollPane sp = new JScrollPane(table);
sp.setBounds(50, 70, 900, 400);
add(sp);
// Manual override buttons
JButton refreshBtn = new JButton("Refresh");
refreshBtn.setBounds(50, 500, 120, 30);
refreshBtn.setBackground(Color.BLACK);
refreshBtn.setForeground(Color.WHITE);
add(refreshBtn);
JButton setMaintenanceBtn = new JButton("Set Maintenance");
setMaintenanceBtn.setBounds(200, 500, 150, 30);
setMaintenanceBtn.setBackground(Color.BLACK);
setMaintenanceBtn.setForeground(Color.WHITE);
add(setMaintenanceBtn);
JButton setAvailableBtn = new JButton("Set Available");
setAvailableBtn.setBounds(380, 500, 150, 30);
setAvailableBtn.setBackground(Color.BLACK);
setAvailableBtn.setForeground(Color.WHITE);
add(setAvailableBtn);
// Load room data
Runnable loadRooms = () -> {
try {
connection con = new connection();
ResultSet rs = con.statement.executeQuery(
"SELECT r.RoomNumber, r.Ava, c.number FROM room r LEFT JOIN customer c ON r.RoomNumber = c.room");
model.setRowCount(0);
while (rs.next()) {
String roomNum = rs.getString("RoomNumber");
String status = rs.getString("Ava");
String custId = rs.getString("number");
if (custId == null) custId = "-";
model.addRow(new Object[]{roomNum, status, custId});
}
} catch (Exception e) {
e.printStackTrace();
}
};
// Initial load
loadRooms.run();
// Refresh every 5 seconds automatically
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
loadRooms.run();
}
}, 0, 5000);
// Refresh button
refreshBtn.addActionListener(e -> loadRooms.run());
// Manual override buttons
setMaintenanceBtn.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
String room = (String) table.getValueAt(selectedRow, 0);
try {
connection con = new connection();
con.statement.executeUpdate("UPDATE room SET Ava = 'Maintenance' WHERE RoomNumber = '" + room + "'");
loadRooms.run();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
setAvailableBtn.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
String room = (String) table.getValueAt(selectedRow, 0);
try {
connection con = new connection();
con.statement.executeUpdate("UPDATE room SET Ava = 'Available' WHERE RoomNumber = '" + room + "'");
loadRooms.run();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
getContentPane().setBackground(new Color(3, 45, 48));
setSize(1000, 600);
setLocation(300, 100);
setVisible(true);
}
public static void main(String[] args) {
new UpdateRoom();
}
}