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

[T2A4][W13-A3]Melvin Tan #300

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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ test/actual.txt
test/localrun.bat
test/data/
/bin/

# DS_Store
.DS_Store
44 changes: 43 additions & 1 deletion src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 : =============================================================
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Copy link

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.

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);
Copy link

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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 @@ -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() {
Expand Down
92 changes: 88 additions & 4 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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!
Expand Down
19 changes: 18 additions & 1 deletion test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down