forked from se-edu/addressbook-level1
-
Notifications
You must be signed in to change notification settings - Fork 111
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
[T2A4][W13-A3]Melvin Tan #300
Open
Melvin-Tan
wants to merge
4
commits into
nus-cs2103-AY1617S2:master
Choose a base branch
from
Melvin-Tan:T2A4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ test/actual.txt | |
test/localrun.bat | ||
test/data/ | ||
/bin/ | ||
|
||
# DS_Store | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,11 @@ | |
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Scanner; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/* | ||
* NOTE : ============================================================= | ||
|
@@ -114,6 +116,10 @@ public class AddressBook { | |
private static final String COMMAND_LIST_WORD = "list"; | ||
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 = "Displays all persons as a list with index numbers, sorted by name."; | ||
private static final String COMMAND_SORT_EXAMPLE = COMMAND_SORT_WORD; | ||
|
||
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 " | ||
|
@@ -375,6 +381,8 @@ private static String executeCommand(String userInputString) { | |
return executeFindPersons(commandArgs); | ||
case COMMAND_LIST_WORD: | ||
return executeListAllPersonsInAddressBook(); | ||
case COMMAND_SORT_WORD: | ||
return executeListAllPersonsInAddressBookSortedByName(); | ||
case COMMAND_DELETE_WORD: | ||
return executeDeletePerson(commandArgs); | ||
case COMMAND_CLEAR_WORD: | ||
|
@@ -483,10 +491,12 @@ private static Set<String> extractKeywordsFromFindPersonArgs(String findPersonCo | |
* @return list of persons in full model with name containing some of the keywords | ||
*/ | ||
private static ArrayList<String[]> getPersonsWithNameContainingAnyKeyword(Collection<String> keywords) { | ||
List<String> keywordsInLowercase = getStringCollectionInLowercase(keywords); | ||
final ArrayList<String[]> matchedPersons = new ArrayList<>(); | ||
for (String[] person : getAllPersonsInAddressBook()) { | ||
final Set<String> wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); | ||
if (!Collections.disjoint(wordsInName, keywords)) { | ||
List<String> wordsInNameInLowercase = getStringCollectionInLowercase(wordsInName); | ||
if (!Collections.disjoint(wordsInNameInLowercase, keywordsInLowercase)) { | ||
matchedPersons.add(person); | ||
} | ||
} | ||
|
@@ -546,6 +556,18 @@ private static int extractTargetIndexFromDeletePersonArgs(String rawArgs) { | |
private static boolean isDisplayIndexValidForLastPersonListingView(int index) { | ||
return index >= DISPLAYED_INDEX_OFFSET && index < latestPersonListingView.size() + DISPLAYED_INDEX_OFFSET; | ||
} | ||
|
||
/** | ||
* Convert all strings from a collection to lowercase. | ||
* | ||
* @param collection of string | ||
* @return list of string in lowercase | ||
*/ | ||
private static List<String> getStringCollectionInLowercase(Collection<String> collection) { | ||
return collection.stream() | ||
.map(String::toLowerCase) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Constructs a feedback message for a successful delete person command execution. | ||
|
@@ -578,6 +600,20 @@ private static String executeListAllPersonsInAddressBook() { | |
showToUser(toBeDisplayed); | ||
return getMessageForPersonsDisplayedSummary(toBeDisplayed); | ||
} | ||
|
||
/** | ||
* Displays all persons in the address book to the user; in sorted order by name. | ||
* | ||
* @return feedback display message for the operation result | ||
*/ | ||
private static String executeListAllPersonsInAddressBookSortedByName() { | ||
ArrayList<String[]> allPersons = getAllPersonsInAddressBook(); | ||
// Creates a copy of allPersons for sorting so ALL_PERSONS do not mutate. | ||
ArrayList<String[]> toBeDisplayed = new ArrayList<String[]>(allPersons); | ||
Collections.sort(toBeDisplayed, (person1, person2) -> person1[0].compareTo(person2[0])); | ||
showToUser(toBeDisplayed); | ||
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. Take note of the usage of magic numbers here |
||
return getMessageForPersonsDisplayedSummary(toBeDisplayed); | ||
} | ||
|
||
/** | ||
* 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 | ||
|
@@ -1123,6 +1160,11 @@ private static String getUsageInfoForViewCommand() { | |
return String.format(MESSAGE_COMMAND_HELP, COMMAND_LIST_WORD, COMMAND_LIST_DESC) + LS | ||
+ String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_LIST_EXAMPLE) + LS; | ||
} | ||
|
||
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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,77 @@ | |
|| | ||
|| 5 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add John Doe p/98765432 e/[email protected]] | ||
|| New person added: John Doe, Phone: 98765432, Email: [email protected] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort] | ||
|| 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] | ||
|| 6. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| | ||
|| 6 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Catie Parry p/12345678 e/[email protected]] | ||
|| New person added: Catie Parry, Phone: 12345678, Email: [email protected] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort] | ||
|| 1. Adam Brown Phone Number: 111111 Email: [email protected] | ||
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 3. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 4. Charlie Dickson Phone Number: 333333 Email: [email protected] | ||
|| 5. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 6. Esther Potato Phone Number: 555555 Email: [email protected] | ||
|| 7. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| | ||
|| 7 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Betty p/92837465 e/[email protected]] | ||
|| New person added: Betty, Phone: 92837465, Email: [email protected] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort] | ||
|| 1. Adam Brown Phone Number: 111111 Email: [email protected] | ||
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 3. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 4. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 5. Charlie Dickson Phone Number: 333333 Email: [email protected] | ||
|| 6. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 7. Esther Potato Phone Number: 555555 Email: [email protected] | ||
|| 8. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| | ||
|| 8 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Zack Low p/99887766 e/[email protected]] | ||
|| New person added: Zack Low, Phone: 99887766, Email: [email protected] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort] | ||
|| 1. Adam Brown Phone Number: 111111 Email: [email protected] | ||
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 3. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 4. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 5. Charlie Dickson Phone Number: 333333 Email: [email protected] | ||
|| 6. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 7. Esther Potato Phone Number: 555555 Email: [email protected] | ||
|| 8. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| 9. Zack Low Phone Number: 99887766 Email: [email protected] | ||
|| | ||
|| 9 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 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] | ||
|| 6. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| 7. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 8. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 9. Zack Low Phone Number: 99887766 Email: [email protected] | ||
|| | ||
|| 9 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: find] | ||
|| | ||
|| 0 persons found! | ||
|
@@ -184,8 +255,9 @@ | |
|| 0 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: find betsy] | ||
|| 1. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| | ||
|| 0 persons found! | ||
|| 1 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: find Betsy] | ||
|| 1. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|
@@ -246,8 +318,12 @@ | |
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 3. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 4. Esther Potato Phone Number: 555555 Email: [email protected] | ||
|| 5. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| 6. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 7. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 8. Zack Low Phone Number: 99887766 Email: [email protected] | ||
|| | ||
|| 4 persons found! | ||
|| 8 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: delete 4] | ||
|| Deleted Person: Esther Potato Phone Number: 555555 Email: [email protected] | ||
|
@@ -256,17 +332,25 @@ | |
|| 1. Adam Brown Phone Number: 111111 Email: [email protected] | ||
|| 2. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 3. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 4. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| 5. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 6. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 7. Zack Low Phone Number: 99887766 Email: [email protected] | ||
|| | ||
|| 3 persons found! | ||
|| 7 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: delete 1] | ||
|| Deleted Person: Adam Brown Phone Number: 111111 Email: [email protected] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 1. Betsy Choo Phone Number: 222222 Email: [email protected] | ||
|| 2. Dickson Ee Phone Number: 444444 Email: [email protected] | ||
|| 3. John Doe Phone Number: 98765432 Email: [email protected] | ||
|| 4. Catie Parry Phone Number: 12345678 Email: [email protected] | ||
|| 5. Betty Phone Number: 92837465 Email: [email protected] | ||
|| 6. Zack Low Phone Number: 99887766 Email: [email protected] | ||
|| | ||
|| 2 persons found! | ||
|| 6 persons found! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: clear] | ||
|| Address book has been cleared! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
list | ||
|
||
########################################################## | ||
# test add person command, setup state for futuer tests | ||
# test add person command, setup state for future tests | ||
########################################################## | ||
|
||
# should catch invalid args format | ||
|
@@ -40,6 +40,23 @@ | |
list | ||
add Esther Potato p/555555 e/[email protected] | ||
list | ||
|
||
########################################################## | ||
# test sort person command | ||
########################################################## | ||
|
||
# should add correctly | ||
add John Doe p/98765432 e/[email protected] | ||
sort | ||
add Catie Parry p/12345678 e/[email protected] | ||
sort | ||
add Betty p/92837465 e/[email protected] | ||
sort | ||
add Zack Low p/99887766 e/[email protected] | ||
sort | ||
|
||
# list should still follow added order | ||
list | ||
|
||
########################################################## | ||
# test find persons command | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good job in adding the relevant comments and changes for the tests.