THINK FIRST·CODE LATER

← All labs

Next bus: navigating a TreeMap

Problem

Write BusTimetable. Read n departures HH:MM line into a TreeMap<Integer, String> keyed by minutes after midnight. Print the first and last bus (firstKey, lastKey). Then, for each query time until the end of input, print:

  • the next bus at or after that time (ceilingEntry) with the waiting time, or no more buses today;
  • the bus just missed (lowerEntry), or none;
  • how many buses are left today (tailMap(now, true).size()).

For the input

5
07:30 B12
08:15 B7
12:00 B12
17:45 B3
22:10 N1
08:00 08:15 23:00 06:00

the program prints

First bus: 07:30, last bus: 22:10
At 08:00:
  next: 08:15 B7 (wait 15 min)
  missed: 07:30 B12
  buses left today: 4
At 08:15:
  next: 08:15 B7 (wait 0 min)
  missed: 07:30 B12
  buses left today: 4
At 23:00:
  next: no more buses today
  missed: 22:10 N1
  buses left today: 0
At 06:00:
  next: 07:30 B12 (wait 90 min)
  missed: none
  buses left today: 5

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.

d5-4-data-1.txt

5
07:30 B12
08:15 B7
12:00 B12
17:45 B3
22:10 N1
08:00 08:15 23:00 06:00
What your program must print for d5-4-data-1.txt
First bus: 07:30, last bus: 22:10
At 08:00:
  next: 08:15 B7 (wait 15 min)
  missed: 07:30 B12
  buses left today: 4
At 08:15:
  next: 08:15 B7 (wait 0 min)
  missed: 07:30 B12
  buses left today: 4
At 23:00:
  next: no more buses today
  missed: 22:10 N1
  buses left today: 0
At 06:00:
  next: 07:30 B12 (wait 90 min)
  missed: none
  buses left today: 5

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 < d5-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content d5-4-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.