1 00:00:00,210 --> 00:00:03,870 Before we start, did you try solving the workbook yourself? 2 00:00:03,900 --> 00:00:09,660 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:11,000 --> 00:00:13,250 Welcome to Workbook 7.9. 4 00:00:13,250 --> 00:00:19,430 Here we have a method called Create Reservation that every object of the airline class is going to be 5 00:00:19,430 --> 00:00:20,570 able to call. 6 00:00:20,900 --> 00:00:27,260 And what the method is going to do is it's going to index an element based on whatever that person's 7 00:00:27,380 --> 00:00:28,590 seat number is. 8 00:00:28,610 --> 00:00:34,790 It's going to set that element equal to a new object of the person class, which will get populated 9 00:00:34,790 --> 00:00:37,010 based on the values of the parameter. 10 00:00:37,010 --> 00:00:42,650 And so by putting that person inside of our people's array, we're basically creating a reservation 11 00:00:42,650 --> 00:00:43,300 for them. 12 00:00:43,310 --> 00:00:49,850 However, inside the function, we need a wire loop that keeps running so long as that spot in the array 13 00:00:49,850 --> 00:00:50,990 is reserved. 14 00:00:51,620 --> 00:00:59,510 So basically, if there is already a person that is sitting in the seat, if we access the element at 15 00:00:59,510 --> 00:01:06,950 that index and it's not null, we need to keep running this while loop and prompt that person to choose 16 00:01:06,950 --> 00:01:08,090 another seat. 17 00:01:10,610 --> 00:01:17,540 But now, if that person ends up choosing another seat, all that is going to do is update that person's 18 00:01:17,640 --> 00:01:18,640 seat number. 19 00:01:18,650 --> 00:01:21,250 It's not going to update this index variable. 20 00:01:21,260 --> 00:01:26,420 So what we need to do is index the people's array based on the seat number. 21 00:01:26,450 --> 00:01:29,160 Otherwise, this while loop is going to run forever. 22 00:01:29,180 --> 00:01:34,460 Actually, once we finally choose a seat number, then we can define the index over here. 23 00:01:35,570 --> 00:01:36,980 Sounds good to me. 24 00:01:36,980 --> 00:01:40,790 While the loop is running, tell them that the spot is already taken. 25 00:01:41,750 --> 00:01:45,890 And we'll be doing that before we get them to choose another seats. 26 00:01:47,090 --> 00:01:52,070 Looks like we accomplished everything we wanted to do inside of create reservation. 27 00:01:52,100 --> 00:01:56,540 The next step is to run our code to see if we get the following output. 28 00:02:00,210 --> 00:02:05,100 So what's happening at first is for every single person that it runs through, it's going to create 29 00:02:05,100 --> 00:02:06,540 a reservation for them. 30 00:02:06,540 --> 00:02:09,900 It's going to reserve them a spot in the people's array. 31 00:02:10,020 --> 00:02:13,410 At first, everything should work nice and fine. 32 00:02:17,290 --> 00:02:22,870 And everything works beautifully because every single person has a unique seat number. 33 00:02:23,380 --> 00:02:25,540 There aren't any conflicts. 34 00:02:25,780 --> 00:02:32,650 But if in task number two, we change Hammurabi's AC number to ten, what do you think will happen? 35 00:02:32,680 --> 00:02:35,110 Well, let us visualize the runtime. 36 00:02:35,650 --> 00:02:38,950 So I'm just going to put one breakpoint over here. 37 00:02:39,400 --> 00:02:40,960 Press the debug button. 38 00:02:43,440 --> 00:02:49,650 I don't really care what happens for the first ten elements, so I'm just going to keep pressing continue. 39 00:02:51,440 --> 00:02:53,990 Six, seven, eight, nine. 40 00:02:56,090 --> 00:03:00,930 And so once I equals ten, that means we are indexing the 11th. 41 00:03:00,950 --> 00:03:04,400 The last element we can step inside. 42 00:03:06,130 --> 00:03:11,200 Hammurabi's estate number is ten, which corresponds to an index of nine. 43 00:03:11,230 --> 00:03:16,750 There is already an element at index nine, so this condition evaluates to true. 44 00:03:16,780 --> 00:03:18,770 The spot is already reserved. 45 00:03:18,790 --> 00:03:22,080 It's going to prompt Almaribe to choose another seat. 46 00:03:22,090 --> 00:03:27,130 So this person object person at 29 is going to end up calling to seat. 47 00:03:28,200 --> 00:03:34,320 This points to the current object that called this method and Amarah is a seat number is going to change 48 00:03:34,320 --> 00:03:35,970 to another random number. 49 00:03:36,450 --> 00:03:38,550 In this case, it changes to seven. 50 00:03:40,090 --> 00:03:41,430 Back to our wild loop. 51 00:03:41,440 --> 00:03:45,080 Seven minus one corresponds to an index of six. 52 00:03:45,100 --> 00:03:48,350 The element at index six is already reserved. 53 00:03:48,370 --> 00:03:54,610 So though, while loop runs again, prompting them to choose another seat, that same person with reference 54 00:03:54,610 --> 00:03:57,820 at 29 is going to call to seat again. 55 00:03:58,090 --> 00:04:01,850 This points to the current object that's calling this method. 56 00:04:01,870 --> 00:04:05,050 They're going to once again choose another random seat. 57 00:04:05,050 --> 00:04:06,010 Six. 58 00:04:07,200 --> 00:04:09,570 Six minus one is five. 59 00:04:09,600 --> 00:04:12,660 The element at index five is already reserved. 60 00:04:12,900 --> 00:04:14,430 It is not equal to null. 61 00:04:14,430 --> 00:04:19,829 This condition evaluates the true of the while loop keeps running and this is just going to keep on 62 00:04:19,829 --> 00:04:26,520 going until they eventually choose a seat that isn't reserved, a seat that corresponds to an index 63 00:04:26,520 --> 00:04:27,050 of ten. 64 00:04:27,060 --> 00:04:31,740 So I'm just going to fast forward the video until they do, this is going to be very long, so wish 65 00:04:31,740 --> 00:04:32,430 me luck. 66 00:04:33,640 --> 00:04:34,180 All right. 67 00:04:34,180 --> 00:04:35,860 Finally, they chose a seat. 68 00:04:35,860 --> 00:04:37,100 Number of 11. 69 00:04:37,120 --> 00:04:40,950 11 minus one corresponds to an index of ten. 70 00:04:40,960 --> 00:04:42,880 That spot does equal null. 71 00:04:42,880 --> 00:04:43,960 It's not reserved. 72 00:04:43,960 --> 00:04:45,970 So this evaluates the false. 73 00:04:46,690 --> 00:04:52,960 So ultimately the valid seat number ended up being 11, which means we're going to update the element 74 00:04:52,960 --> 00:04:54,340 at Index ten. 75 00:04:55,990 --> 00:05:02,410 We set the element at index ten equal to a new object of the person class, and that new object is going 76 00:05:02,410 --> 00:05:05,860 to get populated with values from the source object. 77 00:05:05,860 --> 00:05:11,800 The object that we're passing in here, we populate every single field inside of the current object 78 00:05:11,800 --> 00:05:15,250 that we just created with values from the source. 79 00:05:17,780 --> 00:05:18,380 All right. 80 00:05:18,380 --> 00:05:19,220 Splendid. 81 00:05:21,130 --> 00:05:24,880 Now, every single spot in our airline has been reserved. 82 00:05:24,880 --> 00:05:28,900 And we thank Hammurabi for flying with Java Airlines. 83 00:05:29,150 --> 00:05:30,270 Pressing continue. 84 00:05:30,280 --> 00:05:31,650 We should be done. 85 00:05:31,660 --> 00:05:38,950 We got a very similar output to what we have on learn the part where we thank everyone for flying. 86 00:05:38,950 --> 00:05:46,390 But once they choose a conflicting seat, we keep prompting them to choose another seat until they find 87 00:05:46,390 --> 00:05:47,380 a valid one. 88 00:05:48,880 --> 00:05:49,090 House. 89 00:05:49,090 --> 00:05:50,620 Number three is inside the four loop. 90 00:05:50,620 --> 00:05:53,440 Notice that each person is applying for a passport. 91 00:05:53,470 --> 00:05:55,210 Let us make sure of that. 92 00:05:57,440 --> 00:05:57,740 All right. 93 00:05:57,740 --> 00:05:59,210 We can see it right here. 94 00:05:59,720 --> 00:06:06,110 If their passport is approved, update their passport using set passport and then create reservation. 95 00:06:06,110 --> 00:06:13,190 So here we can say if passport approved, then we're going to create the reservation. 96 00:06:15,120 --> 00:06:16,260 Otherwise. 97 00:06:16,960 --> 00:06:19,030 Print the following message. 98 00:06:22,120 --> 00:06:24,610 All right, Let me just import a raise. 99 00:06:27,510 --> 00:06:28,470 And we're good. 100 00:06:29,340 --> 00:06:31,270 And then you visualize the runtime. 101 00:06:36,080 --> 00:06:41,300 All right, So here we can see that Cleopatra's passport was approved, so it's going to end up creating 102 00:06:41,300 --> 00:06:44,390 a reservation for them at Index zero. 103 00:06:46,080 --> 00:06:47,010 Splendid. 104 00:06:51,300 --> 00:07:00,780 Here we see that Alexander the Great's passport was denied, so we're going to print an apologetic message 105 00:07:00,780 --> 00:07:01,410 to them. 106 00:07:03,740 --> 00:07:04,400 And that's it. 107 00:07:04,400 --> 00:07:08,810 It's going to keep creating reservations for people whose passports have been approved. 108 00:07:12,360 --> 00:07:18,360 And whenever someone's passport gets denied, they will not be added to our list of passengers. 109 00:07:30,760 --> 00:07:37,030 And now, because Sun Tzu is already occupying seat number ten, whereas Hammurabi is also hoping to 110 00:07:37,030 --> 00:07:38,470 get that same seat. 111 00:07:45,630 --> 00:07:51,930 Create reservation is going to keep running its y loop until they choose a seat that has been approved. 112 00:07:53,030 --> 00:07:57,530 They chose seat number five, which corresponds to an index of four. 113 00:07:57,560 --> 00:08:03,800 Thankfully, whoever chose a seat number of five Confucius, they were denied a passport. 114 00:08:03,800 --> 00:08:06,620 So Amira can just take their seats. 115 00:08:07,250 --> 00:08:09,290 All right, let's keep going here. 116 00:08:09,290 --> 00:08:10,820 It assigns Almaribe. 117 00:08:10,850 --> 00:08:12,170 Seat number four. 118 00:08:12,200 --> 00:08:13,610 Looking good. 119 00:08:14,700 --> 00:08:15,780 And we're all done. 120 00:08:19,990 --> 00:08:25,660 So we were able to get a very similar result to what we have on learning the part where people who were 121 00:08:25,660 --> 00:08:32,530 denied a passport do not get their reservation created and people who have conflicting seat numbers 122 00:08:32,530 --> 00:08:37,720 end up choosing another seat until they wind up with one that isn't taken. 123 00:08:38,049 --> 00:08:38,770 All right. 124 00:08:38,770 --> 00:08:46,030 And task force is to admire how complex this application is and how clean the code remains inside of 125 00:08:46,030 --> 00:08:46,600 Maine. 126 00:08:46,900 --> 00:08:49,660 That is all for the workbooks in this section. 127 00:08:49,660 --> 00:08:52,660 I really hope you enjoyed this breakpoint session.