Search code examples
javaarraysrandom

How to randomly seat groups of passengers in an airline in java? Step-by-Step


I'm new to Java and I have this program i'm working on about seating groups of passengers in an airline. ( I would try to make my question simple and easy to understand)

So, I am supposed to seat passengers in an airline in groups. I get the information on how they are being seated by a booking class.

public class Bookings {
public Bookings() {
}

public static void main(String[] var0) {
    System.out.println("Do not run this program.");
    System.out.println("Call the method as follows:");
    System.out.println("  String[] bookings = Bookings.getBookings()");
}

    public static String[] getBookings() {
        return new String[]{"38", "2", "Gurganus, Erik", "Gurganus, Fernando", "1", "Cahn, Lance", "1", "Burrough, Jamie", "3", "Riney, Christian", "Marceau, Hillary", "Marceau, Julio", "3", "Gariepy, Noemi", "Gariepy, Louisa", "Gariepy, Nelson", "2", "Mazzoni, Max", "Fiorita, Tyrone", "3", "Ehle, Clinton", "Minnifield, Clinton", "Blinn, Jamie", "2", "Sokolowski, Kurt", "Sokolowski, Sofia", "2", "Secord, Hugh", "McVeigh, Karina", "1", "McMonagle, Christian", "1", "Canchola, Clayton", "2", "Duer, Julio", "Danos, Ted", "3", "Regal, Christian", "Mun, Allan", "Mun, Lakisha", "2", "Noblitt, Karina", "Tussey, Clayton", "1", "Seckman, Jamie", "2", "Folmar, Edwina", "Lokey, Clayton", "2", "Pippen, Javier", "Saba, Earlene", "4", "Tippit, Lorrie", "Tippit, Harriett", "Tippit, Clare", "Tippit, Lance", "3", "Mazurek, Mallory", "Mazurek, Stefan", "Mazurek, Ihor", "2", "Saini, Amie", "Peavler, Darcy"};
    }
   }

with this information I created a String array called seats and a bookings array that calls the class to retrieve all the information in it

String[] seats = new String[38];
String[] bookings = Bookings.getBookings();

The first value indicates the number of seats available on my flight and the remaining values indicates the numbers of groups to be assigned and their names.

For more clarification, the steps to follow for this procedure is:

Your program will seat passengers as follows: It will create a "seats" array of the appropriate size (given in the first >position of the "bookings" array). DONE

It will process the remaining items in the "bookings" array of strings. >For each group of passengers it will attempt to seat them as follows: DONE

  • First, it will see if there are enough seats remaining on the flight for everyone in the group; if not, it will display an error message and not assign seats to anyone in the group.
  • Secondly, it will go through the "seats" array to determine if a block of empty seats large enough to seat the entire group together is available (for example, if the group size is 3, it will see if there are 3 consecutive empty seats).

    If there is at least one such block of seats anywhere in the array, randomly assign the group to one of these blocks by randomly selecting an element in the "seats" array.

    If that seat is empty, determine if enough successive array elements (seats) are also empty to seat the entire group; if so, seat them there.

    Otherwise, randomly try another seat, repeating until successful.

    If there is no such block, randomly assign each passenger in the group individually to a seat (i.e., the group will be split up). For each passenger, pick random seat numbers until you find an empty seat.

My problem now are these two steps, I wrote this step for solving it down on a sheet of paper but i'm not sure whether or not i'm on the right track. Any contribution/help would be very much appreciated. I can provide more information on the problem if needed.

for(int i =0; i<seats.length; i++) {
        if(seats[i] == null) {
            for(int i =0; i<seats.length; i++) {
                if(seats.equals(null) > passengerGroup) {
                    (randomly seat them consecutively);
                } else {
                    (randomly seat them anywhere)
                }
            }
        } else {
            system.out.println( "No seats available");
        }
    }

Solution

  • Break down the problem before writing loops; counters are the easiest way to keep track of availability in this situation.

    public static void main(String[] args) {
        
        int numSeats = 20;
        int available = numSeats;
        int numGroups = 6;
        
        int groupSizes[] = new int[numGroups];
        
        groupSizes[0] = 2;
        groupSizes[1] = 1;
        groupSizes[2] = 5;
        groupSizes[3] = 3;
        groupSizes[4] = 10;
        groupSizes[5] = 2;
        
        for (int i=0; i < groupSizes.length; i++) {
            
            if (available > groupSizes[i]){
                available -= groupSizes[i];
                System.out.println("Group: " + i + " has been book on!");
            }
            
            else {
                System.out.println("Not enough seats for group: " + i);
            }
            
        }
        System.out.println(available + " Seats remaining");
    }