Java Bootcamp – Workbooks and Challenges
)}

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


  1. The constructor doesn't receive any parameters so there's nothing to check.

  2. Should the setMovie setter check for a null? No.

    • The code would already throw a NullPointerException. So, throwing an IllegalArgumentException would be redundant.
  3. Should the addMovie method check for a null? No.

    • The code would already throw a NullPointerException. So, throwing an IllegalArgumentException would be redundant.
  4. Should action throw an IllegalArgumentException? Yes.

    • The method must consider a parameter that isn't sell, rent, or return illegal.

    • The method must consider a name that is blank or null illegal.

  5. 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 the action 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.

Feedback Summary
4.8
38 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.