The goal of party was to bulletproof the application.
So let's run the application and put it to the test.
Instead of an integer, Oliner, a string.
And the application crashes, that's not good, we get an input mismatch, exception and unchecked exception
because it happens during the runtime and unchecked implies that there's something wrong, there's something
missing in our code.
And what's missing is code that anticipates a scenario where the user enters a type mismatch.
So here we can say if skin that has next intent is not true.
I'll pick it up using next line.
Then I'll print invalid input.
And restart the wire loop.
OK, let's test it out.
A mismatch.
Henry, good.
Now, what if I enter zero or one, then I enter a mismatch.
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
just to make sure.
And I'm going to use breakpoints to visualize the runtime.
So we'll enter a number.
Next will enter a string.
I see next line gets skipped and is consumed by empty space.
It's the classic next line trap when next line runs after the next and next double or next, it's going
to get wasted by empty space.
And you're well aware by now that the solution is that a throwaway next line after the next int?
That way, the throw away consumes the blank space and our other next line can do what it's supposed
to do.
Rewriting the code.
I'll put a valid index, then I'll put a string perfect's.
Now I'm going to pass an invalid index.
And we got another unchecked exception array index out of bounds, so how do we stop this crash from
happening?
Somehow I need to check if the index exceeds the bounds of the array.
How do I access the length of the cars field inside dealership?
I'm not going to use this array because imagine this data was being loaded from a file, which is something
that we're going to be doing very soon.
And in that case, there wouldn't even be an array.
So what I'm going to do is define a method inside the dealership class.
The returns, an integer.
And it will return the length of the era.
And now back here, I'm going to say.
If index is less than zero or if the index that was passed in is higher than the number of cars in the
dealership, minus one, because with indexes we start counting from zero, then we'll print line.
Invalid index, I'm pretty sure in the workbook I told you to print something else, but you can print
whatever you want and restart the wallet and that's all I'm going to test this code.
I'll enter a spot of negative one.
To.
Five pin perfect.
All right, now, what happens if I try to buy a car from an empty spot?
It's going to throw a null pointer exception, which once again is an unchecked exception.
So we need to fix our code.
We need to add code that anticipates such a scenario.
So what I'm going to do is say, elss, I'm going to get the car first.
And check if it equals no.
And if it happens to be, no will print.
And we'll restart the wire loop, prompting the user to enter another value.
Trying that again.
And it still crashes, I wonder.
Wow, and if you actually look into the exception, it's coming from the Geter.
What the heck?
Oh, I see, because the object at this index is no passing, not into the new car copy constructor
is going to throw a nail pointer exception.
Here, I'll let you visualize it, I'll put a break point here.
In the first run, I'll buy this card.
And I'm going to keep stepping through the first run.
And in the second run, I'm going to choose the same empty spot.
Now we're in the geter step into the copy constructor.
And here, right here, see how the source object is No.
And trying to access something from a novel is going to crash the application.
It's going to throw a null pointer exception.
So what I'll do here is use a conditional assignment, I'm going to check if the object that's being
requested is null.
And if it is just going to return, no, otherwise we'll return a copy of the object they're trying
to index.
Now, you can also use it for us if you want.
It would look something like this.
But in such a scenario, I don't like using a false when I can use this syntax because it's a lot cleaner.
Coutry running the app.
And we fixed that crash as well.
I right task five was to check if the user wants to continue.
So at the end of the wire loop, I'm going to ask the user.
Types to continue shopping.
And if they type anything that isn't yes.
Then I'll break the loop.
Caltrain out.
I'll buy a one car, but I'll stop here.
Can task six was to break the loop of the dealership is empty.
Remember, we made an empty method inside dealership.
So at the very beginning, we'll check if the dealership is empty.
And what we'll do in that case, if the result is true, will print, we're all sold out.
And break the.
Trying it out now.
I'll purchase a car.
Purchase another.
And Perfect's.
No matter what the user throws at you, the application is free of bugs and it's never going to crash.
It can process any input and respond gracefully.