1 00:00:00,210 --> 00:00:02,580 The goal of party was to bulletproof the application. 2 00:00:05,970 --> 00:00:08,310 So let's run the application and put it to the test. 3 00:00:16,570 --> 00:00:18,640 Instead of an integer, Oliner, a string. 4 00:00:20,830 --> 00:00:26,920 And the application crashes, that's not good, we get an input mismatch, exception and unchecked exception 5 00:00:26,920 --> 00:00:31,930 because it happens during the runtime and unchecked implies that there's something wrong, there's something 6 00:00:31,930 --> 00:00:32,860 missing in our code. 7 00:00:33,370 --> 00:00:37,910 And what's missing is code that anticipates a scenario where the user enters a type mismatch. 8 00:00:38,740 --> 00:00:43,780 So here we can say if skin that has next intent is not true. 9 00:00:47,630 --> 00:00:49,280 I'll pick it up using next line. 10 00:00:51,680 --> 00:00:53,870 Then I'll print invalid input. 11 00:01:02,020 --> 00:01:03,400 And restart the wire loop. 12 00:01:11,310 --> 00:01:12,440 OK, let's test it out. 13 00:01:16,670 --> 00:01:17,450 A mismatch. 14 00:01:19,660 --> 00:01:20,380 Henry, good. 15 00:01:24,150 --> 00:01:27,870 Now, what if I enter zero or one, then I enter a mismatch. 16 00:01:32,780 --> 00:01:38,600 We get a bug, I've got a feeling I know what it is, but what I'm going to do is store this in a variable 17 00:01:38,600 --> 00:01:39,380 just to make sure. 18 00:01:47,810 --> 00:01:50,180 And I'm going to use breakpoints to visualize the runtime. 19 00:02:01,680 --> 00:02:02,790 So we'll enter a number. 20 00:02:11,940 --> 00:02:13,500 Next will enter a string. 21 00:02:22,360 --> 00:02:26,630 I see next line gets skipped and is consumed by empty space. 22 00:02:27,040 --> 00:02:34,570 It's the classic next line trap when next line runs after the next and next double or next, it's going 23 00:02:34,570 --> 00:02:36,190 to get wasted by empty space. 24 00:02:36,670 --> 00:02:41,430 And you're well aware by now that the solution is that a throwaway next line after the next int? 25 00:02:44,630 --> 00:02:50,360 That way, the throw away consumes the blank space and our other next line can do what it's supposed 26 00:02:50,360 --> 00:02:50,710 to do. 27 00:03:00,150 --> 00:03:01,110 Rewriting the code. 28 00:03:08,930 --> 00:03:12,980 I'll put a valid index, then I'll put a string perfect's. 29 00:03:14,990 --> 00:03:16,940 Now I'm going to pass an invalid index. 30 00:03:19,420 --> 00:03:24,940 And we got another unchecked exception array index out of bounds, so how do we stop this crash from 31 00:03:24,940 --> 00:03:25,390 happening? 32 00:03:26,080 --> 00:03:29,830 Somehow I need to check if the index exceeds the bounds of the array. 33 00:03:32,710 --> 00:03:35,620 How do I access the length of the cars field inside dealership? 34 00:03:36,310 --> 00:03:41,350 I'm not going to use this array because imagine this data was being loaded from a file, which is something 35 00:03:41,350 --> 00:03:42,880 that we're going to be doing very soon. 36 00:03:43,030 --> 00:03:45,200 And in that case, there wouldn't even be an array. 37 00:03:45,610 --> 00:03:48,970 So what I'm going to do is define a method inside the dealership class. 38 00:03:50,020 --> 00:03:51,250 The returns, an integer. 39 00:03:56,800 --> 00:03:58,680 And it will return the length of the era. 40 00:04:04,470 --> 00:04:06,030 And now back here, I'm going to say. 41 00:04:13,940 --> 00:04:19,910 If index is less than zero or if the index that was passed in is higher than the number of cars in the 42 00:04:19,910 --> 00:04:26,450 dealership, minus one, because with indexes we start counting from zero, then we'll print line. 43 00:04:30,210 --> 00:04:35,370 Invalid index, I'm pretty sure in the workbook I told you to print something else, but you can print 44 00:04:35,370 --> 00:04:40,530 whatever you want and restart the wallet and that's all I'm going to test this code. 45 00:04:45,160 --> 00:04:47,140 I'll enter a spot of negative one. 46 00:04:50,470 --> 00:04:50,860 To. 47 00:04:51,920 --> 00:04:54,020 Five pin perfect. 48 00:04:55,950 --> 00:04:58,610 All right, now, what happens if I try to buy a car from an empty spot? 49 00:05:06,060 --> 00:05:10,000 It's going to throw a null pointer exception, which once again is an unchecked exception. 50 00:05:10,530 --> 00:05:11,900 So we need to fix our code. 51 00:05:11,910 --> 00:05:14,430 We need to add code that anticipates such a scenario. 52 00:05:18,620 --> 00:05:22,520 So what I'm going to do is say, elss, I'm going to get the car first. 53 00:05:25,520 --> 00:05:26,930 And check if it equals no. 54 00:05:32,210 --> 00:05:34,130 And if it happens to be, no will print. 55 00:05:41,370 --> 00:05:44,970 And we'll restart the wire loop, prompting the user to enter another value. 56 00:05:49,730 --> 00:05:50,630 Trying that again. 57 00:06:07,910 --> 00:06:10,670 And it still crashes, I wonder. 58 00:06:11,270 --> 00:06:14,390 Wow, and if you actually look into the exception, it's coming from the Geter. 59 00:06:14,630 --> 00:06:15,320 What the heck? 60 00:06:18,100 --> 00:06:24,490 Oh, I see, because the object at this index is no passing, not into the new car copy constructor 61 00:06:24,490 --> 00:06:26,080 is going to throw a nail pointer exception. 62 00:06:29,460 --> 00:06:32,280 Here, I'll let you visualize it, I'll put a break point here. 63 00:06:43,820 --> 00:06:45,620 In the first run, I'll buy this card. 64 00:06:48,140 --> 00:06:50,420 And I'm going to keep stepping through the first run. 65 00:06:52,470 --> 00:06:55,370 And in the second run, I'm going to choose the same empty spot. 66 00:07:07,660 --> 00:07:11,110 Now we're in the geter step into the copy constructor. 67 00:07:14,600 --> 00:07:17,630 And here, right here, see how the source object is No. 68 00:07:18,920 --> 00:07:22,380 And trying to access something from a novel is going to crash the application. 69 00:07:22,400 --> 00:07:24,080 It's going to throw a null pointer exception. 70 00:07:34,430 --> 00:07:40,310 So what I'll do here is use a conditional assignment, I'm going to check if the object that's being 71 00:07:40,310 --> 00:07:41,360 requested is null. 72 00:07:44,310 --> 00:07:49,980 And if it is just going to return, no, otherwise we'll return a copy of the object they're trying 73 00:07:49,980 --> 00:07:50,650 to index. 74 00:07:51,480 --> 00:07:53,750 Now, you can also use it for us if you want. 75 00:07:53,760 --> 00:07:55,230 It would look something like this. 76 00:08:08,150 --> 00:08:13,400 But in such a scenario, I don't like using a false when I can use this syntax because it's a lot cleaner. 77 00:08:15,510 --> 00:08:16,860 Coutry running the app. 78 00:08:34,640 --> 00:08:36,280 And we fixed that crash as well. 79 00:08:46,690 --> 00:08:50,060 I right task five was to check if the user wants to continue. 80 00:08:50,080 --> 00:08:52,540 So at the end of the wire loop, I'm going to ask the user. 81 00:08:55,220 --> 00:08:56,960 Types to continue shopping. 82 00:09:05,850 --> 00:09:07,860 And if they type anything that isn't yes. 83 00:09:20,630 --> 00:09:21,780 Then I'll break the loop. 84 00:09:28,260 --> 00:09:29,130 Caltrain out. 85 00:09:34,130 --> 00:09:36,440 I'll buy a one car, but I'll stop here. 86 00:09:43,210 --> 00:09:46,240 Can task six was to break the loop of the dealership is empty. 87 00:09:46,810 --> 00:09:49,510 Remember, we made an empty method inside dealership. 88 00:09:49,960 --> 00:09:52,800 So at the very beginning, we'll check if the dealership is empty. 89 00:09:57,400 --> 00:10:02,400 And what we'll do in that case, if the result is true, will print, we're all sold out. 90 00:10:05,470 --> 00:10:06,420 And break the. 91 00:10:08,070 --> 00:10:09,140 Trying it out now. 92 00:10:14,680 --> 00:10:15,760 I'll purchase a car. 93 00:10:17,670 --> 00:10:18,630 Purchase another. 94 00:10:22,370 --> 00:10:24,050 And Perfect's. 95 00:10:29,600 --> 00:10:34,330 No matter what the user throws at you, the application is free of bugs and it's never going to crash. 96 00:10:34,760 --> 00:10:38,210 It can process any input and respond gracefully.