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][F12-B3] Tan Wei Jie #733

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

## Sort all persons in list : `sort`
Shows a list of all persons in the address book sorted alphabetically.<br>
Format: `sort`

## Finding all persons containing any keyword in their name: `find`
Finds persons whose names contain any of the given keywords.<br>
Format: `find KEYWORD [MORE_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 displayed a listing of sorted persons.
*
* @param sortedPersonsDisplayed used to generate summary
* @return summary message for persons displayed
*/
public static String getMessageForSortedListShownSummary(List<? extends ReadOnlyPerson> sortedPersonsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, sortedPersonsDisplayed.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
26 changes: 26 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.List;


/**
* Lists all persons in the address book to the user sorted alphabetically.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

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


@Override
public CommandResult execute() {
addressBook.sort();
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForSortedListShownSummary(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: 7 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException
allPersons.remove(toRemove);
}

/**
* Sorts all persons by name in alphabetical order in the address book.
*/
public void sort() {
allPersons.sort();
}

/**
* Clears all persons and tags from the address book.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException {
}
}

/**
* Sorts all persons in list alphabetically. Similar names are sorted lexicographically.
*/
public void sort() {
internalList.sort(
Comparator.comparing((Person p) -> p.getName().toString(),
(s1, s2) -> (s1.compareToIgnoreCase(s2) == 0) ? s1.compareTo(s2) : s1.compareToIgnoreCase(s2))
);
}

/**
* Clears all persons in list.
*/
Expand Down
22 changes: 20 additions & 2 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package seedu.addressbook.parser;

import seedu.addressbook.commands.*;
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.SortCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.data.exception.IllegalValueException;

import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -72,6 +87,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
24 changes: 24 additions & 0 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ public void execute_list_showsAllPersons() throws Exception {
expectedList);
}

@Test
public void execute_sort_showsAllSortedPersons() throws Exception {
TestDataHelper helper = new TestDataHelper();
Person p1 = helper.generatePersonWithName("John doe");
Person p2 = helper.generatePersonWithName("John Doe");
Person p3 = helper.generatePersonWithName("Jane Low");
Person p4 = helper.generatePersonWithName("Blake Stu");
Person p5 = helper.generatePersonWithName("Hob wright");
Person p6 = helper.generatePersonWithName("JohN Doe");
Person p7 = helper.generatePersonWithName("Leonard");
Person p8 = helper.generatePersonWithName("Hob Wright");

List<Person> eightPersons = helper.generatePersonList(p1, p2, p3, p4, p5, p6, p7, p8);
helper.addToAddressBook(addressBook, eightPersons);
List<Person> expectedList = helper.generatePersonList(p4, p8, p5, p3, p6, p2, p1, p7);
AddressBook expectedAB = helper.generateAddressBook(expectedList);

assertCommandBehavior("sort",
Command.getMessageForSortedListShownSummary(expectedList),
expectedAB,
true,
expectedList);
}

@Test
public void execute_view_invalidArgsFormat() throws Exception {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE);
Expand Down
6 changes: 6 additions & 0 deletions test/java/seedu/addressbook/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void listCommand_parsedCorrectly() {
parseAndAssertCommandType(input, ListCommand.class);
}

@Test
public void sortCommand_parsedCorrectly() {
final String input = "sort";
parseAndAssertCommandType(input, SortCommand.class);
}

@Test
public void exitCommand_parsedCorrectly() {
final String input = "exit";
Expand Down