+/EE422C
software design
git clone https://git.julia.blue/+/EE422C
7e47727
master
1package assignment6 ; 2 3import java .util .ArrayList ; 4import java .util .HashMap ; 5import java .util .List ; 6import java .util .Map ; 7 8public class BookingClient { 9 10private Map <String ,Integer >office ; 11private 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*/ 17public BookingClient (Map <String ,Integer >office ,Theater theater ) { 18this .office =office ; 19this .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*/ 29public List <Thread >simulate () { 30List <Thread >threads =new ArrayList <>(); 31int clientID =1 ; 32for (Map .Entry <String ,Integer >entry :office .entrySet ()) { 33final int c =clientID ; 34threads .add (new Thread (() -> { 35int currentClient =c ; 36int numClients =entry .getValue (); 37String boxOfficeID =entry .getKey (); 38while (numClients >0 ) { 39Theater .Seat s =theater .bestAvailableSeat (); 40if (s ==null ) { 41return ; 42 } 43if (theater .printTicket (boxOfficeID ,s ,currentClient ) ==null ) { 44continue ; 45 } 46 --numClients ; 47 ++currentClient ; 48 } 49 })); 50clientID +=entry .getValue (); 51 } 52for (Thread t :threads ) { 53t .start (); 54 } 55return threads ; 56 } 57 58public static void main (String []args ) { 59Map <String ,Integer >office =new HashMap <>(); 60office .put ("BX1" ,3 ); 61office .put ("BX3" ,3 ); 62office .put ("BX2" ,4 ); 63office .put ("BX5" ,3 ); 64office .put ("BX4" ,3 ); 65Theater theater =new Theater (3 ,5 ,"Ouija" ); 66BookingClient b =new BookingClient (office ,theater ); 67b .simulate ().forEach (t -> { 68try { 69t .join (); 70 }catch (InterruptedException e ) { 71e .printStackTrace (); 72 } 73 }); 74System .out .println ("\n\n\nTransaction Log:\n\n\n\n" ); 75theater .getTransactionLog ().forEach (System .out ::println ); 76 } 77}