From 419186618eec3c1628f793a3ea187bb5a6021d4f Mon Sep 17 00:00:00 2001 From: "DESKTOP-BFINHE4\\Gong changda" Date: Tue, 12 Feb 2019 21:06:10 +0800 Subject: [PATCH 1/4] Add a command to sort the address book. --- docs/UserGuide.adoc | 5 +++ .../addressbook/commands/HelpCommand.java | 1 + .../addressbook/commands/SortCommand.java | 16 ++++++++ src/seedu/addressbook/data/AddressBook.java | 4 ++ src/seedu/addressbook/data/person/Person.java | 5 ++- .../data/person/UniquePersonList.java | 3 ++ src/seedu/addressbook/parser/Parser.java | 4 ++ test/expected.txt | 38 +++++++++++++++++++ test/input.txt | 16 ++++++++ .../addressbook/commands/SortCommandTest.java | 7 ++++ 10 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/seedu/addressbook/commands/SortCommand.java create mode 100644 test/java/seedu/addressbook/commands/SortCommandTest.java diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..86c94670e 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -151,6 +151,11 @@ Views all details of the 2nd person in the address book. `viewall 1` + Views all details of the 1st person in the results of the `find` command. +== Sort all persons: `sort` + +Sorts all persons according to their names in ascending order. + +Format: `sort` + == Clearing all entries : `clear` Clears all entries from the address book. + diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..3a7992589 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" + SortCommand.MESSAGE_USAGE ); } } diff --git a/src/seedu/addressbook/commands/SortCommand.java b/src/seedu/addressbook/commands/SortCommand.java new file mode 100644 index 000000000..70fe6547e --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,16 @@ +package seedu.addressbook.commands; + +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + " : Sorts all the persons in the address book according to their names in alphabetical order.\n" + + "Example: " + COMMAND_WORD; + public static final String MESSAGE_SUCCESS = "Address book sorted!"; + + @Override + public CommandResult execute() { + addressBook.sort(); + return new CommandResult(MESSAGE_SUCCESS); + } +} diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..bc792a27b 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -68,10 +68,14 @@ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); } + @Override public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof AddressBook // instanceof handles nulls && this.allPersons.equals(((AddressBook) other).allPersons)); } + public void sort(){ + allPersons.sort(); + } } diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 64551c7fe..35e64fc40 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,7 @@ public String toString() { return getAsTextShowAll(); } + public int compareTo(Person another){ + return this.name.fullName.compareTo(another.name.fullName); + } } diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index d7acd8b4a..a2c7365b8 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -140,4 +140,7 @@ public boolean equals(Object other) { || (other instanceof UniquePersonList // instanceof handles nulls && this.internalList.equals(((UniquePersonList) other).internalList)); } + public void sort(){ + Collections.sort(internalList); + } } diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..691833710 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.SortCommand; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -88,6 +89,9 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); + case SortCommand.COMMAND_WORD: + return new SortCommand(); + case ViewCommand.COMMAND_WORD: return prepareView(arguments); diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..600b8a1fd 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -29,6 +29,8 @@ || Example: help || exit: Exits the program. || Example: exit +|| sort : Sorts all the persons in the address book according to their names in alphabetical order. +|| Example: sort || =================================================== || Enter command: || [Command entered: delete 1] || The person index provided is invalid @@ -297,6 +299,42 @@ || || 0 persons listed! || =================================================== +|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school] +|| New person added: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| =================================================== +|| Enter command: || [Command entered: add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street] +|| New person added: Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| =================================================== +|| Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] +|| New person added: Esther Potato Phone: 555555 Email: esther@not.a.real.potato Address: (private) 555, epsilon street Tags: [tubers][starchy] +|| =================================================== +|| Enter command: || [Command entered: add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive] +|| New person added: Betsy Choo Phone: (private) 222222 Email: (private) benchoo@nus.edu.sg Address: (private) 222, beta street Tags: [secretive] +|| =================================================== +|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends] +|| New person added: Dickson Ee Phone: 444444 Email: (private) dickson@nus.edu.sg Address: 444, delta street Tags: [friends] +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 2. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| 3. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| 4. Betsy Choo Tags: [secretive] +|| 5. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: sort] +|| Address book sorted! +|| =================================================== +|| Enter command: || [Command entered: list] +|| 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 listed! +|| =================================================== || Enter command: || [Command entered: exit] || Exiting Address Book as requested ... || =================================================== diff --git a/test/input.txt b/test/input.txt index eb8df81f8..6918fb6cf 100644 --- a/test/input.txt +++ b/test/input.txt @@ -149,6 +149,22 @@ list ########################################################## +# test sort command +########################################################## + # first we add some persons to the address book. + add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school + add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street + add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy + add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive + add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends + list + + # Now we sort the address book + sort + + # After that we examine what we get + list +########################################################## # test exit command ########################################################## diff --git a/test/java/seedu/addressbook/commands/SortCommandTest.java b/test/java/seedu/addressbook/commands/SortCommandTest.java new file mode 100644 index 000000000..42c66dc94 --- /dev/null +++ b/test/java/seedu/addressbook/commands/SortCommandTest.java @@ -0,0 +1,7 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.*; + +public class SortCommandTest { + +} \ No newline at end of file From b4d296796cfce608ea5683a0a16e6ff17864068d Mon Sep 17 00:00:00 2001 From: "DESKTOP-BFINHE4\\Gong changda" Date: Tue, 12 Feb 2019 21:53:06 +0800 Subject: [PATCH 2/4] Modify the input.txt to make the format correct and also add one JUnit test --- test/input.txt | 12 +++--- .../addressbook/commands/SortCommandTest.java | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/test/input.txt b/test/input.txt index 6918fb6cf..e3490a42d 100644 --- a/test/input.txt +++ b/test/input.txt @@ -152,12 +152,12 @@ # test sort command ########################################################## # first we add some persons to the address book. - add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school - add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street - add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy - add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive - add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends - list + add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school + add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street + add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy + add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive + add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends + list # Now we sort the address book sort diff --git a/test/java/seedu/addressbook/commands/SortCommandTest.java b/test/java/seedu/addressbook/commands/SortCommandTest.java index 42c66dc94..98b8073f2 100644 --- a/test/java/seedu/addressbook/commands/SortCommandTest.java +++ b/test/java/seedu/addressbook/commands/SortCommandTest.java @@ -1,7 +1,49 @@ package seedu.addressbook.commands; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.Address; +import seedu.addressbook.data.person.Email; +import seedu.addressbook.data.person.Name; +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList; +import seedu.addressbook.util.TestUtil; import static org.junit.Assert.*; public class SortCommandTest { + private AddressBook addressBook; + private String testResult; + + @Test + public void sortTest() throws Exception{ + Person johnDoe = new Person(new Name("John Doe"), new Phone("61234567", false), + new Email("john@doe.com", false), new Address("395C Ben Road", false), Collections.emptySet()); + Person janeDoe = new Person(new Name("Jane Doe"), new Phone("91234567", false), + new Email("jane@doe.com", false), new Address("33G Ohm Road", false), Collections.emptySet()); + Person samDoe = new Person(new Name("Sam Doe"), new Phone("63345566", false), + new Email("sam@doe.com", false), new Address("55G Abc Road", false), Collections.emptySet()); + Person davidGrant = new Person(new Name("David Grant"), new Phone("61121122", false), + new Email("david@grant.com", false), new Address("44H Define Road", false), + Collections.emptySet()); + addressBook = TestUtil.createAddressBook(johnDoe, janeDoe, davidGrant, samDoe); + addressBook.sort(); + testResult = addressBook.getAllPersons().immutableListView().get(0).getName().fullName; + assertTrue(testResult.equals("David Grant")); + } } \ No newline at end of file From dd9e653832a6442e10e2492bc7c0bc36ee7aaa92 Mon Sep 17 00:00:00 2001 From: "DESKTOP-BFINHE4\\Gong changda" Date: Tue, 12 Feb 2019 21:56:08 +0800 Subject: [PATCH 3/4] Modify expected.txt to make it correct --- test/expected.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/expected.txt b/test/expected.txt index 600b8a1fd..57a146b99 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -299,22 +299,22 @@ || || 0 persons listed! || =================================================== -|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school] +|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school] || New person added: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] || =================================================== -|| Enter command: || [Command entered: add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street] +|| Enter command: || [Command entered: add Adam Brown p/111111 e/adam@gmail.com a/111, alpha street] || New person added: Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: || =================================================== -|| Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] +|| Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] || New person added: Esther Potato Phone: 555555 Email: esther@not.a.real.potato Address: (private) 555, epsilon street Tags: [tubers][starchy] || =================================================== -|| Enter command: || [Command entered: add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive] +|| Enter command: || [Command entered: add Betsy Choo pp/222222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive] || New person added: Betsy Choo Phone: (private) 222222 Email: (private) benchoo@nus.edu.sg Address: (private) 222, beta street Tags: [secretive] || =================================================== -|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends] +|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends] || New person added: Dickson Ee Phone: 444444 Email: (private) dickson@nus.edu.sg Address: 444, delta street Tags: [friends] || =================================================== -|| Enter command: || [Command entered: list] +|| Enter command: || [Command entered: list] || 1. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] || 2. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: || 3. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] From 007276accd8a01639d925c35309ff8501106ef84 Mon Sep 17 00:00:00 2001 From: "DESKTOP-BFINHE4\\Gong changda" Date: Tue, 12 Feb 2019 21:58:24 +0800 Subject: [PATCH 4/4] Delete some unnecessary imports --- .../addressbook/commands/SortCommandTest.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/test/java/seedu/addressbook/commands/SortCommandTest.java b/test/java/seedu/addressbook/commands/SortCommandTest.java index 98b8073f2..772c0b38a 100644 --- a/test/java/seedu/addressbook/commands/SortCommandTest.java +++ b/test/java/seedu/addressbook/commands/SortCommandTest.java @@ -1,29 +1,17 @@ package seedu.addressbook.commands; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import java.util.Arrays; +import static org.junit.Assert.assertTrue; import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - import org.junit.Test; - import seedu.addressbook.data.AddressBook; -import seedu.addressbook.data.exception.IllegalValueException; import seedu.addressbook.data.person.Address; import seedu.addressbook.data.person.Email; import seedu.addressbook.data.person.Name; import seedu.addressbook.data.person.Person; import seedu.addressbook.data.person.Phone; -import seedu.addressbook.data.person.ReadOnlyPerson; -import seedu.addressbook.data.person.UniquePersonList; import seedu.addressbook.util.TestUtil; -import static org.junit.Assert.*; + public class SortCommandTest { private AddressBook addressBook;