Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I modify this code that if the user book the seat from the first loop then the seat taken from the first loop is not available in second loop ?

What I have tried:

import java.util.Scanner;

public class CinemaBooking {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int[] SeatNo = new int[100];

    int Seats;
    int YesOrNo = 1;
    String CustomerName;

    for(int i = 0; i < 12; i++){
    	SeatNo[i]=0;
    }
		System.out.print("Welcome to Crazy Cinema!\nWhat is your name?\n");
	      CustomerName = input.nextLine();

	      System.out.printf("Welcome %s! Please have a look at the seating plan.\n\n", CustomerName);
    while (YesOrNo == 1) {
      for (int i = 1; i <= 34; i++) {
        System.out.print("*");
      }
      System.out.println();

      System.out.print("      CINEMA 1 SEATING PLAN");
      System.out.println();

      for (int j = 1; j <= 34; j++) {
        System.out.print("*");
      }
      System.out.println();

      for (int SeatCounter = 0; SeatCounter < SeatNo.length; SeatCounter++) {
        System.out.printf(SeatCounter + "\t");

		if( (SeatCounter+1) % 10 == 0 ){
			System.out.println();
		}
      }
      for (int k = 1; k <= 34; k++) {
        System.out.print("*");
      }
      System.out.println();

      System.out.print("Which seat would you like to book? ");
      Seats = input.nextInt();
      int[] takenSeats = {};
      takenSeats.

      while (Seats < 0 || Seats > 100) {
        System.out.println("Only 1 - 100 seats are allowed to book. Please try again: ");
        Seats = input.nextInt();
      }

      for (int SeatCounter = 0; SeatCounter < SeatNo.length; SeatCounter++) {
        if (SeatCounter == Seats) {
          System.out.println("Seat " + Seats + " is successfully booked.");
          System.out.println(
              "Thanks for booking!\n\nWould you like to make next booking? (Type 1 = Yes; Type 2 = No)");
          YesOrNo = input.nextInt();

          if (YesOrNo == 2) {
            System.out.println("Thank you for using this program.");
          }
        }
      }

      while (YesOrNo != 1 && YesOrNo != 2) {
        System.out.println("Invalid input.");
        System.out.println("Type 1 = Continue booking; Type 2 = Exit the program");
        YesOrNo = input.nextInt();

        if (YesOrNo == 2) {
          System.out.println("Thank you for using this program.");
        }
      }
    }
  }
}
Posted
Updated 9-May-18 20:47pm
v2

1 solution

Create a list of available seats. Every time you assign one, remove it from the list, as it's no longer available.
 
Share this answer
 
Comments
Richard MacCutchan 10-May-18 4:32am    
Hey Christian, nice to see you are still around.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900