-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatientDischarge.java
More file actions
297 lines (246 loc) · 10.3 KB
/
Copy pathPatientDischarge.java
File metadata and controls
297 lines (246 loc) · 10.3 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
package hospital.management.system;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Date;
public class PatientDischarge extends JFrame {
private JComboBox<String> patientChoice;
private JTable patientTable;
private DefaultTableModel tableModel;
private JLabel outTimeLabel;
private JButton checkBtn, dischargeBtn, backBtn, showAllBtn;
public PatientDischarge() {
setTitle("Filostack - Patient Discharge");
setSize(1000, 650);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
// 🔹 Header Panel
JPanel headerPanel = new JPanel(new BorderLayout());
headerPanel.setBackground(new Color(30, 136, 229));
headerPanel.setPreferredSize(new Dimension(950, 70));
JLabel title = new JLabel(" Patient Discharge", JLabel.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 26));
title.setForeground(Color.WHITE);
headerPanel.add(title, BorderLayout.CENTER);
add(headerPanel, BorderLayout.NORTH);
// 🔹 Form Panel
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBackground(Color.WHITE);
formPanel.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(12, 12, 12, 12);
gbc.fill = GridBagConstraints.HORIZONTAL;
Font labelFont = new Font("Segoe UI", Font.BOLD, 14);
// Row 1 - Patient ID Search Box
gbc.gridx = 0;
gbc.gridy = 0;
JLabel patientIdLbl = new JLabel("Patient ID:", JLabel.RIGHT);
patientIdLbl.setFont(labelFont);
formPanel.add(patientIdLbl, gbc);
patientChoice = new JComboBox<>();
patientChoice.setEditable(true);
patientChoice.setPreferredSize(new Dimension(200, 28));
loadPatientIDs();
gbc.gridx = 1;
formPanel.add(patientChoice, gbc);
// Row 2 - Table
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(new String[]{"Patient ID", "Name", "Room", "Disease", "In Time"});
patientTable = new JTable(tableModel);
// Style Table
patientTable.setFont(new Font("Segoe UI", Font.PLAIN, 14));
patientTable.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 14));
patientTable.setRowHeight(28);
patientTable.setSelectionBackground(new Color(200, 230, 255));
JScrollPane scrollPane = new JScrollPane(patientTable);
scrollPane.setPreferredSize(new Dimension(850, 250));
formPanel.add(scrollPane, gbc);
// Row 3 - Out Time
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 1;
JLabel outLbl = new JLabel("Out Time:", JLabel.RIGHT);
outLbl.setFont(labelFont);
formPanel.add(outLbl, gbc);
outTimeLabel = new JLabel(new Date().toString());
outTimeLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
gbc.gridx = 1;
formPanel.add(outTimeLabel, gbc);
add(formPanel, BorderLayout.CENTER);
// 🔹 Button Panel
JPanel btnPanel = new JPanel();
btnPanel.setBackground(new Color(245, 245, 245));
btnPanel.setPreferredSize(new Dimension(950, 70));
checkBtn = createStyledButton("Check Details");
dischargeBtn = createStyledButton("Discharge");
showAllBtn = createStyledButton("Show All Patients");
backBtn = createStyledButton("Back");
btnPanel.add(checkBtn);
btnPanel.add(dischargeBtn);
btnPanel.add(showAllBtn);
btnPanel.add(backBtn);
add(btnPanel, BorderLayout.SOUTH);
// Actions
checkBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkPatient();
}
});
dischargeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dischargePatient();
}
});
showAllBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadAllPatients();
}
});
backBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// Live refresh OutTime when combo changes
patientChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
outTimeLabel.setText(new Date().toString());
}
});
// Load all patients initially
loadAllPatients();
setVisible(true);
}
// 🔹 Load Patient IDs
private void loadPatientIDs() {
try {
conn c = new conn();
ResultSet rs = c.connection.createStatement().executeQuery("SELECT number FROM patient_info");
patientChoice.removeAllItems();
while (rs.next()) {
patientChoice.addItem(rs.getString("number"));
}
rs.close();
c.connection.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error loading patient IDs: " + ex.getMessage());
}
}
// 🔹 Load all patients
private void loadAllPatients() {
try {
conn c = new conn();
ResultSet rs = c.connection.createStatement().executeQuery(
"SELECT number, name, Room_Number, disease, Time FROM patient_info");
tableModel.setRowCount(0);
while (rs.next()) {
tableModel.addRow(new Object[]{
rs.getString("number"),
rs.getString("name"),
rs.getString("Room_Number"),
rs.getString("disease"),
rs.getString("Time")
});
}
rs.close();
c.connection.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error loading patients: " + ex.getMessage());
}
}
// 🔹 Check details
private void checkPatient() {
String pid = (String) patientChoice.getSelectedItem();
if (pid == null || pid.trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a Patient ID.");
return;
}
try {
conn c = new conn();
PreparedStatement pst = c.connection.prepareStatement(
"SELECT number, name, Room_Number, disease, Time FROM patient_info WHERE number=?");
pst.setString(1, pid);
ResultSet rs = pst.executeQuery();
tableModel.setRowCount(0);
while (rs.next()) {
tableModel.addRow(new Object[]{
rs.getString("number"),
rs.getString("name"),
rs.getString("Room_Number"),
rs.getString("disease"),
rs.getString("Time")
});
}
rs.close();
pst.close();
c.connection.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error checking patient: " + ex.getMessage());
}
}
// 🔹 Discharge patient (just delete from patient_info + free room)
private void dischargePatient() {
String pid = (String) patientChoice.getSelectedItem();
if (pid == null || pid.trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a Patient ID.");
return;
}
int confirm = JOptionPane.showConfirmDialog(this,
"Are you sure you want to discharge Patient " + pid + "?",
"Confirm Discharge", JOptionPane.YES_NO_OPTION);
if (confirm != JOptionPane.YES_OPTION) return;
try {
conn c = new conn();
// Get room no
PreparedStatement pst1 = c.connection.prepareStatement(
"SELECT Room_Number FROM patient_info WHERE number=?");
pst1.setString(1, pid);
ResultSet rs = pst1.executeQuery();
String roomNo = "";
if (rs.next()) roomNo = rs.getString("Room_Number");
rs.close();
pst1.close();
// Delete from active patients
PreparedStatement pst = c.connection.prepareStatement(
"DELETE FROM patient_info WHERE number=?");
pst.setString(1, pid);
pst.executeUpdate();
// Free room
if (!roomNo.isEmpty()) {
PreparedStatement pst2 = c.connection.prepareStatement(
"UPDATE room SET Availability='Available' WHERE room_no=?");
pst2.setString(1, roomNo);
pst2.executeUpdate();
pst2.close();
}
pst.close();
c.connection.close();
JOptionPane.showMessageDialog(this, "Patient discharged successfully!");
// Refresh
loadPatientIDs();
loadAllPatients();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error discharging patient: " + ex.getMessage());
}
}
// 🔹 Styled Button
private JButton createStyledButton(String text) {
JButton btn = new JButton(text);
btn.setFont(new Font("Segoe UI", Font.BOLD, 14));
btn.setBackground(new Color(33, 150, 243));
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setBorder(BorderFactory.createEmptyBorder(10, 18, 10, 18));
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
return btn;
}
public static void main(String[] args) {
new PatientDischarge();
}
}