Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question: You are consulting for a hospital and they want you to build an a

ID: 3733236 • Letter: J

Question

Java Question:

You are consulting for a hospital and they want you to build an application to take in Patient information.

So you start building out the Patient Object with the following information :

Patient ID – int

Patient First Name – String

Patient Last Name – String

Patient illness – String

Notes – String

You want to make sure there are no duplicate patients. So you use the Set interface and make sure you override the hashcode() and equals() methods of the Patient class.

The patients needs to be in alphabetical order by last name. So you want to make sure you create a PatientLast name Comparator and use the TreeSet.

For the purpose of this assignment you need to create 10 patient records. Use the BufferedReader with System.in to have the user input the records.

Explanation / Answer

Java code to the given requirement:

import java.io.*;

import java.util.*;

public class Patient

{

//instance variables

int PatientID;

String FirstName,LastName,illness,Notes;

//default constructor

Patient() {}

//parameterized constructor

public Patient(int patientID, String firstName, String lastName, String illness, String notes)

{

super();

PatientID = patientID;

FirstName = firstName;

LastName = lastName;

this.illness = illness;

Notes = notes;

}

//getter and setter methods

public int getPatientID() {

return PatientID;

}

public void setPatientID(int patientID) {

PatientID = patientID;

}

public String getFirstName() {

return FirstName;

}

public void setFirstName(String firstName) {

FirstName = firstName;

}

public String getLastName() {

return LastName;

}

public void setLastName(String lastName) {

LastName = lastName;

}

public String getIllness() {

return illness;

}

public void setIllness(String illness) {

this.illness = illness;

}

public String getNotes() {

return Notes;

}

public void setNotes(String notes)

{

Notes = notes;

}

//override the hashcode() method of the Patient class.

@Override

public int hashCode()

{

final int prime = 31;

int result = 1;

result = prime * result + ((FirstName == null) ? 0 : FirstName.hashCode());

result = prime * result + ((LastName == null) ? 0 : LastName.hashCode());

result = prime * result + ((Notes == null) ? 0 : Notes.hashCode());

result = prime * result + PatientID;

result = prime * result + ((illness == null) ? 0 : illness.hashCode());

return result;

}

//override equals() methods of the Patient class.

public boolean equals(Patient obj)

{

Patient other = (Patient) obj;

if (FirstName == null) {

if (other.FirstName != null)

return false;

} else if (!FirstName.equals(other.FirstName))

return false;

if (LastName == null) {

if (other.LastName != null)

return false;

} else if (!LastName.equals(other.LastName))

return false;

if (Notes == null) {

if (other.Notes != null)

return false;

} else if (!Notes.equals(other.Notes))

return false;

if (PatientID != other.PatientID)

return false;

if (illness == null) {

if (other.illness != null)

return false;

} else if (!illness.equals(other.illness))

return false;

return true;

}

//driver method to test the Patient object

public static void main(String[] args) throws NumberFormatException, IOException

{

// Use the BufferedReader with System.in to have the user input the records.  

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

//set

Set<Patient> al=new HashSet<Patient>();

int i=1;

//while loop to input the atleast 10 records

while(i<10)

{

//prompting the Patient details

System.out.print(" Patient Id:");

int patientId=Integer.parseInt(br.readLine());

System.out.print("Patient First Name:");

String patientFirstName=br.readLine();

System.out.print("Patient Last Name:");

String patientLastName=br.readLine();

System.out.print("Patient illness:");

String illness=br.readLine();

System.out.print("Notes:");

String notes=br.readLine();  

al.add(new Patient(patientId,patientFirstName,patientLastName,illness,notes));

//increment i value by 1

i=i+1;

}

//creating a PatientLastName Comparator and using the TreeSet.

TreeSet<Patient> patientTreeSet= new TreeSet<Patient>(new PatientLastName());

patientTreeSet.addAll(al);

System.out.println(" ");

//displaying the output using for-each loop

for(Patient p : patientTreeSet)

System.out.println("PatientId:"+p.PatientID+",Patinet First Name:"+p.FirstName+",Patient Last Name:"+p.LastName+",Patient Illness:"+p.illness+",Patient Notes:"+p.Notes);

}

}

//class PatientLastName Comparator

class PatientLastName implements Comparator<Patient>

{

//compare method which returns the LastNames in alphabetical order

public int compare(Patient ob1, Patient ob2)

{

return ob1.LastName.compareTo(ob2.LastName);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote