Jaszek

Untitled

Apr 18th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.*;
  2. public class planeSeating {
  3.     public static void main(String args[]) {
  4.       int i, j;
  5.       int seatType, seatRow, seatCol;
  6.       int count = 0;                                        //Count used to count up all the taken seats
  7.       Scanner in = new Scanner(System.in);
  8.       char[][] plane = new char[13][6];
  9.       for(i=0; i<plane.length; i++){                    //Filling the array with all * for graphical purposes
  10.         for(j=0; j<plane[0].length; j++){
  11.             plane[i][j]='*';
  12.         }
  13.       }
  14.       output(plane);                                        //Printing the seat map
  15.      
  16.       do{
  17.       System.out.print("\nWhich class would you like?");
  18.       System.out.print("\n1 for First Class \n2 for Economy\n0 to Exit\nYour Choice: ");
  19.       seatType = in.nextInt();
  20.       if(seatType==1){                                  //First Class
  21.             seatRow=rowIn(in);                          //Choosing Row
  22.             while(seatRow>1){                               //Checking if First Class rows are chosen
  23.                 System.out.print("\nWrong class");
  24.                 seatRow=rowIn(in);}
  25.             seatCol=colIn(in);                          //Choosing seat
  26.             if(plane[seatRow][seatCol]!='X'){       //Testing if seat is taken
  27.                 plane[seatRow][seatCol]='X';            //and marking seat as taken if empty
  28.                 count++;                                        //and printing seat map
  29.                 output(plane);}
  30.             else                                                //Error if seat is taken
  31.                 System.out.print("\nSeat taken, please try again");
  32.         }
  33.         else if(seatType==2){                           //Economy Class
  34.             seatRow=rowIn(in);                          //Choosing Row
  35.             while(seatRow<2){                               //Checking if Economy Class rows are chosen
  36.                 System.out.print("\nWrong class");
  37.                 seatRow=rowIn(in);}
  38.             seatCol=colIn(in);                          //Choosing seat
  39.             if(plane[seatRow][seatCol]!='X'){       //Testing if seat is taken
  40.                 plane[seatRow][seatCol]='X';            //and marking seat as taken if empty
  41.                 count++;                                        //and printing seat map
  42.                 output(plane);}
  43.             else                                                //Error if seat is taken
  44.                 System.out.print("\nSeat taken, please try again");
  45.         }
  46.         if(count == 76){                                                        //Checking count of all the seats
  47.             System.out.println("\n\nAll seats taken, sorry");       //If all are taken, the program exits
  48.             seatType=0;}
  49.              
  50. }while(seatType!=0);
  51. }
  52. public static void output(char[][] plane){
  53. int i, j;
  54. System.out.printf("        A B C   D E F");
  55.       for(i=0; i<plane.length; i++){
  56.        System.out.printf("\nRow "+ "%2s ", (i+1));
  57.         for(j=0; j<plane[0].length; j++){
  58.             if(j == 6)                                                  //Next line after 6 columns
  59.                 System.out.println(plane[i][j]);
  60.             else if(j==3)                                               //Space between the middle seats
  61.                 System.out.printf("%4s", plane[i][j]);
  62.             else
  63.                 System.out.printf("%2s", plane[i][j]);
  64.         }
  65. }
  66. }
  67. public static int rowIn(Scanner in)
  68. {
  69.     System.out.println("\nWhich row would you like?");
  70.     int row=in.nextInt();
  71.     while(row<1 || row>13){                                     //Error checking of the row is on the plane
  72.         System.out.print("\nInvalid selection. Please try again: ");
  73.         row=in.nextInt();}
  74.     return (row-1);
  75. }
  76. public static int colIn(Scanner in)
  77. {
  78.     System.out.println("\nWhich seat would you like?");
  79.     System.out.println("1=A|2=B|3=C|4=D|5=E|6=F|");
  80.     int col=in.nextInt();
  81.     while(col<1 || col>6){                                      //Error checking of the row is on the plane
  82.         System.out.print("\nInvalid selection. Please try again: ");
  83.         col=in.nextInt();}
  84.     return (col-1);
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment