3

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");
        }
    }
ekeith
  • 644
  • 1
  • 10
  • 33
  • Your if statement on the second line will say `No seats available` after the first booking. You have to check whether the whole array is full, not just the the specific value. Add the statement specified here to fix that problem: [here](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – DarkHorse Oct 09 '15 at 00:14
  • What do you mean with `if(seats.equals(null) > passengerGroup)` in your pseudo-code? It would not compile (and by [definition](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals%28java.lang.Object%29), `anything.equals(null)` is always `false`). – Mick Mnemonic Oct 09 '15 at 00:16
  • Whats the problem here? The algorithm/steps are explained very well in the explanation you provided. You just need to translate that to java code. – aleksandar Oct 09 '15 at 00:17
  • I was just trying to understand the questions properly. I'm still trying to get ahold of this question & java properly @MickMnemonic. – ekeith Oct 09 '15 at 00:18
  • The problem is I can't translate it to java properly. Perhaps, you can help translate it algorithmically? @wazaaaap The question gets even more confusing to me in the first two steps because i'm still learning the language – ekeith Oct 09 '15 at 00:21
  • In for loops iterator index variable names have to be different. – sam Oct 09 '15 at 00:22
  • @girlCoder, really, you simply need to fire up your IDE and _start coding_. When you run into compilation / run-time problems, you can post a more specific question and will be assisted by the community. – Mick Mnemonic Oct 09 '15 at 00:22
  • I have a problem understanding the first two steps especially searching the arrays consecutively, @MickMnemonic – ekeith Oct 09 '15 at 00:27
  • Any contribution on how to get through would be appreciated, thanks – ekeith Oct 09 '15 at 00:29
  • You could do this: Define two counter variables. Counting `int emptySeats`, you loop through `seats` and simply increment it for each `if(seats[i] == null)`. For `int emptyConsecutiveSeats`, you also need to loop through `seats`, but break out when you find an occupied seat; you can declare `boolean previousSeatEmpty = true;` which you use in the looping condition: `for(int i =0; i – Mick Mnemonic Oct 09 '15 at 00:45
  • For the random seat assignment part, you could also declare an `int[] availableSeats` array (or better, a `List` if you can use dynamic data structures) which keeps track of array indices of `seats` which are available (and which you populate while looping). Then you could use `java.util.Random` for selecting a seat from `availableSeats` randomly. – Mick Mnemonic Oct 09 '15 at 00:50
  • that's what I used to write the code just that i used a method and I used the math.random function instead since I've only learnt up to that level. Can you use it in the comment section instead? Thanks :) @MickMnemonic – ekeith Oct 09 '15 at 01:00
  • Yes, `Math.random()` internally uses `java.util.Random` so it's equivalent (but with less functionality). One final note: you will be better off modeling `seats` with a `boolean` array (`boolean[] seats`) because that way the value of `seat[i]` could explicitly indicate whether the seat is occupied (`true`) or available (`false`). If you do it this way, you need to check for `if (!seats[i])` instead of `if(seats[i] == null)` for seat availability within the loop. Good luck! – Mick Mnemonic Oct 09 '15 at 01:06

1 Answers1

0

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");
}

This is probably a little too much help, assuming it's homework, but it still needs some work so I hope it sets you on the right path. Edit: actually re-reading the question, this is probably a little off what you want, so hopefully it'll just give you an idea of how to start.

Richard Dunn
  • 6,165
  • 1
  • 25
  • 36
  • Thanks for your suggestion, I'm learning Java on my own and I picked the question from a past college test online :) – ekeith Oct 09 '15 at 01:02