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.11r][F14-B4] Liu Jianghao #757

Open
wants to merge 3 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
11 changes: 10 additions & 1 deletion src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ protected Command() {
public static String getMessageForPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}

/**
*Constructs a feedback message to summarise an operation that displayed a sorted list of persons
*
*@param sorted personsDisplayed used to generate summary
*@return summary message for sorted persons displayed
*/
public static String getMessageForPersonSortShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, 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 @@ -16,6 +16,7 @@ public class HelpCommand extends Command {
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE

Choose a reason for hiding this comment

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

Coding standard violation!

+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
Expand Down
27 changes: 27 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.List;


/**
* 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 + ":\n"
+ "Displays all persons sorted by name alphabetically.\n\t"
+ "Example: " + COMMAND_WORD;


@Override
public CommandResult execute() {
addressBook.sort();
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
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 @@ -9,6 +9,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!";
Expand Down
7 changes: 6 additions & 1 deletion src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
syncTagsWithMasterList(toAdd);
allPersons.add(toAdd);
}

/**
* Sort all persons by name in alphabectical order.
*/
public void sort() {
allPersons.sort();
}
/**
* Checks if an equivalent person exists in the address book.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static class PersonNotFoundException extends Exception {}

private final List<Person> internalList = new ArrayList<>();


/**
* Constructs empty person list.
*/
Expand Down Expand Up @@ -106,6 +107,18 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException {
}
}

/**
* Sort all persons in alphabetical order
*
*/
public void sort() {
internalList.sort(
Comparator.comparing((Person p) -> p.getName().toString(),

Choose a reason for hiding this comment

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

Indentation of this line should be 8 more spaces than the previous line.

(p1,p2)->(p1.compareToIgnoreCase(p2)==0) ? p1.compareTo(p2): p1.compareToIgnoreCase(p2))
);
}


/**
* Clears all persons in list.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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