0

So I'm trying to build an airplane seating system. The particular function that I am having trouble is for group seating. The reservation program is supposed to find the first row of seats in a seat row that is sufficient to accommodate the group, or if no such seat row exists, finds the row with the largest number of available seats in any seat row, fills it up with members of the group, and repeats that process (finding the row with the largest number of available seats) until all members of the group have been seated. Here is what I've tried:

    public void addGroup(){
    System.out.print("Group Name: ");
    String group = sc.nextLine();
    System.out.print("Names:" );
    String names = sc.nextLine();
    System.out.print("Service Class: ");
    String serviceClass = sc.nextLine();    

    //int tempCount = count;
    String [] passengers = names.split(",");
    int numPassengers = passengers.length;
   // int count = 1;
    boolean foundFullSeating = false;
    boolean foundPartialSeating = false;

    int rowIndex = 0;
    int taken = 0;
    int emptySeats = 0;
    //add more
    int i;
    int rows = 2;
    int person = 0;
    int mostSeats = 0;
    int availableIndex = 0;

    if(serviceClass.equalsIgnoreCase("First")){
        for(String s: cabin.getFirstClass()){
            if(s == "X"){
                taken++;
            }
        }
        if(cabin.getFirstClass().size() - taken < numPassengers){
            System.out.println("Not enough seating available for group.");
            return;
        }

    if(foundFullSeating == false){

        for(i = rowIndex; i < rowIndex + 4; i++){
            System.out.println("i = " + i);
            System.out.println("cabin.getFirstClass().get(i) = " + cabin.getFirstClass().get(i));
            if(!cabin.getFirstClass().get(i).contains("X")){
                System.out.println("Found one!");
                emptySeats++;
            }
            System.out.println("emptySeats = " + emptySeats + "numPassengers = " + numPassengers);
            if(emptySeats == numPassengers){
                System.out.println("Everyone fits in one row!");
                foundFullSeating = true;
                foundPartialSeating = true;

            }
            if(i == 3){
                if(rows > 1){
                    emptySeats = 0;
                    i = 0;
                    rowIndex += 4; //moves on to the next row
                    rows--;
                }
            }

        }

    } //end trying to find seating for all passengers in one row

    if(foundPartialSeating == false){
        System.out.println("Line# 162");
        if(numPassengers != 0){
            rowIndex = 0; //start from the beginning
            emptySeats = 0;
            mostSeats = 0;
            availableIndex = 0;
            for(int k = rowIndex; k < rowIndex + 4; k++){
                if(cabin.getFirstClass().get(k) != "X"){
                    emptySeats++;
                }
                if(k == 3){
                    if(emptySeats > mostSeats){
                        mostSeats = emptySeats; 
                        availableIndex = rowIndex; //stores row index with most available seats
                    }

                    if(rows > 1){
                        emptySeats = 0;
                        i = 0;
                        rowIndex += 4; //moves on to the next row
                        rows--;
                    }

                }

            }
            for(int j = availableIndex; j < availableIndex + 4; j++){
                if(cabin.getFirstClass().get(j) != "X"){
                    cabin.getReservedFirst().set(j, cabin.getFirstClass().get(j) + ", G, " + passengers[person]); //adds the passengers seat
                    cabin.getFirstClass().set(j , "X"); // marks seat as taken
                    numPassengers--;
                    person++;
                    System.out.println("Seated at " + cabin.getReservedFirst().get(j));
                    if(numPassengers == 0){
                        foundPartialSeating = true; //seated everyone
                    }
                }
            }
        }
    }

    if(foundFullSeating){
        //System.out.println("rowIndex = " + rowIndex);
        //seat passengers
        for(int j = rowIndex; j < rowIndex + 4; j++ ){
            if(numPassengers > 0){
                //System.out.println("numPassengers = " + numPassengers);
                if(cabin.getFirstClass().get(j) != "X"){
                    cabin.getReservedFirst().set(j, cabin.getFirstClass().get(j) + ", G, " + passengers[person]); //adds the passengers seat
                    cabin.getFirstClass().set(j , "X"); // marks seat as taken
                    numPassengers--;
                    person++;
                    System.out.println("Seated at " + cabin.getReservedFirst().get(j));
                }
            }
        }
    }


    }

The first class has 8 seats (2 rows x 4 seats in each row). My debugging statements produce the following:

i = 0
cabin.getFirstClass().get(i) = 1A
Found one!
emptySeats = 1numPassengers = 3
i = 1
cabin.getFirstClass().get(i) = X
emptySeats = 1numPassengers = 3
i = 2
cabin.getFirstClass().get(i) = X
emptySeats = 1numPassengers = 3
i = 3
cabin.getFirstClass().get(i) = 1D
Found one!
emptySeats = 2numPassengers = 3
i = 1
cabin.getFirstClass().get(i) = X
emptySeats = 0numPassengers = 3
i = 2
cabin.getFirstClass().get(i) = X
emptySeats = 0numPassengers = 3
i = 3
cabin.getFirstClass().get(i) = 1D
Found one!
emptySeats = 1numPassengers = 3
i = 4
cabin.getFirstClass().get(i) = 2A
Found one!
emptySeats = 2numPassengers = 3
i = 5
cabin.getFirstClass().get(i) = X
emptySeats = 2numPassengers = 3
i = 6
cabin.getFirstClass().get(i) = X
emptySeats = 2numPassengers = 3
i = 7
cabin.getFirstClass().get(i) = 2D
Found one!
emptySeats = 3numPassengers = 3
Everyone fits in one row!

As you can see, it doesn't seem to handle the changing of rows properly. Could someone please help me out with what's going on? I'd really appreciate it.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
aurora91
  • 478
  • 1
  • 9
  • 25
  • 1
    That function is enormous, I'd recommend breaking it down a bit – beresfordt Feb 21 '15 at 00:47
  • 8
    `if(s == "X"){` no, not again http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Pshemo Feb 21 '15 at 00:48
  • "==" for String comparison deserves its own, special compiler warning. – zubergu Feb 21 '15 at 00:54
  • @beresfordt I know, maybe I'm over-thinking it... But I was planning on getting it to cooperate first and then simplifying it and cutting redundant code afterwards. – aurora91 Feb 21 '15 at 01:03
  • @Pshemo Sorry! I'll update the string comparisons to equals(). But I don't think that will help my current problem. – aurora91 Feb 21 '15 at 01:04
  • @aurora91 I didn't post it as solution, but as thing which shouldn't be placed in code. – Pshemo Feb 21 '15 at 01:08
  • 1
    I haven't finished looking through it yet, but I do notice that it only says enough seats are available if(emptySeats == numPassengers), shouldn't it be if(emptySeats >= numPassengers) – Gregory Basior Feb 21 '15 at 01:13
  • @GregoryBasior Hmm this is true. I didn't think about it in that way. I guess the reason being is that the function stop looking for empty seats once it has found enough to accommodate all of the passengers. So I just have equality checking. – aurora91 Feb 21 '15 at 01:16
  • Does cabin.getFirstClass().get(i) go from 0-7, where 0 is seat 1A, 1 - 1B, 2 - 1C, 3 - 1D, 4 - 2A...? – Gregory Basior Feb 21 '15 at 01:19
  • @GregoryBasior Correct. But I'm specifically trying to seat as many members of the same group in each row as possible. That's what's throwing me off. – aurora91 Feb 21 '15 at 01:21

1 Answers1

0

If I'm understanding correctly how the system is set up, each group of 4 in the indices of cabin.getFirstClase() pertains to 1 row. So, I think the code for just looking at one row should look more like this: int emptyRow = -1; if(foundFullSeating == false){

    for(i = 0; i < 4*rows; i++){
        System.out.println("i = " + i);
        System.out.println("cabin.getFirstClass().get(i) = " + cabin.getFirstClass().get(i));
        if(!cabin.getFirstClass().get(i).contains("X")){
            System.out.println("Found one!");
            emptySeats++;
        }
        System.out.println("emptySeats = " + emptySeats + "numPassengers = " + numPassengers);
        if(emptySeats >= numPassengers){
            System.out.println("Everyone fits in one row!");
            foundFullSeating = true;
            foundPartialSeating = true;

        }
        if(i%4 == 3){
            emptySeats == 0;
            emptyRow = i / 4;
        }

    }

} //end trying to find seating for all passengers in one row

As for the partial seating algorithm, it may be easier if you have a row object and in that row object it stores the number of seats, which would make it much easier to find out which row(s) have the most seats available. You could make a Comparator for the row which returns based on availableSeats followed by rowNumber and then put the rows in a PriorityQueue and pop from that queue until enough seats have been filled. If you were to implement it this way, this would also work for seeing if they all fit in one row.

Gregory Basior
  • 300
  • 1
  • 9