194k views
3 votes
Seat booking program in java

1 Answer

1 vote

Answer:

import java.util.Scanner;

public class SeatBookingProgram {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int rows = 5;

int cols = 10;

boolean[][] seats = new boolean[rows][cols];

int availableSeats = rows * cols;

// Initialize all seats as available

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

seats[i][j] = true;

}

}

while (availableSeats > 0) {

// Print the seating chart

System.out.println("Seating Chart:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if (seats[i][j]) {

System.out.print("O ");

} else {

System.out.print("X ");

}

}

System.out.println();

}

// Prompt the user to select a seat

System.out.print("Select a seat (row, column): ");

int selectedRow = input.nextInt();

int selectedCol = input.nextInt();

// Check if the seat is available and book it if so

if (seats[selectedRow - 1][selectedCol - 1]) {

seats[selectedRow - 1][selectedCol - 1] = false;

availableSeats--;

System.out.println("Seat booked successfully.");

} else {

System.out.println("Seat already booked. Please select another seat.");

}

}

System.out.println("All seats have been booked.");

}

}

Step-by-step explanation:

In this program, the seating chart is represented as a 2D boolean array where true indicates that a seat is available and false indicates that a seat is booked. The program initializes all seats as available, then enters a loop where it displays the seating chart, prompts the user to select a seat, and books the selected seat if it is available. The loop continues until all seats have been booked. At the end, the program displays a message indicating that all seats have been booked.

User Old Nick
by
8.5k points