diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..2b5096df0 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -111,6 +111,26 @@ Deletes the 2nd person in the address book. `delete 1` + Deletes the 1st person in the results of the `find` command. +== Favouriting a person : `favourite` + +Favourites the specified person from the address book. Irreversible. + +Format: `favourite INDEX` + +**** +Favourites the person at the specified `INDEX`. +The index refers to the index number shown in the most recent listing. +**** + +Examples: + +* `list` + +`favourite 2` + +Favourites the 2nd person in the address book. + +* `find Betsy` + +`favourite 1` + +Favourites the 1st person in the results of the `find` command. + == View non-private details of a person : `view` Displays the non-private details of the specified person. + diff --git a/src/seedu/addressbook/commands/FavouriteCommand.java b/src/seedu/addressbook/commands/FavouriteCommand.java new file mode 100644 index 000000000..a7ac029fc --- /dev/null +++ b/src/seedu/addressbook/commands/FavouriteCommand.java @@ -0,0 +1,43 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.common.Messages; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException; +import seedu.addressbook.data.person.UniquePersonList.AlreadyFavouritedException; + +/** + * Favourites a person identified using it's last displayed index from the address book. + */ +public class FavouriteCommand extends Command { + + public static final String COMMAND_WORD = "favourite"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Favourites the person identified by the index number used in the last person listing.\n" + + "Parameters: INDEX\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_FAVOURITE_PERSON_SUCCESS = "Favourited %1$s"; + public static final String MESSAGE_PERSON_ALREADY_FAVOURITED = "Peron is already favourited"; + + public FavouriteCommand(int targetVisibleIndex) { + super(targetVisibleIndex); + } + + + @Override + public CommandResult execute() { + try { + final ReadOnlyPerson target = getTargetPerson(); + addressBook.favouritePerson(target); + return new CommandResult(String.format(MESSAGE_FAVOURITE_PERSON_SUCCESS, target)); + } catch (IndexOutOfBoundsException ie) { + return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } catch (PersonNotFoundException pnfe) { + return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); + } catch (AlreadyFavouritedException pnfe) { + return new CommandResult(MESSAGE_PERSON_ALREADY_FAVOURITED); + } + } + +} diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..ab73847b9 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -5,7 +5,7 @@ import seedu.addressbook.data.person.UniquePersonList; import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException; import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException; - +import seedu.addressbook.data.person.UniquePersonList.AlreadyFavouritedException; /** * Represents the entire address book. Contains the data of the address book. */ @@ -54,6 +54,10 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException allPersons.remove(toRemove); } + public void favouritePerson(ReadOnlyPerson toFavourite) throws PersonNotFoundException, AlreadyFavouritedException { + allPersons.favourite(toFavourite); + } + /** * Clears all persons and tags from the address book. */ diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 64551c7fe..5e523bf9e 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -16,7 +16,7 @@ public class Person implements ReadOnlyPerson { private Phone phone; private Email email; private Address address; - + private boolean isFavourite = false; private final Set tags = new HashSet<>(); /** @@ -62,6 +62,13 @@ public Set getTags() { return new HashSet<>(tags); } + public boolean setFavourite() { + this.isFavourite = true; + return true; + } + + public boolean getFavourite() { return this.isFavourite; } + /** * Replaces this person's tags with the tags in the argument tag set. */ diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index d7acd8b4a..2c1a4f24c 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -29,6 +29,13 @@ protected DuplicatePersonException() { } } + /** + * Signals that the person is already favourited. + */ + public static class AlreadyFavouritedException extends DuplicateDataException { + protected AlreadyFavouritedException() { super("Peron is already favourited"); } + } + /** * Signals that an operation targeting a specified person in the list would fail because * there is no such matching person in the list. @@ -122,6 +129,23 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException { } } + /** + * Favourites the equivalent person from the list. + * + * @throws PersonNotFoundException if no such person could be found in the list. + * @throws AlreadyFavouritedException if person is already favourited. + */ + public void favourite(ReadOnlyPerson toFavourite) throws PersonNotFoundException, AlreadyFavouritedException { + if(internalList.get(internalList.indexOf(toFavourite)).getFavourite()) { + throw new AlreadyFavouritedException(); + } else { + final boolean personFoundAndFavourited = internalList.get(internalList.indexOf(toFavourite)).setFavourite(); + if (!personFoundAndFavourited) { + throw new PersonNotFoundException(); + } + } + } + /** * Clears all persons in list. */ diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..450b26aa0 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -17,6 +17,7 @@ import seedu.addressbook.commands.DeleteCommand; import seedu.addressbook.commands.ExitCommand; import seedu.addressbook.commands.FindCommand; +import seedu.addressbook.commands.FavouriteCommand; import seedu.addressbook.commands.HelpCommand; import seedu.addressbook.commands.IncorrectCommand; import seedu.addressbook.commands.ListCommand; @@ -85,6 +86,9 @@ public Command parseCommand(String userInput) { case FindCommand.COMMAND_WORD: return prepareFind(arguments); + case FavouriteCommand.COMMAND_WORD: + return prepareFavourite(arguments); + case ListCommand.COMMAND_WORD: return new ListCommand(); @@ -174,6 +178,23 @@ private Command prepareDelete(String args) { } } + /** + * Parses arguments in the context of the delete person command. + * + * @param args full command args string + * @return the prepared command + */ + private Command prepareFavourite(String args) { + try { + final int targetIndex = parseArgsAsDisplayedIndex(args); + return new FavouriteCommand(targetIndex); + } catch (ParseException pe) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FavouriteCommand.MESSAGE_USAGE)); + } catch (NumberFormatException nfe) { + return new IncorrectCommand(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + } + /** * Parses arguments in the context of the view command. * diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..d4740df9c 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -33,6 +33,9 @@ || Enter command: || [Command entered: delete 1] || The person index provided is invalid || =================================================== +|| Enter command: || [Command entered: favourite 1] +|| The person index provided is invalid +|| =================================================== || Enter command: || [Command entered: view 1] || The person index provided is invalid || =================================================== @@ -233,6 +236,27 @@ || || 2 persons listed! || =================================================== +|| Enter command: || [Command entered: favourite] +|| Invalid command format! +|| favourite: Favourites the person identified by the index number used in the last person listing. +|| Parameters: INDEX +|| Example: favourite 1 +|| =================================================== +|| Enter command: || [Command entered: favourite should be only one number] +|| The person index provided is invalid +|| =================================================== +|| Enter command: || [Command entered: favourite -1] +|| The person index provided is invalid +|| =================================================== +|| Enter command: || [Command entered: favourite 0] +|| The person index provided is invalid +|| =================================================== +|| Enter command: || [Command entered: favourite 3] +|| The person index provided is invalid +|| =================================================== +|| Enter command: || [Command entered: favourite 2] +|| Favourited Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| =================================================== || Enter command: || [Command entered: delete] || Invalid command format! || delete: Deletes the person identified by the index number used in the last person listing. diff --git a/test/input.txt b/test/input.txt index eb8df81f8..47f51f8f2 100644 --- a/test/input.txt +++ b/test/input.txt @@ -11,6 +11,7 @@ # should show appropriate message when no listing has happened yet delete 1 + favourite 1 view 1 viewall 1 @@ -106,6 +107,22 @@ # find multiple with some keywords find Charlie Betsy +########################################################## +# test favourite person command +########################################################## + +# last active view: [1] betsy [2] charlie + + # should catch invalid args format + favourite + favourite should be only one number + + # should catch invalid index + favourite -1 + favourite 0 + favourite 3 + favourite 2 + ########################################################## # test delete person command ##########################################################