forked from nus-cs2103-AY1718S1/addressbook-level4-old
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #27 from aaronyhsoh/master
Added favourite contact feature
- Loading branch information
Showing
20 changed files
with
299 additions
and
18 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
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
114 changes: 114 additions & 0 deletions
114
src/main/java/seedu/address/logic/commands/FavouriteCommand.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,114 @@ | ||
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 java.util.Set; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.person.Address; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.person.ReadOnlyPerson; | ||
import seedu.address.model.person.exceptions.DuplicatePersonException; | ||
import seedu.address.model.person.exceptions.PersonNotFoundException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Favourites an exisiting contact | ||
*/ | ||
public class FavouriteCommand extends UndoableCommand { | ||
|
||
public static final String COMMAND_WORD = "favourite"; | ||
public static final String COMMAND_ALIAS = "fav"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to 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_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: "; | ||
|
||
private final Index index; | ||
|
||
|
||
/** | ||
* @param index of the person in the filtered person list to edit | ||
*/ | ||
public FavouriteCommand(Index index) { | ||
requireNonNull(index); | ||
|
||
this.index = index; | ||
} | ||
|
||
@Override | ||
public CommandResult executeUndoableCommand() throws CommandException { | ||
List<ReadOnlyPerson> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
ReadOnlyPerson 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.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
|
||
if (personToFavourite.getFavourite()) { | ||
return new CommandResult(String.format(MESSAGE_UNFAVOURITE_PERSON_SUCCESS, favouritedPerson)); | ||
} else { | ||
return new CommandResult(String.format(MESSAGE_FAVOURITE_PERSON_SUCCESS, favouritedPerson)); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Creates and returns a {@code Person} with the details of {@code personToFavourite} | ||
*/ | ||
private static Person createFavouritedPerson(ReadOnlyPerson personToFavourite) { | ||
assert personToFavourite != null; | ||
|
||
Name originalName = personToFavourite.getName(); | ||
Phone originalPhone = personToFavourite.getPhone(); | ||
Email originalEmail = personToFavourite.getEmail(); | ||
Address originalAddress = personToFavourite.getAddress(); | ||
Set<Tag> originalTags = personToFavourite.getTags(); | ||
boolean newFavourite = !personToFavourite.getFavourite(); | ||
|
||
return new Person(originalName, originalPhone, originalEmail, originalAddress, | ||
originalTags, newFavourite); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FavouriteCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
FavouriteCommand e = (FavouriteCommand) 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
30 changes: 30 additions & 0 deletions
30
src/main/java/seedu/address/logic/parser/FavouriteCommandParser.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,30 @@ | ||
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.FavouriteCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new DeleteCommand object | ||
*/ | ||
public class FavouriteCommandParser implements Parser<FavouriteCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FavouriteCommand | ||
* and returns an FavouriteCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FavouriteCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new FavouriteCommand(index); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FavouriteCommand.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
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.