THINK FIRST·CODE LATER

← All labs

Parking fees: top-down design

Problem

Design the program top-down: first write main in terms of methods that do not exist yet, then implement each method.

A campus car park charges: the first 30 minutes free, then 5 yuan per started hour, at most 40 yuan per day. Write ParkingFee with constants and the static methods toMinutes("08:45"), parkedMinutes(in, out), fee(minutes) and duration(minutes) → 2h05.

main reads lines plate in out until the end of input and prints one line per car (%-8s %s %3d yuan), skipping cars whose exit is before the entry (B777: invalid times), then:

Cars: 3, income: 50 yuan
Longest stay: A123 (9h10)

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.

l12-4-data-1.txt

A123 08:00 17:10
B456 09:15 09:40
C789 12:00 13:31
What your program must print for l12-4-data-1.txt
A123     9h10   40 yuan
B456     0h25    0 yuan
C789     1h31   10 yuan
Cars: 3, income: 50 yuan
Longest stay: A123 (9h10)

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

Windows PowerShell does not understand <:

Get-Content l12-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.