-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[W5.7][F11-2]Chew Yong Soon #457
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.common.Messages; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
|
||
import java.security.InvalidParameterException; | ||
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 all persons in the address book according to the specifier (name, phone, email, address) and " + | ||
"display in a sorted list.\n" | ||
+ "Example: " + COMMAND_WORD + " address"; | ||
|
||
private final String keyword; | ||
|
||
public SortCommand(String keyword) { | ||
this.keyword = keyword.trim(); | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
try { | ||
addressBook.sort((ReadOnlyPerson p1, ReadOnlyPerson p2) -> { | ||
switch (keyword) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the coding standard for switch statements |
||
case "name": return p1.getName().toString().compareTo(p2.getName().toString()); | ||
case "phone": return p1.getPhone().toString().compareTo(p2.getPhone().toString()); | ||
case "email": return p1.getEmail().toString().compareTo(p2.getEmail().toString()); | ||
case "address": return p1.getAddress().toString().compareTo(p2.getAddress().toString()); | ||
default: | ||
throw new InvalidParameterException(); | ||
} | ||
}); | ||
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView(); | ||
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons); | ||
} catch (InvalidParameterException e) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_SORT); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ public class Utils { | |
* Returns true if any of the given items are null. | ||
*/ | ||
public static boolean isAnyNull(Object... items) { | ||
if (items == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated code change? |
||
return true; | ||
} | ||
for (Object item : items) { | ||
if (item == null) { | ||
return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we allowed to do this? If not I think you may need to tweak the behaviour of Alt-Enter in Intellij if you wish to use the shortcut. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use wild cards, show each import explicitly. Check coding convention. |
||
import seedu.addressbook.data.exception.IllegalValueException; | ||
|
||
/** | ||
|
@@ -96,6 +86,9 @@ public Command parseCommand(String userInput) { | |
|
||
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
|
||
case SortCommand.COMMAND_WORD: | ||
return new SortCommand(arguments); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -297,6 +297,73 @@ | |
|| | ||
|| 0 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Adam Brown p/111111 e/[email protected] a/111, alpha street] | ||
|| New person added: Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Betsy Choo pp/222222 pe/[email protected] pa/222, beta street t/secretive] | ||
|| New person added: Betsy Choo Phone: (private) 222222 Email: (private) [email protected] Address: (private) 222, beta street Tags: [secretive] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/[email protected] a/333, gamma street t/friends t/school] | ||
|| New person added: Charlie Dickson Phone: (private) 333333 Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/[email protected] a/444, delta street t/friends] | ||
|| New person added: Dickson Ee Phone: 444444 Email: (private) [email protected] Address: 444, delta street Tags: [friends] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Esther Potato p/155555 e/[email protected] pa/355, epsilon street t/tubers t/starchy] | ||
|| New person added: Esther Potato Phone: 155555 Email: [email protected] Address: (private) 355, epsilon street Tags: [tubers][starchy] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Betsy Choo Tags: [secretive] | ||
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 5. Esther Potato Phone: 155555 Email: [email protected] Tags: [tubers][starchy] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort phone] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Esther Potato Phone: 155555 Email: [email protected] Tags: [tubers][starchy] | ||
|| 3. Betsy Choo Tags: [secretive] | ||
|| 4. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 5. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort email] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Esther Potato Phone: 155555 Email: [email protected] Tags: [tubers][starchy] | ||
|| 3. Betsy Choo Tags: [secretive] | ||
|| 4. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 5. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort address] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Betsy Choo Tags: [secretive] | ||
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 4. Esther Potato Phone: 155555 Email: [email protected] Tags: [tubers][starchy] | ||
|| 5. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: sort name] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Betsy Choo Tags: [secretive] | ||
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 5. Esther Potato Phone: 155555 Email: [email protected] Tags: [tubers][starchy] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: clear] | ||
|| Address book has been cleared! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| | ||
|| 0 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: exit] | ||
|| Exiting Address Book as requested ... | ||
|| =================================================== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,34 @@ | |
# clears all | ||
clear | ||
list | ||
|
||
########################################################## | ||
# test sort command | ||
########################################################## | ||
|
||
# add some users first | ||
add Adam Brown p/111111 e/[email protected] a/111, alpha street | ||
add Betsy Choo pp/222222 pe/[email protected] pa/222, beta street t/secretive | ||
add Charlie Dickson pp/333333 e/[email protected] a/333, gamma street t/friends t/school | ||
add Dickson Ee p/444444 pe/[email protected] a/444, delta street t/friends | ||
add Esther Potato p/155555 e/[email protected] pa/355, epsilon street t/tubers t/starchy | ||
list | ||
|
||
# test sorting by phone | ||
sort phone | ||
|
||
# test sorting by email | ||
sort email | ||
|
||
# test sorting by address | ||
sort address | ||
|
||
# test sorting by name | ||
sort name | ||
|
||
# clears all | ||
clear | ||
list | ||
|
||
########################################################## | ||
# test exit command | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
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.common.Messages; | ||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.util.TypicalPersons; | ||
|
||
public class SortCommandTest { | ||
|
||
private final AddressBook addressBook = new TypicalPersons().getTypicalAddressBook(); | ||
private final TypicalPersons td = new TypicalPersons(); | ||
|
||
@Test | ||
public void execute() throws IllegalValueException { | ||
//sort by valid keyword | ||
assertSortCommandBehavior("phone", Arrays.asList( td.dan, td.amy, td.bill, td.candy)); | ||
assertSortCommandBehavior("address", Arrays.asList(td.amy, td.dan, td.bill, td.candy)); | ||
|
||
//sort by invalid keyword | ||
assertSortCommandBehavior("emale", Messages.MESSAGE_INVALID_SORT); | ||
assertSortCommandBehavior("Name", Messages.MESSAGE_INVALID_SORT); | ||
} | ||
|
||
/** | ||
* Executes the sort command for the given keywords and verifies | ||
* the result matches the persons in the expectedPersonList exactly. | ||
*/ | ||
private void assertSortCommandBehavior(String keyword, List<ReadOnlyPerson> expectedPersonList) { | ||
SortCommand command = createSortCommand(keyword); | ||
CommandResult result = command.execute(); | ||
|
||
assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser); | ||
} | ||
|
||
private void assertSortCommandBehavior(String keyword, String errorMessage) { | ||
SortCommand command = createSortCommand(keyword); | ||
CommandResult result = command.execute(); | ||
|
||
assertEquals(errorMessage, result.feedbackToUser); | ||
} | ||
|
||
private SortCommand createSortCommand(String keyword) { | ||
SortCommand command = new SortCommand(keyword); | ||
command.setData(addressBook, Collections.emptyList()); | ||
return command; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,15 @@ public void elementsAreUnique() throws Exception { | |
assertNotUnique(null, 1, Integer.valueOf(1)); | ||
assertNotUnique(null, null); | ||
assertNotUnique(null, "a", "b", null); | ||
|
||
// some empty objects | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated code changes? Please refrain for adding unrelated code changes to your future PRs |
||
assertAnyNull(1, 2, null); | ||
assertAnyNull(null); | ||
|
||
// non-empty objects | ||
assertNoneNull(1, 2, 3); | ||
assertNoneNull("a", "b", "c"); | ||
|
||
} | ||
|
||
private void assertAreUnique(Object... objects) { | ||
|
@@ -43,4 +52,12 @@ private void assertAreUnique(Object... objects) { | |
private void assertNotUnique(Object... objects) { | ||
assertFalse(Utils.elementsAreUnique(Arrays.asList(objects))); | ||
} | ||
|
||
private void assertAnyNull(Object... objects) { | ||
assertTrue(Utils.isAnyNull(objects)); | ||
} | ||
|
||
private void assertNoneNull(Object... objects) { | ||
assertFalse(Utils.isAnyNull(objects)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
*/ | ||
public class TypicalPersons { | ||
|
||
public Person amy, bill, candy, dan; | ||
public Person amy, bill, candy, dan, emily; | ||
|
||
public TypicalPersons() { | ||
try { | ||
|
@@ -27,7 +27,7 @@ public TypicalPersons() { | |
candy = new Person(new Name("Candy Destiny"), new Phone("93339333", true), | ||
new Email("[email protected]", false), new Address("3 Clementi Road", true), Collections.emptySet()); | ||
dan = new Person(new Name("Dan Smith"), new Phone("1234556", true), new Email("[email protected]", true), | ||
new Address("NUS", true), Collections.singleton(new Tag("test"))); | ||
new Address("2 Ang Mo Kio", true), Collections.singleton(new Tag("test"))); | ||
} catch (IllegalValueException e) { | ||
e.printStackTrace(); | ||
assert false : "not possible"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to change documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update your comments