Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class planeSeating {
- public static void main(String args[]) {
- int i, j;
- int seatType, seatRow, seatCol;
- int count = 0; //Count used to count up all the taken seats
- Scanner in = new Scanner(System.in);
- char[][] plane = new char[13][6];
- for(i=0; i<plane.length; i++){ //Filling the array with all * for graphical purposes
- for(j=0; j<plane[0].length; j++){
- plane[i][j]='*';
- }
- }
- output(plane); //Printing the seat map
- do{
- System.out.print("\nWhich class would you like?");
- System.out.print("\n1 for First Class \n2 for Economy\n0 to Exit\nYour Choice: ");
- seatType = in.nextInt();
- if(seatType==1){ //First Class
- seatRow=rowIn(in); //Choosing Row
- while(seatRow>1){ //Checking if First Class rows are chosen
- System.out.print("\nWrong class");
- seatRow=rowIn(in);}
- seatCol=colIn(in); //Choosing seat
- if(plane[seatRow][seatCol]!='X'){ //Testing if seat is taken
- plane[seatRow][seatCol]='X'; //and marking seat as taken if empty
- count++; //and printing seat map
- output(plane);}
- else //Error if seat is taken
- System.out.print("\nSeat taken, please try again");
- }
- else if(seatType==2){ //Economy Class
- seatRow=rowIn(in); //Choosing Row
- while(seatRow<2){ //Checking if Economy Class rows are chosen
- System.out.print("\nWrong class");
- seatRow=rowIn(in);}
- seatCol=colIn(in); //Choosing seat
- if(plane[seatRow][seatCol]!='X'){ //Testing if seat is taken
- plane[seatRow][seatCol]='X'; //and marking seat as taken if empty
- count++; //and printing seat map
- output(plane);}
- else //Error if seat is taken
- System.out.print("\nSeat taken, please try again");
- }
- if(count == 76){ //Checking count of all the seats
- System.out.println("\n\nAll seats taken, sorry"); //If all are taken, the program exits
- seatType=0;}
- }while(seatType!=0);
- }
- public static void output(char[][] plane){
- int i, j;
- System.out.printf(" A B C D E F");
- for(i=0; i<plane.length; i++){
- System.out.printf("\nRow "+ "%2s ", (i+1));
- for(j=0; j<plane[0].length; j++){
- if(j == 6) //Next line after 6 columns
- System.out.println(plane[i][j]);
- else if(j==3) //Space between the middle seats
- System.out.printf("%4s", plane[i][j]);
- else
- System.out.printf("%2s", plane[i][j]);
- }
- }
- }
- public static int rowIn(Scanner in)
- {
- System.out.println("\nWhich row would you like?");
- int row=in.nextInt();
- while(row<1 || row>13){ //Error checking of the row is on the plane
- System.out.print("\nInvalid selection. Please try again: ");
- row=in.nextInt();}
- return (row-1);
- }
- public static int colIn(Scanner in)
- {
- System.out.println("\nWhich seat would you like?");
- System.out.println("1=A|2=B|3=C|4=D|5=E|6=F|");
- int col=in.nextInt();
- while(col<1 || col>6){ //Error checking of the row is on the plane
- System.out.print("\nInvalid selection. Please try again: ");
- col=in.nextInt();}
- return (col-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment