Skip to content

Commit

Permalink
Change to warnings for addfav, removefav
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-ruiheng committed Apr 3, 2024
1 parent d00a846 commit 038b265
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -44,22 +47,27 @@ public AddFavouriteCommand(Set<Index> indices) {
public CommandResult execute(Model model) throws CommandException {
List<Person> people = model.getFilteredPersonList();
List<String> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,17 +55,19 @@ 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());
modifiedContacts.add(person.getName().fullName);
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Index> indices = Set.of(Index.fromOneBased(1));
List<String> modifiedContacts = List.of(firstPerson.getName().fullName);
AddFavouriteCommand addFavouriteCommand = new AddFavouriteCommand(indices);
Expand All @@ -59,14 +59,20 @@ public void execute_invalidIndex_throwsCommandException() {
}

@Test
public void execute_favouriteContact_throwsCommandException() {
public void execute_alreadyFavourite_success() {
Set<Index> 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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void execute_removeFavourite_success() {
List<String> 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);
Expand All @@ -59,11 +58,17 @@ public void execute_invalidIndex_throwsCommandException() {
}

@Test
public void execute_nonFavouriteContact_throwsCommandException() {
Set<Index> 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<Index> indices = Set.of(Index.fromOneBased(1));
List<String> 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
Expand Down

0 comments on commit 038b265

Please sign in to comment.