The last step is to bulletproof the application, we're going to make sure the application doesn't fail
or crash no matter what happens right now, there are many places where unchecked exceptions could be
thrown during the runtime and cross the application, but we can't allow that to happen.
Let's run the app.
And the first vulnerability is if the user enters the wrong and put the Atheros in input mismatch exception.
And if we get an unchecked exception, it means we have to improve our code.
And what we're missing is code that anticipates a scenario where the user may enter a value that is
not an integer.
So what I'll do is I'll apply conditional assignment here, I'll check if scanned on has next and if
the next thing the user inputs is an integer, then we'll pick up the input using next and otherwise
we'll assign it the value of 404.
And how convenient is it that we already have a next line right here?
Because if 404 returns, we still need to pick up the input.
Otherwise things are going to get out of whack and next line can pick up anything.
So that's perfect.
Same thing here, if the next input that scanner reads is an integer.
Then we'll pick it up, otherwise we'll just assign the value for for.
And now before creating the object, I'll just say if either value is Foro for.
Then just restart the wire loop.
Rewriting the code.
I'm going to say, don't know how beautiful.
And the next vulnerability in the app is if the user index is outside the bounds of the array and the
app throws an array index out of bounds, exception, unchecked exceptions are the result of badly written
code.
And what we're missing in the code is logic that anticipates the user indexing on a bounce.
So what I'll do is I'll see if the aisle number is smaller than zero or bigger than six.
Or if the column number is smaller than zero.
Or bigger than two.
Then the user is indexing outside the bounds of the array, so we have to restart the loop.
If I index outside the bounds of the array, it just restarts the loop and doesn't crash, perfect.
Last thing we might get is an illegal state exception if I try to remove from an empty cart, the app
is going to throw an illegal state exception.
This exception is thrown by the remove method, and that's because we shouldn't be calling the remove
function when there's nothing in the cart that's just bad and I'm glad it crashed.
It means we get to improve our code.
So I'm going to add a method that returns a bullion named is empty.
And it's going to return a boolean that indicates if the array list is empty.
So back here, I'll say.
If Kurt.
Is empty.
I'll restart the while loop.
Try to remove from an empty cart.
And we're all good.
Now, what if I tried to check out an empty cart this time, the checkout method throws an illegal STD
exception.
That's because our app is calling checkout when there is nothing inside the cart.
And that's just bad code.
It's telling us to improve our code.
So once again, will check.
If Kurt.
Is empty.
Then restart the wire loop.
And with this final line of code, I'm proud to say we're done building the shopping cart application,
I'm going to run the app.
And now I can just keep playing around with it with full confidence that it's always going to work.
And it's going to keep letting me do my shopping, this app is really rewarding because it involves
everything from two to you raise.
We created objects of three different classes and we made them work together.
We implemented exception handling, you learn how to write an equals method to compare it to objects,
you've come this far and I hope you're proud of yourself.