如果等待60秒以上的患者类别将从“低”变为“高”并排在最新的“高”患者类别之后,谁可以帮助我实现此功能。有人可以帮我解决吗?
EmergencyRoomSimulator1.java:
package emergencyroomsimulator1;
import java.util.Random;
public class EmergencyRoomSimulator1 {
private static final int WAIT_LIMIT = 1000; // 1000 = 1 second
private PatientQueue pq = new PatientQueue();
private void t() {
try { Thread.sleep(new Random().nextInt(WAIT_LIMIT));
} catch (InterruptedException e) {
}
} // end t method
private void patientArrives(Patient p) {
pq.registerPatient(p);
System.out.println(" ARRIVAL: " + p.getID());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------");
t();
} // end patientArrives method
public void doctorVisits() {
Patient p = pq.getNextPatient();
System.out.println(" VISIT: " + p.getID());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------");
t();
} // end doctorVisits method
private void simulate() {
System.out.println("------------------------------------");
System.out.println("ER OPEN");
System.out.println("------------------------------------");
patientArrives(new Patient(100, "high"));
patientArrives(new Patient(101, "low"));
patientArrives(new Patient(102, "low"));
patientArrives(new Patient(103, "high"));
patientArrives(new Patient(104, "high"));
patientArrives(new Patient(105, "low"));
patientArrives(new Patient(106, "low"));
patientArrives(new Patient(107, "high"));
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
System.out.println("------------------------------------");
System.out.println("ER CLOSED");
System.out.println("------------------------------------");
}
public static void main(String[] args) {
// TODO code application logic here
EmergencyRoomSimulator1 er = new EmergencyRoomSimulator1();
er.simulate();
} // end main
}
Patient.java:
package emergencyroomsimulator1;
import java.util.Date;
public class Patient implements Comparable<Patient> {
protected int id;
protected String category;
protected Date timeArrived;
EmergencyRoomSimulator1 emg;
//protected Date CurrentTime;
// accessors and mutators
public int getID() {
return id;
}
public void setID(int idIn) {
this.id = idIn;
}
public String getCategory() {
return category;
}
public void setCategory(String categoryIn) {
this.category = categoryIn;
}
public java.util.Date getTimeArrived() {
return timeArrived;
}
public int compareTo(Patient other) {
// This code doesn't handle nulls
int result = getCategory().compareTo(other.getCategory());
if (result == 0) {
result = getTimeArrived().compareTo(other.getTimeArrived());
}
return result;
}
// default constructor
public Patient() {
this.id = 0;
this.category = "low"; // unclassified Patients go to the end of the queue
this.timeArrived = new Date();
}
// overloaded constructor
public Patient(int idIn, String categoryIn) {
this.id = idIn;
this.category = categoryIn;
this.timeArrived = new Date();
}
}
PatientComparator.java:
package emergencyroomsimulator1;
import java.util.Comparator;
public class PatientComparator implements Comparator<Patient>{
public int compare(Patient p1, Patient p2) {
int result = p1.getCategory().compareTo(p2.getCategory());
if (result == 0) {
result = p1.getTimeArrived().compareTo(p2.getTimeArrived());
}
return result;
}
}
PatientQueue.java:
package emergencyroomsimulator1;
import java.util.Comparator;
import java.util.PriorityQueue;
public class PatientQueue {
PriorityQueue pq;
// default constructor
public PatientQueue() {
this.pq = new PriorityQueue<Patient>(1, new PatientComparator());
}
public void registerPatient(Patient p) {
this.pq.add(p);
} // end registerPatient method
public Patient getNextPatient() {
return (Patient) this.pq.poll();
} // end getNextPatient method
}