Skip to content

Commit

Permalink
Merge pull request #66 from CS2103-AY1819S1-T13-2/wip/unfavourite
Browse files Browse the repository at this point in the history
add unfavourite command
  • Loading branch information
DanKhoo authored Oct 31, 2018
2 parents 591ae31 + f9b5f22 commit 367957b
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 20 deletions.
31 changes: 17 additions & 14 deletions src/main/java/seedu/address/logic/commands/FavouriteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;

/**
* Favourites an exisiting contact
Expand All @@ -27,9 +25,9 @@ public class FavouriteCommand extends Command {
+ "by the index number used in the last person listing.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1 ";
public static final String MESSAGE_FAVOURITE_PERSON_FAIL = "Person already favourited: %1$s";
public static final String MESSAGE_FAVOURITE_PERSON_SUCCESS = "Added person to favourites: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
public static final String MESSAGE_UNFAVOURITE_PERSON_SUCCESS = "Person removed from favourites: %1$s";
private final Index index;
/**
* @param index of the person in the filtered person list to edit
Expand All @@ -38,6 +36,7 @@ public FavouriteCommand(Index index) {
requireNonNull(index);
this.index = index;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();
Expand All @@ -46,36 +45,40 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
}
Person personToFavourite = lastShownList.get(index.getZeroBased());
Person favouritedPerson = createFavouritedPerson(personToFavourite);
try {
model.favouritePerson(personToFavourite, favouritedPerson);
} catch (DuplicatePersonException dpe) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
} catch (PersonNotFoundException pnfe) {
throw new AssertionError("The target person cannot be missing");
}

model.favouritePerson(personToFavourite, favouritedPerson);

model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
if (personToFavourite.getFavourite()) {
return new CommandResult(String.format(MESSAGE_UNFAVOURITE_PERSON_SUCCESS,
if (!personToFavourite.getFavourite()) {
return new CommandResult(String.format(MESSAGE_FAVOURITE_PERSON_SUCCESS,
favouritedPerson.getName().fullName));
} else {
return new CommandResult(String.format(MESSAGE_FAVOURITE_PERSON_SUCCESS,
return new CommandResult(String.format(MESSAGE_FAVOURITE_PERSON_FAIL,
favouritedPerson.getName().fullName));
}
}

/**
* Creates and returns a {@code Person} with the details of {@code personToFavourite}
*/
private static Person createFavouritedPerson(Person personToFavourite) {
assert personToFavourite != null;

boolean newFavourite = !personToFavourite.getFavourite();
boolean newFavourite;

if (personToFavourite.getFavourite()) {
newFavourite = personToFavourite.getFavourite();
} else {
newFavourite = !personToFavourite.getFavourite();
}

return new Person(personToFavourite.getName(), personToFavourite.getPhone(), personToFavourite.getEmail(),
personToFavourite.getAddress(), personToFavourite.getRating(), personToFavourite.getDepartment(),
personToFavourite.getManager(), personToFavourite.getSalary(), personToFavourite.getOtHours(),
personToFavourite.getOtRate(), personToFavourite.getDeductibles(), personToFavourite.getFeedback(),
personToFavourite.getTags(), newFavourite);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/seedu/address/logic/commands/UnfavouriteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

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.person.Person;

/**
* Unfavourites an exisiting contact
*/

public class UnfavouriteCommand extends Command {

public static final String COMMAND_WORD = "unfavourite";
public static final String COMMAND_ALIAS = "unfav";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes a person from your favourite contacts "
+ "by the index number used in the last person listing.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1 ";
public static final String MESSAGE_UNFAVOURITE_PERSON_FAIL = "Person not in favourites: %1$s";
public static final String MESSAGE_UNFAVOURITE_PERSON_SUCCESS = "Person removed from favourites: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
private final Index index;
/**
* @param index of the person in the filtered person list to edit
*/
public UnfavouriteCommand(Index index) {
requireNonNull(index);
this.index = index;
}
@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Person personToUnfavourite = lastShownList.get(index.getZeroBased());
Person unfavouritedPerson = createFavouritedPerson(personToUnfavourite);

model.unfavouritePerson(personToUnfavourite, unfavouritedPerson);

model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
if (personToUnfavourite.getFavourite()) {
return new CommandResult(String.format(MESSAGE_UNFAVOURITE_PERSON_SUCCESS,
unfavouritedPerson.getName().fullName));
} else {
return new CommandResult(String.format(MESSAGE_UNFAVOURITE_PERSON_FAIL,
unfavouritedPerson.getName().fullName));
}
}
/**
* Creates and returns a {@code Person} with the details of {@code personToFavourite}
*/
private static Person createFavouritedPerson(Person personToUnfavourite) {
assert personToUnfavourite != null;

boolean newFavourite;

if (personToUnfavourite.getFavourite()) {
newFavourite = !personToUnfavourite.getFavourite();
} else {
newFavourite = personToUnfavourite.getFavourite();
}

return new Person(personToUnfavourite.getName(), personToUnfavourite.getPhone(),
personToUnfavourite.getEmail(), personToUnfavourite.getAddress(), personToUnfavourite.getRating(),
personToUnfavourite.getDepartment(), personToUnfavourite.getManager(), personToUnfavourite.getSalary(),
personToUnfavourite.getOtHours(), personToUnfavourite.getOtRate(), personToUnfavourite.getDeductibles(),
personToUnfavourite.getFeedback(), personToUnfavourite.getTags(), newFavourite);
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof UnfavouriteCommand)) {
return false;
}
// state check
UnfavouriteCommand e = (UnfavouriteCommand) other;
return index.equals(e.index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.commands.UnfavouriteCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -71,6 +72,10 @@ public Command parseCommand(String userInput) throws ParseException {
case FavouriteCommand.COMMAND_ALIAS:
return new FavouriteCommandParser().parse(arguments);

case UnfavouriteCommand.COMMAND_WORD:
case UnfavouriteCommand.COMMAND_ALIAS:
return new UnfavouriteCommandParser().parse(arguments);

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
* Parses input arguments and creates a new FavouriteCommand object
*/

public class FavouriteCommandParser implements Parser<FavouriteCommand> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.UnfavouriteCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new FavouriteCommand object
*/

public class UnfavouriteCommandParser implements Parser<UnfavouriteCommand> {

/**
* Parses the given {@code String} of arguments in the context of the UnfavouriteCommand
* and returns an UnfavouriteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public UnfavouriteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new UnfavouriteCommand(index);
} catch (IllegalValueException ive) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnfavouriteCommand.MESSAGE_USAGE));
}
}

}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public interface Model {
*/
void favouritePerson(Person target, Person favouritedPerson);

/**
* Remove the given person from favourite.
* The person must exist in the favourites.
*/
void unfavouritePerson(Person target, Person unfavouritedPerson);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public void favouritePerson(Person target, Person favouritedPerson) {
indicateAddressBookChanged();
}

@Override
public void unfavouritePerson(Person target, Person unfavouritedPerson) {
requireAllNonNull(target, unfavouritedPerson);

versionedAddressBook.favouritePerson(target, unfavouritedPerson);
indicateAddressBookChanged();
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public void favouritePerson(Person target, Person favouritedPerson) {
throw new AssertionError("This method should not be called.");
}

@Override
public void unfavouritePerson(Person target, Person unfavouritedPerson) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getFilteredPersonList() {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public void execute_validIndexUnfilteredList_success() {

assertCommandSuccess(favouriteCommand, model, commandHistory, expectedFavouriteMessage, model);

// unfavourite success
// favourite fail

FavouriteCommand unfavouriteCommand = new FavouriteCommand(INDEX_FIRST_PERSON);
FavouriteCommand failFavouriteCommand = new FavouriteCommand(INDEX_FIRST_PERSON);

String expectedUnfavouriteMessage = String.format(FavouriteCommand.MESSAGE_UNFAVOURITE_PERSON_SUCCESS,
String expectedFailFavouriteMessage = String.format(FavouriteCommand.MESSAGE_FAVOURITE_PERSON_FAIL,
favouritedPerson.getName().fullName);

assertCommandSuccess(unfavouriteCommand, model, commandHistory, expectedUnfavouriteMessage, model);
assertCommandSuccess(failFavouriteCommand, model, commandHistory, expectedFailFavouriteMessage, model);
}

@Test
Expand Down
Loading

0 comments on commit 367957b

Please sign in to comment.