diff --git a/src/main/java/verkocht/VerkochtStreamHandler.java b/src/main/java/verkocht/VerkochtStreamHandler.java index 97aca4a..492eb3f 100644 --- a/src/main/java/verkocht/VerkochtStreamHandler.java +++ b/src/main/java/verkocht/VerkochtStreamHandler.java @@ -60,7 +60,7 @@ private static Skill getSkill() { new SetNumberOfPeopleIntentHandler(), new TellRecipeStepsIntentHandler(), new TellMeCategoriesIntentHandler()) -// .withSkillId("amzn1.ask.skill.c5bc074f-b96e-4343-ba1e-393f8084fe94") + .withSkillId("amzn1.ask.skill.4c560721-fd0a-48db-9101-37b3f9c67a4d") .build(); } diff --git a/src/main/java/verkocht/handlers/SelectRecipeByCategoryIntentHandler.java b/src/main/java/verkocht/handlers/SelectRecipeByCategoryIntentHandler.java index 6bfb343..9fbd978 100644 --- a/src/main/java/verkocht/handlers/SelectRecipeByCategoryIntentHandler.java +++ b/src/main/java/verkocht/handlers/SelectRecipeByCategoryIntentHandler.java @@ -43,10 +43,9 @@ public boolean canHandle(HandlerInput input) { return input.matches(intentName("SelectRecipeByCategoryIntent")); } - @SuppressWarnings("null") @Override public Optional handle(HandlerInput input) { - String speechText; + String speechText = ""; Request request = input.getRequestEnvelope().getRequest(); IntentRequest intentRequest = (IntentRequest) request; Intent intent = intentRequest.getIntent(); @@ -56,31 +55,34 @@ public Optional handle(HandlerInput input) { String chosenCategorie = chosenCategorieSlot.getValue(); input.getAttributesManager().setSessionAttributes(Collections.singletonMap(CATEGORY_KEY, chosenCategorie)); - - + String actualCategorie = (String) input.getAttributesManager().getSessionAttributes().get(CATEGORY_KEY); - CookingBook cookingBook = new CookingBook(); - Category[] categories = Category.values(); - List foundRecipes = new ArrayList<>(); - if (actualCategorie != null || !actualCategorie.isEmpty()) { - - for (int i = 0; i <= Category.values().length; i++) { - if (categories[i].getName() == actualCategorie) { + if (actualCategorie == null || actualCategorie.isEmpty()) { + speechText = PhrasesForAlexa.CATEGORIES_UNKOWN; + } else { + CookingBook cookingBook = new CookingBook(); + Category[] categories = Category.values(); + List foundRecipes = new ArrayList<>(); + + for (int i = 0; i < categories.length; i++) { + if (categories[i].getName().equalsIgnoreCase(actualCategorie)) { foundRecipes = cookingBook.findByCategory(categories[i]); - } - } - - String responseMessage = null; - for (int i = 0; i <= foundRecipes.size(); i++){ - responseMessage += foundRecipes.get(i).getName(); - } - - speechText = String.format(PhrasesForAlexa.TELL_CATEGORIES, responseMessage); - - } - - speechText = PhrasesForAlexa.CATEGORIES_UNKOWN; + } + } + + String responseMessage = ""; + + if (foundRecipes.size() == 1) { + speechText = PhrasesForAlexa.TELL_CATEGORIES; + } else { + for (int i = 0; i < foundRecipes.size() - 1 ; i++){ + responseMessage += foundRecipes.get(i).getName(); + } + responseMessage += "und" + foundRecipes.get(foundRecipes.size()); + speechText = String.format(PhrasesForAlexa.TELL_CATEGORIES, responseMessage); + } + } return input.getResponseBuilder() .withSpeech(speechText) diff --git a/src/main/java/verkocht/handlers/SetNumberOfPeopleIntentHandler.java b/src/main/java/verkocht/handlers/SetNumberOfPeopleIntentHandler.java index 7056ac2..f156697 100644 --- a/src/main/java/verkocht/handlers/SetNumberOfPeopleIntentHandler.java +++ b/src/main/java/verkocht/handlers/SetNumberOfPeopleIntentHandler.java @@ -63,29 +63,31 @@ public Optional handle(HandlerInput input) { case "eine": case "mich": recipe.setNumberOfPeople(1); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 1); break; case "zwei": recipe.setNumberOfPeople(2); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 2); break; case "drei": recipe.setNumberOfPeople(3); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 3); break; case "vier": recipe.setNumberOfPeople(4); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 4); break; case "fuenf": recipe.setNumberOfPeople(5); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 5); break; case "sechs": recipe.setNumberOfPeople(6); - recipe.changeIngredientAmounts(); + speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 6); + break; + default: + speechText = PhrasesForAlexa.PEOPLE_NUMBER_UNCLEAR; } - speechText = String.format(PhrasesForAlexa.PEOPLE_SET, actualNumber); } return input.getResponseBuilder() diff --git a/src/main/java/verkocht/model/PhrasesForAlexa.java b/src/main/java/verkocht/model/PhrasesForAlexa.java index c712564..984955e 100644 --- a/src/main/java/verkocht/model/PhrasesForAlexa.java +++ b/src/main/java/verkocht/model/PhrasesForAlexa.java @@ -40,7 +40,7 @@ private PhrasesForAlexa() { "Sag mir den Rezeptnamen. Sage zum Beispiel: ich moechte Schnitzel kochen."; // Categories - public static final String TELL_CATEGORIES = "Folgende Rezepte befinden sich in der Kategorie"; + public static final String TELL_CATEGORIES = "Die Rezepte %s befinden sich in der ausgewählten Kategorie"; public static final String CATEGORIES_UNKOWN = "Ich weiss nicht welche Kategorie ich vorlesen soll. Sag mir den Namen der Kategorie. Sage zum Beispiel: Sage mir alle Rezepte der Kategorie Vegan."; // Favorites @@ -59,7 +59,8 @@ private PhrasesForAlexa() { //Number of People public static final String PEOPLE_UNKNOWN = "Ich habe die Anzahl der Personen leider nicht genau verstanden, sage zum Beispiel: Ich moechte fuer zwei Personen kochen"; - public static final String PEOPLE_SET = "Dein Rezept ist nun fuer %s ausgerichtet"; + public static final String PEOPLE_SET = "Dein Rezept ist nun fuer %i ausgerichtet"; + public static final String PEOPLE_NUMBER_UNCLEAR = "Die Gerichte koennen fuer maximal sechs Leute ausgerichtet werden"; // Cancel-/Stop-/Help-/Fallback-Intent public static final String SORRY = "Tut mir leid, das weiss ich nicht. Sage einfach Hilfe."; diff --git a/src/main/java/verkocht/model/Recipe.java b/src/main/java/verkocht/model/Recipe.java index a53094a..2ebf127 100644 --- a/src/main/java/verkocht/model/Recipe.java +++ b/src/main/java/verkocht/model/Recipe.java @@ -46,11 +46,13 @@ public Map getIngredients() { * * @return */ - public Recipe changeIngredientAmounts() { + public Recipe changeIngredientAmounts(int numberOfPerson) { for (Ingredient key : this.ingredientAmounts.keySet() ) { Integer r = ingredientAmounts.get(key); - ingredientAmounts.replace(key, r, r * this.numberOfPeople); + int a = (int) r * numberOfPerson / this.numberOfPeople; + ingredientAmounts.replace(key, r, a); } + this.setNumberOfPeople(numberOfPerson); return null; } diff --git a/src/test/java/test/CookingBookTest.java b/src/test/java/test/CookingBookTest.java index 880c4c4..e615643 100644 --- a/src/test/java/test/CookingBookTest.java +++ b/src/test/java/test/CookingBookTest.java @@ -47,6 +47,5 @@ public void testFindByCategory() { listOfRecipes.clear(); listOfRecipes.add(cookingBook.getAllRecipes().get(2)); assertEquals(cookingBook.findByCategory(Category.VEGAN),listOfRecipes); - } - + } } \ No newline at end of file diff --git a/src/test/java/test/RecipeTest.java b/src/test/java/test/RecipeTest.java index 0315176..45ba505 100644 --- a/src/test/java/test/RecipeTest.java +++ b/src/test/java/test/RecipeTest.java @@ -46,15 +46,13 @@ public void testChangeIngredientAmount() { CookingBook cookingBook = new CookingBook(); CookingBook cookingBook2 = new CookingBook(); - Integer a = 100; - cookingBook.getAllRecipes().get(0).setNumberOfPeople(2); - cookingBook.getAllRecipes().get(0).changeIngredientAmounts(); + Integer a = 25; + cookingBook.getAllRecipes().get(0).changeIngredientAmounts(2); Integer b = cookingBook.getAllRecipes().get(0).getIngredients().get(cookingBook.getIngredientByName("Mehl")); assertEquals(a,b); - a = 200; - cookingBook2.getAllRecipes().get(0).setNumberOfPeople(4); - cookingBook2.getAllRecipes().get(0).changeIngredientAmounts(); + a = 50; + cookingBook2.getAllRecipes().get(0).changeIngredientAmounts(4); b = cookingBook2.getAllRecipes().get(0).getIngredients().get(cookingBook2.getIngredientByName("Mehl")); assertEquals(a,b); }