Bus Seat Reservation
Please do exactly like this:
1. The program should ask the user to enter what row and seat number to reserve. Indicate
an ‘X’ mark if the seat is reserved.
2. Repeat Step 1; the program will stop until the user enters a negative number.
Please do exactly like this:
Screen/layout
Bus Seat Reservation:
Col 1 Col 2 Col 3 Col 4
row 1 | * * * *
row 2 | * * * *
row 3 | * * * *
row 4 | * * * *
row 5 | * * * *
row 6 | * * * *
row 7 | * * * *
up to row 10
Enter row and column no. to reserve separated by space (enter negative no. to exit): 4 4
Second Seat reservation sample output
Col 1 Col 2 Col 3 Col 4
row 1 | * * * *
row 2 | * * * *
row 3 | * * * *
row 4 | * * X X
up to row 10
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[][] seats = new int[10][4];
System.out.println("Bus Seat Reservation:");
while (true) {
System.out.println("Col 1 Col 2 Col 3 Col 4");
for (int i = 0; i < seats.length; i++) {
System.out.print("row " + (i + 1) + " | ");
for (int j = 0; j < seats[i].length; j++) {
System.out.print(seats[i][j] == 0 ? "* " : "X ");
}
System.out.println();
}
System.out.print("Enter row and column no. to reserve separated by space (enter negative no. to exit): ");
int row = in.nextInt();
int column = in.nextInt();
if (row < 0 || column < 0) {
break;
}
seats[row - 1][column - 1]++;
}
}
}
Comments
Leave a comment