Here is how the code works
1. Patient.java is an Abstract Class which contains the required attributes and a constructor for
initializing the Patient object
2. PatientImpl.java is a class which extends the Abstract class Patient.java and contains a
constructor which makes a call to the superclass and initializes the superclass with passed
arguments
3. PatientComparator.java is a class which implements the comparator interface and specfies the
criteria for comparing two Patient objects
4. Main.java drives the complete program by providing a menu to the user with the three options
that is for adding a patient, finding the next highest priority patient to be served and exiting the
program
Please find the code as follows:-
Patient.java Code:-
package com.chegg.patient;
import java.util.Date;
abstract class Patient {
private String name;
private Date dob;
private Date admission;
private String complaint;
private int priority;
public Patient(String name, Date dob, Date admission, String complaint, int priority) {
this.name = name;
this.dob = dob;
this.admission = admission;
this.complaint = complaint;
this.priority = priority;
}
public String toString() {
return this.name + this.dob + this.admission + this.complaint + this.priority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Date getAdmission() {
return admission;
}
public void setAdmission(Date admission) {
this.admission = admission;
}
public String getComplaint() {
return complaint;
}
public void setComplaint(String complaint) {
this.complaint = complaint;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
PatientImpl.java Code
package com.chegg.patient;
import java.util.Date;
public class PatientImpl extends Patient {
public PatientImpl(String name, Date dob, Date admission, String complaint, int priority) {
super(name, dob, admission, complaint, priority);
}
}
PatientComparator.java Code
package com.chegg.patient;
import java.util.Comparator;
public class PatientComparator implements Comparator {
@Override
public int compare(Patient arg0, Patient arg1) {
if(arg0.getPriority() == arg1.getPriority()){
if(arg0.getAdmission().compareTo(arg1.getAdmission())==-1){
return -1;
}
else
return 1;
}
if(arg0.getPriority() comparator = new PatientComparator();
PriorityQueue Pq = new PriorityQueue(10, comparator);
while (true) {
System.out.println("1. Enter New Patient Data");
System.out.println("2. Retrieve Next Patient to treat");
System.out.println("3. Exit");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1: {
System.out.println("Enter Name");
name = sc.nextLine();
System.out.println("Enter Date of birth in format yyyy-mm-dd");
String dob_String = sc.next();
dob = parseDate(dob_String);
admission = new Date();
System.out.println("Enter Complaint");
sc.nextLine();
complaint = sc.nextLine();
System.out.println("Enter Priority");
priority = sc.nextInt();
Patient myPatient = new PatientImpl(name, dob, admission, complaint, priority);
Pq.add(myPatient);
System.out.println(myPatient.toString());
break;
}
case 2: {
if (Pq.size() > 0) {
Patient nextPatient = Pq.remove();
System.out.println(nextPatient.toString());
}else
System.out.println("No Patients Pending");
break;
}
case 3: {
if(Pq.size()>0){
System.out.println("Patients are pending in the queue");
continue;
}else{
sc.close();
System.exit(0);
}
}
}
}
}
public static Date parseDate(String date) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
return null;
}
}
}
Solution
Here is how the code works
1. Patient.java is an Abstract Class which contains the required attributes and a constructor for
initializing the Patient object
2. PatientImpl.java is a class which extends the Abstract class Patient.java and contains a
constructor which makes a call to the superclass and initializes the superclass with passed
arguments
3. PatientComparator.java is a class which implements the comparator interface and specfies the
criteria for comparing two Patient objects
4. Main.java drives the complete program by providing a menu to the user with the three options
that is for adding a patient, finding the next highest priority patient to be served and exiting the
program
Please find the code as follows:-
Patient.java Code:-
package com.chegg.patient;
import java.util.Date;
abstract class Patient {
private String name;
private Date dob;
private Date admission;
private String complaint;
private int priority;
public Patient(String name, Date dob, Date admission, String complaint, int priority) {
this.name = name;
this.dob = dob;
this.admission = admission;
this.complaint = complaint;
this.priority = priority;
}
public String toString() {
return this.name + this.dob + this.admission + this.complaint + this.priority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Date getAdmission() {
return admission;
}
public void setAdmission(Date admission) {
this.admission = admission;
}
public String getComplaint() {
return complaint;
}
public void setComplaint(String complaint) {
this.complaint = complaint;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
PatientImpl.java Code
package com.chegg.patient;
import java.util.Date;
public class PatientImpl extends Patient {
public PatientImpl(String name, Date dob, Date admission, String complaint, int priority) {
super(name, dob, admission, complaint, priority);
}
}
PatientComparator.java Code
package com.chegg.patient;
import java.util.Comparator;
public class PatientComparator implements Comparator {
@Override
public int compare(Patient arg0, Patient arg1) {
if(arg0.getPriority() == arg1.getPriority()){
if(arg0.getAdmission().compareTo(arg1.getAdmission())==-1){
return -1;
}
else
return 1;
}
if(arg0.getPriority() comparator = new PatientComparator();
PriorityQueue Pq = new PriorityQueue(10, comparator);
while (true) {
System.out.println("1. Enter New Patient Data");
System.out.println("2. Retrieve Next Patient to treat");
System.out.println("3. Exit");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1: {
System.out.println("Enter Name");
name = sc.nextLine();
System.out.println("Enter Date of birth in format yyyy-mm-dd");
String dob_String = sc.next();
dob = parseDate(dob_String);
admission = new Date();
System.out.println("Enter Complaint");
sc.nextLine();
complaint = sc.nextLine();
System.out.println("Enter Priority");
priority = sc.nextInt();
Patient myPatient = new PatientImpl(name, dob, admission, complaint, priority);
Pq.add(myPatient);
System.out.println(myPatient.toString());
break;
}
case 2: {
if (Pq.size() > 0) {
Patient nextPatient = Pq.remove();
System.out.println(nextPatient.toString());
}else
System.out.println("No Patients Pending");
break;
}
case 3: {
if(Pq.size()>0){
System.out.println("Patients are pending in the queue");
continue;
}else{
sc.close();
System.exit(0);
}
}
}
}
}
public static Date parseDate(String date) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
return null;
}
}
}

Here is how the code works1. Patient.java is an Abstract Class whi.pdf

  • 1.
    Here is howthe code works 1. Patient.java is an Abstract Class which contains the required attributes and a constructor for initializing the Patient object 2. PatientImpl.java is a class which extends the Abstract class Patient.java and contains a constructor which makes a call to the superclass and initializes the superclass with passed arguments 3. PatientComparator.java is a class which implements the comparator interface and specfies the criteria for comparing two Patient objects 4. Main.java drives the complete program by providing a menu to the user with the three options that is for adding a patient, finding the next highest priority patient to be served and exiting the program Please find the code as follows:- Patient.java Code:- package com.chegg.patient; import java.util.Date; abstract class Patient { private String name; private Date dob; private Date admission; private String complaint; private int priority; public Patient(String name, Date dob, Date admission, String complaint, int priority) { this.name = name; this.dob = dob; this.admission = admission; this.complaint = complaint; this.priority = priority; } public String toString() { return this.name + this.dob + this.admission + this.complaint + this.priority; } public String getName() { return name; } public void setName(String name) {
  • 2.
    this.name = name; } publicDate getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } public Date getAdmission() { return admission; } public void setAdmission(Date admission) { this.admission = admission; } public String getComplaint() { return complaint; } public void setComplaint(String complaint) { this.complaint = complaint; } public int getPriority() { return priority; } public void setPriority(int priority) { this.priority = priority; } } PatientImpl.java Code package com.chegg.patient; import java.util.Date; public class PatientImpl extends Patient { public PatientImpl(String name, Date dob, Date admission, String complaint, int priority) { super(name, dob, admission, complaint, priority); } } PatientComparator.java Code
  • 3.
    package com.chegg.patient; import java.util.Comparator; publicclass PatientComparator implements Comparator { @Override public int compare(Patient arg0, Patient arg1) { if(arg0.getPriority() == arg1.getPriority()){ if(arg0.getAdmission().compareTo(arg1.getAdmission())==-1){ return -1; } else return 1; } if(arg0.getPriority() comparator = new PatientComparator(); PriorityQueue Pq = new PriorityQueue(10, comparator); while (true) { System.out.println("1. Enter New Patient Data"); System.out.println("2. Retrieve Next Patient to treat"); System.out.println("3. Exit"); choice = sc.nextInt(); sc.nextLine(); switch (choice) { case 1: { System.out.println("Enter Name"); name = sc.nextLine(); System.out.println("Enter Date of birth in format yyyy-mm-dd"); String dob_String = sc.next(); dob = parseDate(dob_String); admission = new Date(); System.out.println("Enter Complaint"); sc.nextLine(); complaint = sc.nextLine(); System.out.println("Enter Priority"); priority = sc.nextInt(); Patient myPatient = new PatientImpl(name, dob, admission, complaint, priority); Pq.add(myPatient); System.out.println(myPatient.toString());
  • 4.
    break; } case 2: { if(Pq.size() > 0) { Patient nextPatient = Pq.remove(); System.out.println(nextPatient.toString()); }else System.out.println("No Patients Pending"); break; } case 3: { if(Pq.size()>0){ System.out.println("Patients are pending in the queue"); continue; }else{ sc.close(); System.exit(0); } } } } } public static Date parseDate(String date) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(date); } catch (ParseException e) { return null; } } } Solution Here is how the code works 1. Patient.java is an Abstract Class which contains the required attributes and a constructor for initializing the Patient object
  • 5.
    2. PatientImpl.java isa class which extends the Abstract class Patient.java and contains a constructor which makes a call to the superclass and initializes the superclass with passed arguments 3. PatientComparator.java is a class which implements the comparator interface and specfies the criteria for comparing two Patient objects 4. Main.java drives the complete program by providing a menu to the user with the three options that is for adding a patient, finding the next highest priority patient to be served and exiting the program Please find the code as follows:- Patient.java Code:- package com.chegg.patient; import java.util.Date; abstract class Patient { private String name; private Date dob; private Date admission; private String complaint; private int priority; public Patient(String name, Date dob, Date admission, String complaint, int priority) { this.name = name; this.dob = dob; this.admission = admission; this.complaint = complaint; this.priority = priority; } public String toString() { return this.name + this.dob + this.admission + this.complaint + this.priority; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDob() { return dob;
  • 6.
    } public void setDob(Datedob) { this.dob = dob; } public Date getAdmission() { return admission; } public void setAdmission(Date admission) { this.admission = admission; } public String getComplaint() { return complaint; } public void setComplaint(String complaint) { this.complaint = complaint; } public int getPriority() { return priority; } public void setPriority(int priority) { this.priority = priority; } } PatientImpl.java Code package com.chegg.patient; import java.util.Date; public class PatientImpl extends Patient { public PatientImpl(String name, Date dob, Date admission, String complaint, int priority) { super(name, dob, admission, complaint, priority); } } PatientComparator.java Code package com.chegg.patient; import java.util.Comparator; public class PatientComparator implements Comparator { @Override
  • 7.
    public int compare(Patientarg0, Patient arg1) { if(arg0.getPriority() == arg1.getPriority()){ if(arg0.getAdmission().compareTo(arg1.getAdmission())==-1){ return -1; } else return 1; } if(arg0.getPriority() comparator = new PatientComparator(); PriorityQueue Pq = new PriorityQueue(10, comparator); while (true) { System.out.println("1. Enter New Patient Data"); System.out.println("2. Retrieve Next Patient to treat"); System.out.println("3. Exit"); choice = sc.nextInt(); sc.nextLine(); switch (choice) { case 1: { System.out.println("Enter Name"); name = sc.nextLine(); System.out.println("Enter Date of birth in format yyyy-mm-dd"); String dob_String = sc.next(); dob = parseDate(dob_String); admission = new Date(); System.out.println("Enter Complaint"); sc.nextLine(); complaint = sc.nextLine(); System.out.println("Enter Priority"); priority = sc.nextInt(); Patient myPatient = new PatientImpl(name, dob, admission, complaint, priority); Pq.add(myPatient); System.out.println(myPatient.toString()); break; } case 2: { if (Pq.size() > 0) {
  • 8.
    Patient nextPatient =Pq.remove(); System.out.println(nextPatient.toString()); }else System.out.println("No Patients Pending"); break; } case 3: { if(Pq.size()>0){ System.out.println("Patients are pending in the queue"); continue; }else{ sc.close(); System.exit(0); } } } } } public static Date parseDate(String date) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(date); } catch (ParseException e) { return null; } } }