Skip to content

Commit

Permalink
#28 #29 merged again
Browse files Browse the repository at this point in the history
  • Loading branch information
Plugsocket committed Dec 11, 2018
2 parents 784e9d2 + 6cd7b9a commit dcf1daf
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 90 deletions.
110 changes: 68 additions & 42 deletions models/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,19 @@
{
"name": "ModifyRecipeByUnitsIntent",
"slots": [
{
"name": "Ingredient_Value",
"type": "AMAZON.NUMBER"
},
{
"name": "Ingredient",
"type": "AMAZON.NUMBER",
"samples": [
"Bitte sage mir die neue Menge dieser Zutat"
]
"type": "Ingredient"
}
],
"samples": [
"{Ingredient} anpassen",
"kann ich ein Rezept nach Zutaten aendern"
"kann ich ein Rezept nach Zutaten aendern",
"{Ingredient} auf {Ingredient_Value} anpassen"
]
},
{
Expand Down Expand Up @@ -263,49 +265,73 @@
}
}
]
}
]
},
"dialog": {
"intents": [
},
{
"name": "ModifyRecipeByUnitsIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
"name": "Ingredient",
"values": [
{
"name": "Ingredient",
"type": "AMAZON.NUMBER",
"confirmationRequired": true,
"elicitationRequired": true,
"prompts": {
"confirmation": "Confirm.Slot.170099915505.1240512610375",
"elicitation": "Elicit.Slot.170099915505.1240512610375"
"name": {
"value": "Eis"
}
},
{
"name": {
"value": "Oel"
}
},
{
"name": {
"value": "Paniermehl"
}
},
{
"name": {
"value": "Tomatensauce"
}
},
{
"name": {
"value": "Salz"
}
},
{
"name": {
"value": "Zucker"
}
},
{
"name": {
"value": "Erdbeeren"
}
},
{
"name": {
"value": "Fleisch"
}
},
{
"name": {
"value": "Spaghetti"
}
},
{
"name": {
"value": "Milch"
}
},
{
"name": {
"value": "Ei"
}
},
{
"name": {
"value": "Mehl"
}
}
]
}
]
},
"prompts": [
{
"id": "Elicit.Slot.170099915505.1240512610375",
"variations": [
{
"type": "PlainText",
"value": "Bitte sage mir die neue Menge dieser Zutat"
}
]
},
{
"id": "Confirm.Slot.170099915505.1240512610375",
"variations": [
{
"type": "PlainText",
"value": "Ist {Ingredient} richtig?"
}
]
}
]
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sprint_ressources/sprint2/SaveRecipeToFavoriteHandler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/8.8.0 Chrome/61.0.3163.100 Electron/2.0.2 Safari/537.36" version="8.8.0" editor="www.draw.io" type="device"><diagram id="bcb13173-9494-129e-c73b-83b3c3b45b11" name="Page-1">5Vldk9o2FP01zLQPy/hLNn5c2Gzy0M5kSjJtH4UtbE2E5cgysPvrK9mSbVkQnIXdJhMeQLr69Dn3Hl2Zmb/aHd8zWOZ/0hSRmeekx5n/MPO8CPjiWxqeWkMYRa0hYzhtTW5vWONnpIyOstY4RZXRkVNKOC5NY0KLAiXcsEHG6MHstqXEXLWEGbIM6wQS2/o3TnmurG4Y9w0fEM5yvXQIgrZlA5MvGaN1oRacef62+bTNO6gnU09a5TClh4HJfzfzV4xS3pZ2xxUiEluNWzvu8Uxrt3GGCj5lgN73HpJaPfw9QUc480IiJlhumChlsqT2y580SGLrpSzWO/IH3iKCC1FblojhHeKIiRaizB972/KQY47WJUzk0IPwHmHL+Y6ImiuKglEOxRDW1QmBZYU3zaqOsDCU1KzCe/QXqlrHkVZac7nSqnMIp9vyEAyFzx4xjo4DkwLnPaJin+xJdFGtvq+IetJMK08+9H7h+cqWD1wCAOUrUPli1s3d0yEKipEz7ACLHYuGAXglxQVvlgPLGXgYsUEZz2lGC0iGfNgYdS4xGSTX9Q2QFo6FkY7sIUSu7ncNRK4m6FsYoSK9l7IgUShRYbqcaHzEcv7GZ1JY5ShVTefB4ZBliI8eH6WGrNh4DQABJwDRNoYI5MLDTV07AZJa4aPkfUhHYNDhgXgOzEkqWrMEqXFDeRhNBS7O1AJhzdTQ1j35NGePLCLXUIZ5gkv0iT7CPWVCOz7AIiXCdX9JOQIL77IcBeC15Gjxf8hR9L0guW5kguRN0yP/BnIUTlCjHwKihRnYzjSIgltAZB9qCSzasP7t9wmJxwA/+cRY5G33BGdC1h82lHO6a0VdK/6G0ORLYxIb/UfFYlP591JgtjJpnsVD5dcBcVH4r1R0Px7RtYgm6fkLVDgMLXY2IvW+lgR17Bqnq2hWqf9C1o6Yd+yIckPOPAaT+Vmc4Ae8FT+xwY/vvtopGdqnZN6Fzg8bKFoWLxKhAL1z5o7jqOTjSnIAMIPHD6cFTz+R7ki32wpdS2Bky59IHUpaVOgNg2wuTwAj0GJdH5w100nXBL8o+hRRknM95krOg9i8kQRjwTwTkFM4txZbxP7cCRdxCIAbecEo+w6dcN63+dEoD7udcruuZ3nW5+pXzZQ915TkU5my6yhluHmm3F0J3zQP7D1gMkyBqY3aOy+kgd+MyOkY2e+eLIwG6pYQWFU4Md3urKcYQhRMFKK3uZ97kXk/6fL3772dB954onCSyr1EXHzboaW4VA0cX2tUcYs74YLcZIvJyIe9JNhuD2tOlTrIAVCddgRt+YlDkFOpQpUQJVxkn2Tl4S640Ys/HUsaW8/Wj/hUaNzkndaZt7It2meShZ8c7tA8sJ1TSvRaeAenbu3tPTTFewPm8Gst380vJdp3Cq970SMRzy7PWt3e317VfXZs2NImlO2ZZcNdS4qc2AvKoz3tWsZc8ZzijMHdTnR7ruX3Z+kXzgo2P/0o/e6skAuWCCc5YoUxKWl/H9tdmTsVZmv3wtYgc+aK/lP74jh1iOzID4PbuKKo9v/1tLLc/6Hmv/sP</diagram></mxfile>
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,56 @@

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Intent;
import com.amazon.ask.model.IntentRequest;
import com.amazon.ask.model.Request;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.Slot;

import verkocht.model.PhrasesForAlexa;
import verkocht.model.Recipe;

public class ModifyRecipeByUnitsIntentHandler implements RequestHandler {
static int step;
static boolean state = false;

public static final String INGREDIENT_SLOT = "Ingredient";
public static final String INGREDIENT_VALUE_SLOT = "IngredientValue";

private String ingredientToModify;
private String ingredientValue;

@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("ModifyRecipeByUnitsIntent"));
}

@Override
public Optional<Response> handle(HandlerInput input) {
Map<String, Object> sessionAttributes = input.getAttributesManager().getSessionAttributes();
String speechText = PhrasesForAlexa.MODIFY_UNIT_ERROR;
Recipe recipeToModify = Recipe.getSavedRecipe();

try {
if (step == 3) {
recipeToModify.modifyByUnit(this.ingredientToModify, this.ingredientValue);
speechText = PhrasesForAlexa.MODIFY_UNIT_DONE;
step = 0;
} else if (step == 2) {
Request request = input.getRequestEnvelope().getRequest();
IntentRequest intentRequest = (IntentRequest) request;
Intent intent = intentRequest.getIntent();
Map<String, Slot> slots = intent.getSlots();
String getIngredientValue = slots.get(INGREDIENT_VALUE_SLOT).getValue();

if (getIngredientValue.isEmpty()) {
speechText = PhrasesForAlexa.MODIFY_UNIT_VALUE;
} else {
speechText = "Ok!";
this.ingredientValue = getIngredientValue;
step++;
}
} else if (step == 1) {
Request request = input.getRequestEnvelope().getRequest();
IntentRequest intentRequest = (IntentRequest) request;
Intent intent = intentRequest.getIntent();
Map<String, Slot> slots = intent.getSlots();
String getIngredientToModify = slots.get(INGREDIENT_SLOT).getValue();

if (getIngredientToModify.isEmpty()) {
speechText = PhrasesForAlexa.MODIFY_UNIT_SELECT_INGREDIENT;
if (recipeToModify != null) {
String getIngredient = "";
String getIngredientSlotValue = "";
int getIngredientValue = 0;
boolean worked = false;
if (state) {
try {
getIngredient = (String) sessionAttributes.get(INGREDIENT_SLOT);
getIngredientSlotValue = (String) sessionAttributes.get(INGREDIENT_VALUE_SLOT);
getIngredientValue = Integer.parseInt(getIngredientSlotValue);
worked = recipeToModify.modifyByUnit(getIngredient, getIngredientValue);
speechText = getIngredientSlotValue;
if (worked) {
speechText = PhrasesForAlexa.MODIFY_UNIT_DONE;
resetState();
} else {
speechText = PhrasesForAlexa.MODIFY_UNIT_NOT_DONE;
}
} catch (Exception e) {
speechText = PhrasesForAlexa.MODIFY_UNIT_NOT_DONE;
}
} else {
speechText = "Ok!";
this.ingredientToModify = getIngredientToModify;
step++;
speechText = PhrasesForAlexa.MODIFY_UNIT_WELCOME;
toggleState();
}
} else if (step == 0) {
speechText = PhrasesForAlexa.MODIFY_UNIT_WELCOME;
step++;
} else {
speechText = PhrasesForAlexa.MODIFY_UNIT_SELECT_RECIPE_FIRST;
}
} catch (Exception e) {
speechText = PhrasesForAlexa.MODIFY_UNIT_ERROR;
Expand All @@ -79,4 +65,12 @@ public Optional<Response> handle(HandlerInput input) {
return input.getResponseBuilder().withSpeech(speechText).withSimpleCard("Rezeptschritte", speechText)
.withReprompt("Wie kann ich dir helfen?").withShouldEndSession(false).build();
}

public static void resetState() {
state = false;
}

public static void toggleState() {
state = !state;
}
}
7 changes: 3 additions & 4 deletions src/main/java/verkocht/model/PhrasesForAlexa.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ private PhrasesForAlexa() {
+ "Markiere zuerst Favoriten, damit ich sie dir vorlesen kann.";

// Modify Unit
public static final String MODIFY_UNIT_WELCOME = "Hier kannst du ein Rezept entsprechend einer Zutat modifizieren. Sage {Zutat} anpassen.";
public static final String MODIFY_UNIT_WELCOME = "Hier kannst du ein Rezept entsprechend einer Zutat modifizieren. Sage: {Zutat} auf {Menge} anpassen.";
public static final String MODIFY_UNIT_REPROMT = "Bitte nenne eine Zutat des Rezept.";
public static final String MODIFY_UNIT_SELECT_FIRST = "Du musst erst ein Rezept auswaehlen, bevor du es modifizieren kannst.";
public static final String MODIFY_UNIT_SELECT_INGREDIENT = "Waehle eine Zutat.";
public static final String MODIFY_UNIT_VALUE = "Auf welchen Wert soll die Zutat geaendert werden.";
public static final String MODIFY_UNIT_ERROR = "Dies ist nicht moeglich.";
public static final String MODIFY_UNIT_SELECT_RECIPE_FIRST = "Waehle bitte zuerst ein Rezept aus.";
public static final String MODIFY_UNIT_NOT_DONE = "Das Rezept konnte nicht angepasst werden.";
public static final String MODIFY_UNIT_DONE = "Das Rezept wurde angepasst.";

//Number of People
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/verkocht/model/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import verkocht.handlers.ModifyRecipeByUnitsIntentHandler;

/*
* The class that represents a recipe.
Expand Down Expand Up @@ -50,9 +53,41 @@ public Recipe changeIngredientAmounts() {
}
return null;
}

public void addIngredient(Ingredient ingredient, int value) {
if (ingredientAmounts.containsKey(ingredient)) {
ingredientAmounts.replace(ingredient, value);
} else {
ingredientAmounts.put(ingredient, value);
}
}

public void modifyByUnit(String ingredient, String value) {
throw new UnsupportedOperationException();
public boolean modifyByUnit(String ingredient, int value) {
Set<Ingredient> keys = this.ingredientAmounts.keySet();
int originValue = 1;
boolean found = false;

for (Ingredient key : keys) {
found = key.getIngredient().equals(ingredient);

if (found) {
originValue = ingredientAmounts.get(key);
break;
}
}

if (found) {
int difference = ((value * 100) / originValue);
ingredientAmounts.forEach((k, v) -> {
if (k.getIngredient() != ingredient) {
ingredientAmounts.replace(k, (v * difference) / 100);
} else {
ingredientAmounts.replace(k, value);
}
});
}

return found;
}

public String getName() {
Expand Down Expand Up @@ -81,6 +116,7 @@ public static Recipe getSavedRecipe() {

public static void saveRecipe(Recipe recipeToSave) {
Recipe.savedRecipe = recipeToSave;
ModifyRecipeByUnitsIntentHandler.resetState();
}

public int getNumberOfPeople() {
Expand Down
43 changes: 42 additions & 1 deletion src/test/java/test/RecipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.Test;

<<<<<<< HEAD
import verkocht.model.*;

/**
Expand All @@ -27,4 +28,44 @@ public void testChangeIngredientAmount() {
b = cookingBook2.getAllRecipes().get(0).getIngredients().get(cookingBook2.getIngredientByName("Mehl"));
assertEquals(a,b);
}
}
}
=======
import verkocht.model.Category;
import verkocht.model.Ingredient;
import verkocht.model.Recipe;
import verkocht.model.Unit;

public class RecipeTest {
Recipe recipe = new Recipe("Pfannenkuchen", Category.VEGETARIAN, 2, 20);
Ingredient mehl = new Ingredient("Mehl", Unit.GRAMM);
Ingredient salz = new Ingredient("Salz", Unit.BRISE);
Ingredient ei = new Ingredient("Ei", Unit.STUECK);
Ingredient milch = new Ingredient("Milch", Unit.MILLILITER);
Ingredient notAdded = new Ingredient("NotAdded", Unit.ESSLOEFFEL);

@Test
public void testConstructorAndGetter() {
assertEquals("Should be Pfannenkuchen", "Pfannenkuchen", recipe.getName());
assertEquals("Should be vegetarian", Category.VEGETARIAN, recipe.getCategory());
assertEquals("Should be for two persons", 2, recipe.getNumberOfPeople());
assertEquals("Should be done in 20 minutes", 20, recipe.getCookingTime());
}

@Test
public void testIngredients() {
recipe.addIngredient(mehl, 200);
recipe.addIngredient(ei, 1);
recipe.addIngredient(salz, 1);
recipe.addIngredient(milch, 100);

assertEquals("Should have four ingredients", 4, recipe.getIngredients().size());

assertFalse(recipe.modifyByUnit("NotAdded", 4));
assertTrue(recipe.modifyByUnit("Mehl", new Integer(300)));
assertEquals("Should have four ingredients", 4, recipe.getIngredients().size());

assertEquals("Should be 300 now", 300, (int) recipe.getIngredients().get(mehl));
}

}
>>>>>>> 6cd7b9a227d3be0fdd7007c012075d0391a6f23d

0 comments on commit dcf1daf

Please sign in to comment.