-
Notifications
You must be signed in to change notification settings - Fork 101
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
*/ | ||
|
@@ -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|-)+"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm this doesn't seem to be related to |
||
} | ||
|
||
/** | ||
|
@@ -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+|-)+"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
|
||
/** | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: [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! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
########################################################## | ||
|
There was a problem hiding this comment.
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?