diff --git a/.travis.yml b/.travis.yml index d075c8a77..e71c51f4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ deploy: provider: script script: ./config/travis/deploy_github_pages.sh on: - branch: master + all_branches: true addons: apt: diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..f122517b6 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -72,6 +72,11 @@ Examples: Shows a list of all persons, along with their non-private details, in the address book. + Format: `list` +== Sorting all persons by name : `sort` + +Sorts and displays a list of all persons, based on their names alphabetically, along with their non-private details, in the address book. + +Format: `sort` + == Finding all persons containing any keyword in their name: `find` Finds persons whose names contain any of the given keywords. + diff --git a/out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module b/out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module new file mode 100644 index 000000000..8fb60192d Binary files /dev/null and b/out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module differ diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index 2ff8f1575..f5bee9587 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -36,6 +36,10 @@ public static String getMessageForPersonListShownSummary(List personsDisplayed) { + return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, personsDisplayed.size()); + } + /** * Executes the command and returns the result. */ diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..d20a0125c 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -19,6 +19,7 @@ public CommandResult execute() { + "\n" + ClearCommand.MESSAGE_USAGE + "\n" + FindCommand.MESSAGE_USAGE + "\n" + ListCommand.MESSAGE_USAGE + + "\n" + SortCommand.MESSAGE_USAGE + "\n" + ViewCommand.MESSAGE_USAGE + "\n" + ViewAllCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE diff --git a/src/seedu/addressbook/commands/SortCommand.java b/src/seedu/addressbook/commands/SortCommand.java new file mode 100644 index 000000000..cda1678ac --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,25 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.List; + + +/** + * Lists all persons in the address book to the user. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Sorts and display all persons in the address book as a list with index numbers.\n" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + List allPersons = addressBook.sortAllPersons().immutableListView(); + return new CommandResult(getMessageForSortedPersonListShownSummary(allPersons), allPersons); + } +} diff --git a/src/seedu/addressbook/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 9e30d0f28..b23c6da0f 100644 --- a/src/seedu/addressbook/common/Messages.java +++ b/src/seedu/addressbook/common/Messages.java @@ -11,6 +11,7 @@ public class Messages { public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid"; public static final String MESSAGE_PERSON_NOT_IN_ADDRESSBOOK = "Person could not be found in address book"; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; + public static final String MESSAGE_PERSONS_SORTED_OVERVIEW = "%1$d persons sorted!"; public static final String MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE = "Launch command format: " + "java seedu.addressbook.Main [STORAGE_FILE_PATH]"; public static final String MESSAGE_WELCOME = "Welcome to your Address Book!"; diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..e5854ad84 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -68,6 +68,11 @@ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); } + public UniquePersonList sortAllPersons() { + allPersons.sort(); + return new UniquePersonList(allPersons); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 131d71b46..893cc1772 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -27,6 +27,7 @@ public Address(String address, boolean isPrivate) throws IllegalValueException { throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); } this.value = trimmedAddress; + } /** diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index d1d271414..c3636c122 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -43,6 +43,11 @@ public List getWordsInName() { return Arrays.asList(fullName.split("\\s+")); } + public int compareTo(Name target) { + int compare = this.fullName.compareTo(target.fullName); + return compare; + } + @Override public String toString() { return fullName; diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 64551c7fe..fe5e00e61 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -1,5 +1,6 @@ package seedu.addressbook.data.person; +import java.util.Comparator; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -62,6 +63,15 @@ public Set getTags() { return new HashSet<>(tags); } + /** + * Compare selected person's name with target's name + */ + public static Comparator compareByName = new Comparator() { + public int compare(Person selected, Person target) { + return selected.name.compareTo(target.name); + } + }; + /** * 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..19c3242ba 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -129,6 +129,13 @@ public void clear() { internalList.clear(); } + /** + * Sorts the current list in alphabetical order of names + */ + public void sort() { + Collections.sort(internalList, Person.compareByName); + } + @Override public Iterator iterator() { return internalList.iterator(); diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..7c1b0cc39 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -11,17 +11,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -76,6 +66,12 @@ public Command parseCommand(String userInput) { case AddCommand.COMMAND_WORD: return prepareAdd(arguments); + case SortCommand.COMMAND_WORD: + if (arguments.isEmpty()) { + return new SortCommand(); + } + return new HelpCommand(); + case DeleteCommand.COMMAND_WORD: return prepareDelete(arguments); diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..b3a5292bd 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -19,6 +19,8 @@ || Example: find alice bob charlie || list: Displays all persons in the address book as a list with index numbers. || Example: list +|| sort: Sorts and display all persons in the address book as a list with index numbers. +|| Example: sort || view: Views the non-private details of the person identified by the index number in the last shown person listing. || Parameters: INDEX || Example: view 1 @@ -73,6 +75,33 @@ || Enter command: || [Command entered: add Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag] || Tags names should be alphanumeric || =================================================== +|| Enter command: || [Command entered: sort this] +|| add: Adds a person to the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: add John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| delete: Deletes the person identified by the index number used in the last person listing. +|| Parameters: INDEX +|| Example: delete 1 +|| Clears address book permanently. +|| Example: clear +|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers. +|| Parameters: KEYWORD [MORE_KEYWORDS]... +|| Example: find alice bob charlie +|| list: Displays all persons in the address book as a list with index numbers. +|| Example: list +|| sort: Sorts and display all persons in the address book as a list with index numbers. +|| Example: sort +|| view: Views the non-private details of the person identified by the index number in the last shown person listing. +|| Parameters: INDEX +|| Example: view 1 +|| viewall: Views the non-private details of the person identified by the index number in the last shown person listing. +|| Parameters: INDEX +|| Example: viewall 1 +|| help: Shows program usage instructions. +|| Example: help +|| exit: Exits the program. +|| Example: exit +|| =================================================== || Enter command: || [Command entered: add []\[;] p/12345 e/valid@e.mail a/valid, address] || Person names should be spaces or alphabetic characters || =================================================== @@ -135,6 +164,15 @@ || || 5 persons listed! || =================================================== +|| Enter command: || [Command entered: sort] +|| 1. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons sorted! +|| =================================================== || Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] || This person already exists in the address book || =================================================== diff --git a/test/input.txt b/test/input.txt index eb8df81f8..7fa1a6e98 100644 --- a/test/input.txt +++ b/test/input.txt @@ -23,7 +23,7 @@ list ########################################################## -# test add person command, setup state for further tests +# test add/sort person command, setup state for further tests ########################################################## # should catch invalid args format @@ -32,6 +32,7 @@ add Valid Name p/12345 valid@email.butNoPrefix a/valid, address add Valid Name p/12345 e/valid@email.butNoAddressPrefix valid, address add Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag + sort this # should catch invalid person data add []\[;] p/12345 e/valid@e.mail a/valid, address @@ -50,6 +51,7 @@ list add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy list + sort # should not allow adding duplicate persons add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy