Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi, I need help with the Java program for reservation of seat. It's ruunning but it's not prompting the Enter menu number: first. At the
Hi,
I need help with the Java program for reservation of seat. It's ruunning but it's not prompting the "Enter menu number:" first. At the same time, it should be able to show me the available seats and those that have been reserved.
import java.util.Scanner; public class SeatReservation_Salvatierra { public static String[][] seats=new String[5][7]; public static Scanner scan=new Scanner(System.in); public static boolean userContinue=true; public static void populate(){ int num=1; for(int i=0; i<5; i++){ for(int j=0; j<7; j++) seats[i][j]= num++ + "-O"; } } public static void selectSeat(){ int seatNum=1; do{ System.out.print("Enter seat number to reserve. Enter 0 to go back to the Main Menu: "); seatNum=scan.nextInt(); scan.nextLine(); while( isTaken(seatNum) || !isValidSeatNum(seatNum)){ if(seatNum==0) break; if(isTaken(seatNum)) System.out.println("Seat is taken."); else if(!isValidSeatNum(seatNum)) System.out.println("Seat Number is not valid."); System.out.print("Enter seat number to reserve. Enter 0 to go back to the Main Menu: "); seatNum=scan.nextInt(); scan.nextLine(); } takeSeat(seatNum); }while(seatNum!=0); } public static boolean isTaken(int seatNum){ boolean taken=false; for(int row=0; row<5; row++){ for(int col=0; col<7; col++){ String[] seat=seats[row][col].split("-"); if(Integer.parseInt(seat[0])==seatNum && seat[1].equals("X")){ taken=true; break; } } } return taken; } public static boolean isValidSeatNum(int seatNum){ boolean valid=false; for(int row=0; row<5; row++){ for(int col=0; col<7; col++){ String[] seat=seats[row][col].split("-"); if(Integer.parseInt(seat[0])==seatNum){ valid=true; break; } } } return valid; } public static void takeSeat(int seatNum){ for(int row=0; row<5; row++){ for(int col=0; col<7; col++){ String[] seat=seats[row][col].split("-"); if(Integer.parseInt(seat[0])==seatNum){ seats[row][col]= seatNum + "-X"; } } } System.out.println(" Seat successfully reserved"); } public static void displaySeats(){ System.out.println(" \tAll Seats ( O = Vacant, X = Taken)"); for(int i=0; i<5; i++){ for(int j=0; j<7; j++){ System.out.print(seats[i][j]+"\t"); } System.out.println(); } System.out.println(); } public static void main(String[] args) { populate(); while(userContinue){ System.out.println("1: Reserve seat"); System.out.println("2: Display seat order"); System.out.println("3: Exit"); System.out.print(" Enter menu number: "); int choice=scan.nextInt(); scan.nextLine(); switch(choice){ case 1: selectSeat(); break; case 2: displaySeats(); break; case 3: System.out.println("Exiting Program"); System.out.println("Thank you."); userContinue=false; break; default: System.out.println("Invalid input. Please Try again."); break; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started