THINK FIRST·CODE LATER

← All labs

Emergency room triage with a priority queue

Problem

Write Triage. Patients arrive with a severity from 1 (most urgent) to 5. Commands until the end of input:

  • arrive NAME SEVERITY;
  • next — treat the most urgent waiting patient; among equal severities, the one who arrived first (nobody is waiting if the queue is empty). Also report how many patients who arrived earlier are still waiting.

Use a PriorityQueue<Patient> where Patient implements Comparable<Patient> and compares severity, then arrival number. At the end print how many were treated, how many are still waiting, who would be next, and the total number of times somebody was overtaken.

For the input

arrive Lina 3 arrive Omar 1 arrive Mei 3 next next arrive Ana 2 next next next

the program prints

treat Omar (severity 1, arrived #2) ahead of 1 earlier patient(s)
treat Lina (severity 3, arrived #1)
treat Ana (severity 2, arrived #4) ahead of 1 earlier patient(s)
treat Mei (severity 3, arrived #3)
nobody is waiting
Treated: 4, still waiting: 0
Times somebody was overtaken: 2

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d11-6-data-1.txt

arrive Lina 3 arrive Omar 1 arrive Mei 3 next next arrive Ana 2 next next next
What your program must print for d11-6-data-1.txt
treat Omar (severity 1, arrived #2) ahead of 1 earlier patient(s)
treat Lina (severity 3, arrived #1)
treat Ana (severity 2, arrived #4) ahead of 1 earlier patient(s)
treat Mei (severity 3, arrived #3)
nobody is waiting
Treated: 4, still waiting: 0
Times somebody was overtaken: 2

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d11-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content d11-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.