PLEASE Follow every single line in sample run and Please include public class Pa
ID: 3801334 • Letter: P
Question
PLEASE Follow every single line in sample run
and Please include public class Patient, too.
Focus: Java Collection Framework In this assignment, you are required to write a menu-driven Java program that allows the user to add patients to a priority queue, displaythe next patient (and remove him/her from the queue), show a list of all patients currently waiting for treatment, and exit the program. The program should simulate the scheduling of patients in a clinic. Use the attached Patient class provided along with this assignment Your program should schedule patients in the queue according to the emergency of their cases from 1 to 5 (the higher the value, the higher the priority). If two patients have the same emergency value, use their order of arrival to set their priority (the lower the order, the higher the priority). It is up to you to have the Patient class implement either Comparable or Comparator Create a class PatientManager that has an attribute named waitingList of the type PriorityQueue Patient and a public method, start When start is called, it should display the following menu of choices to the user, and then ask the user to enter a choice from 1 to 4: (1) New Patien 2) Next Patient (3) Waiting List (4) Exit Here is a description of each choice: (1) Ask the user for the patient's name and the emergency from 1 to 5 (1 ow, and 5 fe and-death). Your program should create an instance of Patient using the entered data and add it to the priority queue. Note that your program should not askthe user for the value of the patient's order of arrival. Instead, it should use a counter that is automatically incremented whenever a patient is added to the queue (2) Display the name of the next patient in the priority queue and remove him/her from the queue (3) Display the full list of all patients that are still in the queue. (4) End the program. Make sure your PatientManager class is robust. It should not crash when a user enters invalid value. Instead, it should display an error message followed by an action depending on the type of error (see the sample run below) Test your program by instantiating your PatientManager class in a main method and calling the start method Note: Add more helper methods and attributes as needed to the PatientManager classExplanation / Answer
Solution: See the code below:
1. Updated Patient class: Patient.java
-----------------------------------------
package patientscheduledemo;
public class Patient implements Comparable<Patient>{
//attributes
private String name;
private int order; //order of arrival
private int emergency; //1 is normal, 5 is life-and-death situation
//constructor
public Patient(int order, String name, int priority) {
this.order = order;
this.name = name;
this.emergency = priority;
}
//getters and setters
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEmergency() {
return emergency;
}
public void setEmergency(int emergency) {
this.emergency = emergency;
}
public String toString() {
return name;
}
@Override
public int compareTo(Patient arg0) {
// TODO Auto-generated method stub
int result = 0;
if(emergency < arg0.emergency)
{
result=1;
}
else if(emergency > arg0.emergency)
{
result=-1;
}
else
{
if(order < arg0.order)
{
result=1;
}
else if(order > arg0.order)
{
result=-1;
}
}
return result;
}
}
-----------------------------------
2. PatientManager class: PatientManager.java
---------------------------------
package patientscheduledemo;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* PatientManager class
*
*/
public class PatientManager {
private PriorityQueue<Patient> waitingList; //waiting list of patients
/**
* Constructor
*/
public PatientManager() {
// TODO Auto-generated constructor stub
waitingList=new PriorityQueue<Patient>();
}
/**
* start() method
*/
public void start()
{
String choice;
Scanner in=new Scanner(System.in);
String name;
int emergency=0;
int order=0;
Patient patient;
while(true)
{
System.out.println("-------------------------------");
System.out.println("(1) New Patient.");
System.out.println("(2) Next Patient.");
System.out.println("(3) Waiting List.");
System.out.println("(4) Exit.");
System.out.println("* Choose an item from the menu:");
choice=in.next();
switch(choice)
{
case "1":
System.out.println("Enter patient's name:");
name=in.next();
System.out.println("Enter emergency [1 (low) to 5 (life-and-death)]:");
emergency=in.nextInt();
while(true)
{
if(emergency>=1 && emergency<=5)
break;
else
{
System.out.println("(x) Wrong value. Try again:");
emergency=in.nextInt();
}
}
order++;
patient=new Patient(order, name, emergency);
waitingList.add(patient);
System.out.println("Patient added to the waiting list.");
break;
case "2":
if(waitingList.isEmpty())
{
System.out.println("No more patients.");
}
else
{
patient=waitingList.remove();
System.out.println(patient.getName()+" is treated.");
}
break;
case "3":
if(waitingList.size()==0)
{
System.out.println("No patients in the list.");
}
else
{
System.out.println("Waiting list includes:");
Iterator<Patient> iterator=waitingList.iterator();
while(iterator.hasNext())
{
patient=(Patient)iterator.next();
System.out.println("- "+patient.toString());
}
}
break;
case "4":
System.out.println("Program terminared. Good bye!!");
in.close();
System.exit(0);
break;
default:
System.out.println("(x) Wrong choice.");
break;
}
//in.close();
}
}
}
-----------------------------------
3. PatientScheduleDemo class containing main() function: PatientScheduleDemo.java
--------------------------------
package patientscheduledemo;
/**
* PatientScheduleDemo class
*
*/
public class PatientScheduleDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PatientManager manager=new PatientManager();
manager.start();
}
}
-----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.