+/EE422C
software design
git clone https://git.julia.blue/+/EE422C
7e47727
master
1package assignment6 ; 2 3import java .util .ArrayList ; 4import java .util .List ; 5 6public class Theater { 7 8/** 9* the delay time you will use when print tickets 10*/ 11private int printDelay =50 ;// 50 ms. Use it in your Thread.sleep() 12 13public void setPrintDelay (int printDelay ) { 14this .printDelay =printDelay ; 15 } 16 17public int getPrintDelay () { 18return printDelay ; 19 } 20 21private String show ; 22private int totalSeats ; 23private int seatsPerRow ; 24private int currentSeat ; 25private final List <Ticket >transactions ; 26private boolean notifiedAllOut =false ; 27 28/** 29* Represents a seat in the theater 30* A1, A2, A3, ... B1, B2, B3 ... 31*/ 32static class Seat implements Comparable <Seat > { 33private int rowNum ; 34private int seatNum ; 35 36public Seat (int seatNum ,int seatsPerRow ) { 37this .rowNum =seatNum /seatsPerRow ; 38this .seatNum =seatNum %seatsPerRow +1 ; 39 } 40 41public int getSeatNum () { 42return seatNum ; 43 } 44 45public int getRowNum () { 46return rowNum ; 47 } 48 49@ Override 50public String toString () { 51String result ="" ; 52int tempRowNumber =rowNum +1 ; 53do { 54tempRowNumber --; 55result = ((char ) ('A' +tempRowNumber %26 )) +result ; 56tempRowNumber =tempRowNumber /26 ; 57 }while (tempRowNumber >0 ); 58result +=seatNum ; 59return result ; 60 } 61 62@ Override 63public int compareTo (Seat other ) { 64if (this .rowNum !=other .rowNum ) { 65return Integer .compare (this .rowNum ,other .rowNum ); 66 } 67return Integer .compare (this .seatNum ,other .seatNum ); 68 } 69 } 70 71/** 72* Represents a ticket purchased by a client 73*/ 74static class Ticket { 75private String show ; 76private String boxOfficeId ; 77private Seat seat ; 78private int client ; 79public static final int ticketStringRowLength =31 ; 80 81 82public Ticket (String show ,String boxOfficeId ,Seat seat ,int client ) { 83this .show =show ; 84this .boxOfficeId =boxOfficeId ; 85this .seat =seat ; 86this .client =client ; 87 } 88 89public Seat getSeat () { 90return seat ; 91 } 92 93public String getShow () { 94return show ; 95 } 96 97public String getBoxOfficeId () { 98return boxOfficeId ; 99 } 100 101public int getClient () { 102return client ; 103 } 104 105@ Override 106public String toString () { 107String result ,dashLine ,showLine ,boxLine ,seatLine ,clientLine ,eol ; 108 109eol =System .getProperty ("line.separator" ); 110 111dashLine =new String (new char [ticketStringRowLength ]).replace ('\0' ,'-' ); 112 113showLine ="| Show: " +show ; 114for (int i =showLine .length ();i <ticketStringRowLength -1 ; ++i ) { 115showLine +=" " ; 116 } 117showLine +="|" ; 118 119boxLine ="| Box Office ID: " +boxOfficeId ; 120for (int i =boxLine .length ();i <ticketStringRowLength -1 ; ++i ) { 121boxLine +=" " ; 122 } 123boxLine +="|" ; 124 125seatLine ="| Seat: " +seat .toString (); 126for (int i =seatLine .length ();i <ticketStringRowLength -1 ; ++i ) { 127seatLine +=" " ; 128 } 129seatLine +="|" ; 130 131clientLine ="| Client: " +client ; 132for (int i =clientLine .length ();i <ticketStringRowLength -1 ; ++i ) { 133clientLine +=" " ; 134 } 135clientLine +="|" ; 136 137result =dashLine +eol + 138showLine +eol + 139boxLine +eol + 140seatLine +eol + 141clientLine +eol + 142dashLine ; 143 144return result ; 145 } 146 } 147 148public Theater (int numRows ,int seatsPerRow ,String show ) { 149this .show =show ; 150this .seatsPerRow =seatsPerRow ; 151this .totalSeats =numRows *seatsPerRow ; 152currentSeat =0 ; 153transactions =new ArrayList <>(); 154 } 155 156/** 157* Calculates the best seat not yet reserved 158* 159* @return the best seat or null if theater is full 160*/ 161public Seat bestAvailableSeat () { 162if (currentSeat ==totalSeats ) { 163if (!notifiedAllOut ) { 164System .out .println ("Sorry, we are sold out!" ); 165notifiedAllOut =true ; 166 } 167return null ; 168 } 169return new Seat (currentSeat ,seatsPerRow ); 170 } 171 172/** 173* Prints a ticket for the client after they reserve a seat 174* Also prints the ticket to the console 175* 176* @param seat a particular seat in the theater 177* @return a ticket or null if a box office failed to reserve the seat 178*/ 179public Ticket printTicket (String boxOfficeId ,Seat seat ,int client ) { 180Ticket t ; 181synchronized (transactions ) { 182if (transactions .size () >0 &&transactions .get (transactions .size () -1 ).seat .compareTo (seat ) >=0 ) { 183return null ; 184 } 185 ++currentSeat ; 186t =new Ticket (show ,boxOfficeId ,seat ,client ); 187transactions .add (t ); 188 } 189System .out .println (t ); 190try { 191Thread .sleep (getPrintDelay ()); 192 }catch (InterruptedException e ) { 193e .printStackTrace (); 194 } 195return t ; 196 } 197 198/** 199* Lists all tickets sold for this theater in order of purchase 200* 201* @return list of tickets sold 202*/ 203public List <Ticket >getTransactionLog () { 204return transactions ; 205 } 206}