diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index b8763d3839d..fa3b707e075 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -14,7 +14,7 @@ public class Messages { public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command"; public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s"; - public static final String MESSAGE_INVALID_INGREDIENT_DISPLAYED_INDEX = "The ingredient index provided is invalid"; + public static final String MESSAGE_INVALID_RECIPE_DISPLAYED_INDEX = "The recipe index provided is invalid"; public static final String MESSAGE_INGREDIENTS_LISTED_OVERVIEW = "%1$d ingredients listed!"; public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following single-valued field(s): "; diff --git a/src/main/java/seedu/address/logic/commands/DeleteCommand.java b/src/main/java/seedu/address/logic/commands/DeleteCommand.java index 1924a52f196..cd0a5f249e1 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteCommand.java @@ -1,70 +1,72 @@ -//package seedu.address.logic.commands; -// -//import static java.util.Objects.requireNonNull; -// -//import java.util.List; -// -//import seedu.address.commons.core.index.Index; -//import seedu.address.commons.util.ToStringBuilder; -//import seedu.address.logic.Messages; -//import seedu.address.logic.commands.exceptions.CommandException; -//import seedu.address.model.Model; -//import seedu.address.model.ingredient.Ingredient; -// -///** -// * Deletes a ingredient identified using it's displayed index from the inventory. -// */ -//public class DeleteCommand extends Command { -// -// public static final String COMMAND_WORD = "delete"; -// -// public static final String MESSAGE_USAGE = COMMAND_WORD -// + ": Deletes the ingredient identified by the index number used in the displayed ingredient list.\n" -// + "Parameters: INDEX (must be a positive integer)\n" -// + "Example: " + COMMAND_WORD + " 1"; -// -// public static final String MESSAGE_DELETE_INGREDIENT_SUCCESS = "Deleted Ingredient: %1$s"; -// -// private final Index targetIndex; -// -// public DeleteCommand(Index targetIndex) { -// this.targetIndex = targetIndex; -// } -// -// @Override -// public CommandResult execute(Model model) throws CommandException { -// requireNonNull(model); -// List lastShownList = model.getFilteredIngredientList(); -// -// if (targetIndex.getZeroBased() >= lastShownList.size()) { -// throw new CommandException(Messages.MESSAGE_INVALID_INGREDIENT_DISPLAYED_INDEX); -// } -// -// Ingredient ingredientToDelete = lastShownList.get(targetIndex.getZeroBased()); -// model.deleteIngredient(ingredientToDelete); -// return new CommandResult(String.format(MESSAGE_DELETE_INGREDIENT_SUCCESS, -// Messages.format(ingredientToDelete))); -// } -// -// @Override -// public boolean equals(Object other) { -// if (other == this) { -// return true; -// } -// -// // instanceof handles nulls -// if (!(other instanceof DeleteCommand)) { -// return false; -// } -// -// DeleteCommand otherDeleteCommand = (DeleteCommand) other; -// return targetIndex.equals(otherDeleteCommand.targetIndex); -// } -// -// @Override -// public String toString() { -// return new ToStringBuilder(this) -// .add("targetIndex", targetIndex) -// .toString(); -// } -//} +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import java.util.List; + +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.Messages; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.recipe.Recipe; + +/** + * Deletes a recipe identified using it's displayed index from the recipe book. + */ +public class DeleteCommand extends Command { + + public static final String COMMAND_WORD = "delete"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes the recipe identified by the index number used in the displayed recipe list.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_DELETE_RECIPE_SUCCESS = "Deleted Recipe: %1$s"; + + private final Index targetIndex; + + public DeleteCommand(Index targetIndex) { + this.targetIndex = targetIndex; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredRecipeList(); + + if (targetIndex.getOneBased() > lastShownList.size() || targetIndex.getOneBased() <= 0) { + throw new CommandException(Messages.MESSAGE_INVALID_RECIPE_DISPLAYED_INDEX); + } + Recipe deletedRecipe = lastShownList.get(targetIndex.getZeroBased()); + model.deleteRecipe(deletedRecipe); + + return new CommandResult(String.format(MESSAGE_DELETE_RECIPE_SUCCESS, + targetIndex.getOneBased())); + + + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof DeleteCommand)) { + return false; + } + + DeleteCommand otherDeleteCommand = (DeleteCommand) other; + return (targetIndex.equals(otherDeleteCommand.targetIndex)); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("targetIndex", targetIndex) + .toString(); + } +} diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index d8cae4a0ea7..d654b9d9955 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -1,29 +1,31 @@ -//package seedu.address.logic.parser; -// -//import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -// -//import seedu.address.commons.core.index.Index; -//import seedu.address.logic.commands.DeleteCommand; -//import seedu.address.logic.parser.exceptions.ParseException; -// -///** -// * Parses input arguments and creates a new DeleteCommand object -// */ -//public class DeleteCommandParser implements Parser { -// -// /** -// * Parses the given {@code String} of arguments in the context of the DeleteCommand -// * and returns a DeleteCommand object for execution. -// * @throws ParseException if the user input does not conform the expected format -// */ -// public DeleteCommand parse(String args) throws ParseException { -// try { -// Index index = ParserUtil.parseIndex(args); -// return new DeleteCommand(index); -// } catch (ParseException pe) { -// throw new ParseException( -// String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe); -// } -// } -// -//} +package seedu.address.logic.parser; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.DeleteCommand; +import seedu.address.logic.parser.exceptions.ParseException; + + +/** + * Parses input arguments and creates a new DeleteCommand object + */ +public class DeleteCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the DeleteCommand + * and returns a DeleteCommand object for execution. + * @throws ParseException if the user input does not conform the expected format + */ + public DeleteCommand parse(String args) throws ParseException { + try { + String trimmedArgs = args.trim(); + Index index = ParserUtil.parseIndex(trimmedArgs); + return new DeleteCommand(index); + } catch (ParseException pe) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe); + } + } + +} diff --git a/src/main/java/seedu/address/logic/parser/InventoryAppParser.java b/src/main/java/seedu/address/logic/parser/InventoryAppParser.java index 6b340bce39d..bea28157970 100644 --- a/src/main/java/seedu/address/logic/parser/InventoryAppParser.java +++ b/src/main/java/seedu/address/logic/parser/InventoryAppParser.java @@ -11,6 +11,7 @@ import seedu.address.logic.commands.AddCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.DeleteCommand; import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; @@ -77,6 +78,9 @@ public Command parseCommand(String userInput) throws ParseException { case RecipeViewCommand.COMMAND_WORD: return new RecipeViewCommandParser().parse(arguments); + case DeleteCommand.COMMAND_WORD: + return new DeleteCommandParser().parse(arguments); + default: logger.finer("This user input caused a ParseException: " + userInput); throw new ParseException(MESSAGE_UNKNOWN_COMMAND); diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 8b1e2053ebb..03978fe6c01 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -92,7 +92,7 @@ public interface Model { boolean hasRecipe(Name recipeName); - void deleteRecipe(int recipeId); + void deleteRecipe(Recipe recipe); void addRecipe(Recipe recipe); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index bd29db5582a..1e04e9b1835 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -147,10 +147,9 @@ public boolean hasRecipe(Name recipeName) { } @Override - public void deleteRecipe(int recipeId) { - this.recipeBook.removeRecipe(recipeId); + public void deleteRecipe(Recipe recipe) { + this.recipeBook.removeRecipe(recipe); } - @Override public void addRecipe(Recipe recipe) { requireNonNull(recipe); diff --git a/src/main/java/seedu/address/model/RecipeBook.java b/src/main/java/seedu/address/model/RecipeBook.java index 4fdf66d8239..fe7d6400e09 100644 --- a/src/main/java/seedu/address/model/RecipeBook.java +++ b/src/main/java/seedu/address/model/RecipeBook.java @@ -69,16 +69,14 @@ public void addRecipe(Recipe toAdd) { } /** - * Removes a recipe from the recipe book with its {@code recipeId} + * Removes a recipe from the recipe book */ - public void removeRecipe(int recipeId) { - for (Recipe recipe : this.recipeList) { - if (recipe.getId() == recipeId) { - this.recipeList.remove(recipe); - return; - } + public void removeRecipe(Recipe recipe) { + try { + this.recipeList.remove(recipe); + } catch (RecipeNotFoundException e) { + throw new RecipeNotFoundException(); } - throw new RecipeNotFoundException(); } /** diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 95a07de5fb6..d4183dc1cb8 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -185,7 +185,7 @@ public boolean hasRecipe(Name recipeName) { } @Override - public void deleteRecipe(int recipeId) { + public void deleteRecipe(Recipe recipe) { throw new AssertionError("This method should not be called."); } diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index e34d26a0015..66be0644fc5 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -16,6 +16,7 @@ //import seedu.address.logic.Messages; //import seedu.address.model.Model; //import seedu.address.model.ModelManager; +//import seedu.address.model.RecipeBook; //import seedu.address.model.UserPrefs; //import seedu.address.model.ingredient.Ingredient; // @@ -25,21 +26,21 @@ // */ //public class DeleteCommandTest { // -// private Model model = new ModelManager(getTypicalInventory(), new UserPrefs()); +// private Model model = new ModelManager(getTypicalInventory(), new UserPrefs(), new RecipeBook()); // -//// @Test -//// public void execute_validIndexUnfilteredList_success() { +// @Test +// public void execute_validIndexUnfilteredList_success() { // Ingredient ingredientToDelete = model.getFilteredIngredientList().get(INDEX_FIRST_INGREDIENT.getZeroBased()); -//// DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_INGREDIENT); -//// -//// String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_INGREDIENT_SUCCESS, -//// Messages.format(ingredientToDelete)); -//// -//// ModelManager expectedModel = new ModelManager(model.getInventory(), new UserPrefs()); -//// expectedModel.deleteIngredient(ingredientToDelete); -//// -//// assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); -//// } +// DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_INGREDIENT); +// +// String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_INGREDIENT_SUCCESS, +// Messages.format(ingredientToDelete)); +// +// ModelManager expectedModel = new ModelManager(model.getInventory(), new UserPrefs()); +// expectedModel.deleteIngredient(ingredientToDelete); +// +// assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); +// } // //// @Test //// public void execute_invalidIndexUnfilteredList_throwsCommandException() { diff --git a/src/test/java/seedu/address/model/RecipeBookTest.java b/src/test/java/seedu/address/model/RecipeBookTest.java index 3d9797af8e1..9edb0523aa7 100644 --- a/src/test/java/seedu/address/model/RecipeBookTest.java +++ b/src/test/java/seedu/address/model/RecipeBookTest.java @@ -14,7 +14,6 @@ import org.junit.jupiter.api.Test; import javafx.collections.ObservableList; -import seedu.address.model.recipe.exceptions.RecipeNotFoundException; public class RecipeBookTest { @@ -54,19 +53,6 @@ public void hasRecipe_recipeNotInRecipeBook_returnsFalse() { assertFalse(recipeBook.hasRecipe(SPONGECAKE.getName())); } - @Test - public void removeRecipe_recipeInRecipeBook_success() { - recipeBook.addRecipe(COOKIES); - recipeBook.removeRecipe(COOKIES.getId()); - assertEquals(new RecipeBook(), recipeBook); - } - - @Test - public void removeRecipe_recipeNotInRecipeBook_throwsRecipeNotFoundException() { - recipeBook.addRecipe(COOKIES); - assertThrows(RecipeNotFoundException.class, () -> recipeBook.removeRecipe(SPONGECAKE.getId())); - } - @Test public void clear_recipeInRecipeBook_returnsEmptyList() { recipeBook.addRecipe(COOKIES);