-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminScreen.java
More file actions
338 lines (287 loc) · 15.1 KB
/
AdminScreen.java
File metadata and controls
338 lines (287 loc) · 15.1 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
package HealthTracker;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class AdminScreen {
public static void showAdminScreen() {
JFrame adminFrame = new JFrame("Admin Dashboard");
adminFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
adminFrame.setSize(900, 500);
String[] colNames = {"ID", "User", "Date", "Time", "Reason", "Doctor","specialization","status"};
DefaultTableModel tableModel = new DefaultTableModel(colNames, 0);
JTable table = new JTable(tableModel);
try {
Connection con = DatabaseConnection.getConnection();
String sql = "SELECT * FROM appointments ORDER BY a_id ASC";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("a_id"),
rs.getString("user"),
rs.getDate("date"),
rs.getTime("time"),
rs.getString("reason"),
rs.getString("doctor"),
rs.getString("specialization"),
rs.getString("status")
};
tableModel.addRow(row);
}
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(table);
JButton manageAppointmentButton = new JButton("Manage Appointment");
JButton viewUsersButton = new JButton("View Users");
JButton logoutButton = new JButton("Logout");
manageAppointmentButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
int id = (int) tableModel.getValueAt(selectedRow, 0);
Date sqlDate = (Date) tableModel.getValueAt(selectedRow, 2);
String date = sqlDate.toString();
Time sqlTime = (Time) tableModel.getValueAt(selectedRow, 3);
String time = sqlTime.toString();
String currentStatus = (String) tableModel.getValueAt(selectedRow, 7);
showAppointmentManagement(id, date, time, currentStatus, selectedRow, tableModel);
} else {
JOptionPane.showMessageDialog(null, "Please select an appointment first.");
}
}
});
viewUsersButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
NavigationManager.pushPage(adminFrame);
JFrame userFrame = new JFrame("Registered Users");
userFrame.setSize(600, 400);
String[] cols = { "ID", "User ID", "Full Name", "Password" };
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable userTable = new JTable(model);
try {
Connection con = DatabaseConnection.getConnection();
String sql = "SELECT id, user_id, full_name, password FROM users";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String uid = rs.getString("user_id");
String fullname = rs.getString("full_name");
String pass = rs.getString("password");
model.addRow(new Object[] { id, uid, fullname, pass });
}
rs.close();
ps.close();
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(userTable);
JButton delUserBtn = new JButton("Delete Selected User");
JButton editUserBtn = new JButton("Edit Selected User");
JButton backBtn = new JButton("Back");
delUserBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = userTable.getSelectedRow();
if (selectedRow >= 0) {
int userId = (int) model.getValueAt(selectedRow, 0);
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement(
"DELETE FROM users WHERE id = ?");
ps.setInt(1, userId);
int rows = ps.executeUpdate();
ps.close();
con.close();
if (rows > 0) {
JOptionPane.showMessageDialog(userFrame, "User deleted successfully.");
model.removeRow(selectedRow);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(userFrame, "Select a row first.");
}
}
});
editUserBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = userTable.getSelectedRow();
if (selectedRow >= 0) {
int userId = (int) model.getValueAt(selectedRow, 0);
String currentUid = (String) model.getValueAt(selectedRow, 1);
String currentName = (String) model.getValueAt(selectedRow, 2);
String currentPass = (String) model.getValueAt(selectedRow, 3);
String newUid = JOptionPane.showInputDialog(userFrame, "Edit User ID:", currentUid);
String newName = JOptionPane.showInputDialog(userFrame, "Edit Full Name:", currentName);
String newPass = JOptionPane.showInputDialog(userFrame, "Edit Password:", currentPass);
if (newUid != null && newName != null && newPass != null) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement(
"UPDATE users SET user_id = ?, full_name = ?, password = ? WHERE id = ?");
ps.setString(1, newUid);
ps.setString(2, newName);
ps.setString(3, newPass);
ps.setInt(4, userId);
int rows = ps.executeUpdate();
ps.close();
con.close();
if (rows > 0) {
JOptionPane.showMessageDialog(userFrame, "User updated successfully.");
model.setValueAt(newUid, selectedRow, 1);
model.setValueAt(newName, selectedRow, 2);
model.setValueAt(newPass, selectedRow, 3);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else {
JOptionPane.showMessageDialog(userFrame, "Select a row first.");
}
}
});
backBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame previous = NavigationManager.popPage();
userFrame.dispose();
previous.setVisible(true);
}
});
JPanel panelSouth = new JPanel();
panelSouth.setLayout(new GridLayout(1, 3, 10, 10));
panelSouth.add(delUserBtn);
panelSouth.add(editUserBtn);
panelSouth.add(backBtn);
userFrame.setLayout(new BorderLayout());
userFrame.add(scrollPane, BorderLayout.CENTER);
userFrame.add(panelSouth, BorderLayout.SOUTH);
userFrame.setVisible(true);
}
});
logoutButton.addActionListener(e -> System.exit(0));
JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 10, 10));
buttonPanel.add(manageAppointmentButton);
buttonPanel.add(viewUsersButton);
buttonPanel.add(logoutButton);
adminFrame.setLayout(new BorderLayout());
adminFrame.add(scrollPane, BorderLayout.CENTER);
adminFrame.add(buttonPanel, BorderLayout.SOUTH);
adminFrame.setVisible(true);
}
static void showAppointmentManagement(int appointmentId, String date, String time,
String currentStatus, int selectedRow, DefaultTableModel tableModel) {
JFrame managementFrame = new JFrame("Manage Appointment");
managementFrame.setSize(400, 400);
managementFrame.setLayout(new GridLayout(3, 1, 10, 10));
managementFrame.setLocationRelativeTo(null);
JPanel infoPanel = new JPanel(new GridLayout(0, 2, 5, 5));
infoPanel.setBorder(BorderFactory.createTitledBorder("Appointment Details"));
infoPanel.add(new JLabel("ID:"));
infoPanel.add(new JLabel(String.valueOf(appointmentId)));
infoPanel.add(new JLabel("Date:"));
infoPanel.add(new JLabel(date));
infoPanel.add(new JLabel("Time:"));
infoPanel.add(new JLabel(time));
infoPanel.add(new JLabel("Current Status:"));
infoPanel.add(new JLabel(currentStatus));
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 10));
JButton updateStatusButton = new JButton("Update Status");
JButton deleteButton = new JButton("Delete Appointment");
updateStatusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newStatus = (String) JOptionPane.showInputDialog(
managementFrame,
"Select new status:",
"Update Appointment Status",
JOptionPane.QUESTION_MESSAGE,
null,
new String[]{"Scheduled", "Completed", "Cancelled", "Rescheduled"},
currentStatus
);
if (newStatus != null && !newStatus.equals(currentStatus)) {
try {
Connection con = DatabaseConnection.getConnection();
String sql = "UPDATE appointments SET status = ? WHERE a_id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, newStatus);
ps.setInt(2, appointmentId);
int rows = ps.executeUpdate();
ps.close();
con.close();
if (rows > 0) {
tableModel.setValueAt(newStatus, selectedRow, 7);
JOptionPane.showMessageDialog(managementFrame,
"Status updated successfully to: " + newStatus);
managementFrame.dispose();
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(managementFrame,
"Failed to update status: " + ex.getMessage());
}
}
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(
managementFrame,
"Are you sure you want to delete this appointment?\nThis action cannot be undone.",
"Confirm Deletion",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (confirm == JOptionPane.YES_OPTION) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement(
"DELETE FROM appointments WHERE a_id = ?");
ps.setInt(1, appointmentId);
int rows = ps.executeUpdate();
ps.close();
con.close();
if (rows > 0) {
tableModel.removeRow(selectedRow);
JOptionPane.showMessageDialog(managementFrame,
"Appointment deleted successfully!");
managementFrame.dispose();
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(managementFrame,
"Failed to delete appointment: " + ex.getMessage());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
});
buttonPanel.add(updateStatusButton);
buttonPanel.add(deleteButton);
JPanel closePanel = new JPanel();
JButton closeButton = new JButton("Close");
closeButton.addActionListener(e -> managementFrame.dispose());
closePanel.add(closeButton);
managementFrame.add(infoPanel);
managementFrame.add(buttonPanel);
managementFrame.add(closePanel);
managementFrame.setVisible(true);
}
}