Objectifying Coffee Vending Machine¶
Goal¶
As we did in MealOrder
, you will pull out the order contents, i.e., the size of coffee and the creamer, into separate classes.
For reference, the MealOrder, Burger, etc. classes are here.
A. Extract to Separate Classes¶
-
Pull out the size and creamer aspects of the coffee into their own classes.
- First extract the size and creamer tests into their own classes, e.g.,
SizeTest
andCreamerTest
. - Then extract the code into the classes.
- Make sure those tests work before moving on.
- First extract the size and creamer tests into their own classes, e.g.,
-
Change
CoffeeOrder
so that it has references to theSize
andCreamer
objects.- Ask yourself how
CoffeeOrder
knows how to create the appropriateSize
andCreamer
objects.
- Ask yourself how
-
Change the
price()
method onCoffeeOrder
so that it delegates the price to the size and creamer objects. -
Make sure all of your tests continue to pass!
B. Generalize Size and Creamer¶
-
Create a "base" class,
CoffeeItem
that just has a price method:public int price() { return 0; }
-
Change
Size
andCreamer
so that they inherit (extends
) from the base class and override theprice()
method. -
All tests should still pass!
C. CoffeeOrder Has List of Items¶
-
Change
CoffeeOrder
to have aList
ofCoffeeItem
instead of references to size and creamer.- That is a member (instance) variable:
List<CoffeeItem> items = new ArrayList<>();
- That is a member (instance) variable:
-
Update the
price()
method inCoffeeOrder
so that it sums over all of the items using afor
loop.- You can find more info about this "enhanced" form of the
for
loop here
- You can find more info about this "enhanced" form of the
-
Are all tests still passing?
Once you've completed the above steps, do not move on until you have checked in with the instructor.
D. Add Sweetener Item¶
-
Add a new item,
Sweetener
, that has these options, writing code test-first:None
: 0 centsSugar
: 10 centsSplenda
: 15 cents
-
Make sure you have tests that try out a full coffee order with and without sugar.
E. Support New Coffee Size¶
-
Add a new size of coffee that is "XL" (extra large) and costs $3.
- Think about what you need to change.
- How much is $3 in cents?
-
Make sure all tests continue to pass.
F. Print Out a Coffee Order¶
-
Create a new
display()
method on CoffeeOrder that displays to the screen (usingSystem.out.println()
) the coffee order.For example:
Size: Medium Creamer: Milk Sweetener: Sugar Price: 185
-
Only display Creamer or Sweetener if there were added, otherwise leave them out. For example, a Large coffee order with no additions would display as:
Size: Large Price: 200
-
Think about what is in common across the different types of
CoffeeItem
subclasses -- how is this similar toprice()
?
-
-
Create a new class (the main source code),
CoffeeVendingMachine
and implement thepublic static void main(String[] args)
method that instantiates a few differentCoffeeOrder
s and have them displayed.
Once you've completed the above steps, do not move on until you have checked in with the instructor.
The next step is optional...
(Optional) G. Display as Dollars¶
-
When displaying the CoffeeOrder, display the price as a dollar amount instead of in cents as in the highlighted line below:
Size: Medium Creamer: Milk Sweetener: Sugar Price: $1.85