Skip to content
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][W17-1] Lee Wei Kang #144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 sorted by their names : `sort`

Shows a list of all persons sorted by their names, along with their non-private details, in the address book. +
Format: `list`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think perhaps the format is wrong? For a sort command, the format should be: 'sort'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thanks for pointing that out!


== Finding all persons containing any keyword in their name: `find`

Finds persons whose names contain any of the given keywords. +
Expand Down
23 changes: 23 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortCommand extends Command{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do look out for the missing whitespace between Command and {, and the javadoc comment for the class.

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Displays all persons in the address book as a list with index numbers sorted by their name.\n"
+ "Example: " + COMMAND_WORD;

@Override
public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
List<ReadOnlyPerson> newList = new ArrayList<>(allPersons);
Collections.sort(newList);
return new CommandResult(getMessageForPersonListShownSummary(newList), newList);
}
}
7 changes: 6 additions & 1 deletion src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import seedu.addressbook.data.exception.IllegalValueException;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {
public class Name implements Comparable<Name> {

public static final String EXAMPLE = "John Doe";
public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphabetic characters";
Expand Down Expand Up @@ -60,4 +61,8 @@ public int hashCode() {
return fullName.hashCode();
}

@Override
public int compareTo(Name o) {
return this.fullName.compareTo(o.fullName);
}
}
4 changes: 4 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ public String toString() {
return getAsTextShowAll();
}

@Override
public int compareTo(ReadOnlyPerson o) {
return this.getName().compareTo(o.getName());
}
}
3 changes: 2 additions & 1 deletion src/seedu/addressbook/data/person/ReadOnlyPerson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.addressbook.data.person;

import java.util.Comparator;
import java.util.Set;

import seedu.addressbook.data.tag.Tag;
Expand All @@ -8,7 +9,7 @@
* A read-only immutable interface for a Person in the addressbook.
* Implementations should guarantee: details are present and not null, field values are validated.
*/
public interface ReadOnlyPerson {
public interface ReadOnlyPerson extends Comparable<ReadOnlyPerson> {

Name getName();
Phone getPhone();
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.SortCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.data.exception.IllegalValueException;
Expand Down Expand Up @@ -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);

Expand Down
44 changes: 31 additions & 13 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,45 @@
||
|| 3 persons listed!
|| ===================================================
|| 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/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy]
|| New person added: Esther Potato Phone: 555555 Email: [email protected] Address: (private) 555, 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]
|| 4. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
||
|| 4 persons listed!
|| ===================================================
|| Enter command: || [Command entered: add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy]
|| New person added: Esther Potato Phone: 555555 Email: [email protected] Address: (private) 555, epsilon street Tags: [tubers][starchy]
|| 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: 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. Esther Potato Phone: 555555 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]
|| 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: 555555 Email: [email protected] Tags: [tubers][starchy]
||
||
|| 5 persons listed!
|| ===================================================
|| 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. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
|| 5. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
||
|| 5 persons listed!
|| ===================================================
|| Enter command: || [Command entered: add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy]
Expand Down Expand Up @@ -183,19 +201,19 @@
|| Enter command: || [Command entered: view 3]
|| Viewing person: Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| ===================================================
|| Enter command: || [Command entered: view 4]
|| Enter command: || [Command entered: view 5]
|| Viewing person: Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| ===================================================
|| Enter command: || [Command entered: view 5]
|| Enter command: || [Command entered: view 4]
|| Viewing person: Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
|| ===================================================
|| Enter command: || [Command entered: viewall 3]
|| Viewing person: Charlie Dickson Phone: (private) 333333 Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| ===================================================
|| Enter command: || [Command entered: viewall 4]
|| Enter command: || [Command entered: viewall 5]
|| Viewing person: Dickson Ee Phone: 444444 Email: (private) [email protected] Address: 444, delta street Tags: [friends]
|| ===================================================
|| Enter command: || [Command entered: viewall 5]
|| Enter command: || [Command entered: viewall 4]
|| Viewing person: Esther Potato Phone: 555555 Email: [email protected] Address: (private) 555, epsilon street Tags: [tubers][starchy]
|| ===================================================
|| Enter command: || [Command entered: find]
Expand Down Expand Up @@ -266,12 +284,12 @@
|| Enter command: || [Command entered: list]
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags:
|| 2. Betsy Choo Tags: [secretive]
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 4. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
|| 3. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
||
|| 4 persons listed!
|| ===================================================
|| Enter command: || [Command entered: delete 4]
|| Enter command: || [Command entered: delete 3]
|| Deleted Person: Esther Potato Phone: 555555 Email: [email protected] Address: (private) 555, epsilon street Tags: [tubers][starchy]
|| ===================================================
|| Enter command: || [Command entered: list]
Expand Down
10 changes: 6 additions & 4 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
list
add Charlie Dickson pp/333333 e/[email protected] a/333, gamma street t/friends t/school
list
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy
list
add Dickson Ee p/444444 pe/[email protected] a/444, delta street t/friends
list
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy
sort
list

# should not allow adding duplicate persons
Expand Down Expand Up @@ -78,13 +80,13 @@

# should show only non private details for view
view 3
view 4
view 5
view 4

# should show all details even private for viewall
viewall 3
viewall 4
viewall 5
viewall 4

##########################################################
# test find persons command
Expand Down Expand Up @@ -133,7 +135,7 @@
list

# deletes correct person
delete 4
delete 3
list

# listing indexes get updated on next request
Expand Down
50 changes: 50 additions & 0 deletions test/java/seedu/addressbook/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.addressbook.commands;

import org.junit.Test;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.util.TestUtil;
import seedu.addressbook.util.TypicalPersons;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SortCommandTest {
private TypicalPersons td = new TypicalPersons();

private AddressBook typicalAddressBook = td.getTypicalAddressBook();
private List<ReadOnlyPerson> listWithAllTypicalPersons = Arrays.asList(td.getTypicalPersons());
private List<ReadOnlyPerson> listWithInorderedTypicalPersons = Arrays.asList(td.amy, td.candy, td.bill, td.dan);

@Test
public void execute_invalidIndex_returnsInvalidIndexMessage() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this method seems a little unclear - are you testing for an invalid index?


SortCommand sortCommand = new SortCommand();
sortCommand.setData(typicalAddressBook, listWithAllTypicalPersons);
String expectedMessage = sortCommand.execute().feedbackToUser;

assertSortBehavior(sortCommand, typicalAddressBook, listWithAllTypicalPersons,
expectedMessage);
}

/**
* Executes the sort command for the given addressbook data.
* Checks that SortCommand exhibits the correct command behavior, namely:
* 1. Returns a sorted list of person in the address book
*/
private static void assertSortBehavior(Command sortCommand, AddressBook addressBook,
List<ReadOnlyPerson> relevantPersons, String expectedMessage) {
AddressBook expectedAddressBook = TestUtil.clone(addressBook);

sortCommand.setData(addressBook, relevantPersons);
CommandResult result = sortCommand.execute();

// feedback message is as expected and there are no relevant persons returned.
assertEquals(expectedMessage, result.feedbackToUser);

// addressbook was not modified.
assertEquals(expectedAddressBook.getAllPersons(), addressBook.getAllPersons());
}
}