Java Bootcamp – Workbooks and Challenges
)}

Dealership Workbook – Part 1

Goal: Quality control the Car and Dealership classes.

Setting up

From Java Bootcamp Resources -> Module 2 -> 8\. Exception Handling -> Exception Handling Workbooks, open the folder dealership.

Unchecked exceptions

An unchecked exception crashes the app as a result of badly written code.

You should throw an:

  • IllegalArgumentException when the caller passes bad arguments into a method/constructor.

  • IllegalStateException when an object calls its method at a "bad time".

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

Task 1 – Run the code


If you run the code, both cars have negative price values and blank make values.

These values don't make sense. You need to apply quality control and forbid the caller from passing such values.

Task 2 – Car Constructor


The first place to apply quality control is the Car constructor.

  • price < 0:

    • throw new IllegalArgumentException("Price cannot be less than zero");
  • make == null || make.isBlank()

    • throw new IllegalArgumentException("make cannot be null/blank");

Expected Output:

The application throws an IllegalArgumentException. It tells you price cannot be less than 0. So, fix the price.

Expected Output:

The application throws another IllegalArgumentException. It tells you make cannot be blank. So, fix your code.

Expected Output:

By this point, the caller is correctly using the constructor. However, the second car's make and price don't make any sense.

Task 3 – Setters


The setter is another place where quality control needs to take place.

  • setPrice

    • if price < 0: throw new IllegalArgumentException("Price cannot be less than zero");
  • setMake

    • if make == null || make.isBlank(): throw new IllegalArgumentException("make cannot be null/blank");

Expected Output:

The application throws an IllegalArgumentException. It forces the caller (you) to pass a valid make value.

Expected Output:

The applications throws another IllegalArgumentException. It tells you price cannot be less than zero. So, fix your code.

Expected Output:

Task 4 – Inspecting the Dealership class


  1. Should the Dealership constructor check for a null?

    • Nope. The code would already throw a NullPointerException. So, throwing another exception would be redundant.
  2. Should the setCar setter check a null?

    • Nope. The code would already throw a NullPointerException. So, throwing another exception would be redundant.
  3. Should the sell method check if the index is valid?

    • Nope. The code would already throw an ArrayIndexOutOfBoundsException. So, throwing another exception would be redundant.
  4. Should sell throw an exception if the indexed object is null?

    • Not necessary. The code will already throw a NullPointerException. So, throwing another exception would be redundant.
  5. Is there a need to throw an IllegalStateException anywhere?

    • Yes. If the dealership is empty, then it's not in a valid state to call the sell method.

Hint: create an isEmpty method inside the Dealership class.

/** * Name: isEmpty * * @return (boolean) * Inside the function: * - return true if there are no more cars. * - return false otherwise. * */

Call the isEmpty() method from sell(). If the result is true, throw an IllegalStateException.

That's all!

You added checkpoints to the Car and Dealership classes. 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.