Advertisement
Crenox

SimpleShow Java Program

Apr 21st, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. // Sammy Samkough
  2. // Simple Show
  3. // Spec: A seat could / should have more information (be it's own object), but here it is either taken or not taken only.
  4. // Default hall is a small 50 seat venue.
  5. // The first 5 rows go for $40 each... yes cheap by today's standards... but I remember prices like that :)
  6. // subsequent rows are just $20 each
  7.  
  8. public class SimpleShow
  9. {
  10. private boolean[][] seat;
  11.  
  12. /** Creates a concert hall with 10 rows and 5 seats in each row
  13. */
  14. public SimpleShow()
  15. {
  16. seat = new boolean[10][5];
  17. }
  18.  
  19. /** Creates a concert hall with the size specified by rows and cols
  20. */
  21. public SimpleShow(int rows, int cols)
  22. {
  23. seat = new boolean[rows][cols];
  24. }
  25.  
  26. /** If the seat is not already occupied then it makes it occupied and returns true
  27. /* If it is already occupied the method will return false
  28. */
  29. public boolean reserveSeat(int row, int col)
  30. {
  31. boolean isOccupied = false;
  32.  
  33. if (seat[row][col] == false)
  34. {
  35. isOccupied = true;
  36. }
  37. else
  38. {
  39. isOccupied = false;
  40. }
  41.  
  42. return isOccupied;
  43. }
  44.  
  45. /** Makes the seat at row, col empty
  46. */
  47. public void clearSeat(int row, int col)
  48. {
  49. seat[row][col] = false;
  50. }
  51.  
  52. /** Resets all seats to available.
  53. */
  54. public void clearAllSeats()
  55. {
  56. for (int r = 0; r < seat.length; r++)
  57. {
  58. for (int c = 0; c < seat[r].length; c++)
  59. {
  60. seat[r][c] = false;
  61. }
  62. }
  63. }
  64.  
  65. /** @return the number of seats still available in the hall
  66. */
  67. public int numSeatsAvailable()
  68. {
  69. int count = 0;
  70.  
  71. for (int r = 0; r < seat.length; r++)
  72. {
  73. for (int c = 0; c < seat[r].length; c++)
  74. {
  75. if (seat[r][c] == true)
  76. {
  77. count++;
  78. }
  79. }
  80. }
  81.  
  82. return count;
  83. }
  84.  
  85. /** @return the total revenue for the show based upon tickets sold
  86. /* The first 5 rows go for $40 each
  87. /* The next 5 rows are just $20 each
  88. */
  89. public int getTotalRevenue()
  90. {
  91. int total = 0;
  92.  
  93. for (int r = 0; r < seat.length; r++)
  94. {
  95. for (int c = 0; c < seat.length; c++)
  96. {
  97. if (seat[r][c].length <= 5)
  98. {
  99. if (seat[r][c] == true)
  100. {
  101. total += 40;
  102. }
  103. }
  104. else
  105. {
  106. total += 20;
  107. }
  108. }
  109. }
  110.  
  111. return total;
  112. }
  113.  
  114. /** Reserve the first 2 adjacent seats and return true if successful
  115. /* If two adjacent seats could not be found, leave the state of the show unchanged
  116. /* and return false.
  117. /* return true if two adjacent seats were found, false otherwise
  118. */
  119. public boolean getTwoTogether()
  120. {
  121. boolean isAdjacent = false;
  122.  
  123. for (int r = 0; r < seat.length; r++)
  124. {
  125. for (int c = 0; c < seat[r].length; c++)
  126. {
  127. if (seat[r][c] == false && seat[r][c + 1] == false)
  128. {
  129. isAdjacent = true;
  130. break;
  131. }
  132. }
  133. }
  134.  
  135. return isAdjacent;
  136. }
  137.  
  138. /** Return the lowest seat number in the specified row for a block
  139. /* of adjacent seats. If no such block exists, return -1.
  140. /* @param row the row number to check
  141. /* @param seatsNeeded the number of adjacent empty seats needed
  142. /* @return lowest seat number for a block of contiguous adjacent seats of the given size
  143. /* or -1 if no such block exists
  144. */
  145. public int findAdjacent(int row, int seatsNeeded)
  146. {
  147. int index = 0, count = 0, lowIndex = 0;
  148.  
  149. for (int r = 0; r < seat.length; r++)
  150. {
  151. for (int c = 0; c < seat.length; c++)
  152. {
  153. // if (row.getTwoTogether)
  154. }
  155. }
  156.  
  157. return -1;
  158. }
  159.  
  160. /** If a seat is available show a 'O', if occumpied show an 'X'
  161. /* return a String with one row per line
  162. */
  163. public String toString()
  164. {
  165. String result = "AP Concert Hall - Simplified\n";
  166.  
  167. return result;
  168. }
  169. }
  170. -------------------------------------------------------------------------------------------------------------------------------
  171. // Mr. A
  172. // Simple Show Runner
  173. // Spec: A simple client to test out the SimpleShow object
  174.  
  175. import java.util.*;
  176.  
  177. public class SimpleShowRunner
  178. {
  179. public static void main(String[] args)
  180. {
  181. SimpleShow show = new SimpleShow();
  182. int seatNum;
  183.  
  184. show.reserveSeat(0,0);
  185. show.reserveSeat(0,1);
  186. show.reserveSeat(0,2);
  187. show.reserveSeat(0,4);
  188. show.reserveSeat(2,2);
  189. show.reserveSeat(3,2);
  190. show.reserveSeat(4,2);
  191. show.reserveSeat(5,1);
  192. show.getTwoTogether();
  193.  
  194. System.out.println(show);
  195. System.out.println("Seats Still Available: " + show.numSeatsAvailable());
  196. System.out.println("Total Revenue: $" + show.getTotalRevenue());
  197.  
  198. System.out.println("\nLooking for 3 adjacent seats in row 2 (third row)... ");
  199. seatNum = show.findAdjacent(2,3);
  200. if(seatNum == -1)
  201. System.out.println("\t\tNot available");
  202. else
  203. System.out.println("\t\tAvailable in row 3 beginning with seat number " + seatNum);
  204.  
  205. System.out.println("Looking for 3 adjacent seats in row 6... ");
  206. seatNum = show.findAdjacent(6,3);
  207. if(seatNum == -1)
  208. System.out.println("\t\tNot available");
  209. else
  210. System.out.println("\t\tAvailable in row 6 beginning with seat number " + seatNum);
  211.  
  212. System.out.println("Looking for 8 adjacent seats in row 7 (third row)... ");
  213. seatNum = show.findAdjacent(7,8);
  214. if(seatNum == -1)
  215. System.out.println("\t\tNot available");
  216. else
  217. System.out.println("\t\tAvailable in row 7 beginning with seat number " + seatNum);
  218.  
  219. System.out.println("Looking for 3 adjacent seats in row 5 (third row)... ");
  220. seatNum = show.findAdjacent(5,3);
  221. if(seatNum == -1)
  222. System.out.println("\t\tnot available");
  223. else
  224. System.out.println("\t\tAvailable in row 5 beginning with seat number " + seatNum);
  225.  
  226. show.clearAllSeats();
  227. System.out.println("\nNew Show\nSeats Still Available: " + show.numSeatsAvailable());
  228. System.out.println("Total Revenue: $" + show.getTotalRevenue());
  229.  
  230. show = new SimpleShow(2,4);
  231. System.out.println(show);
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement