Java Bootcamp – Workbooks and Challenges
)}

Shopping Cart – Part 2

Goal: Quality control the Item 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 an object calls its method at a "bad time" (object not in a legal state).

Throwing an unchecked exception forces the caller to improve their code.

Task 1 – Item Constructor


In the Item constructor, throw an IllegalArgumentException if the:

  • name is null or blank.

  • price is less than 0.

Task 2 – setName


Inside setName, throw an IllegalArgumentException if the caller passes a name that is blank or null.

Task 3 – setPrice


Inside setPrice, throw an IllegalArgumentException if the price is less than 0.

That's all!

You added checkpoints to the Item class. Each checkpoint forbids the caller from misusing the methods/constructors.

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.