Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 7.9.
Here we have a method called Create Reservation that every object of the airline class is going to be
able to call.
And what the method is going to do is it's going to index an element based on whatever that person's
seat number is.
It's going to set that element equal to a new object of the person class, which will get populated
based on the values of the parameter.
And so by putting that person inside of our people's array, we're basically creating a reservation
for them.
However, inside the function, we need a wire loop that keeps running so long as that spot in the array
is reserved.
So basically, if there is already a person that is sitting in the seat, if we access the element at
that index and it's not null, we need to keep running this while loop and prompt that person to choose
another seat.
But now, if that person ends up choosing another seat, all that is going to do is update that person's
seat number.
It's not going to update this index variable.
So what we need to do is index the people's array based on the seat number.
Otherwise, this while loop is going to run forever.
Actually, once we finally choose a seat number, then we can define the index over here.
Sounds good to me.
While the loop is running, tell them that the spot is already taken.
And we'll be doing that before we get them to choose another seats.
Looks like we accomplished everything we wanted to do inside of create reservation.
The next step is to run our code to see if we get the following output.
So what's happening at first is for every single person that it runs through, it's going to create
a reservation for them.
It's going to reserve them a spot in the people's array.
At first, everything should work nice and fine.
And everything works beautifully because every single person has a unique seat number.
There aren't any conflicts.
But if in task number two, we change Hammurabi's AC number to ten, what do you think will happen?
Well, let us visualize the runtime.
So I'm just going to put one breakpoint over here.
Press the debug button.
I don't really care what happens for the first ten elements, so I'm just going to keep pressing continue.
Six, seven, eight, nine.
And so once I equals ten, that means we are indexing the 11th.
The last element we can step inside.
Hammurabi's estate number is ten, which corresponds to an index of nine.
There is already an element at index nine, so this condition evaluates to true.
The spot is already reserved.
It's going to prompt Almaribe to choose another seat.
So this person object person at 29 is going to end up calling to seat.
This points to the current object that called this method and Amarah is a seat number is going to change
to another random number.
In this case, it changes to seven.
Back to our wild loop.
Seven minus one corresponds to an index of six.
The element at index six is already reserved.
So though, while loop runs again, prompting them to choose another seat, that same person with reference
at 29 is going to call to seat again.
This points to the current object that's calling this method.
They're going to once again choose another random seat.
Six.
Six minus one is five.
The element at index five is already reserved.
It is not equal to null.
This condition evaluates the true of the while loop keeps running and this is just going to keep on
going until they eventually choose a seat that isn't reserved, a seat that corresponds to an index
of ten.
So I'm just going to fast forward the video until they do, this is going to be very long, so wish
me luck.
All right.
Finally, they chose a seat.
Number of 11.
11 minus one corresponds to an index of ten.
That spot does equal null.
It's not reserved.
So this evaluates the false.
So ultimately the valid seat number ended up being 11, which means we're going to update the element
at Index ten.
We set the element at index ten equal to a new object of the person class, and that new object is going
to get populated with values from the source object.
The object that we're passing in here, we populate every single field inside of the current object
that we just created with values from the source.
All right.
Splendid.
Now, every single spot in our airline has been reserved.
And we thank Hammurabi for flying with Java Airlines.
Pressing continue.
We should be done.
We got a very similar output to what we have on learn the part where we thank everyone for flying.
But once they choose a conflicting seat, we keep prompting them to choose another seat until they find
a valid one.
House.
Number three is inside the four loop.
Notice that each person is applying for a passport.
Let us make sure of that.
All right.
We can see it right here.
If their passport is approved, update their passport using set passport and then create reservation.
So here we can say if passport approved, then we're going to create the reservation.
Otherwise.
Print the following message.
All right, Let me just import a raise.
And we're good.
And then you visualize the runtime.
All right, So here we can see that Cleopatra's passport was approved, so it's going to end up creating
a reservation for them at Index zero.
Splendid.
Here we see that Alexander the Great's passport was denied, so we're going to print an apologetic message
to them.
And that's it.
It's going to keep creating reservations for people whose passports have been approved.
And whenever someone's passport gets denied, they will not be added to our list of passengers.
And now, because Sun Tzu is already occupying seat number ten, whereas Hammurabi is also hoping to
get that same seat.
Create reservation is going to keep running its y loop until they choose a seat that has been approved.
They chose seat number five, which corresponds to an index of four.
Thankfully, whoever chose a seat number of five Confucius, they were denied a passport.
So Amira can just take their seats.
All right, let's keep going here.
It assigns Almaribe.
Seat number four.
Looking good.
And we're all done.
So we were able to get a very similar result to what we have on learning the part where people who were
denied a passport do not get their reservation created and people who have conflicting seat numbers
end up choosing another seat until they wind up with one that isn't taken.
All right.
And task force is to admire how complex this application is and how clean the code remains inside of
Maine.
That is all for the workbooks in this section.
I really hope you enjoyed this breakpoint session.