Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions assignments/strategy_pattern/Coffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package assignments.strategy_pattern;

public class Coffee extends Drink {
@Override
public void createDrink() {
System.out.println("Drip hot water through filter and ground coffee.");
}

@Override
public void addMore(boolean addition) {
this.addition = addition;

if (addition) {
System.out.println("Add milk and sugar.");
}
}
}
25 changes: 25 additions & 0 deletions assignments/strategy_pattern/Drink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package assignments.strategy_pattern;

public abstract class Drink {
boolean addition = true;

public final void order(String addition) {
boilWater();
createDrink();
serveDrink();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AM-1: The hook method should be on the base class. The subclasses can implement "helper" private methods, but not other public methods.

}

public final void boilWater() {
System.out.println("Boil Water.");
}

public abstract void createDrink();

public void addMore(boolean addition) {
this.addition = addition;
}

public final void serveDrink() {
System.out.println("Pour into cup.");
}
}
24 changes: 24 additions & 0 deletions assignments/strategy_pattern/Egg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package assignments.strategy_pattern;

public class Egg extends Food {
@Override
public void gatherIngredients() {
super.gatherIngredients();
System.out.println("Get 2 eggs.");
System.out.println("Get 1 tablespoon of butter.");
System.out.println("Get salt.");
System.out.println("Get pepper.");
}

@Override
public void prepareFood() {
super.prepareFood();
System.out.println("Crack the eggs into a bowl.");
System.out.println("Wisk the eggs.");
System.out.println("Melt butter in pan on medium heat.");
System.out.println("When the butter is melted and hot, pour in eggs.");
System.out.println("Fold eggs as they cook.");
System.out.println("When eggs are cooked through turn off stove.");
System.out.println("Add salt and pepper to taste.");
}
}
23 changes: 23 additions & 0 deletions assignments/strategy_pattern/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package assignments.strategy_pattern;

public abstract class Food {
public final void order() {
gatherIngredients();
prepareFood();
serveFood();
}

public void gatherIngredients() {
System.out.println("Gather your ingredients.");
}

public void prepareFood() {
System.out.println("Prepare your food.");
}

public final void serveFood() {
System.out.println("Serve the food.");
System.out.println("Serve on plate with any condiments you want.");
System.out.println("Enjoy!");
}
}
27 changes: 27 additions & 0 deletions assignments/strategy_pattern/Pancakes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package assignments.strategy_pattern;

public class Pancakes extends Food {
@Override
public void gatherIngredients() {
super.gatherIngredients();
System.out.println("Get 1 and 1/2 cups flour.");
System.out.println("Get 3 and 1/2 teaspoons baking powder.");
System.out.println("Get 2 tablespoon sugar.");
System.out.println("Get 1 1/4 cups milk.");
System.out.println("Get 3 tablespoons of melted butter.");
System.out.println("Get 1 egg.");
}

@Override
public void prepareFood() {
super.prepareFood();
System.out.println("Sift flour, baking powder, sugar, and salt together in a large bowl.");
System.out.println("Make a well in the center and add milk, melted butter, and egg.");
System.out.println("Mix until smooth.");
System.out.println("Heat a lightly oiled griddle or pan over medium-high heat.");
System.out.println("Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake.");
System.out.println("Cook until bubbles form and the edges are dry, about 2 to 3 minutes.");
System.out.println("Flip and cook until browned on the other side.");
System.out.println("Repeat with remaining batter.");
}
}
8 changes: 8 additions & 0 deletions assignments/strategy_pattern/Tea.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package assignments.strategy_pattern;

public class Tea extends Drink {
@Override
public void createDrink() {
System.out.println("Steep the tea.");
}
}
53 changes: 53 additions & 0 deletions assignments/strategy_pattern/TemplateMethodRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package assignments.strategy_pattern;

import java.util.Scanner;

public class TemplateMethodRecipe {

public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String userChoice;
Food userFood = null;
Drink userDrink = null;

System.out.println("Hello! We are going to learn how to make breakfast!");
System.out.println("What do you want for breakfast?");
System.out.println("Choose eggs or pancakes.");

userChoice = userInput.nextLine();

if (userChoice.equalsIgnoreCase("eggs")) {
userFood = new Egg();
} else if (userChoice.equalsIgnoreCase("pancakes")) {
userFood = new Pancakes();
}

userFood.order();

System.out.println("Would you like coffee or tea as well?");

userChoice = userInput.nextLine();

if (userChoice.equalsIgnoreCase("coffee")) {
userDrink = new Coffee();
} else if (userChoice.equalsIgnoreCase("tea")) {
userDrink = new Tea();
}

userDrink.boilWater();
userDrink.createDrink();
userDrink.serveDrink();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AM-2: You should only be calling the order method. Without it you are not enforcing the pattern.


if (userChoice.equalsIgnoreCase("coffee")) {
System.out.println("Would you like milk and sugar with your coffee (y/n)?");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AM-1.5: The code inside here should be the hook method, and in the base class you default it to false. As this is now the Drinks are not using the template method.

userChoice = userInput.nextLine();
if (userChoice.equalsIgnoreCase("y") || userChoice.equalsIgnoreCase("yes")) {
userDrink.addMore(true);
}
}

System.out.println("Enjoy your Breakfast!");

userInput.close();
}
}