diff --git a/src/seedu/addressbook/commands/FindCommand.java b/src/seedu/addressbook/commands/FindCommand.java index c8e9a380f..75a1602fd 100644 --- a/src/seedu/addressbook/commands/FindCommand.java +++ b/src/seedu/addressbook/commands/FindCommand.java @@ -2,6 +2,7 @@ import seedu.addressbook.data.person.ReadOnlyPerson; +import javax.print.DocFlavor; import java.util.*; /** @@ -44,9 +45,17 @@ public CommandResult execute() { */ private List getPersonsWithNameContainingAnyKeyword(Set keywords) { final List matchedPersons = new ArrayList<>(); + + /* Converts all keywords into lowercase and puts them into a new ArrayList */ + ArrayList lowercaseKeywords = new ArrayList(); + for (String word : keywords ){ + lowercaseKeywords.add(word.toLowerCase()); + } + for (ReadOnlyPerson person : addressBook.getAllPersons()) { - final Set wordsInName = new HashSet<>(person.getName().getWordsInName()); - if (!Collections.disjoint(wordsInName, keywords)) { + final Set wordsInName = new HashSet<>(person.getName().getLowercaseWordsInName()); + + if (!Collections.disjoint(wordsInName, lowercaseKeywords)) { matchedPersons.add(person); } } diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index ef2ed7d0e..0c8006167 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -16,6 +16,7 @@ public class HelpCommand extends Command { + "\n" + ClearCommand.MESSAGE_USAGE + "\n" + FindCommand.MESSAGE_USAGE + "\n" + ListCommand.MESSAGE_USAGE + + "\n" + SortCommand.MESSAGE_USAGE + "\n" + ViewCommand.MESSAGE_USAGE + "\n" + ViewAllCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE diff --git a/src/seedu/addressbook/commands/SortCommand.java b/src/seedu/addressbook/commands/SortCommand.java new file mode 100644 index 000000000..e06cf9cf3 --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,29 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.ArrayList; +import java.util.List; + +/** + * Sorts all persons in the address book in alphabetic order to the user. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + + "Displays all persons in the address book in alphabetic order as a list with index numbers.\n\t" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + + List SortAllPersons = addressBook.getAllPersons().SortAllPersons(); + + return new CommandResult(getMessageForPersonListShownSummary(SortAllPersons), SortAllPersons); + + } +} diff --git a/src/seedu/addressbook/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 02cfe6155..6c77a664e 100644 --- a/src/seedu/addressbook/common/Messages.java +++ b/src/seedu/addressbook/common/Messages.java @@ -9,6 +9,7 @@ public class Messages { public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid"; public static final String MESSAGE_PERSON_NOT_IN_ADDRESSBOOK = "Person could not be found in address book"; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; + public static final String MESSAGE_PERSONS_SORTED_OVERVIEW = "%1$d persons sorted!"; public static final String MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE = "Launch command format: " + "java seedu.addressbook.Main [STORAGE_FILE_PATH]"; public static final String MESSAGE_WELCOME = "Welcome to your Address Book!"; diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 487b7ad9c..754b5f639 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -2,6 +2,7 @@ import seedu.addressbook.data.exception.IllegalValueException; +import java.lang.reflect.Array; import java.util.Arrays; import java.util.List; @@ -39,10 +40,15 @@ public static boolean isValidName(String test) { /** * Retrieves a listing of every word in the name, in order. + * */ - public List getWordsInName() { - return Arrays.asList(fullName.split("\\s+")); - } + public List getWordsInName() { return Arrays.asList(fullName.split("\\s+")); } + + /** + * Retrieves a listing of every word in the name, in order, in lowercase. + * + */ + public List getLowercaseWordsInName() { return Arrays.asList(fullName.toLowerCase().split("\\s+")); } @Override public String toString() { diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index c4848a1b4..2e481dd5f 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -82,6 +82,23 @@ public boolean contains(ReadOnlyPerson toCheck) { return internalList.contains(toCheck); } + + /** + * Sorts all persons by their name, in alphabetic order a to z. + */ + public List SortAllPersons() { + + List SortedAllPersons = new ArrayList<>(); + + for (Person p: internalList){ + SortedAllPersons.add(p); + } + + SortedAllPersons.sort((person1, person2)->(person1.getName().fullName.compareToIgnoreCase(person2.getName().fullName))); + + return SortedAllPersons; + } + /** * Adds a person to the list. * diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 58f4f7e6c..c4ed061c8 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -69,6 +69,9 @@ public Command parseCommand(String userInput) { case FindCommand.COMMAND_WORD: return prepareFind(arguments); + case SortCommand.COMMAND_WORD: + return new SortCommand(); + case ListCommand.COMMAND_WORD: return new ListCommand();