I'm given an assignment where the user enters the number of seats they would like to book for a trip. After, I set an array of the seats all to false, as they are empty. Then depending on the number of seats the user wants to reserve, the program will output a boarding pass with the seat number. And the output for the boarding passes cannot have the same seat numbers, as they are either empty or not.
My code is working great except for the little snippet you see below. As you see I set all the seats for the club and economy classes to false. Then I prompt the user to enter the seats they would like to reserve. The issue I'm having is at the bottom of the code, since there are 5 club seats, if I wanted to reserve 5 club seats, it should output the 5 boarding passes with the distinct seat numbers. But for some reason it doesn't output all 5 seats, for example, if I enter that I would like 5 seats, and that they are all club seats, my program outputs:
Club Pass: Seat 1
Club Pass: Seat 1
Club Pass: Seat 3
Club Pass: Seat 2
Club Pass: Seat 1
I'm new to arrays and I know that subscripts begins at 0, I was thinking that maybe I needed to subtract 1 from the Math.random line below. I understand the issue is most likely regarding the final snippet of the code and the Math.Random line but I'm not sure what is exactly causing this error.**
import java.util.Scanner;
public class MyClass
{
public static void main(String args[])
{
Scanner input = new Scanner (System.in);
System.out.println ("Welcome to the new bullet train from Beefalo Bay Transportation Company.");
System.out.println ("Our new bullet train has 20 seats, 5 club, and 15 economy seats.");
System.out.println ("Would you like to make a booking? If so enter true, if not enter false.");
boolean wantToReserve = input.nextBoolean();
while (wantToReserve == false)
{
System.out.println ("You entered false. Would you like to make a reservation?");
wantToReserve = input.nextBoolean();
if (wantToReserve == false)
{
System.out.println ("You may close the program.");
}
}
System.out.println ("How many seats would you like to book?");
System.out.println ("Please note we only have 20 seats, 5 club and 15 economy.");
System.out.println ("If you would like more than 20 seats, please execute the program multiple times.");
int numberOfSeats = input.nextInt();
while (numberOfSeats > 20 | numberOfSeats <= 0){
System.out.println ("You entered a value greater than 20 or less than 0.");
System.out.println ("You must modify the seats you would like to book, as we only have 20 seats.");
System.out.println ("If you would like more than 20 seats, please execute the program multiple times.");
numberOfSeats = input.nextInt();
}
boolean clubSeating [] = {false, false, false, false, false};
boolean economySeating [] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
System.out.println ("How many club seats would you like?");
int numberOfClub = input.nextInt();
System.out.println ("How many economy seats would you like?");
int numberOfEconomy = input.nextInt();
while (numberOfClub + numberOfEconomy != numberOfSeats || numberOfEconomy > 15 || numberOfClub > 5 || numberOfEconomy < 0 && numberOfClub < 0 ){
System.out.println ("At least one of the previous inputs were not allowed. Note that we have 5 club and 15 economy seats. Please re-enter.");
System.out.println ("How many club seats would you like?");
numberOfClub = input.nextInt();
System.out.println ("How many economy seats would you like?");
numberOfEconomy = input.nextInt();
}
for (int j = 0; j < numberOfClub ; j++){
int seatForClub1 = (int) (Math.random()*(numberOfClub));
clubSeating [seatForClub1] = true;
int seatForClub = (int) (Math.random()*(numberOfClub));
if (clubSeating [seatForClub] == false){
clubSeating [seatForClub] = true;
System.out.println ("Boarding Pass: Seat " + seatForClub);
}
if (clubSeating [seatForClub] == true){
seatForClub = (int) (Math.random()*(numberOfClub));
System.out.println ("Boarding Pass: Seat " + seatForClub);
}
}
}
}