Menu
CoddyTech

Final: Booking Desk

Lesson 48 of 48 in Coddy's Java for Dart Developers course.

challenge icon

Challenge

Easy

Input is nonnegative capacity then a space-separated group-size list, possibly empty. Implement Room(int), remaining(), reserve(int). Reservation rejects nonpositive or excessive sizes without mutation; constructor rejects negative capacity. BookingDesk(Room).book(int[]) delegates reservations to the supplied Room and returns yes/no results separated by spaces then remaining capacity on a new line. Empty groups produce an empty first line. Repeated book calls use the same supplied room.

Return the exact result text without a final newline from Exercise.solve(List<String> input). The visible locked Main.java reads all input lines and prints the returned text with one final newline. Do not print inside editable methods. All integers and arithmetic results fit Java int unless stated otherwise.

Editable files: Room.java, BookingDesk.java, Exercise.java, Text.java. Text.java supplies reusable parsing and formatting helpers. All files are in the unnamed package and compile together. Main.java is the only entry point and cannot be edited.

The visible locked driver also calls the required class APIs on independent fresh objects before running the input case. Implement those APIs; hard-coding the final output alone is insufficient.

Try it yourself

import java.io.*;
import java.util.*;
public class Main {
    private static void check(boolean condition) {
        if (!condition) throw new AssertionError("Required public API behavior failed");
    }
    public static void main(String[] args) throws Exception {
        List<String> input = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = reader.readLine()) != null) input.add(line);
        Room room=new Room(5);check(!room.reserve(0)&&!room.reserve(-1)&&!room.reserve(6)&&room.remaining()==5);BookingDesk desk=new BookingDesk(room);check(desk.book(new int[]{2}).equals("yes\n3")&&room.remaining()==3);check(desk.book(new int[]{3,1}).equals("yes no\n0"));boolean bad=false;try{new Room(-1);}catch(IllegalArgumentException e){bad=true;}check(bad);
        System.out.println(Exercise.solve(input));
    }
}

All lessons in Java for Dart Developers

Practice on your own: Online Java compiler