diff --git a/assignments/strategy_pattern/Coffee.java b/assignments/strategy_pattern/Coffee.java new file mode 100644 index 0000000..e3d7db3 --- /dev/null +++ b/assignments/strategy_pattern/Coffee.java @@ -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."); + } + } +} diff --git a/assignments/strategy_pattern/Drink.java b/assignments/strategy_pattern/Drink.java new file mode 100644 index 0000000..0e081c6 --- /dev/null +++ b/assignments/strategy_pattern/Drink.java @@ -0,0 +1,25 @@ +package assignments.strategy_pattern; + +public abstract class Drink { + boolean addition = true; + + public final void order(String addition) { + boilWater(); + createDrink(); + serveDrink(); + } + + 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."); + } +} diff --git a/assignments/strategy_pattern/Egg.java b/assignments/strategy_pattern/Egg.java new file mode 100644 index 0000000..63a0b00 --- /dev/null +++ b/assignments/strategy_pattern/Egg.java @@ -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."); + } +} diff --git a/assignments/strategy_pattern/Food.java b/assignments/strategy_pattern/Food.java new file mode 100644 index 0000000..6e39d6f --- /dev/null +++ b/assignments/strategy_pattern/Food.java @@ -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!"); + } +} diff --git a/assignments/strategy_pattern/Pancakes.java b/assignments/strategy_pattern/Pancakes.java new file mode 100644 index 0000000..505b1bf --- /dev/null +++ b/assignments/strategy_pattern/Pancakes.java @@ -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."); + } +} diff --git a/assignments/strategy_pattern/Tea.java b/assignments/strategy_pattern/Tea.java new file mode 100644 index 0000000..cae0560 --- /dev/null +++ b/assignments/strategy_pattern/Tea.java @@ -0,0 +1,8 @@ +package assignments.strategy_pattern; + +public class Tea extends Drink { + @Override + public void createDrink() { + System.out.println("Steep the tea."); + } +} diff --git a/assignments/strategy_pattern/TemplateMethodRecipe.java b/assignments/strategy_pattern/TemplateMethodRecipe.java new file mode 100644 index 0000000..31bac57 --- /dev/null +++ b/assignments/strategy_pattern/TemplateMethodRecipe.java @@ -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(); + + if (userChoice.equalsIgnoreCase("coffee")) { + System.out.println("Would you like milk and sugar with your coffee (y/n)?"); + userChoice = userInput.nextLine(); + if (userChoice.equalsIgnoreCase("y") || userChoice.equalsIgnoreCase("yes")) { + userDrink.addMore(true); + } + } + + System.out.println("Enjoy your Breakfast!"); + + userInput.close(); + } +} \ No newline at end of file