From 49d64a47c1a25d2eab21848abcf541bd1da803ba Mon Sep 17 00:00:00 2001 From: Sreyans Sipani Date: Wed, 30 Jan 2019 03:09:09 +0800 Subject: [PATCH 1/4] Completed 2 TODOs - more permissive validations for name and phone --- src/seedu/addressbook/AddressBook.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..8e2aaa73 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -1048,8 +1048,8 @@ && isPersonPhoneValid(person[PERSON_DATA_INDEX_PHONE]) * @param name to be validated */ private static boolean isPersonNameValid(String name) { - return name.matches("(\\w|\\s)+"); // name is nonempty mixture of alphabets and whitespace - //TODO: implement a more permissive validation + // name is nonempty mixture of alphabets and whitespace and hyphens + return name.matches("(\\w|\\s|-)+"); } /** @@ -1058,8 +1058,8 @@ private static boolean isPersonNameValid(String name) { * @param phone to be validated */ private static boolean isPersonPhoneValid(String phone) { - return phone.matches("\\d+"); // phone nonempty sequence of digits - //TODO: implement a more permissive validation + // phone nonempty sequence of digits and hyphens with an optional plus in the front + return phone.matches("\\+*(\\d+|-)+"); } /** From 415ca2225ae943e114e87cc406c2307b939b7f72 Mon Sep 17 00:00:00 2001 From: Sreyans Sipani Date: Wed, 30 Jan 2019 03:38:34 +0800 Subject: [PATCH 2/4] Implemented sort by field feature --- src/seedu/addressbook/AddressBook.java | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 8e2aaa73..8f6b8a75 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Optional; import java.util.Scanner; @@ -115,6 +116,10 @@ public class AddressBook { private static final String COMMAND_LIST_DESC = "Displays all persons as a list with index numbers."; private static final String COMMAND_LIST_EXAMPLE = COMMAND_LIST_WORD; + private static final String COMMAND_SORT_WORD = "sort"; + private static final String COMMAND_SORT_DESC = "Sorts all persons by given field ('name' or 'phone' or 'email') and lists them out"; + private static final String COMMAND_SORT_EXAMPLE = COMMAND_SORT_WORD + " name"; + private static final String COMMAND_DELETE_WORD = "delete"; private static final String COMMAND_DELETE_DESC = "Deletes a person identified by the index number used in " + "the last find/list call."; @@ -375,6 +380,8 @@ private static String executeCommand(String userInputString) { return executeFindPersons(commandArgs); case COMMAND_LIST_WORD: return executeListAllPersonsInAddressBook(); + case COMMAND_SORT_WORD: + return executeSortAllPersonsInAddressBook(commandArgs); case COMMAND_DELETE_WORD: return executeDeletePerson(commandArgs); case COMMAND_CLEAR_WORD: @@ -579,6 +586,35 @@ private static String executeListAllPersonsInAddressBook() { return getMessageForPersonsDisplayedSummary(toBeDisplayed); } + /** + * Displays all persons in the address book to the user; in added order. + * + * @return feedback display message for the operation result + */ + private static String executeSortAllPersonsInAddressBook(String commandArgs) { + int sortIndex = getColumnNumber(commandArgs); + if (sortIndex == -1) { + return getMessageForInvalidCommandInput(COMMAND_SORT_WORD, getUsageInfoForSortCommand()); + } + ArrayList toBeDisplayed = getAllPersonsInAddressBook(); + Collections.sort(toBeDisplayed, new Comparator() { + public int compare(String[] person, String[] otherPerson) { + return person[sortIndex].compareTo(otherPerson[sortIndex]); + } + }); + showToUser(toBeDisplayed); + return getMessageForPersonsDisplayedSummary(toBeDisplayed); + } + + private static int getColumnNumber(String rawArgs) { + switch(rawArgs.trim()) { + case "name": return PERSON_DATA_INDEX_NAME; + case "phone": return PERSON_DATA_INDEX_PHONE; + case "email": return PERSON_DATA_INDEX_EMAIL; + default: return -1; + } + } + /** * Requests to terminate the program. */ @@ -1085,6 +1121,7 @@ private static String getUsageInfoForAllCommands() { return getUsageInfoForAddCommand() + LS + getUsageInfoForFindCommand() + LS + getUsageInfoForViewCommand() + LS + + getUsageInfoForSortCommand() + LS + getUsageInfoForDeleteCommand() + LS + getUsageInfoForClearCommand() + LS + getUsageInfoForExitCommand() + LS @@ -1124,6 +1161,12 @@ private static String getUsageInfoForViewCommand() { + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_LIST_EXAMPLE) + LS; } + /** Returns the string for showing 'sort' command usage instruction */ + private static String getUsageInfoForSortCommand() { + return String.format(MESSAGE_COMMAND_HELP, COMMAND_SORT_WORD, COMMAND_SORT_DESC) + LS + + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_SORT_EXAMPLE) + LS; + } + /** Returns string for showing 'help' command usage instruction */ private static String getUsageInfoForHelpCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_HELP_WORD, COMMAND_HELP_DESC) From 08090333e577a9feb5974c11b5b74b6dcbf25ef1 Mon Sep 17 00:00:00 2001 From: Sreyans Sipani Date: Wed, 30 Jan 2019 04:24:26 +0800 Subject: [PATCH 3/4] Added tests for sorting --- test/expected.txt | 42 ++++++++++++++++++++++++++++++++++++++++++ test/input.txt | 13 +++++++++++++ 2 files changed, 55 insertions(+) diff --git a/test/expected.txt b/test/expected.txt index f18922ac..1a7bbeab 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -62,6 +62,9 @@ || list: Displays all persons as a list with index numbers. || Example: list || +|| sort: Sorts all persons by given field ('name' or 'phone' or 'email') and lists them out +|| Example: sort name +|| || delete: Deletes a person identified by the index number used in the last find/list call. || Parameters: INDEX || Example: delete 1 @@ -171,6 +174,45 @@ || || 5 persons found! || =================================================== +|| Enter command: || [Command entered: sort] +|| Invalid command format: sort +|| sort: Sorts all persons by given field ('name' or 'phone' or 'email') and lists them out +|| Example: sort name +|| +|| =================================================== +|| Enter command: || [Command entered: sort random field] +|| Invalid command format: sort +|| sort: Sorts all persons by given field ('name' or 'phone' or 'email') and lists them out +|| Example: sort name +|| +|| =================================================== +|| Enter command: || [Command entered: sort name] +|| 1. Adam Brown Phone Number: 111111 Email: adam@gmail.com +|| 2. Betsy Choo Phone Number: 222222 Email: benchoo@nus.edu.sg +|| 3. Charlie Dickson Phone Number: 333333 Email: charlie.d@nus.edu.sg +|| 4. Dickson Ee Phone Number: 444444 Email: dickson@nus.edu.sg +|| 5. Esther Potato Phone Number: 555555 Email: esther@notreal.potato +|| +|| 5 persons found! +|| =================================================== +|| Enter command: || [Command entered: sort phone] +|| 1. Adam Brown Phone Number: 111111 Email: adam@gmail.com +|| 2. Betsy Choo Phone Number: 222222 Email: benchoo@nus.edu.sg +|| 3. Charlie Dickson Phone Number: 333333 Email: charlie.d@nus.edu.sg +|| 4. Dickson Ee Phone Number: 444444 Email: dickson@nus.edu.sg +|| 5. Esther Potato Phone Number: 555555 Email: esther@notreal.potato +|| +|| 5 persons found! +|| =================================================== +|| Enter command: || [Command entered: sort email] +|| 1. Adam Brown Phone Number: 111111 Email: adam@gmail.com +|| 2. Betsy Choo Phone Number: 222222 Email: benchoo@nus.edu.sg +|| 3. Charlie Dickson Phone Number: 333333 Email: charlie.d@nus.edu.sg +|| 4. Dickson Ee Phone Number: 444444 Email: dickson@nus.edu.sg +|| 5. Esther Potato Phone Number: 555555 Email: esther@notreal.potato +|| +|| 5 persons found! +|| =================================================== || Enter command: || [Command entered: find] || || 0 persons found! diff --git a/test/input.txt b/test/input.txt index 0b99df54..9edea121 100644 --- a/test/input.txt +++ b/test/input.txt @@ -41,6 +41,19 @@ add Esther Potato p/555555 e/esther@notreal.potato list +########################################################## +# test sort persons command +########################################################## + + # should catch invalid args format + sort + sort random field + + # should sort by fields correctly + sort name + sort phone + sort email + ########################################################## # test find persons command ########################################################## From 4335b16656e7a83ab8255e63dc678c9c37f4805c Mon Sep 17 00:00:00 2001 From: Sreyans Sipani Date: Wed, 30 Jan 2019 04:27:26 +0800 Subject: [PATCH 4/4] Updated user guide --- docs/UserGuide.adoc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index e66f0952..39fe370b 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -62,6 +62,12 @@ Shows a list of persons, as an indexed list, in the order they were added to the oldest first. + Format: `list` +=== Sorting all persons: `sort` + +Shows a list of persons, as an indexed list, sorted by the field ('name' or 'phone' +or 'email') provided. + +Format: `sort FIELDNAME` + === Finding a person by keyword `find` Finds persons that match given keywords. +