package assignment6; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class BookingClient { private Map office; private Theater theater; /** * @param office maps box office id to number of customers in line * @param theater the theater where the show is playing */ public BookingClient(Map office, Theater theater) { this.office = office; this.theater = theater; } /** * Starts the box office simulation by creating (and starting) threads * for each box office to sell tickets for the given theater * * @return list of threads used in the simulation, * should have as many threads as there are box offices */ public List simulate() { List threads = new ArrayList<>(); int clientID = 1; for (Map.Entry entry : office.entrySet()) { final int c = clientID; threads.add(new Thread(() -> { int currentClient = c; int numClients = entry.getValue(); String boxOfficeID = entry.getKey(); while (numClients > 0) { Theater.Seat s = theater.bestAvailableSeat(); if (s == null) { return; } if (theater.printTicket(boxOfficeID, s, currentClient) == null) { continue; } --numClients; ++currentClient; } })); clientID += entry.getValue(); } for (Thread t : threads) { t.start(); } return threads; } public static void main(String[] args) { Map office = new HashMap<>(); office.put("BX1", 3); office.put("BX3", 3); office.put("BX2", 4); office.put("BX5", 3); office.put("BX4", 3); Theater theater = new Theater(3, 5, "Ouija"); BookingClient b = new BookingClient(office, theater); b.simulate().forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("\n\n\nTransaction Log:\n\n\n\n"); theater.getTransactionLog().forEach(System.out::println); } }