Skip to content

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

  1. Pull out the size and creamer aspects of the coffee into their own classes.

    1. First extract the size and creamer tests into their own classes, e.g., SizeTest and CreamerTest.
    2. Then extract the code into the classes.
    3. Make sure those tests work before moving on.
  2. Change CoffeeOrder so that it has references to the Size and Creamer objects.

    • Ask yourself how CoffeeOrder knows how to create the appropriate Size and Creamer objects.
  3. Change the price() method on CoffeeOrder so that it delegates the price to the size and creamer objects.

  4. Make sure all of your tests continue to pass!

B. Generalize Size and Creamer

  1. Create a "base" class, CoffeeItem that just has a price method:

    public int price() {
      return 0;
    }
    
  2. Change Size and Creamer so that they inherit (extends) from the base class and override the price() method.

  3. All tests should still pass!

C. CoffeeOrder Has List of Items

  1. Change CoffeeOrder to have a List of CoffeeItem instead of references to size and creamer.

    • That is a member (instance) variable: List<CoffeeItem> items = new ArrayList<>();
  2. Update the price() method in CoffeeOrder so that it sums over all of the items using a for loop.

    • You can find more info about this "enhanced" form of the for loop here
  3. 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

  1. Add a new item, Sweetener, that has these options, writing code test-first:

    • None: 0 cents
    • Sugar: 10 cents
    • Splenda: 15 cents
  2. Make sure you have tests that try out a full coffee order with and without sugar.

E. Support New Coffee Size

  1. 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?
  2. Make sure all tests continue to pass.

F. Print Out a Coffee Order

  1. Create a new display() method on CoffeeOrder that displays to the screen (using System.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 to price()?

  2. Create a new class (the main source code), CoffeeVendingMachine and implement the public static void main(String[] args) method that instantiates a few different CoffeeOrders 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