The problem is described as follows:And for this problem I decided to have a Seat class to provide a 2D Seat array in driver and make use of it. Actually there is no big problem, I nearly implemented all the requirements but there is only one problem within giving a message to the user that "it is taken" when a previously taken seat is chosen. I do not understand what the problem is with this code, can anybody help me? Seat and Airplane(driver) classes that I wrote is below:Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
After displaying the seats available, the program should prompt for the seat desired, the user can type in a seat, and then the display of available seats should be updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another seat.
Code:public class Seat { private boolean visible; private String name; public Seat(String name) { visible=true; this.name=name; } public void setVisible(boolean b){ visible=b; } public String getName(){ return name; } public boolean getVisible(){ return visible; } public String toString(){ if(visible) return name; else name="X"; return name; } }Code:import java.util.*; public class Airplane { public static void main (String[] args) { Scanner scan=new Scanner(System.in); Seat[][]array=new Seat[7][4]; Seat desiredSeat; int count=0; int row; String name; for (int i = 0; i<7; i++){ array[i][0]=new Seat("A"); array[i][1]=new Seat("B"); array[i][2]=new Seat("C"); array[i][3]=new Seat("D"); } do{ do{ printArray(array); System.out.println("Which place do you want to take: "); row=scan.nextInt(); name=scan.next(); }while(row>7); for(int i=0;i<4;i++){ if(array[row-1][i].getName().equalsIgnoreCase(name)){ if(array[row-1][i].getVisible()==false){ System.out.println("The seat is already taken, please select another one."); } else{ array[row-1][i].setVisible(false); count++; } } } }while(count!=28); printArray(array); System.out.println("All seats have been sold!!!"); } public static void printArray(Seat[][] a){ for(int i=0;i<7;i++){ System.out.print(i+1+" "); for(int j=0;j<4;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }


LinkBack URL
About LinkBacks




Reply With Quote

Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum