Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sort command #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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. +
Expand Down
51 changes: 47 additions & 4 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.";
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -579,6 +586,35 @@ private static String executeListAllPersonsInAddressBook() {
return getMessageForPersonsDisplayedSummary(toBeDisplayed);
}

/**
* Displays all persons in the address book to the user; in added order.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is not correct?

*
* @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<String[]> toBeDisplayed = getAllPersonsInAddressBook();
Collections.sort(toBeDisplayed, new Comparator<String[]>() {
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to put these case strings as constants?

default: return -1;
}
}

/**
* Requests to terminate the program.
*/
Expand Down Expand Up @@ -1048,8 +1084,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|-)+");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this doesn't seem to be related to sort command? If so, it should have its own PR.

}

/**
Expand All @@ -1058,8 +1094,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+|-)+");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

}

/**
Expand All @@ -1085,6 +1121,7 @@ private static String getUsageInfoForAllCommands() {
return getUsageInfoForAddCommand() + LS
+ getUsageInfoForFindCommand() + LS
+ getUsageInfoForViewCommand() + LS
+ getUsageInfoForSortCommand() + LS
+ getUsageInfoForDeleteCommand() + LS
+ getUsageInfoForClearCommand() + LS
+ getUsageInfoForExitCommand() + LS
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [email protected]
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected]
|| 3. Charlie Dickson Phone Number: 333333 Email: [email protected]
|| 4. Dickson Ee Phone Number: 444444 Email: [email protected]
|| 5. Esther Potato Phone Number: 555555 Email: [email protected]
||
|| 5 persons found!
|| ===================================================
|| Enter command: || [Command entered: sort phone]
|| 1. Adam Brown Phone Number: 111111 Email: [email protected]
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected]
|| 3. Charlie Dickson Phone Number: 333333 Email: [email protected]
|| 4. Dickson Ee Phone Number: 444444 Email: [email protected]
|| 5. Esther Potato Phone Number: 555555 Email: [email protected]
||
|| 5 persons found!
|| ===================================================
|| Enter command: || [Command entered: sort email]
|| 1. Adam Brown Phone Number: 111111 Email: [email protected]
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected]
|| 3. Charlie Dickson Phone Number: 333333 Email: [email protected]
|| 4. Dickson Ee Phone Number: 444444 Email: [email protected]
|| 5. Esther Potato Phone Number: 555555 Email: [email protected]
||
|| 5 persons found!
|| ===================================================
|| Enter command: || [Command entered: find]
||
|| 0 persons found!
Expand Down
13 changes: 13 additions & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
add Esther Potato p/555555 e/[email protected]
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
##########################################################
Expand Down