Coffee Vending Machine¶
The goal of this lab is to write code that will provide the price for a cup of coffee, similar to the MealOrder code we wrote in class. For reference, that code is here.
The coffee order has the following options with pricing:
Category | Choices | Price (cents) |
---|---|---|
Size | Small | 100 |
Medium | 150 | |
Large | 200 | |
Creamer | None | 0 |
Milk | 25 | |
Half-n-half | 35 |
A. Create CoffeeOrder Class¶
Create a new, empty class CoffeeOrder under the src/main/java/com/welltestedlearning/cvm
directory.
- Open up the Project Tool Window on the left side, press
Command + 1
. - Right-click (
Ctrl + Click
) on thecvm
directory and chooseNew >
and thenJava Class
. - Name it
CoffeeOrder
B. Write the Test First¶
-
Create a new test class called
CoffeeOrderTest
under the test directory:src/test/java/com/welltestedlearning/cvm
- Right-click on the
cvm
underneathtest
and chooseNew >
and thenJava Class
. - Name it
CoffeeOrderTest
- Right-click on the
-
Add the following
import
statements above the class name:import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat;
-
Create a test method
smallCoffeeIs100
and write test code that asserts a newCoffeeOrder
with a"small"
coffee has a price is100
. It should look something like this:@Test public void smallCoffeeIs100() throws Exception { }
-
Inside the test method:
- Instantiate the
CoffeeOrder
class (this is the "given" part) - Call a method named
coffeeSize()
and pass in"small"
-- you'll need to create this method in the class, but do not implement any code yet. - Now assert that calling
price()
returns 100
- Instantiate the
C. Test Fail and Pass¶
-
Run the test (right-click on the test and run it) and it should fail.
-
Write the minimum amount of code in
CoffeeOrder
to make the test pass.
D. Create More Size Tests¶
-
Create tests for the other size options (Medium and Large), writing the test first and then writing the minimum amount of code to make each test pass.
- Make sure to use the Price List at the top of this lab.
-
Create a test for an "empty" Coffee order (just a
new
order where you haven't specified a size) that should have a price of0
. -
Run all of the tests in
CoffeeOrderTest
and don't move until until they all pass.
Note
You should have at least 4 test methods, one for each size and one for an empty order.
Once you've completed the above steps, do not move on until you have checked in with the instructor.
E. Support Creamer¶
-
Add tests for Creamer options and then write the code to support that. For example:
@Test public void milkCreamerIs25() throws Exception { CoffeeOrder coffeeOrder = new CoffeeOrder(); coffeeOrder.creamer("milk"); assertThat(coffeeOrder.price()) .isEqualTo(25); }
-
Make sure you have tests for the other creamer options:
none
andhalf-n-half
.
F. Support Size + Creamer¶
Write tests that exercise having both the Size and Creamer options set, e.g., a coffee order with size Medium and Milk must return 175.
Once you've completed the above steps, check in with the instructor.