Java Bootcamp – Workbooks and Challenges
)}

Vending Machine – Part 1

Goal: Quality control the Item and Machine classes.

Setting up

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

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".

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

Run the code


Some of the items have negative price values, negative quantities, and blank names.

Such values are illegal, and the constructor should forbid the caller from passing them.

Task 1 - Item Constructor


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

  • name is null OR blank

    • throw new IllegalArgumentException("name cannot be null/blank.");
  • price is less than zero.

    • throw new IllegalArgumentException("price cannot be less than zero.");
  • quantity is less than zero.

    • throw new IllegalArgumentException("quantity cannot be less than zero.");

Now, the code will keep crashing until the caller passes the correct values.

Task 2 - Use the following values


Item[][] items = new Item[][] { 
{ new Item("Pepsi", 1.99, 3) , new Item("Fresca", 1.49, 3), new Item("Brisk", 2.49, 2) }, 
{ new Item("Fanta", 1.99, 2) , new Item("Barq's", 1.49, 2), new Item("A & W", 2.49, 3) }, 
{ new Item("Crush", 1.99, 2) , new Item("C-Cola", 1.49, 2), new Item("Berry", 2.49, 1) }
};

Task 3 - setName


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

Task 4 - setPrice


Inside setPrice, throw an IllegalArgumentException if the caller passes a price that is less than zero.

Task 5 - setQuantity


Inside setQuantity, throw an IllegalArgumentException if the caller passes a quantity that is less than zero.

Task 6 - Inspecting the Machine class


  1. Should the Machine constructor check for a null? No.

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

    • The code would already throw a NullPointerException. So, throwing another exception would be redundant.
  3. Should the dispense method check if the row and spot are valid? No.

    • The code would already throw an ArrayIndexOutOfBoundsException. So, throwing another exception would be redundant.
  4. Should dispense throw an IllegalArgumentException? Yes.

    • The caller should not call dispense for an item with a quantity of zero.
  5. Is there a need to throw an IllegalStateException anywhere? No.

    • There isn't a clear scenario when the machine object can call a method at a "bad time". In other words, the machine is always in a valid state to call any method.

That's all!

You added checkpoints to the Item and Machine 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.