Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Airline
  4. {
  5. boolean[] seating = new boolean[11]; /* create 10 seat numbers (array[0] will not be used). Empty seat indicated by false*/
  6. Scanner input = new Scanner(System.in);
  7.  
  8. public void start()
  9. {
  10. while ( true )
  11. {
  12. makeReservation();
  13. }
  14. }
  15.  
  16. public void makeReservation()
  17. {
  18. System.out.println("Please type 1 for First Class or 2 for Economy: ");
  19. int section = input.nextInt();
  20. if ( section == 1 )
  21. {
  22. firstClassSeat();
  23. }
  24. else
  25. {
  26. economySeat();
  27. }
  28. }
  29.  
  30. public void firstClassSeat() // assign a first class seat
  31. {
  32. for ( int count = 1; count <= 5; count++ )
  33. {
  34. if ( seating[count] == false ) // if false, then a seat is available for assignment
  35. {
  36. seating[count] = true; // assign seat
  37. System.out.printf("First Class. Seat# %dn", count);
  38. break;
  39. }
  40. else if ( seating[5] == true ) // If seating[5] is true then first class is fully booked
  41. {
  42. if ( seating[10] == true) // If seating[10] is true then economy (and therefore whole flight) is fully booked
  43. {
  44. System.out.println("Sorry, flight fully booked. Next flight is in 3 hours.");
  45. }
  46. else // ask passenger if they would like an economy ticket instead
  47. {
  48. System.out.println("First Class is fully booked. Would you like Economy? 1 for Yes 2 for No");
  49. int choice = input.nextInt();
  50. if ( choice == 1 )
  51. {
  52. economySeat();
  53. start();
  54. }
  55. else
  56. {
  57. System.out.println("Next flight is in 3 hours.");
  58. System.exit(0);
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
  65. public void economySeat() // assign an economy seat
  66. {
  67. for ( int count = 6; count <= 10; count++ )
  68. {
  69. if ( seating[count] == false ) // if false, then a seat is available for assignment
  70. {
  71. seating[count] = true; // assign seat
  72. System.out.printf("Economy. Seat# %dn", count);
  73. break;
  74. }
  75. else if ( seating[10] == true ) // If seating[10] is true then economy is fully booked
  76. {
  77. if ( seating[5] == true) // If seating[5] is true then first class (and therefore whole flight) is fully booked
  78. {
  79. System.out.println("Sorry, flight fully booked. Next flight is in 3 hours.");
  80. System.exit(0);
  81. }
  82. else // ask if passenger would like a first class ticket instead
  83. {
  84. System.out.println("Economy is fully booked. Would you like First Class? 1 for Yes 2 for No");
  85. int choice = input.nextInt();
  86. if ( choice == 1 )
  87. {
  88. firstClassSeat();
  89. start();
  90. }
  91. else
  92. {
  93. System.out.println("Next flight is in 3 hours");
  94. System.exit(0);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101.  
  102. public enum SeatType {
  103. FIRSTCLASS, ECONOMY }
  104.  
  105. private Integer tryReservation(SeatType s) {
  106. Integer reservation=null;
  107. int start=(s==SeatType.FIRSTCLASS)?firstClassStart:economyStart;
  108. int stop=start+capacity;
  109. for(int number=start;number<stop;number+=1){
  110. if(seats[number]==free){
  111. seats[number]=!free;
  112. reservation=number;
  113. break;
  114. }
  115. }
  116. return reservation;
  117. }
  118.  
  119. private Integer makeReservation(SeatType s) {
  120. SeatType alternative=getAlternative(s);
  121. Integer reservedSeat=tryReservation(s);
  122. if(reservedSeat==null && !isClassFull(alternative)){
  123. if(alternativeWanted){
  124. reservedSeat=tryReservation(alternative);
  125. }
  126. }
  127. return reservedSeat;
  128. }
  129.  
  130. if ( !seating[count] ) // if false, then a seat is available for assignment... Notice the use of the negation operator instead of == false
  131. {
  132. seating[count] = true; // assign seat
  133. System.out.printf("First Class. Seat# %dn", count);
  134. break;
  135. }
  136. else if ( seating[5] ) // If seating[5] is true then first class is fully booked
  137. {
  138. if ( seating[10] )
  139.  
  140. //use this kind of comment as often as possible
  141.  
  142. while ( true )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement