Repository navigation
Expand file tree
/
Copy pathERPriorityQueue.java
More file actions
executable file
·218 lines (175 loc) · 6.48 KB
/
Copy pathERPriorityQueue.java
File metadata and controls
executable file
·218 lines (175 loc) · 6.48 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
/**
*
* @author egehanhatipoglu
*/
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
public class ERPriorityQueue {
// INNER CLASS: ERPatient (Public Static)
// ERPatient main class'ta görüntülendiği için public olmalı
public static class ERPatient {
private Patient patient;
private final int severity; // ← FINAL yap - değiştirilemez
private LocalDateTime arrivalTime;
private String complaint;
// Constructor: Validates input to ensure critical triage data is correct.
// Enforces severity limits (1-10) to prevent logic errors in the priority queue.
public ERPatient(Patient patient, int severity, String complaint) {
// ← DOĞRULAMA EKLE
if (patient == null) {
throw new IllegalArgumentException("Hasta bilgisi null olamaz!");
}
if (severity < 1 || severity > 10) {
throw new IllegalArgumentException("Şiddet derecesi 1-10 arasında olmalı! Girilen: " + severity);
}
if (complaint == null || complaint.trim().isEmpty()) {
throw new IllegalArgumentException("Şikayet bilgisi boş olamaz!");
}
this.patient = patient;
this.severity = severity;
this.complaint = complaint.trim();
this.arrivalTime = LocalDateTime.now();
}
public Patient getPatient() {
return patient;
}
public int getSeverity() {
return severity;
}
public LocalDateTime getArrivalTime() {
return arrivalTime;
}
public String getComplaint() {
return complaint;
}
@Override
public String toString() {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm");
return patient.getFirstName() + " " + patient.getLastName() +
" (Severity: " + severity + ", Complaint: " + complaint +
", Arrival: " + arrivalTime.format(f) + ")";
}
}
private ArrayList<ERPatient> heap;
public ERPriorityQueue() {
this.heap = new ArrayList<>();
}
public boolean isEmpty() {
return heap.isEmpty();
}
public int size() {
return heap.size();
}
// Add: Inserts a new patient and maintains Max-Heap property.
// Adds to the bottom (leaf) and 'bubbles up' based on severity score.
// Time Complexity: O(log n)
public void addPatient(ERPatient patient) {
heap.add(patient);
heapifyUp(heap.size() - 1);
}
// Remove Highest: Extracts the root (most critical patient).
// Replaces root with the last element and 'bubbles down' to restore order.
// Time Complexity: O(log n)
public ERPatient removeHighestPriority() {
if (isEmpty()) {
return null;
}
ERPatient highest = heap.get(0);
ERPatient last = heap.remove(heap.size() - 1);
if (!isEmpty()) {
heap.set(0, last);
heapifyDown(0);
}
return highest;
}
// Helper: Restores heap order after insertion.
// Swaps the current node with its parent if the current node has higher severity.
private void heapifyUp(int index) {
while (index > 0) {
int parentIndex = (index - 1) / 2;
if (heap.get(index).getSeverity() > heap.get(parentIndex).getSeverity()) {
swap(index, parentIndex);
index = parentIndex;
} else {
break;
}
}
}
// Helper: Restores heap order after deletion.
// Compares parent with children and swaps with the largest child to ensure
// the highest severity node remains at the top.
private void heapifyDown(int index) {
int size = heap.size();
while (true) {
int largest = index;
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
if (leftChild < size && heap.get(leftChild).getSeverity() > heap.get(largest).getSeverity()) {
largest = leftChild;
}
if (rightChild < size && heap.get(rightChild).getSeverity() > heap.get(largest).getSeverity()) {
largest = rightChild;
}
if (largest != index) {
swap(index, largest);
index = largest;
} else {
break;
}
}
}
private void swap(int i, int j) {
ERPatient temp = heap.get(i);
heap.set(i, heap.get(j));
heap.set(j, temp);
}
// Display: Shows patients sorted by severity without modifying the actual heap structure.
// Uses a temporary copy to sort, ensuring O(n log n) display complexity.
public void display() {
if (isEmpty()) {
System.out.println(" ER queue is empty.");
return;
}
System.out.println(" Emergency Room Queue (By Priority):");
ArrayList<ERPatient> copy = new ArrayList<>(heap);
copy.sort((a, b) -> b.getSeverity() - a.getSeverity());
for (int i = 0; i < copy.size(); i++) {
System.out.println(" " + (i + 1) + ". " + copy.get(i));
}
}
// Remove by ID: Specialized removal for "Undo" functionality.
// Finds an element in the middle of the heap, removes it, and re-balances (Up or Down).
public boolean removePatientById(int patientId) {
for (int i = 0; i < heap.size(); i++) {
if (heap.get(i).getPatient().getId() == patientId) {
removeAtIndex(i);
return true;
}
}
return false;
}
private void removeAtIndex(int index) {
if (index >= heap.size()) return;
ERPatient last = heap.remove(heap.size() - 1);
if (index < heap.size()) {
heap.set(index, last);
// Decide whether to bubble Up or Down based on the parent's value
int parent = (index - 1) / 2;
if (index > 0 && heap.get(index).getSeverity() > heap.get(parent).getSeverity()) {
heapifyUp(index);
} else {
heapifyDown(index);
}
}
}
}