forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #66 from CS2103-AY1819S1-T13-2/wip/unfavourite
add unfavourite command
- Loading branch information
Showing
13 changed files
with
395 additions
and
20 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
93 changes: 93 additions & 0 deletions
93
src/main/java/seedu/address/logic/commands/UnfavouriteCommand.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,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); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/seedu/address/logic/parser/UnfavouriteCommandParser.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,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)); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.