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.