forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Delete Command #93
Merged
prawnzyy
merged 5 commits into
AY2324S1-CS2103T-F10-3:master
from
dhruvir29:branch-delete-command
Oct 26, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 72 additions & 70 deletions
142
src/main/java/seedu/address/logic/commands/DeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Ingredient> 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<Recipe> 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(); | ||
} | ||
} |
60 changes: 31 additions & 29 deletions
60
src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DeleteCommand> { | ||
// | ||
// /** | ||
// * 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<DeleteCommand> { | ||
|
||
/** | ||
* 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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this actually checks that the item is missing because how the expected model is generated uses the same function of deleteIngredient. deleteIngredient function from the model could be faulty. |
||
// | ||
// assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); | ||
// } | ||
// | ||
//// @Test | ||
//// public void execute_invalidIndexUnfilteredList_throwsCommandException() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fine not catching the exception here considering the exception was thrown in the previous level. It's a runtime exception so we don't necessarily need to catch it, same as other runtime exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think throwing an exception is fine so that the code can catch and display the corresponding error if needed