forked from nus-cs2103-AY1819S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from skpai27/deleteCommand
deleteReview command
- Loading branch information
Showing
6 changed files
with
146 additions
and
4 deletions.
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
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
101 changes: 101 additions & 0 deletions
101
src/main/java/seedu/address/logic/commands/DeleteReviewCommand.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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_RESTAURANTS; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.restaurant.Restaurant; | ||
import seedu.address.model.review.Review; | ||
|
||
/** | ||
* Edits the details of an existing review tagged to a restaurant in the Food Diary. | ||
*/ | ||
public class DeleteReviewCommand extends Command { | ||
public static final String COMMAND_WORD = "deleteReview"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the review identified " | ||
+ "by the index number used in the displayed review list " | ||
+ "of the selected restaurant first, i.e. user needs to first " | ||
+ "select a restaurant from the list of restaurants.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " 1 "; | ||
|
||
public static final String MESSAGE_DELETE_REVIEW_SUCCESS = "Deleted review number %1$s for restaurant: %2$s"; | ||
|
||
private final Index index; | ||
|
||
/** | ||
* @param index of the restaurant in the filtered restaurant list to edit | ||
*/ | ||
public DeleteReviewCommand(Index index) { | ||
requireNonNull(index); | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
Restaurant selectedRestaurant = model.getSelectedRestaurant(); | ||
|
||
if (selectedRestaurant == null) { | ||
throw new CommandException(Messages.MESSAGE_NO_RESTAURANT_SELECTED); | ||
} | ||
|
||
if (selectedRestaurant.getReviews().size() == 0) { | ||
throw new CommandException(Messages.MESSAGE_NO_REVIEWS); | ||
} | ||
if (selectedRestaurant.getReviews().size() < index.getOneBased()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_REVIEW_INDEX); | ||
} | ||
|
||
Restaurant restaurantWithDeletedReview = createRestaurantWithDeletedReview(selectedRestaurant, index); | ||
|
||
model.setRestaurant(selectedRestaurant, restaurantWithDeletedReview); | ||
model.updateFilteredRestaurantList(PREDICATE_SHOW_ALL_RESTAURANTS); | ||
model.commitFoodDiary(); | ||
return new CommandResult(String.format(MESSAGE_DELETE_REVIEW_SUCCESS, index.getOneBased(), | ||
restaurantWithDeletedReview.getName())); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Restaurant} with the details of {@code restaurantToEdit} | ||
* edited with {@code editRestaurantDescriptor}. | ||
*/ | ||
private static Restaurant createRestaurantWithDeletedReview(Restaurant restaurantSelected, Index index) { | ||
assert restaurantSelected != null; | ||
|
||
List<Review> editedReviews = new ArrayList<>(); | ||
editedReviews.addAll(restaurantSelected.getReviews()); | ||
editedReviews.remove(index.getZeroBased()); | ||
|
||
return new Restaurant(restaurantSelected.getName(), restaurantSelected.getPhone(), | ||
restaurantSelected.getEmail(), | ||
restaurantSelected.getAddress(), restaurantSelected.getTags(), | ||
restaurantSelected.getWeblink(), restaurantSelected.getOpeningHours(), | ||
restaurantSelected.getCategories(), editedReviews); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteReviewCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
DeleteReviewCommand e = (DeleteReviewCommand) other; | ||
return index.equals(e.index); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/parser/DeleteReviewCommandParser.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.DeleteReviewCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new DeleteCommand object | ||
*/ | ||
public class DeleteReviewCommandParser implements Parser<DeleteReviewCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the DeleteCommand | ||
* and returns an DeleteCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public DeleteReviewCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new DeleteReviewCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteReviewCommand.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