there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General
ID: 3583468 • Letter: T
Question
there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General Hospital. there are five seats available for the patients to wait in queue. P1 comes first followed by P2 then P3... and the last person is P10.
Write a single JAVA program that will implement the queue of the patients.[ Either Array or Linked list can be used for the queue]
Hints
Create a queue of 5 seats
place first 5 patients in the queue
print content of the queue
Remove from patient
Add another patient
print content of the queue
Repeat removing and adding patient until P10 is added and removed
Explanation / Answer
Hospital.java
import java.util.List;
public class Hospital
{
public String removeFromQueue(List<String> queueList)
{
String patientName = null;
if(!queueList.isEmpty())
{
patientName = queueList.get(0);
queueList.remove(0);
}
return patientName;
}
public String addToQueue(List<String> patientList, List<String> queueList)
{
String patientName = null;
if(!patientList.isEmpty() && queueList.size()!=5)
{
patientName = patientList.get(0);
queueList.add(queueList.size(), patientList.get(0));
patientList.remove(0);
}
return patientName;
}
}
*Note: This class is used for Simulating the scenario.
Simulator.java
import java.util.LinkedList;
import java.util.List;
public class Simulator
{
public static void main (String args[]) {
String patientName = null;
int queueLimit = 5;
List<String> patientList = new LinkedList<String>();
List<String> queueList = new LinkedList<String>();
Hospital hospital = new Hospital();
for(int i=0; i<10; i++)
{
patientName = "P"+(i+1);
patientList.add(patientName);
}
while(!patientList.isEmpty())
{
if(!patientList.isEmpty() && queueList.size()<queueLimit)
{
patientName = hospital.addToQueue(patientList, queueList);
System.out.println("Patient" + patientName + " is added into Queue");
}
else
{
System.out.println("Queue is full");
patientName = hospital.removeFromQueue(queueList);
System.out.println("Patient "+patientName + " has consulted and removed from Queue");
}
}
while(!queueList.isEmpty())
{
patientName = hospital.removeFromQueue(queueList);
System.out.println("Patient "+patientName + " has consulted and removed from Queue");
}
System.out.println("No more patient left for consultancy");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.