Question

Code in Java please

For this assignment you are to write a simulator that will simulate a triage situation, taking input from file. In the file, you will have the following format FName LName Tri ode Where FName is the patients first name, LName is the patients last name, and TriageCode is a two letter code indicating the injury or illness. To determine the meaning of the code or the severity of the illness or injury in terms of degree of pain or threat to survival, use the following chart (which includes the priority from I to 3, I being the most urgent): Priority Code AL HA ST BL Meaning Amputated limb or digit Heart attack Stroke Broken leg Infected wound (abscess) Kidney stones KS OT HN Hangnail

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Java code for implementing this functionality using PriorityQueue available in Java.util Package.

The main important thing in this is, i created a comparator that compares the priorites while storing and retrieving from the queue.

Please add the below files in your editor and test. Execute the PatientPriorityQueueExample program which is having the main method.

1. Patient.java -----> This file is the value object store patient details.

2. TriageCodeComparator.java ---> compares the patient triagecode while storing and retreiving from the queue.

3. TriageCodeSimulator.java ---> have the actual methods like add/remove/isEmpty

4. PatientPriorityQueueExample.java ----> Used for testing the queue.

Patient.java

package javaapplication1;

public class Patient {

private String fName;
private String lName;
private String triageCode;
private int priority;

public Patient() {
}

public Patient(String fName, String lName, String triageCode) {
this.fName = fName;
this.lName = lName;
this.triageCode = triageCode;
switch (triageCode) {
case "AL":
case "HA":
case "ST":
this.setPriority(1);
break;
case "BL":
case "KS":
case "IW":
case "OT":
this.setPriority(2);
break;
case "HN":
this.setPriority(3);
break;

}
}

public String getfName() {
return fName;
}

public void setfName(String fName) {
this.fName = fName;
}

public String getlName() {
return lName;
}

public void setlName(String lName) {
this.lName = lName;
}

public String getTriageCode() {
return triageCode;
}

public void setTriageCode(String triageCode) {
this.triageCode = triageCode;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
}

}

TriageCodeComparator.java

package javaapplication1;

import java.util.Comparator;

/**
*
* @author Murthy
*/
public class TriageCodeComparator implements Comparator<Patient> {

@Override
public int compare(Patient p1, Patient p2) {
return (int) (p1.getPriority() - p2.getPriority());
}

}

package javaapplicationl: import java.util.Comparator: 亥 @author Murthy public class TriageCodeComparator implements Comparat

TriageCodeSimulator.java

package javaapplication1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class TriagecodeSimulator {
  
Comparator triageCodeComparator = new TriageCodeComparator();
Queue<Patient> patientPriorityQueue = new PriorityQueue<>(7, triageCodeComparator);

//utility method to add random data to Queue
public void add() throws FileNotFoundException {
File file = new File("D:\\test.txt");
Scanner sc = new Scanner(file);

while (sc.hasNextLine()) {
String patientData = sc.nextLine();
String tokens[] = patientData.split(" ");
patientPriorityQueue.add(new Patient(tokens[0], tokens[1], tokens[2]));
}
}

//utility method to poll data from queue
public Patient remove() {
Patient patient = patientPriorityQueue.remove();
System.out.println("returning patient with FName =" + patient.getfName() + " & TriageCode = " + patient.getTriageCode());
return patient;
}
  
public boolean isEmpty(){
return patientPriorityQueue.isEmpty();
}

}

public class TriagecodeSimulator i Comparator triageCodeComparator-new TriageCodeComparator O QueuecPatient> patientPriorityQ

PatientPriorityQueueExample.java

package javaapplication1;

import java.io.FileNotFoundException;

public class PatientPriorityQueueExample {

public static void main(String[] args) throws FileNotFoundException {
TriagecodeSimulator simulator = new TriagecodeSimulator();
simulator.add();
while( !simulator.isEmpty()){
simulator.remove();
}

}

}

Output :

run:
returning patient with FName =Surya & TriageCode = HA
returning patient with FName =Vijaya & TriageCode = KS
returning patient with FName =Ramani & TriageCode = KS
returning patient with FName =Narayana & TriageCode = BL
returning patient with FName =Padmini & TriageCode = HN
BUILD SUCCESSFUL (total time: 0 seconds)

returning patient with FName -Surya &TriageCodeHA returning patient with FName =Vijaya & TriageCode = KS returning patient wi

My Input file is stored in D:\ drive with the name as test.txt. Its contents is.

Surya Murthy HA
Narayana Murthy BL
Padmini Murthy HN
Ramani Murthy KS
Vijaya Murthy KS

Add a comment
Know the answer?
Add Answer to:
Code in Java please For this assignment you are to write a simulator that will simulate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT