+/EE422C

software design

git clone https://git.julia.blue/+/EE422C

Joseph RyanAdd the rest of the files7e47727

master
2.4 KiB77 linesraw
1package assignment6;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8public class BookingClient {
9
10    private Map<String, Integer> office;
11    private Theater theater;
12
13    /**
14     * @param office  maps box office id to number of customers in line
15     * @param theater the theater where the show is playing
16     */
17    public BookingClient(Map<String, Integer> office, Theater theater) {
18        this.office = office;
19        this.theater = theater;
20    }
21
22    /**
23     * Starts the box office simulation by creating (and starting) threads
24     * for each box office to sell tickets for the given theater
25     *
26     * @return list of threads used in the simulation,
27     * should have as many threads as there are box offices
28     */
29    public List<Thread> simulate() {
30        List<Thread> threads = new ArrayList<>();
31        int clientID = 1;
32        for (Map.Entry<String, Integer> entry : office.entrySet()) {
33            final int c = clientID;
34            threads.add(new Thread(() -> {
35                int currentClient = c;
36                int numClients = entry.getValue();
37                String boxOfficeID = entry.getKey();
38                while (numClients > 0) {
39                    Theater.Seat s = theater.bestAvailableSeat();
40                    if (s == null) {
41                        return;
42                    }
43                    if (theater.printTicket(boxOfficeID, s, currentClient) == null) {
44                        continue;
45                    }
46                    --numClients;
47                    ++currentClient;
48                }
49            }));
50            clientID += entry.getValue();
51        }
52        for (Thread t : threads) {
53            t.start();
54        }
55        return threads;
56    }
57
58    public static void main(String[] args) {
59        Map<String, Integer> office = new HashMap<>();
60        office.put("BX1", 3);
61        office.put("BX3", 3);
62        office.put("BX2", 4);
63        office.put("BX5", 3);
64        office.put("BX4", 3);
65        Theater theater = new Theater(3, 5, "Ouija");
66        BookingClient b = new BookingClient(office, theater);
67        b.simulate().forEach(t -> {
68            try {
69                t.join();
70            } catch (InterruptedException e) {
71                e.printStackTrace();
72            }
73        });
74        System.out.println("\n\n\nTransaction Log:\n\n\n\n");
75        theater.getTransactionLog().forEach(System.out::println);
76    }
77}