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.7][F10-2]Liu Xiaohang #446

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 @@ -176,6 +176,11 @@ Example:

* `java seedu.addressbook.Main mydata.txt`

== Sorting people : `sort`

Sort all the people in the addressbook by name in alphabetically ascending order.
Format: `sort`

[NOTE]
====
The file name must end in `.txt` for it to be acceptable to the program.
Expand Down
10 changes: 10 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public static String getMessageForPersonListShownSummary(List<? extends ReadOnly
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}

/**
* Constructs a feedback message to summarise an operation that displayed a list of sorted persons.
Copy link

Choose a reason for hiding this comment

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

summarize (use American spelling), displays

*
* @param personsDisplayed used to generate summary
* @return summary message for persons displayed
*/
public static String getMessageForPersonSortShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
Copy link

Choose a reason for hiding this comment

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

Hmm... method name is not that intuitive

return String.format(Messages.MESSAGE_SORTED, personsDisplayed.size());
}

/**
* Executes the command and returns the result.
*/
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CommandResult execute() {
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE
Copy link

Choose a reason for hiding this comment

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

/nit: Why is sort at the end?

);
}
}
33 changes: 33 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.addressbook.commands;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

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.data.tag.Tag;

/**
* Sort 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 + ": sort people in the address book.\n"
+ "Example: " + COMMAND_WORD;
Copy link

Choose a reason for hiding this comment

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

Inconsistant '+' alignment.


//public static final String MESSAGE_SUCCESS = "People sorted.";

@Override
public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().sortedListView();
return new CommandResult(getMessageForPersonSortShownSummary(allPersons), allPersons);
}

}
1 change: 1 addition & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class Messages {
"java seedu.addressbook.Main [STORAGE_FILE_PATH]";
public static final String MESSAGE_WELCOME = "Welcome to your Address Book!";
public static final String MESSAGE_USING_STORAGE_FILE = "Using storage file : %1$s";
public static final String MESSAGE_SORTED = "%1$s persons sorted!";
}
4 changes: 4 additions & 0 deletions src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public int hashCode() {
return fullName.hashCode();
}

public int compareTo(Name name) {
return fullName.compareTo(name.fullName);
}

}
7 changes: 6 additions & 1 deletion src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> {

private Name name;
private Phone phone;
Expand Down Expand Up @@ -88,4 +88,9 @@ public String toString() {
return getAsTextShowAll();
}

@Override
public int compareTo(Person person) {
return name.compareTo(person.name);
}

}
11 changes: 11 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public List<ReadOnlyPerson> immutableListView() {
return Collections.unmodifiableList(internalList);
}

/**
* Returns an unmodifiable java List view with elements cast as immutable {@link ReadOnlyPerson}s.
Copy link

Choose a reason for hiding this comment

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

Use @return and @param.

* For use with other methods/libraries.
* Any changes to the internal list/elements are immediately visible in the returned list.
*/
public List<ReadOnlyPerson> sortedListView() {
//internalList.sort(); <--how to make this line work instead of using Collections.sort()? :(
Copy link

Choose a reason for hiding this comment

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

https://docs.oracle.com/javase/8/docs/api/java/util/List.html
You have to pass in a comparator in this case. However, we usually just use Collections.sort/ Arrays.sort

Collections.sort(internalList);
return Collections.unmodifiableList(internalList);
}


/**
* Checks if the list contains an equivalent person as the given argument.
Expand Down
16 changes: 5 additions & 11 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -97,7 +87,11 @@ public Command parseCommand(String userInput) {
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand();

case HelpCommand.COMMAND_WORD: // Fallthrough

Copy link

Choose a reason for hiding this comment

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

Avoid adding unnecessary new line

default:
return new HelpCommand();
}
Expand Down
2 changes: 2 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
|| Example: help
|| exit: Exits the program.
|| Example: exit
|| sort: sort people in the address book.
|| Example: sort
|| ===================================================
|| Enter command: || [Command entered: delete 1]
|| The person index provided is invalid
Expand Down
10 changes: 10 additions & 0 deletions test/java/seedu/addressbook/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package seedu.addressbook.commands;

//import ...

public class SortCommandTest {
//TODO: add some tests for sort command.
//assertListSorted(list) {
// assertEqual(list, list.sort());
// }
}