Enum for Safety¶
Goal¶
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
enumcalledSizeOptionthat has three choices:SMALL,MEDIUM, andLARGENote: If you implemented Extra Large in the prior labs, then also create an enum named
EXTRA_LARGEJava 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 { HIGH, MEDIUM, LOW }
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);
assertThat(order.price())
.isEqualTo(200);
This will fail to compile, which you'll fix in the next step.
Step 3: Create Constructor¶
-
Create a new constructor in
CoffeeOrderthat hasSizeOptionas a parameter. -
Convert as appropriate, the
SizeOptionenum to a string so you can use the existingSizeclass. -
Make sure all tests pass.
Once you've completed the above steps, check in with the instructor to review your code.
Once you've completed the above steps, check in with the instructor to review your code.