Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import java.util.*;
  2. public class Cinema{
  3. public static void main (String[] args){
  4. //Variables
  5. Scanner reader = new Scanner (System.in);
  6. Seat[][] seats = new Seat [5][6];
  7. int row, input, howmany, count = 0;
  8. String name;
  9. String menu = "Menu"
  10. + "\n1: Book seats"
  11. + "\n2: View current seating"
  12. + "\n0: Quit program";
  13. //Assigns numbers to the seats.
  14. for (int i = 0; i<5; i++){
  15. seats[i][0]=new Seat("1");
  16. seats[i][1]=new Seat("2");
  17. seats[i][2]=new Seat("3");
  18. seats[i][3]=new Seat("4");
  19. seats[i][4]=new Seat("5");
  20. seats[i][5]=new Seat("6");
  21. }
  22. do{
  23. // Asks the user what she wants to do
  24. System.out.println("\n"+menu);
  25. input = Integer.parseInt(reader.next());
  26. reader.nextLine();
  27. switch(input){
  28. case 1:
  29. //When all the 30 seats are booked, the program informs the user that the cinema is full.
  30. if(count == 30)
  31. {
  32. System.out.println("\nThe cinema is fully booked.");
  33. }
  34. else
  35. {
  36. do
  37. {
  38. System.out.print("How many seats would you like to book? ");
  39. howmany = reader.nextInt();
  40. if(howmany>4)
  41. {
  42. System.out.println("The maximum number of seats per booking is 4.");
  43. }
  44. if(howmany<1)
  45. {
  46. System.out.println("The minimum number of seats per booking is 1.");
  47. }
  48. }
  49. while(howmany>4 || howmany<1);
  50. for(int j=0;j<howmany;j++)
  51. {
  52. printArray(seats);
  53. do
  54. {
  55. //Asks the user on what row he would like to book a seat
  56. System.out.println("Where would person number "+(j+1)+" like to sit?");
  57. do
  58. {
  59. System.out.print("Row: ");
  60. row=reader.nextInt();
  61. //If the user choses a nonexistent row
  62. if(row>5)
  63. {
  64. System.out.println("There are only 5 rows, please choose a new row.");
  65. }
  66. }
  67. while(row>5);
  68. //Asks what seat the user would like.
  69. do
  70. {
  71. System.out.print("Seat: ");
  72. name=reader.next();
  73. //If the user choses a nonexistent seat
  74. if(!name.equals("1") && !name.equals("2") && !name.equals("3") &&
  75. !name.equals("4") && !name.equals("5") && !name.equals("6"))
  76. {
  77. System.out.println("There are only 6 seats per row, please choose a new seat.");
  78. }
  79. }
  80. while(!name.equals("1") && !name.equals("2") && !name.equals("3") &&
  81. !name.equals("4") && !name.equals("5") && !name.equals("6"));
  82. }
  83. while(row>5);
  84. for(int i=0;i<6;i++)
  85. {
  86. if(seats[row-1][i].getName().equalsIgnoreCase(name))
  87. {
  88. //If the seat is already taken, it informs the user about it
  89. if(seats[row-1][i].getVisible()==false)
  90. {
  91. System.out.println("\nThat seat is already taken, please select another one.\n");
  92. j--;
  93. }
  94. //If the seat is free, the program books it and marks it as taken.
  95. else
  96. {
  97. seats[row-1][i].setVisible(false);
  98. count++;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. break;
  105.  
  106. case 2:
  107. printArray(seats);
  108. break;
  109.  
  110. case 0:
  111. System.out.println("Good bye!");
  112. break;
  113.  
  114. default:
  115. //Informs the user that the input is not valid
  116. System.out.println("Invalid input.");
  117. break;
  118. }
  119. }
  120. while(input != 0);
  121. }
  122.  
  123. //This method prints the current seating situation
  124. public static void printArray(Seat[][] a)
  125. {
  126. //Prints the rows
  127. System.out.println("-SCREEN-");
  128. for(int i=0;i<5;i++)
  129. {
  130. System.out.print(i+1+" ");
  131. //Prints the seats
  132. for(int j=0;j<6;j++)
  133. {
  134. System.out.print(a[i][j]);
  135. }
  136. System.out.print(" ");
  137. System.out.println();
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement