From 91a04c3027434f70ddb0beb2af64c0d0f739898a Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 02:30:31 +0800 Subject: [PATCH 1/7] add sortedlist command --- .../addressbook/commands/DeleteCommand.java | 2 +- .../addressbook/commands/HelpCommand.java | 1 + .../commands/SortedListCommand.java | 25 +++++++++++++++++++ src/seedu/addressbook/data/AddressBook.java | 7 ++++++ src/seedu/addressbook/data/person/Person.java | 6 ++++- .../data/person/UniquePersonList.java | 9 +++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/seedu/addressbook/commands/SortedListCommand.java diff --git a/src/seedu/addressbook/commands/DeleteCommand.java b/src/seedu/addressbook/commands/DeleteCommand.java index 493d75da6..f12cf011f 100644 --- a/src/seedu/addressbook/commands/DeleteCommand.java +++ b/src/seedu/addressbook/commands/DeleteCommand.java @@ -6,7 +6,7 @@ /** - * Deletes a person identified using it's last displayed index from the address book. + * Deletes a person identified using its last displayed index from the address book. */ public class DeleteCommand extends Command { diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..f49f52a73 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -23,6 +23,7 @@ public CommandResult execute() { + "\n" + ViewAllCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE + "\n" + ExitCommand.MESSAGE_USAGE + + "\n" + SortedListCommand.MESSAGE_USAGE ); } } diff --git a/src/seedu/addressbook/commands/SortedListCommand.java b/src/seedu/addressbook/commands/SortedListCommand.java new file mode 100644 index 000000000..1896b4d64 --- /dev/null +++ b/src/seedu/addressbook/commands/SortedListCommand.java @@ -0,0 +1,25 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.List; + + +/** + * Lists all persons in the address book to the user in ascending alphabetical order. + */ +public class SortedListCommand extends Command { + + public static final String COMMAND_WORD = "sortedlist"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Displays all persons in the address book as a list in ascending alphabetical order.\n" + + "Example: " + COMMAND_WORD; + + @Override + public CommandResult execute() { + List sortedAllPersons = addressBook.getSortedAllPersons().immutableListView(); + return new CommandResult(getMessageForPersonListShownSummary(sortedAllPersons), sortedAllPersons); + } + +} diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..5364c47a5 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -68,6 +68,13 @@ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); } + /** + * Returns a new UniquePersonList of all persons in the address book at the time of the call sorted in alphabetical order. + */ + public UniquePersonList getSortedAllPersons() { + return new UniquePersonList(allPersons).sort(); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 64551c7fe..0a49f38ff 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -10,7 +10,7 @@ * Represents a Person in the address book. * Guarantees: details are present and not null, field values are validated. */ -public class Person implements ReadOnlyPerson { +public class Person implements ReadOnlyPerson, Comparable { private Name name; private Phone phone; @@ -88,4 +88,8 @@ public String toString() { return getAsTextShowAll(); } + @Override + public int compareTo(Person other) { + return name.toString().compareTo(other.name.toString()); + } } diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index d7acd8b4a..d373e9d53 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -72,6 +72,15 @@ public UniquePersonList(UniquePersonList source) { internalList.addAll(source.internalList); } + /** + * Returns a shallow sorted copy of the list. + */ + public UniquePersonList sort(UniquePersonList source) { + internalList.addAll(source.internalList); + Collections.sort(internalList); + return this; + } + /** * Returns an unmodifiable java List view with elements cast as immutable {@link ReadOnlyPerson}s. * For use with other methods/libraries. From bd5466ecd4921834c68948c095e672ce832d458f Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 02:48:23 +0800 Subject: [PATCH 2/7] edit user guide --- docs/UserGuide.adoc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..f9022306a 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -72,6 +72,11 @@ Examples: Shows a list of all persons, along with their non-private details, in the address book. + Format: `list` +== Listing all persons in ascending alphabetical order : `sortedlist` + +Shows an alphabetically sorted list of all persons, along with their non-private details, in the address book. + +Format: `list` + == Finding all persons containing any keyword in their name: `find` Finds persons whose names contain any of the given keywords. + @@ -160,7 +165,7 @@ Format: `clear` Exits the program. + Format: `exit` - +l == Saving the data Address book data are saved in the hard disk automatically after any command that changes the data. From f18a64b74c2739883815f6e45e7956965fee77a0 Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 03:06:34 +0800 Subject: [PATCH 3/7] edit code to prevent compilation error --- src/seedu/addressbook/data/person/UniquePersonList.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index d373e9d53..972f4f6f7 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -75,8 +75,7 @@ public UniquePersonList(UniquePersonList source) { /** * Returns a shallow sorted copy of the list. */ - public UniquePersonList sort(UniquePersonList source) { - internalList.addAll(source.internalList); + public UniquePersonList sort() { Collections.sort(internalList); return this; } From b3d7b0790fc08b47691ab3833ec7aea9a11ebc02 Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 03:27:21 +0800 Subject: [PATCH 4/7] edit parse.java --- src/seedu/addressbook/parser/Parser.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..a175a961b 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -22,6 +22,7 @@ import seedu.addressbook.commands.ListCommand; import seedu.addressbook.commands.ViewAllCommand; import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.SortedListCommand; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -88,6 +89,9 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); + case SortedListCommand.COMMAND_WORD; + return new SortedListCommand(); + case ViewCommand.COMMAND_WORD: return prepareView(arguments); From 880c727fefe3e7bf3f706e1c2857b50b51ff7445 Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 03:36:40 +0800 Subject: [PATCH 5/7] no message --- src/seedu/addressbook/parser/Parser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index a175a961b..a3e4dbfac 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -20,9 +20,9 @@ import seedu.addressbook.commands.HelpCommand; import seedu.addressbook.commands.IncorrectCommand; import seedu.addressbook.commands.ListCommand; +import seedu.addressbook.commands.SortedListCommand; import seedu.addressbook.commands.ViewAllCommand; import seedu.addressbook.commands.ViewCommand; -import seedu.addressbook.commands.SortedListCommand; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -89,7 +89,7 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); - case SortedListCommand.COMMAND_WORD; + case SortedListCommand.COMMAND_WORD: return new SortedListCommand(); case ViewCommand.COMMAND_WORD: From 5c5789d3221ca93e4014e48829b5651ff27ae201 Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 13:41:26 +0800 Subject: [PATCH 6/7] no message --- src/seedu/addressbook/data/AddressBook.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 5364c47a5..824616982 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -72,7 +72,8 @@ public UniquePersonList getAllPersons() { * Returns a new UniquePersonList of all persons in the address book at the time of the call sorted in alphabetical order. */ public UniquePersonList getSortedAllPersons() { - return new UniquePersonList(allPersons).sort(); + UniquePersonList toSort = new UniquePersonList(allPersons); + return toSort.sort(); } @Override From b9f3d3f986a998652789422f2fbac9cbed3026fd Mon Sep 17 00:00:00 2001 From: Ng Yuan Yun Nigel Date: Thu, 13 Sep 2018 14:34:52 +0800 Subject: [PATCH 7/7] no message --- src/seedu/addressbook/commands/SortedListCommand.java | 2 +- test/expected.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/seedu/addressbook/commands/SortedListCommand.java b/src/seedu/addressbook/commands/SortedListCommand.java index 1896b4d64..03deccdc9 100644 --- a/src/seedu/addressbook/commands/SortedListCommand.java +++ b/src/seedu/addressbook/commands/SortedListCommand.java @@ -13,7 +13,7 @@ public class SortedListCommand extends Command { public static final String COMMAND_WORD = "sortedlist"; public static final String MESSAGE_USAGE = COMMAND_WORD - + ": Displays all persons in the address book as a list in ascending alphabetical order.\n" + + ": Displays all persons in the address book as a list in ascending alphabetical order with index numbers.\n" + "Example: " + COMMAND_WORD; @Override diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..412165405 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -19,6 +19,8 @@ || Example: find alice bob charlie || list: Displays all persons in the address book as a list with index numbers. || Example: list +|| sortedlist: Displays all persons in the address book as a list in ascending alphabetical order with index numbers. +|| Example: sortedlist || view: Views the non-private details of the person identified by the index number in the last shown person listing. || Parameters: INDEX || Example: view 1