Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. // -----------------------------------------------------//
  2. // Name:
  3. // Week 2 Discussion 1
  4. //
  5. // Author:
  6. // James J. Boyer
  7. //
  8. // Purpose: Books airline seats and outputs a seating
  9. // chart when full or when user quits
  10. //
  11. // Date Last Modified:
  12. // 2015-06-08 @ 10:29 PM
  13. //
  14. // Class:
  15. // CSC 262 June 2015
  16. // -----------------------------------------------------//
  17.  
  18. import java.util.Scanner;
  19. import java.util.*;
  20.  
  21. public class Plane
  22. {
  23. // checks customers in and assigns them a boarding pass
  24. // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers
  25. //
  26. public void reserveSeats()
  27. {
  28. int counter = 0;
  29. int section = 0;
  30. int choice = 0;
  31. String eatRest = ""; //to hold junk in input buffer
  32. String inName = "";
  33. String name = "";
  34. boolean goOn = true ;
  35. List<String> firstClassPassengers = new ArrayList<String>();
  36. List<String> economyClassPassengers = new ArrayList<String>();
  37.  
  38. Scanner input = new Scanner( System.in );
  39.  
  40. String[] passenger = new String[ 5 ]; // array of seats
  41. int firstClass = 0; // next available first class seat
  42. int economy = 2; // next available economy seat
  43.  
  44.  
  45. while ( (goOn == true) && ( ( firstClass < 2 ) || ( economy < 5 ) ))
  46. {
  47.  
  48. System.out.println( "Please type 1 for First Class" );
  49. System.out.println( "Please type 2 for Economy" );
  50. System.out.println( "Please type 3 to quit");
  51. System.out.print( "choice: " );
  52. section = input.nextInt();
  53. eatRest = input.nextLine(); //get any end of line character remaining in the buffer to prepare for the next nextLine()
  54. System.out.println(" ");
  55.  
  56. switch (section)
  57. {
  58. case 1: //user chose first class
  59.  
  60.  
  61. if ( firstClass < 2 ) //if first class available
  62. {
  63. System.out.print("Please enter your name: ");
  64. inName = input.nextLine();
  65. name = inName.trim();
  66. passenger[firstClass] = name;
  67.  
  68. firstClass = firstClass + 1;
  69. System.out.printf( "First Class. Seat #%d for %s\n", firstClass, name);
  70.  
  71. // Add to the economy class list
  72. firstClassPassengers.add("Seat "+firstClass+"\t\t"+name);
  73.  
  74. } // end if
  75.  
  76. else //first class is full
  77. {
  78.  
  79. if ( economy < 5 ) // but if economy class is available
  80. {
  81. System.out.println("First Class is full, Economy Class?" );
  82. System.out.print( "1. Yes, 2. No. Your choice: " );
  83. choice = input.nextInt();
  84. eatRest = input.nextLine();
  85.  
  86. if ( choice == 1 )
  87. {
  88. System.out.print("Please enter your name: ");
  89. inName = input.nextLine();
  90. name = inName.trim();
  91.  
  92. passenger[economy] = name;
  93.  
  94. economy = economy + 1;
  95. System.out.printf( "Economy Class. Seat #%d for %s\n", economy, name );
  96.  
  97. // Add to the economy class list
  98. economyClassPassengers.add("Seat "+economy+"\t\t"+name);
  99.  
  100. }
  101. else
  102. System.out.println( "Sorry. The next flight leaves in 3 hours." );
  103.  
  104. } // end if (economy < 5)
  105.  
  106. } // end else
  107.  
  108. break;
  109.  
  110.  
  111. case 2: //user chose economy
  112.  
  113. if ( economy < 5 ) //if economy class is available
  114. {
  115. System.out.print("Please enter your name: ");
  116. inName = input.nextLine();
  117. name = inName.trim();
  118.  
  119. passenger[economy] = name;
  120.  
  121. economy = economy + 1;
  122. System.out.printf( "Economy Class. Seat #%d for %s\n", economy, name );
  123.  
  124. // Add to the economy class list
  125. economyClassPassengers.add("Seat "+economy+"\t\t"+name);
  126.  
  127. } // end if (economy < 5)
  128.  
  129. else //economy class is full
  130. {
  131.  
  132. if ( firstClass < 2 ) // but if first class available
  133. {
  134. System.out.println("Economy Class is full, First Class?" );
  135. System.out.print( "1. Yes, 2. No. Your choice: " );
  136. choice = input.nextInt();
  137. eatRest = input.nextLine();
  138.  
  139. if ( choice == 1 )
  140. {
  141. System.out.print("Please enter your name: ");
  142. inName = input.nextLine();
  143. name = inName.trim();
  144.  
  145. passenger[firstClass] = name;
  146.  
  147. firstClass = firstClass + 1;
  148. System.out.printf( "First Class. Seat #%d for %s\n", firstClass, name );
  149. //Add to the economy class list
  150. economyClassPassengers.add("Seat "+economy+"\t\t"+name);
  151. } // end if
  152.  
  153. else
  154.  
  155. System.out.println( "Next flight leaves in 3 hours." );
  156.  
  157. } // if (firstClass < 2)
  158. } // end else
  159.  
  160. break;
  161.  
  162. case 3: //user chose to quit
  163.  
  164. System.out.println("OK");
  165. goOn = false;
  166. break;
  167.  
  168. default:
  169.  
  170. System.out.println("1 or 2 only. Please try again.");
  171. break;
  172.  
  173. }// end switch
  174.  
  175.  
  176. System.out.println();
  177. } // end while
  178.  
  179.  
  180. if ((firstClass > 1) && (economy > 4))
  181.  
  182. System.out.println( "The plane is now full." );
  183.  
  184. // Make sure the Passenger list has a value before printing it
  185. if(!firstClassPassengers.isEmpty())
  186. {
  187. for(int z =0; z <= firstClassPassengers.size()-1; z++)
  188. {
  189. System.out.print(firstClassPassengers.get(z) + "\n");
  190. }
  191. }
  192.  
  193. // Make sure the Passenger list has a value before printing it
  194. if(!economyClassPassengers.isEmpty())
  195. {
  196. for(int z =0; z <= economyClassPassengers.size()-1; z++)
  197. {
  198. System.out.print(economyClassPassengers.get(z) + "\n");
  199. }
  200. }
  201. System.out.println();
  202. System.out.println("Bye");
  203.  
  204. } // end method reserveSeats()
  205. } // end class Plane
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement