Movie Store – Part 4
Goal: Quality control the Store
class.
Unchecked exceptions
An unchecked exception crashes the app as a result of badly written code.
You should throw an:
-
IllegalArgumentException
when the caller passes faulty arguments into a method/constructor. -
IllegalStateException
when the caller invokes a method at a bad time (object not in a valid state).
Throwing an unchecked exception forces the caller to improve their code.
Task 1 – Inspecting the Store
class
-
The constructor doesn't receive any parameters so there's nothing to check.
-
Should the
setMovie
setter check for anull
? No.- The code would already throw a
NullPointerException
. So, throwing anIllegalArgumentException
would be redundant.
- The code would already throw a
-
Should the
addMovie
method check for anull
? No.- The code would already throw a
NullPointerException
. So, throwing anIllegalArgumentException
would be redundant.
- The code would already throw a
-
Should
action
throw anIllegalArgumentException
? Yes.-
The method must consider a parameter that isn't
sell
,rent
, orreturn
illegal. -
The method must consider a name that is
blank
ornull
illegal.
-
-
Is there a need to throw an
IllegalStateException
anywhere? Yes, in two places.-
If the
movies
ArrayList
is empty, the store is not in a valid state to call theaction
method. -
The store cannot sell a movie that has already been rented. The state of the
Movie
object would be inappropriate for this operation.
-
That's all!
You added checkpoints to the Store
class. Each checkpoint forbids the caller from misusing the methods/constructors.