Skip to content

Enum for Safety


In this lab, you'll add a new constructor for your CoffeeOrder that takes a SizeOption enum instead of a String, so that we can be sure it's used correctly at compile-time instead of getting a runtime error.

Step 1: Create an Enum

  • Create an enum called SizeOption that has three choices: SMALL, MEDIUM, and LARGE

    Note: If you implemented Extra Large in the prior labs, then also create an enum named EXTRA_LARGE

    Java Style

    It's standard coding style to have Enums be all upper-case.

    Enum Example

    Here's an example of an enum:

    public enum Priority {

Step 2: Write a New Test

Create a new CoffeeOrder test that uses this new enum to create a coffee order, something like this:

CoffeeOrder order = new CoffeeOrder(SizeOption.LARGE);


This will fail to compile, which you'll fix in the next step.

Step 3: Create Constructor

  • Create a new constructor in CoffeeOrder that has SizeOption as a parameter.

  • Convert as appropriate, the SizeOption enum to a string so you can use the existing Size class.

  • Make sure all tests pass.

Once you've completed the above steps, check in with the instructor to review your code.