The very last step is to bulletproof the application, in other words, to make sure the application
doesn't fail or crash no matter what happens right now, there are many scenarios where an unchecked
exception could be thrown during the run time and crash the application.
We can't allow that to happen.
So let's run the up.
The first vulnerability in the app is if the user enters a blank movie name.
The movie constructor is going to throw an illegal argument exception, and as you know by now, if
we get an unchecked exception, it's our fault and we have to fix it.
And what we're missing is code that anticipates a scenario where the user may enter a blank name.
So here will say if name is blank.
Then we'll print the following message.
Then we'll restart the loop with continue.
OK, let's run our code.
And if we had a blank name, our application handles the response with grace.
You know what, if I rent a movie?
Then I'll try to purchase that same movie.
And the Atheros, an illegal state exception, crashing the app, we cannot allow the user to purchase
a movie that has already been rented.
So I'll go back in main.
And I'm going to check if the movie is rented.
Wait, how do I do that?
Right now, we can't we need to declare a method inside Staudt Java that returns a movie based on a
name.
So public movie get movie.
It's going to take one parameter string name.
I'm going to create a loop that runs through the size of the array list.
And will say if movies.
We'll get the movie at the request index.
doGet name will check if it equals the name being Pastan.
Then we're going to return the movie object at that inducts.
Otherwise, we're going to return now.
Now, back in Maine, I can get the movie the user is interested in iStore get movie.
We need to check if it's not available.
And if that's the case, will print the following message.
Movie is not available for purchase and restart the while loop.
OK, let's test this out.
I'm going to rent a movie.
Try to purchase that same movie.
And the application responds with Grace, it's not available for purchase.
Now, I'm going to try to purchase another movie, but I'm going to type a movie that isn't on their.
We'll say Django.
And it crashes null pointer exception.
The issue is that the function we just added returns and null if it can't find the movie.
And trying to dot syntax from a nul is going to result in a null pointer exception.
So we need to handle that as well.
So here, instead of checking if the name is blank, I can just make the condition check if the movie
they added doesn't exist.
This condition would account for everything.
Kate running the app.
I'll put a movie that doesn't exist in the collection, we're a good.
I'm going to put a blank movie, we're good.
Our condition is perfect.
And with this final line of code, I'm proud to say that we're all finished no matter what we do, the
application is going to respond with Grace and it's working really well, just like that.
Our application is bulletproof.
No matter what the user throws at us, the application is free of bugs and it's never going to crash.
It can process any input and respond with grace.
In the end, exception handling, which involves catching checked exceptions and fixing unchecked exceptions,
is the key to building bulletproof applications.