-
Java Code Help
I have a Java assignment which states:
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 prompts for the seat desired, the user types in a seat, and then the display of available seats is 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 choice.
Everything works except that I can't get my program to exit when all fields have been inputed or the user decides to terminate the program.
Here is my code:
Code:
package airplane.seats;
import java.util.ArrayList;
import java.util.Scanner;
public class AirplaneSeats {
char[][] seats = new char[7][4];
ArrayList<String> reservedSeats = new ArrayList<>();
public static void main(String[] args) {
AirplaneSeats reservation = new AirplaneSeats();
reservation.buildSeats();
reservation.printSeats();
System.out.println();
int filled = 0;
System.out.println("Enter Seat number to assign (e.g. 1A or 1a) or a blank line to quit:");
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.next();
int row = Integer.parseInt(s.toUpperCase().substring(0, 1));
char col = s.toUpperCase().charAt(1);
reservation.reserveSeat(row, col);
while((filled < 28) && (scan != null)){
System.out.println("Thank you. Have a nice day!");
System.exit(0);
}
System.out.println("Please enter next seat number:");
}
}
public void buildSeats() {
for (int i = 0; i < 7; i++) {
seats[i][0] = 'A';
seats[i][1] = 'B';
seats[i][2] = 'C';
seats[i][3] = 'D';
}
}
public void printSeats() {
System.out.println("Available Seats:");
for (int i = 0; i < seats.length; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < seats[i].length; j++)
System.out.print(seats[i][j] + " ");
System.out.println();
}
}
public void reserveSeat(int row, char col) {
String seatNo=String.valueOf(row)+col;
if (checkAvailability(seatNo)) {
reservedSeats.add(seatNo);
for (int i = row - 1; i == row - 1; i++) {
for (int j = 0; j < seats[i].length; j++) {
if (seats[i][j] == col) {
seats[i][j] = 'X';
}
}
}
System.out.println("Seat " + seatNo + " is Reserved ");
System.out.println();
}
else
System.out.println("Sorry! The Seat "+seatNo+" is NOT available." +
"Please select another seat.");
printSeats();
}
public boolean checkAvailability(String seatNo) {
boolean available = true;
for(int i = 0; i < reservedSeats.size(); i++){
if(reservedSeats.get(i).equalsIgnoreCase(seatNo)){
available = false;
}
}
return available;
}
}
Any help would be appreciated. Thank You.
-
Re: Java Code Help
Look around the FAQs section of this site and find out how to use the code tags so that your code retains its formatting.
db