Hi everybody,

Good morning...need to ask some question..

Below is my code for the cinema booking system.

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

public class CinemaBooking
{
final int rows = 7, cols = 4;
char[][] seats = new char[rows][cols];
ArrayList<String> reservedSeats = new ArrayList<String>();

public static void main(String[] args) 
{
CinemaBooking q = new CinemaBooking();
q.buildSeats();
q.printSeats();
System.out.println("Enter Seat numbers:");
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);
q.reserveSeat(row, col);
}
}

public void buildSeats() 
{
char seatLetter = 'A';
for (int i = 0; i < seats.length; i++) 
{
for (int j = 0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A';
}
}

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 ");
}
else
System.out.println("Sorry! The Seat "+seatNo+" is NOT available.Please look up for 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;
}
}

How can I alter the code so that the user can continue when enter his/her selection or exit(user choose to exit or all the seats are chosen)? What code and where should i put it? For example when the user enter their choices, the user can choose (0) to exit or (1) to continue. So if the user chosen to continue, it will loop back into the code. Else, the program will exit. And if all the seats are chosen, the program should exit.
Looking forward to your reply..
Thanks so much in advance for all the valuable comments.

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

Generally speaking, it should be a loop in main() that is outside your current loop. That is, you enter the main loop to get the user's choice (1= reserve, 0 = exit). If the user enters a 1, you then get the input for the row and column and check the availability. If, when you get to the top of the (new) outer loop, the number of reserved seats (i.e., size of your "reservedSeats" array) equals the total number of seats, you can just exit.

This article has been dead for over six months. Start a new discussion instead.