1 00:00:00,360 --> 00:00:04,860 The goal apart, too, is the bullet proof, the application, a bullet proof application is free of 2 00:00:04,860 --> 00:00:05,420 bugs. 3 00:00:05,430 --> 00:00:09,330 It behaves exactly like we want it to and it doesn't fail or crash no matter what. 4 00:00:10,060 --> 00:00:13,350 Let's run the application and put it to the test kick first. 5 00:00:13,350 --> 00:00:19,890 Instead of an individual, enter a string and the application crashes, we get an input mismatch exception. 6 00:00:20,430 --> 00:00:25,380 It's an unchecked exception because that happens during the runtime and as you know by now, unchecked 7 00:00:25,380 --> 00:00:28,860 implies that there's something wrong, there's something missing in our code. 8 00:00:29,310 --> 00:00:34,140 And what's missing in this case is code that anticipates a scenario where the user may enter a type 9 00:00:34,140 --> 00:00:34,840 mismatch. 10 00:00:35,580 --> 00:00:40,530 So here, before assigning a value for the row, I'm going to check if the next value scanner is going 11 00:00:40,530 --> 00:00:42,150 to pick up is an integer. 12 00:00:42,150 --> 00:00:44,130 If scanned, it has an int. 13 00:00:47,820 --> 00:00:53,250 And if that's the case, then we'll pick up the injuries next and no problem, and if that's not the 14 00:00:53,250 --> 00:00:55,800 case, I'll assign it a value of 404. 15 00:00:56,820 --> 00:01:02,610 But we still need to pick up the user input, so will you scan the next line because next line can pick 16 00:01:02,610 --> 00:01:07,860 up anything and this is perfect because let's just say it picks up an integer. 17 00:01:08,070 --> 00:01:12,300 The next line is going to get consumed anyway by virtue of the reference trap. 18 00:01:12,780 --> 00:01:17,270 On the other hand, if we return for 04, we still need to pick up the input. 19 00:01:17,280 --> 00:01:18,360 We can't just leave it there. 20 00:01:18,750 --> 00:01:21,960 And next line, as you know, can pick up just about anything. 21 00:01:22,920 --> 00:01:24,810 All right, we'll do the same thing for Spot. 22 00:01:41,640 --> 00:01:41,980 Good. 23 00:01:42,000 --> 00:01:47,460 Now, let's imagine that the user messes up one of the inputs, one of these values is going to be four 24 00:01:47,460 --> 00:01:47,940 of four. 25 00:01:48,660 --> 00:01:50,460 So here we can say if RO. 26 00:01:53,150 --> 00:01:57,980 Is equal to 404 or if the spot is equal to four 04. 27 00:02:00,630 --> 00:02:03,030 Then we'll print invalid inputs. 28 00:02:07,320 --> 00:02:09,630 And restart the wire loop with continue. 29 00:02:15,160 --> 00:02:15,960 Let's test it out. 30 00:02:21,370 --> 00:02:22,660 Eleanor, a mismatch. 31 00:02:24,720 --> 00:02:29,670 And where to get and what's good about the way we designed this code is that I don't have to worry about 32 00:02:29,670 --> 00:02:35,610 the reference because in the event that next event gets called, we've already got a throwaway next 33 00:02:35,610 --> 00:02:37,140 line that's going to get consumed anyway. 34 00:02:37,440 --> 00:02:38,480 So that's pretty cool. 35 00:02:40,460 --> 00:02:42,770 OK, now I'm going to pass an invalid index. 36 00:02:44,530 --> 00:02:48,630 And we get another unchecked exception array index out of bounds. 37 00:02:49,170 --> 00:02:51,150 So how do we stop this crash from happening? 38 00:02:51,640 --> 00:02:55,350 Somehow I need to check if the index exceeds the bounds of the array. 39 00:02:55,810 --> 00:02:59,140 But once again, I'm not going to use this array for the same reason as before. 40 00:02:59,680 --> 00:03:04,330 Imagine this data was being loaded from a file, which is something we're going to be doing very, very 41 00:03:04,330 --> 00:03:04,750 soon. 42 00:03:05,140 --> 00:03:07,060 There wouldn't even be an array in Maine. 43 00:03:07,630 --> 00:03:10,720 So what I'm going to do is define a method inside the machine costs. 44 00:03:12,920 --> 00:03:14,300 It's going to be called get length. 45 00:03:17,310 --> 00:03:22,470 And this method is going to return the length of the array, which happens to be the number of rows. 46 00:03:29,400 --> 00:03:34,770 I'll define another method, Gaturro length, that's going to return the length of Arrow. 47 00:03:36,550 --> 00:03:41,980 In our case, all the rulings, lengths are uniform so we can choose any row and it's going to return 48 00:03:41,980 --> 00:03:42,430 its length. 49 00:03:48,850 --> 00:03:50,230 And now back here, I'm going to. 50 00:03:54,380 --> 00:03:56,120 Check if the row is negative. 51 00:03:58,820 --> 00:04:04,310 Or if the ROE index exceeds the length of the array, which happens to be the number of rows, don't 52 00:04:04,310 --> 00:04:07,050 forget the minus one because we start indexing from zero. 53 00:04:07,850 --> 00:04:13,490 We'll do the same thing for the spot and we'll check if the spot is it negative and if it exceeds the 54 00:04:13,490 --> 00:04:14,840 number of spots in each row. 55 00:04:17,000 --> 00:04:18,800 Again, don't forget the negative one. 56 00:04:34,710 --> 00:04:39,720 If either of these comparisons ends up being true, we're going to print invalid range. 57 00:04:43,650 --> 00:04:45,180 And restart the wire loop. 58 00:04:47,450 --> 00:04:49,460 And that's all I'm going to test this code. 59 00:04:59,930 --> 00:05:05,300 I'm just going to put random indexes that don't make any sense and perfect's. 60 00:05:10,190 --> 00:05:14,130 OK, now what happens if I tried to purchase an item with a quantity of zero? 61 00:05:14,750 --> 00:05:20,150 I'm going to purchase the berry twice now it's a zero if I try to purchase it again. 62 00:05:23,520 --> 00:05:28,770 The application throws an illegal argument exception once again, this exception is unchecked, which 63 00:05:28,770 --> 00:05:30,240 means I need to fix my code. 64 00:05:30,720 --> 00:05:35,760 In this case, what's missing is code that anticipates a scenario where the user may purchase an item 65 00:05:35,760 --> 00:05:36,980 with a quantity of zero. 66 00:05:37,650 --> 00:05:40,980 So we'll add another else if if the machine. 67 00:05:44,380 --> 00:05:51,640 Dot, and we'll get the item, get item at the requested spot, and Rowe will get its quantity and if 68 00:05:51,640 --> 00:05:53,710 that quantity ends up being zero. 69 00:06:01,220 --> 00:06:03,500 Then we're going to have to print empty slot. 70 00:06:11,230 --> 00:06:12,670 And restart the wire loop. 71 00:06:16,100 --> 00:06:17,140 Now we can try again. 72 00:06:25,020 --> 00:06:26,280 I'll purchase the very. 73 00:06:30,780 --> 00:06:34,050 I'll purchase it once more, and that's perfect's. 74 00:06:35,320 --> 00:06:40,660 The application is now bulletproof, no matter what the user throws at you, the application is free 75 00:06:40,660 --> 00:06:42,580 of bugs and it's never going to crash. 76 00:06:43,010 --> 00:06:45,970 It can process any input no matter what you think about it. 77 00:06:45,970 --> 00:06:48,070 And it's going to respond with grace.