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

[W7][T09-02] Cheah Zhi Kang #34

Open
wants to merge 4 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
4 changes: 4 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Examples:
Shows a list of all persons in the address book. +
Format: `list`

== Sorting all persons : `sort`
Sorts all persons in the address book based on their names lexicographically. +
Format: `sort`

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

Finds persons whose names contain any of the given keywords. +
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 sorted a listing of persons.
*
* @param personsDisplayed used to generate summary
* @return summary message for persons sorted
*/
public static String getMessageForSortedPersonListShownSummary(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
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
Expand Down
25 changes: 25 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.person.ReadOnlyPerson;

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 + ":\n"
+ "Sorts all persons in the address book based on their names lexicographically.\n\t"
+ "Example: " + COMMAND_WORD;


@Override
public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.sortAllPersons().immutableListView();
return new CommandResult(getMessageForSortedPersonListShownSummary(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
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

public UniquePersonList sortAllPersons() {
allPersons.sort();
return new UniquePersonList(allPersons);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public List<String> getWordsInName() {
return Arrays.asList(fullName.split("\\s+"));
}

public int compareTo(Name target) {
int compare = this.fullName.compareTo(target.fullName);
return compare;
}

@Override
public String toString() {
return fullName;
Expand Down
10 changes: 10 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.Comparator;

import seedu.addressbook.data.tag.Tag;

Expand Down Expand Up @@ -61,6 +62,15 @@ public Set<Tag> getTags() {
return new HashSet<>(tags);
}

/**
* Compare selected person's name with target's name
*/
public static Comparator<Person> compareByName = new Comparator<Person>() {
public int compare(Person selected, Person target) {
return selected.name.compareTo(target.name);
}
};

/**
* Replaces this person's tags with the tags in {@code replacement}.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public void clear() {
internalList.clear();
}

/**
* Sorts the current list in lexicographocal order of names
*/
public void sort() {
Collections.sort(internalList, Person.compareByName);
}

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand Down
6 changes: 6 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public Command parseCommand(String userInput) {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
if (arguments.isEmpty()) {
return new SortCommand();
}
return new HelpCommand();

case ViewCommand.COMMAND_WORD:
return prepareView(arguments);

Expand Down