From 0a5d7f59195aad5e920988c8c063080d2a2af653 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 Nov 2018 20:02:02 +0100 Subject: [PATCH] #28 sending updated method via master as before discussed --- src/main/java/verkocht/model/CookingBook.java | 45 ++++++------------- src/main/java/verkocht/model/Recipe.java | 7 +-- src/test/java/test/CookingBookTest.java | 20 ++++++++- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/verkocht/model/CookingBook.java b/src/main/java/verkocht/model/CookingBook.java index 809a5c9..faa3a7a 100644 --- a/src/main/java/verkocht/model/CookingBook.java +++ b/src/main/java/verkocht/model/CookingBook.java @@ -30,7 +30,7 @@ public CookingBook() { // Schnitzel - Recipe schniztelRec = new Recipe("schnitzel"); + Recipe schniztelRec = new Recipe("schnitzel", Category.MEAT); schniztelRec. getIngredients().put(getIngredientByName("Fleisch"),200); schniztelRec. getIngredients().put(getIngredientByName("Ei"),1); schniztelRec. getIngredients().put(getIngredientByName("Mehl"),100); @@ -48,7 +48,7 @@ public CookingBook() { // Erdbeermilchshake - Recipe milchshakeRec = new Recipe("milchshake"); + Recipe milchshakeRec = new Recipe("milchshake", Category.VEGETARIAN); milchshakeRec.getIngredients().put(getIngredientByName("Erdbeeren"),200); milchshakeRec.getIngredients().put(getIngredientByName("Milch"),400); milchshakeRec.getIngredients().put(getIngredientByName("Zucker"),20); @@ -60,7 +60,7 @@ public CookingBook() { allRecipes.add(milchshakeRec); // Nudeln mit Tomatensauce - Recipe nudelnMitTomatensoßeRec = new Recipe("nudelnMitTomatensoße"); + Recipe nudelnMitTomatensoßeRec = new Recipe("nudelnMitTomatensoße", Category.VEGAN); nudelnMitTomatensoßeRec.getIngredients().put(getIngredientByName("Nudeln"),200); nudelnMitTomatensoßeRec.getIngredients().put(getIngredientByName("Salz"),1); nudelnMitTomatensoßeRec.getIngredients().put(getIngredientByName("Tomatensauce"),100); @@ -85,37 +85,18 @@ public Recipe findByName(String name) { } /** - * + * This method filters all recipes by their category * @param category - * @return + * @return list of filtered recipes */ - public List findByCategory(Category category) { - List a = null; - switch (category) { - case MEAT: - for (int i = 0; i <= allRecipes.size(); i++) { - if(allRecipes.get(i).getCategory() == category.MEAT) { - a.add(allRecipes.get(i)); - } - } - return a; - case VEGETARIAN: - for (int i = 0; i <= allRecipes.size(); i++) { - if(allRecipes.get(i).getCategory() == category.VEGETARIAN) { - a.add(allRecipes.get(i)); - } - } - return a; - case VEGAN: - for (int i = 0; i <= allRecipes.size(); i++) { - if(allRecipes.get(i).getCategory() == category.VEGAN) { - a.add(allRecipes.get(i)); - } - } - return a; - default: - throw new IllegalArgumentException("Unbekannte Kategorie: "+ category); - } + public List findByCategory(Category category) { + List a = new ArrayList(); + for (int i = 0; i < allRecipes.size(); i++) { + if(allRecipes.get(i).getCategory() == category) { + a.add(allRecipes.get(i)); + } + } + return a; } /** diff --git a/src/main/java/verkocht/model/Recipe.java b/src/main/java/verkocht/model/Recipe.java index fae12ff..e5ced6f 100644 --- a/src/main/java/verkocht/model/Recipe.java +++ b/src/main/java/verkocht/model/Recipe.java @@ -16,9 +16,10 @@ public class Recipe { private Category category; private Map ingredientAmounts = new HashMap (); - - public Recipe(String name) { - this.name = name; } + public Recipe(String name, Category category) { + this.name = name; + this.category = category; + } public Category getCategory() { return category; diff --git a/src/test/java/test/CookingBookTest.java b/src/test/java/test/CookingBookTest.java index 4dc325e..479f0c1 100644 --- a/src/test/java/test/CookingBookTest.java +++ b/src/test/java/test/CookingBookTest.java @@ -2,10 +2,13 @@ import static org.junit.Assert.assertEquals; +import java.util.ArrayList; +import java.util.List; + import org.junit.Test; -import verkocht.model.CookingBook; +import verkocht.model.*; public class CookingBookTest { CookingBook cookingBook = new CookingBook(); @@ -31,6 +34,19 @@ public void testConstructor() { assertEquals ("Zuerst wird wird das Mehl auf einen Teller gestreut.", cookingBook.findByName("schnitzel").getSteps().get(0)); assertEquals ("Alle Zutaten mit einem Mixer vermischen.", cookingBook.findByName("milchshake").getSteps().get(1)); assertEquals ("Die Nudeln abgießen und mit der Sauce heiß servieren.", cookingBook.findByName("nudelnMitTomatensoße").getSteps().get(2)); - } + + @Test + public void testFindByCategory() { + List b = new ArrayList(); + b.add(cookingBook.getAllRecipes().get(0)); + assertEquals(cookingBook.findByCategory(Category.MEAT),b); + b.clear(); + b.add(cookingBook.getAllRecipes().get(1)); + assertEquals(cookingBook.findByCategory(Category.VEGETARIAN),b); + b.clear(); + b.add(cookingBook.getAllRecipes().get(2)); + assertEquals(cookingBook.findByCategory(Category.VEGAN),b); + } + } \ No newline at end of file