Java: Event booking with a waiting list

A complete program with several collections working together and rules enforced in one class.

What to build

A booking system for events with limited seats and a waiting list.

Requirements

  • Commands: event NAME SEATS creates an event (error: event exists), book EVENT PERSON (prints booked PERSON or waiting PERSON when full; error: already booked if the person is booked or waiting for that event; error: no event EVENT for an unknown event), cancel EVENT PERSON (prints cancelled PERSON, and if a waiting person moves up, booked NEXT; error: not booked if the person is neither booked nor waiting; cancelling a waiting person just removes them), show EVENT (prints EVENT: booked A, B; waiting C with names in booking order, none for an empty list).

Input

Line 1: q. Then q commands.

Output

One line per message, as described.

Constraints

1 ≤ q ≤ 300; 1 ≤ SEATS ≤ 50; names are single words.

Example

Input:

8
event talk 2
book talk Rina
book talk Arif
book talk Zoya
book talk Rina
cancel talk Arif
show talk
book gig Omar

Output:

booked Rina
booked Arif
waiting Zoya
error: already booked
cancelled Arif
booked Zoya
talk: booked Rina, Zoya; waiting none
error: no event gig

Why it works

Each event owns its seats and its queue, so the rules about full events and moving people up live in one place, and the queue makes the first person waiting the first to get a freed seat.

Examples

Example 1 · Input
8
event talk 2
book talk Rina
book talk Arif
book talk Zoya
book talk Rina
cancel talk Arif
show talk
book gig Omar
Expected output
booked Rina
booked Arif
waiting Zoya
error: already booked
cancelled Arif
booked Zoya
talk: booked Rina, Zoya; waiting none
error: no event gig
Example 2 · Input
5
event x 1
event x 3
book x A
book x B
show x
Expected output
error: event exists
booked A
waiting B
x: booked A; waiting B

Hints

Hint 1

Per event keep a capacity, a LinkedHashSet of booked names and an ArrayDeque waiting queue.

Hint 2

When someone cancels, the first person in the queue gets the seat.

Solution

Show one correct solution (try it yourself first)
import java.util.*;

public class Main {
    static class Event {
        final int seats;
        final Set<String> booked = new LinkedHashSet<>();
        final Deque<String> waiting = new ArrayDeque<>();

        Event(int seats) { this.seats = seats; }

        String book(String person) {
            if (booked.contains(person) || waiting.contains(person)) return "error: already booked";
            if (booked.size() < seats) {
                booked.add(person);
                return "booked " + person;
            }
            waiting.addLast(person);
            return "waiting " + person;
        }

        List<String> cancel(String person) {
            List<String> out = new ArrayList<>();
            if (booked.remove(person)) {
                out.add("cancelled " + person);
                if (!waiting.isEmpty()) {
                    String next = waiting.pollFirst();
                    booked.add(next);
                    out.add("booked " + next);
                }
            } else if (waiting.remove(person)) {
                out.add("cancelled " + person);
            } else {
                out.add("error: not booked");
            }
            return out;
        }

        String show(String name) {
            String b = booked.isEmpty() ? "none" : String.join(", ", booked);
            String w = waiting.isEmpty() ? "none" : String.join(", ", waiting);
            return name + ": booked " + b + "; waiting " + w;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = Integer.parseInt(in.nextLine().trim());
        Map<String, Event> events = new HashMap<>();
        for (int i = 0; i < q; i++) {
            String[] p = in.nextLine().split(" ");
            if (p[0].equals("event")) {
                if (events.containsKey(p[1])) System.out.println("error: event exists");
                else events.put(p[1], new Event(Integer.parseInt(p[2])));
                continue;
            }
            Event e = events.get(p[1]);
            if (e == null) {
                System.out.println("error: no event " + p[1]);
            } else if (p[0].equals("book")) {
                System.out.println(e.book(p[2]));
            } else if (p[0].equals("cancel")) {
                e.cancel(p[2]).forEach(System.out::println);
            } else {
                System.out.println(e.show(p[1]));
            }
        }
    }
}

Lesson: Putting it together: designing a complete program →