From 0965196c8a299ef42d3d763d448e9634e64e13cf Mon Sep 17 00:00:00 2001 From: cheahzk Date: Sat, 9 Feb 2019 16:43:33 +0800 Subject: [PATCH 1/5] Split Address class into multiple classes --- src/seedu/addressbook/data/person/Address.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 131d71b46..81fe986dc 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -14,6 +14,10 @@ public class Address { public final String value; private boolean isPrivate; + private String block; + private String street; + private String unit; + private String postalCode; /** * Validates given address. @@ -27,6 +31,14 @@ public Address(String address, boolean isPrivate) throws IllegalValueException { throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); } this.value = trimmedAddress; + this.unit = this.postalCode = null; + String[] arrSplit = value.split(", "); + for (int i = 0; i < arrSplit.length; i++){ + if (i == 0) this.block = arrSplit[i]; + if (i == 1) this.street = arrSplit[i]; + if (i == 2) this.unit = arrSplit[i]; + if (i == 3) this.postalCode = arrSplit[i]; + } } /** From 69aaace315494539e4fb4ab3c91b589df3db5859 Mon Sep 17 00:00:00 2001 From: cheahzk Date: Sun, 10 Feb 2019 16:30:23 +0800 Subject: [PATCH 2/5] Add Sort Command: Working Build --- .../addressbook/commands/SortCommand.java | 25 +++++++++++++++++++ src/seedu/addressbook/data/AddressBook.java | 5 ++++ .../addressbook/data/person/Address.java | 13 +--------- src/seedu/addressbook/data/person/Name.java | 5 ++++ src/seedu/addressbook/data/person/Person.java | 10 ++++++++ .../data/person/UniquePersonList.java | 7 ++++++ 6 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 src/seedu/addressbook/commands/SortCommand.java diff --git a/src/seedu/addressbook/commands/SortCommand.java b/src/seedu/addressbook/commands/SortCommand.java new file mode 100644 index 000000000..cda1678ac --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.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. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Sorts and display all persons in the address book as a list with index numbers.\n" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + List allPersons = addressBook.sortAllPersons().immutableListView(); + return new CommandResult(getMessageForSortedPersonListShownSummary(allPersons), allPersons); + } +} diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..e5854ad84 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -68,6 +68,11 @@ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); } + public UniquePersonList sortAllPersons() { + allPersons.sort(); + return new UniquePersonList(allPersons); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 81fe986dc..893cc1772 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -14,10 +14,6 @@ public class Address { public final String value; private boolean isPrivate; - private String block; - private String street; - private String unit; - private String postalCode; /** * Validates given address. @@ -31,14 +27,7 @@ public Address(String address, boolean isPrivate) throws IllegalValueException { throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); } this.value = trimmedAddress; - this.unit = this.postalCode = null; - String[] arrSplit = value.split(", "); - for (int i = 0; i < arrSplit.length; i++){ - if (i == 0) this.block = arrSplit[i]; - if (i == 1) this.street = arrSplit[i]; - if (i == 2) this.unit = arrSplit[i]; - if (i == 3) this.postalCode = arrSplit[i]; - } + } /** diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index d1d271414..c3636c122 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -43,6 +43,11 @@ public List getWordsInName() { return Arrays.asList(fullName.split("\\s+")); } + public int compareTo(Name target) { + int compare = this.fullName.compareTo(target.fullName); + return compare; + } + @Override public String toString() { return fullName; diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 64551c7fe..fe5e00e61 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -1,5 +1,6 @@ package seedu.addressbook.data.person; +import java.util.Comparator; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -62,6 +63,15 @@ public Set getTags() { return new HashSet<>(tags); } + /** + * Compare selected person's name with target's name + */ + public static Comparator compareByName = new Comparator() { + public int compare(Person selected, Person target) { + return selected.name.compareTo(target.name); + } + }; + /** * Replaces this person's tags with the tags in the argument tag set. */ diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index d7acd8b4a..19c3242ba 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -129,6 +129,13 @@ public void clear() { internalList.clear(); } + /** + * Sorts the current list in alphabetical order of names + */ + public void sort() { + Collections.sort(internalList, Person.compareByName); + } + @Override public Iterator iterator() { return internalList.iterator(); From fde754bb4100981683c720122f6439016ab0384a Mon Sep 17 00:00:00 2001 From: cheahzk Date: Sun, 10 Feb 2019 16:32:12 +0800 Subject: [PATCH 3/5] Add Sort Command: Updated messages --- src/seedu/addressbook/commands/Command.java | 4 ++++ src/seedu/addressbook/commands/HelpCommand.java | 1 + src/seedu/addressbook/common/Messages.java | 1 + 3 files changed, 6 insertions(+) diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index 2ff8f1575..f5bee9587 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -36,6 +36,10 @@ public static String getMessageForPersonListShownSummary(List personsDisplayed) { + return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, personsDisplayed.size()); + } + /** * Executes the command and returns the result. */ diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..d20a0125c 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -19,6 +19,7 @@ public CommandResult execute() { + "\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/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 9e30d0f28..b23c6da0f 100644 --- a/src/seedu/addressbook/common/Messages.java +++ b/src/seedu/addressbook/common/Messages.java @@ -11,6 +11,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!"; From 786161f98ba51fec1f4d19d9c9dc40d9cdd85e47 Mon Sep 17 00:00:00 2001 From: cheahzk Date: Sun, 10 Feb 2019 16:33:47 +0800 Subject: [PATCH 4/5] Add Sort Command: Able to detect correct sort command --- src/seedu/addressbook/parser/Parser.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..7c1b0cc39 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -11,17 +11,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -76,6 +66,12 @@ public Command parseCommand(String userInput) { case AddCommand.COMMAND_WORD: return prepareAdd(arguments); + case SortCommand.COMMAND_WORD: + if (arguments.isEmpty()) { + return new SortCommand(); + } + return new HelpCommand(); + case DeleteCommand.COMMAND_WORD: return prepareDelete(arguments); From 597bb642f9fcf3a6a12128e1ede524573e92a58d Mon Sep 17 00:00:00 2001 From: cheahzk Date: Sun, 10 Feb 2019 16:34:33 +0800 Subject: [PATCH 5/5] Update User Guide; Update IO Test for Sort Command --- .travis.yml | 2 +- docs/UserGuide.adoc | 5 +++ .../META-INF/AddressBook-Level2.kotlin_module | Bin 0 -> 16 bytes test/expected.txt | 38 ++++++++++++++++++ test/input.txt | 4 +- 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module diff --git a/.travis.yml b/.travis.yml index d075c8a77..e71c51f4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ deploy: provider: script script: ./config/travis/deploy_github_pages.sh on: - branch: master + all_branches: true addons: apt: diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..f122517b6 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` +== Sorting all persons by name : `sort` + +Sorts and displays a list of all persons, based on their names alphabetically, along with their non-private details, in the address book. + +Format: `sort` + == Finding all persons containing any keyword in their name: `find` Finds persons whose names contain any of the given keywords. + diff --git a/out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module b/out/production/AddressBook-Level2/META-INF/AddressBook-Level2.kotlin_module new file mode 100644 index 0000000000000000000000000000000000000000..8fb60192d378759239a3ecbf60eac8c8de446e9c GIT binary patch literal 16 RcmZQzU|?ooU|@t|UH|}6022TJ literal 0 HcmV?d00001 diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..b3a5292bd 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 +|| sort: Sorts and display all persons in the address book as a list with index numbers. +|| Example: sort || 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 @@ -73,6 +75,33 @@ || Enter command: || [Command entered: add Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag] || Tags names should be alphanumeric || =================================================== +|| Enter command: || [Command entered: sort this] +|| add: Adds a person to the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: add John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| delete: Deletes the person identified by the index number used in the last person listing. +|| Parameters: INDEX +|| Example: delete 1 +|| Clears address book permanently. +|| Example: clear +|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers. +|| Parameters: KEYWORD [MORE_KEYWORDS]... +|| Example: find alice bob charlie +|| list: Displays all persons in the address book as a list with index numbers. +|| Example: list +|| sort: Sorts and display all persons in the address book as a list with index numbers. +|| Example: sort +|| 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 +|| viewall: Views the non-private details of the person identified by the index number in the last shown person listing. +|| Parameters: INDEX +|| Example: viewall 1 +|| help: Shows program usage instructions. +|| Example: help +|| exit: Exits the program. +|| Example: exit +|| =================================================== || Enter command: || [Command entered: add []\[;] p/12345 e/valid@e.mail a/valid, address] || Person names should be spaces or alphabetic characters || =================================================== @@ -135,6 +164,15 @@ || || 5 persons listed! || =================================================== +|| Enter command: || [Command entered: sort] +|| 1. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons sorted! +|| =================================================== || Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] || This person already exists in the address book || =================================================== diff --git a/test/input.txt b/test/input.txt index eb8df81f8..7fa1a6e98 100644 --- a/test/input.txt +++ b/test/input.txt @@ -23,7 +23,7 @@ list ########################################################## -# test add person command, setup state for further tests +# test add/sort person command, setup state for further tests ########################################################## # should catch invalid args format @@ -32,6 +32,7 @@ add Valid Name p/12345 valid@email.butNoPrefix a/valid, address add Valid Name p/12345 e/valid@email.butNoAddressPrefix valid, address add Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag + sort this # should catch invalid person data add []\[;] p/12345 e/valid@e.mail a/valid, address @@ -50,6 +51,7 @@ list add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy list + sort # should not allow adding duplicate persons add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy