From b5f8f7a367b640b73597aaea4bbd400e57bf5662 Mon Sep 17 00:00:00 2001 From: XavierMaYuqian <32877925+XavierMaYuqian@users.noreply.github.com> Date: Thu, 8 Mar 2018 17:19:24 +0800 Subject: [PATCH] new command 'sort' --- doc/UserGuide.md | 5 +++ .../addressbook/commands/HelpCommand.java | 1 + .../addressbook/commands/SortCommand.java | 21 +++++++++ src/seedu/addressbook/common/Messages.java | 1 + src/seedu/addressbook/data/AddressBook.java | 7 +++ .../data/person/UniquePersonList.java | 7 +++ src/seedu/addressbook/parser/Parser.java | 3 ++ .../seedu/addressbook/logic/LogicTest.java | 44 +++++++++++++++++++ .../seedu/addressbook/parser/ParserTest.java | 5 +++ 9 files changed, 94 insertions(+) create mode 100644 src/seedu/addressbook/commands/SortCommand.java diff --git a/doc/UserGuide.md b/doc/UserGuide.md index d03dfce73..7792ce21b 100644 --- a/doc/UserGuide.md +++ b/doc/UserGuide.md @@ -38,6 +38,11 @@ Examples: Shows a list of all persons in the address book.
Format: `list` +## Sorting all persons : `sort` +Sorts all the entries in address book based on people's full name in alphabetical order.
+Format: `sort` + + ## Finding all persons containing any keyword in their name: `find` Finds persons whose names contain any of the given keywords.
Format: `find KEYWORD [MORE_KEYWORDS]` 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..50a7f6124 --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,21 @@ +package seedu.addressbook.commands; +import seedu.addressbook.common.Messages; + +/** + * based on first names, sort entries in the address book in order. + */ +public class SortCommand extends Command{ + + public static final String COMMAND_WORD = "sort"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Sorts entries in the address book according to first name.\n" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + this.addressBook.sort(); + return new CommandResult(Messages.MESSAGE_SORTED); + } +} + diff --git a/src/seedu/addressbook/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 02cfe6155..952a4a426 100644 --- a/src/seedu/addressbook/common/Messages.java +++ b/src/seedu/addressbook/common/Messages.java @@ -13,4 +13,5 @@ public class Messages { "java seedu.addressbook.Main [STORAGE_FILE_PATH]"; public static final String MESSAGE_WELCOME = "Welcome to your Address Book!"; public static final String MESSAGE_USING_STORAGE_FILE = "Using storage file : %1$s"; + public static final String MESSAGE_SORTED = "Address Book is sorted successfully!"; } diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 7af05d271..611a24931 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -133,4 +133,11 @@ public int hashCode() { // use this method for custom fields hashing instead of implementing your own return Objects.hash(allPersons, allTags); } + + /** + * Based on full names, sort all persons in the list in order. + */ + public void sort(){ + allPersons.sort(); + } } diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index c4848a1b4..23c34293d 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -131,4 +131,11 @@ public int hashCode() { return internalList.hashCode(); } + /** + * Sorts all persons in the list according to their full name + */ + public void sort(){ + internalList.sort((personA, personB) -> (personA.getName().fullName.toLowerCase().compareTo(personB.getName().fullName.toLowerCase()))); + } + } diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 58f4f7e6c..4b3bcfb8e 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -78,6 +78,9 @@ public Command parseCommand(String userInput) { case ViewAllCommand.COMMAND_WORD: return prepareViewAll(arguments); + case SortCommand.COMMAND_WORD: + return new SortCommand(); + case ExitCommand.COMMAND_WORD: return new ExitCommand(); diff --git a/test/java/seedu/addressbook/logic/LogicTest.java b/test/java/seedu/addressbook/logic/LogicTest.java index 7efa921ca..2ded07d63 100644 --- a/test/java/seedu/addressbook/logic/LogicTest.java +++ b/test/java/seedu/addressbook/logic/LogicTest.java @@ -457,6 +457,50 @@ public void execute_find_matchesIfAnyKeywordPresent() throws Exception { expectedList); } + @Test + public void execute_sort_nonEmptyAddressBook() throws Exception { + TestDataHelper testDatahelper = new TestDataHelper(); + Person target1 = testDatahelper.generatePersonWithName("A"); + Person target2 = testDatahelper.generatePersonWithName("B"); + Person target3 = testDatahelper.generatePersonWithName("C"); + Person target4 = testDatahelper.generatePersonWithName("D"); + Person person1 = testDatahelper.generatePersonWithName("D"); + Person person2 = testDatahelper.generatePersonWithName("B"); + Person person3 = testDatahelper.generatePersonWithName("C"); + Person person4 = testDatahelper.generatePersonWithName("A"); + //get an unsorted list + List unsortedList = testDatahelper.generatePersonList(person1,person2,person3,person4); + //Expect sorted list + List expectedList = testDatahelper.generatePersonList(target1,target2,target3,target4); + //No last shown list so expect empty + List emptyList = new ArrayList<>(); + //Sorted AddressBook expected + AddressBook expectedAB = testDatahelper.generateAddressBook(expectedList); + //add unsorted list into AddressBook + testDatahelper.addToAddressBook(addressBook, unsortedList); + assertCommandBehavior("sort", + Messages.MESSAGE_SORTED, + expectedAB, + false, + emptyList); + } + + @Test + public void execute_sort_emptyAddressBook() throws Exception { + TestDataHelper testDatahelper = new TestDataHelper(); + List emptyList = new ArrayList<>(); + List expectedList = new ArrayList<>(); + //Empty AddressBook expected + AddressBook expectedAB = testDatahelper.generateAddressBook(expectedList); + //add empty list into AddressBook + testDatahelper.addToAddressBook(addressBook, emptyList); + assertCommandBehavior("sort", + Messages.MESSAGE_SORTED, + expectedAB, + false, + expectedList); + } + /** * A utility class to generate test data. */ diff --git a/test/java/seedu/addressbook/parser/ParserTest.java b/test/java/seedu/addressbook/parser/ParserTest.java index f01be613c..6b5e83877 100644 --- a/test/java/seedu/addressbook/parser/ParserTest.java +++ b/test/java/seedu/addressbook/parser/ParserTest.java @@ -52,6 +52,11 @@ public void clearCommand_parsedCorrectly() { final String input = "clear"; parseAndAssertCommandType(input, ClearCommand.class); } + @Test + public void sortCommand_parsedCorrectly() { + final String input = "sort"; + parseAndAssertCommandType(input, SortCommand.class); + } @Test public void listCommand_parsedCorrectly() {