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
-
Should the
Dealership
constructor check for anull
?- Nope. The code would already throw a
NullPointerException
. So, throwing another exception would be redundant.
- Nope. The code would already throw a
-
Should the
setCar
setter check anull
?- Nope. The code would already throw a
NullPointerException
. So, throwing another exception would be redundant.
- Nope. The code would already throw a
-
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.
- Nope. The code would already throw an
-
Should
sell
throw an exception if the indexed object isnull
?- Not necessary. The code will already throw a
NullPointerException
. So, throwing another exception would be redundant.
- Not necessary. The code will already throw a
-
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.
- Yes. If the dealership is empty, then it's not in a valid state to call the
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.