The goal of this workbook was to quality control the item in machine classes, because if you're on
the out the way it is.
Some of the items have negative prices, negative quantities and blank names.
Once again, these values don't make any sense and the caller should be forbidden from passing them.
So when the item constructor, we're going to start by throwing an illegal argument exception, if the
name is blank, so if name is equal to null.
Or if the name is blank.
Throw new.
A legal argument, exception name cannot be null or blank.
We're going to throw another exception if the price is less than zero, so if the price is less than
zero, there are a new illegal argument exception.
Price cannot be less than zero.
We need to throw another exception if the quantity is less than zero.
In which case will once again throw a new illegal argument, exception quantity cannot be less than
zero.
Katri around the code.
The constructor forbids the caller from passing a legal values.
That's good.
This unchecked exception is forcing us to fix our code.
So we're going to copy over the values from the workbook.
Rerunning, running the app.
And now we're good.
Remember that centers are another place where the color may pass on illegal values, so we need to apply
quality control there as well.
So inside set name right through another illegal argument, exception, if the caller passes a name
that is no or a blank.
There are new legal argument, exception name cannot be null or blank.
We'll do the same thing and set price if the culture passes a price that is less than zero, we'll throw
a new illegal.
Are you an exception?
Price cannot be less than zero.
Last one quantity, if they pass a quantity that is less than zero, throw a new illegal argument,
exception quantity cannot be less than zero.
Kate, now in the machine class, there is no need to throw a legal argument, exceptions in the constructor
again, worst thing that could happen is a null being passed, but the output already throw a null pointer
exception and the same can be said for set item.
But what if the caller passes an invalid index here?
Well, once again, the code would already throw an array index out of bounds exception.
Same thing can be said about the center.
So throwing another exception wouldn't make any sense.
However, we can expect an illegal argument right here.
There's a chance the caller may pass on an item with a quantity of zero.
And in such a case, we need to index the item that they're interested in.
And check if it's quantity is equal to zero.
If that's the case, we need to throw an illegal argument exception.
But says cannot dispense an item with a quantity of zero.
That's all for a legal argument, exceptions.
What about illegal state exceptions?
Well, looking at this class, I can't really think of a scenario when a machine object can call a method
at a bad time.
Basically, the machine object would always be in a valid state to call any of its methods, and that's
really it.
You had to check points to the item in machine and classes, and every checkpoint forbids the caller
from misusing the methods or constructor's.