1 00:00:00,090 --> 00:00:04,620 The last step is to bulletproof the application, we're going to make sure the application doesn't fail 2 00:00:04,620 --> 00:00:10,080 or crash no matter what happens right now, there are many places where unchecked exceptions could be 3 00:00:10,080 --> 00:00:14,810 thrown during the runtime and cross the application, but we can't allow that to happen. 4 00:00:15,300 --> 00:00:16,200 Let's run the app. 5 00:00:16,560 --> 00:00:22,650 And the first vulnerability is if the user enters the wrong and put the Atheros in input mismatch exception. 6 00:00:23,070 --> 00:00:26,610 And if we get an unchecked exception, it means we have to improve our code. 7 00:00:27,060 --> 00:00:31,860 And what we're missing is code that anticipates a scenario where the user may enter a value that is 8 00:00:31,860 --> 00:00:32,670 not an integer. 9 00:00:35,140 --> 00:00:41,710 So what I'll do is I'll apply conditional assignment here, I'll check if scanned on has next and if 10 00:00:41,710 --> 00:00:47,800 the next thing the user inputs is an integer, then we'll pick up the input using next and otherwise 11 00:00:47,800 --> 00:00:49,900 we'll assign it the value of 404. 12 00:00:50,410 --> 00:00:54,250 And how convenient is it that we already have a next line right here? 13 00:00:54,640 --> 00:00:58,570 Because if 404 returns, we still need to pick up the input. 14 00:00:58,570 --> 00:01:02,940 Otherwise things are going to get out of whack and next line can pick up anything. 15 00:01:02,950 --> 00:01:03,850 So that's perfect. 16 00:01:05,200 --> 00:01:09,130 Same thing here, if the next input that scanner reads is an integer. 17 00:01:11,740 --> 00:01:16,480 Then we'll pick it up, otherwise we'll just assign the value for for. 18 00:01:18,310 --> 00:01:23,160 And now before creating the object, I'll just say if either value is Foro for. 19 00:01:36,580 --> 00:01:38,380 Then just restart the wire loop. 20 00:01:40,960 --> 00:01:41,950 Rewriting the code. 21 00:01:48,830 --> 00:01:52,220 I'm going to say, don't know how beautiful. 22 00:01:55,280 --> 00:02:01,160 And the next vulnerability in the app is if the user index is outside the bounds of the array and the 23 00:02:01,160 --> 00:02:07,370 app throws an array index out of bounds, exception, unchecked exceptions are the result of badly written 24 00:02:07,370 --> 00:02:07,840 code. 25 00:02:07,850 --> 00:02:12,380 And what we're missing in the code is logic that anticipates the user indexing on a bounce. 26 00:02:17,480 --> 00:02:23,030 So what I'll do is I'll see if the aisle number is smaller than zero or bigger than six. 27 00:02:25,750 --> 00:02:28,210 Or if the column number is smaller than zero. 28 00:02:32,570 --> 00:02:33,710 Or bigger than two. 29 00:02:40,240 --> 00:02:45,550 Then the user is indexing outside the bounds of the array, so we have to restart the loop. 30 00:02:58,510 --> 00:03:04,600 If I index outside the bounds of the array, it just restarts the loop and doesn't crash, perfect. 31 00:03:05,750 --> 00:03:12,010 Last thing we might get is an illegal state exception if I try to remove from an empty cart, the app 32 00:03:12,010 --> 00:03:14,290 is going to throw an illegal state exception. 33 00:03:36,560 --> 00:03:41,330 This exception is thrown by the remove method, and that's because we shouldn't be calling the remove 34 00:03:41,330 --> 00:03:46,280 function when there's nothing in the cart that's just bad and I'm glad it crashed. 35 00:03:46,530 --> 00:03:48,430 It means we get to improve our code. 36 00:03:48,710 --> 00:03:52,040 So I'm going to add a method that returns a bullion named is empty. 37 00:03:59,550 --> 00:04:03,180 And it's going to return a boolean that indicates if the array list is empty. 38 00:04:10,490 --> 00:04:11,780 So back here, I'll say. 39 00:04:13,010 --> 00:04:13,880 If Kurt. 40 00:04:16,529 --> 00:04:17,100 Is empty. 41 00:04:17,640 --> 00:04:18,930 I'll restart the while loop. 42 00:04:31,940 --> 00:04:33,880 Try to remove from an empty cart. 43 00:04:35,640 --> 00:04:36,600 And we're all good. 44 00:04:39,270 --> 00:04:44,780 Now, what if I tried to check out an empty cart this time, the checkout method throws an illegal STD 45 00:04:44,790 --> 00:04:45,320 exception. 46 00:04:45,750 --> 00:04:49,680 That's because our app is calling checkout when there is nothing inside the cart. 47 00:04:49,680 --> 00:04:51,120 And that's just bad code. 48 00:04:51,120 --> 00:04:53,180 It's telling us to improve our code. 49 00:04:53,190 --> 00:04:54,750 So once again, will check. 50 00:04:57,470 --> 00:04:58,370 If Kurt. 51 00:05:00,010 --> 00:05:00,700 Is empty. 52 00:05:03,510 --> 00:05:05,010 Then restart the wire loop. 53 00:05:07,990 --> 00:05:12,790 And with this final line of code, I'm proud to say we're done building the shopping cart application, 54 00:05:13,280 --> 00:05:14,560 I'm going to run the app. 55 00:05:16,050 --> 00:05:20,660 And now I can just keep playing around with it with full confidence that it's always going to work. 56 00:05:22,530 --> 00:05:27,750 And it's going to keep letting me do my shopping, this app is really rewarding because it involves 57 00:05:27,750 --> 00:05:29,370 everything from two to you raise. 58 00:05:29,970 --> 00:05:33,990 We created objects of three different classes and we made them work together. 59 00:05:35,240 --> 00:05:40,510 We implemented exception handling, you learn how to write an equals method to compare it to objects, 60 00:05:41,300 --> 00:05:44,390 you've come this far and I hope you're proud of yourself.