Skip to content

Commit

Permalink
Merge pull request #94 from TohLiYuan/branch-recipe-modify
Browse files Browse the repository at this point in the history
Branch recipe modify
  • Loading branch information
prawnzyy authored Oct 26, 2023
2 parents 985c1ba + 10bab8b commit a18539a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import seedu.address.model.Name;
import seedu.address.model.ingredient.Ingredient;
import seedu.address.model.ingredient.exceptions.IngredientNotFoundException;

/**
* Represents a recipe in the recipe book.
Expand Down Expand Up @@ -90,6 +91,22 @@ public Recipe modifyRecipeStep(int stepNumber, int newStepNumber) {
return new Recipe(this.uuid.getId(), this.name, this.ingredientList, stepCopy);
}

/**
* Changes the specified ingredient with a new ingredient.
*/
public Recipe modifyIngredients(String oldIngredient, Ingredient newIngredient) {
requireAllNonNull(oldIngredient, newIngredient);
for (Ingredient ingredient : ingredientList) {
if (ingredient.getName().fullName.equals(oldIngredient)) {
List<Ingredient> ingredientListCopy = new ArrayList<>(ingredientList);
ingredientListCopy.remove(ingredient);
ingredientListCopy.add(newIngredient);
return new Recipe(this.uuid.getId(), this.name, ingredientListCopy, this.recipeSteps);
}
}
throw new IngredientNotFoundException();
}

/**
* Returns the full recipe.
*/
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/seedu/address/model/recipe/RecipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalIngredients.EGG;
import static seedu.address.testutil.TypicalIngredients.FLOUR;
import static seedu.address.testutil.TypicalRecipe.COOKIES;
import static seedu.address.testutil.TypicalRecipe.COOKIES_STRING;
import static seedu.address.testutil.TypicalRecipe.SPONGECAKE;
Expand All @@ -15,6 +17,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.model.Name;
import seedu.address.model.ingredient.exceptions.IngredientNotFoundException;

public class RecipeTest {
@Test
Expand Down Expand Up @@ -102,7 +105,26 @@ public void modifyRecipe_nullRecipeInstruction_throwsNullPointerException() {

@Test
public void modifyRecipe_indexGreaterThanAvailableSteps_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> COOKIES.modifyRecipeStep(7, "Hello"));
assertThrows(IllegalArgumentException.class, () ->
COOKIES.modifyRecipeStep(7, "Hello"));
}

@Test
public void modifyRecipeIngredients_nullIngredient_throwsNullPointerException() {
assertThrows(NullPointerException.class, () ->
COOKIES.modifyIngredients("Flour", null));
}

@Test
public void modifyRecipeIngredients_ingredientNotInList_throwsIngredientNotFoundException() {
assertThrows(IngredientNotFoundException.class, () ->
COOKIES.modifyIngredients("Eggs", FLOUR));
}

@Test
public void modifyRecipeIngredients_ingredientsInList_success() {
Recipe modifiedCookies = COOKIES.modifyIngredients("Flour", EGG);
assertTrue(modifiedCookies.getIngredients().contains(EGG));
}

@Test
Expand Down

0 comments on commit a18539a

Please sign in to comment.