diff --git a/src/main/java/seedu/address/logic/commands/AddFavouriteCommand.java b/src/main/java/seedu/address/logic/commands/AddFavouriteCommand.java index 0523b1cd6a9..676528f9cbf 100644 --- a/src/main/java/seedu/address/logic/commands/AddFavouriteCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddFavouriteCommand.java @@ -22,6 +22,9 @@ public class AddFavouriteCommand extends Command { public static final String MESSAGE_SUCCESS = "The following contacts have been added to favourites: %s"; + public static final String MESSAGE_SUCCESS_WITH_WARNING = "The following contacts have been added to favourites: %s" + + "\nWarning: There are contacts in your input that were already favourites."; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds contacts identified by index number " + "as favourites.\n" @@ -44,22 +47,27 @@ public AddFavouriteCommand(Set indices) { public CommandResult execute(Model model) throws CommandException { List people = model.getFilteredPersonList(); List modifiedContacts = new ArrayList<>(); + boolean anyGreaterThanSize = this.indices.stream().anyMatch(index -> index.getZeroBased() >= people.size()); if (anyGreaterThanSize) { throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } + boolean anyFavourite = this.indices.stream().anyMatch(index -> people.get(index.getZeroBased()).getIsFavourite()); - if (anyFavourite) { - throw new CommandException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE)); - } + for (Index index : this.indices) { Person person = people.get(index.getZeroBased()); modifiedContacts.add(person.getName().fullName); person.addFavourite(); model.setPerson(person, person); } + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + + if (anyFavourite) { + return new CommandResult(String.format(MESSAGE_SUCCESS_WITH_WARNING, modifiedContacts)); + } return new CommandResult(String.format(MESSAGE_SUCCESS, modifiedContacts)); } diff --git a/src/main/java/seedu/address/logic/commands/RemoveFavouriteCommand.java b/src/main/java/seedu/address/logic/commands/RemoveFavouriteCommand.java index 047f4b64e40..9b81a1af4a6 100644 --- a/src/main/java/seedu/address/logic/commands/RemoveFavouriteCommand.java +++ b/src/main/java/seedu/address/logic/commands/RemoveFavouriteCommand.java @@ -22,6 +22,9 @@ public class RemoveFavouriteCommand extends Command { public static final String MESSAGE_SUCCESS = "The following contacts have been removed from favourites: %s"; + public static final String MESSAGE_SUCCESS_WITH_WARNING = "The following contacts have been removed from" + + " favourites: %s" + "\nWarning: There are contacts in your input that were already not favourites."; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes contacts identified by index number " + "as favourites.\n" @@ -52,9 +55,6 @@ public CommandResult execute(Model model) throws CommandException { boolean anyNotFavourite = this.indices.stream().anyMatch(index -> !people.get(index.getZeroBased()).getIsFavourite()); - if (anyNotFavourite) { - throw new CommandException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE)); - } for (Index index : this.indices) { Person person = people.get(index.getZeroBased()); @@ -62,7 +62,12 @@ public CommandResult execute(Model model) throws CommandException { person.removeFavourite(); model.setPerson(person, person); } + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + + if (anyNotFavourite) { + return new CommandResult(String.format(MESSAGE_SUCCESS_WITH_WARNING, modifiedContacts)); + } return new CommandResult(String.format(MESSAGE_SUCCESS, modifiedContacts)); } diff --git a/src/test/java/seedu/address/logic/commands/AddFavouriteCommandTest.java b/src/test/java/seedu/address/logic/commands/AddFavouriteCommandTest.java index 04a2f579b5d..8676dd5a9cb 100644 --- a/src/test/java/seedu/address/logic/commands/AddFavouriteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddFavouriteCommandTest.java @@ -38,7 +38,7 @@ public void constructor_nullIndices_throwsNullPointerException() { @Test public void execute_addFavourite_success() { Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - Person editedPerson = new PersonBuilder(firstPerson).withFavourite(true).build(); + Person editedPerson = new PersonBuilder(firstPerson).build(); Set indices = Set.of(Index.fromOneBased(1)); List modifiedContacts = List.of(firstPerson.getName().fullName); AddFavouriteCommand addFavouriteCommand = new AddFavouriteCommand(indices); @@ -59,14 +59,20 @@ public void execute_invalidIndex_throwsCommandException() { } @Test - public void execute_favouriteContact_throwsCommandException() { + public void execute_alreadyFavourite_success() { Set indices = Set.of(Index.fromOneBased(1)); Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); Person editedPerson = new PersonBuilder(firstPerson).withFavourite(true).build(); model.setPerson(firstPerson, editedPerson); AddFavouriteCommand addFavouriteCommand = new AddFavouriteCommand(indices); - assertThrows(CommandException.class, MESSAGE_INVALID_FORMAT, () -> - addFavouriteCommand.execute(model)); + + List modifiedContacts = List.of(firstPerson.getName().fullName); + String expectedMessage = String.format(AddFavouriteCommand.MESSAGE_SUCCESS_WITH_WARNING, modifiedContacts); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertCommandSuccess(addFavouriteCommand, model, expectedMessage, expectedModel); } @Test diff --git a/src/test/java/seedu/address/logic/commands/RemoveFavouriteCommandTest.java b/src/test/java/seedu/address/logic/commands/RemoveFavouriteCommandTest.java index 4b42f2ae1b3..c4cf665ed9e 100644 --- a/src/test/java/seedu/address/logic/commands/RemoveFavouriteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/RemoveFavouriteCommandTest.java @@ -44,7 +44,6 @@ public void execute_removeFavourite_success() { List modifiedContacts = List.of(firstPerson.getName().fullName); RemoveFavouriteCommand removeFavouriteCommand = new RemoveFavouriteCommand(indices); String expectedMessage = String.format(RemoveFavouriteCommand.MESSAGE_SUCCESS, modifiedContacts); - Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); assertCommandSuccess(removeFavouriteCommand, model, expectedMessage, expectedModel); @@ -59,11 +58,17 @@ public void execute_invalidIndex_throwsCommandException() { } @Test - public void execute_nonFavouriteContact_throwsCommandException() { - Set indices = Set.of(Index.fromOneBased(2)); + public void execute_alreadyNonFavouriteContact_success() { + Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person editedPerson = new PersonBuilder(firstPerson).withFavourite(false).build(); + model.setPerson(firstPerson, editedPerson); + Set indices = Set.of(Index.fromOneBased(1)); + List modifiedContacts = List.of(firstPerson.getName().fullName); RemoveFavouriteCommand removeFavouriteCommand = new RemoveFavouriteCommand(indices); - assertThrows(CommandException.class, MESSAGE_INVALID_FORMAT, () -> - removeFavouriteCommand.execute(model)); + String expectedMessage = String.format(RemoveFavouriteCommand.MESSAGE_SUCCESS_WITH_WARNING, modifiedContacts); + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + + assertCommandSuccess(removeFavouriteCommand, model, expectedMessage, expectedModel); } @Test