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.4r][W13-B4]Ang Tian Lan #924

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 @@ -34,6 +34,10 @@ Examples:
* `add John Doe p/98765432 e/johnd@gmail.com a/John street, block 123, #01-01`
* `add Betsy Crowe pp/1234567 e/betsycrowe@gmail.com pa/Newgate Prison t/criminal t/friend`

## Sort list : 'sort'
Sort list alphabetically by person name.<br>
Format: 'sort'

## Listing all persons : `list`
Shows a list of all persons in the address book.<br>
Format: `list`
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static String getMessageForPersonListShownSummary(List<? extends ReadOnly
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}

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 @@ -12,6 +12,7 @@ public class HelpCommand extends Command {
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_ALL_USAGES = AddCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE
+ "\n" + DeleteCommand.MESSAGE_USAGE
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
Expand Down
28 changes: 28 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.addressbook.commands;

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

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Sort list alphabetically by person's name.\n\t"
+ "Example: " + COMMAND_WORD;

public List<Person> sortByName(){

return addressBook.sortList();
}

public CommandResult execute() {
List<Person> sortedList=sortByName();
return new CommandResult(getMessageForPersonSortShownSummary(sortedList),sortedList);
}

}
2 changes: 2 additions & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ 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 = "List sorted successfully";
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!";
public static final String MESSAGE_USING_STORAGE_FILE = "Using storage file : %1$s";

}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
}

/**
* Sort list alphabetically by person name
*/
public List<Person> sortList(){
return allPersons.sort();
}

/**
* Checks if an equivalent person exists in the address book.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 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{

public static final String EXAMPLE = "John Doe";
public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters";
Expand Down
2 changes: 2 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import seedu.addressbook.data.tag.UniqueTagList;

import java.util.Comparator;
import java.util.Objects;

/**
Expand All @@ -16,6 +17,7 @@ public class Person implements ReadOnlyPerson {
private Address address;

private final UniqueTagList tags;

/**
* Assumption: Every field must be present and not null.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ public void add(Person toAdd) throws DuplicatePersonException {
internalList.add(toAdd);
}

/**
* Sort List alphabetically by person name
*/
public List<Person> sort(){

List<Person> sortedList = internalList;
Collections.sort(sortedList, new Comparator<Person>() {
@Override
public int compare(Person firstPerson, Person secondPerson) {
return firstPerson.getName().fullName.compareTo(secondPerson.getName().fullName);
}

});

return sortedList;
}

/**
* Get reference of the list which is in ReadOnlyPerson form
*/
public List<Person> getInternalList(){

return internalList;

}

/**
* Removes the equivalent person from the 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 @@ -60,6 +60,9 @@ public Command parseCommand(String userInput) {
case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);

case SortCommand.COMMAND_WORD:
return new SortCommand();

case DeleteCommand.COMMAND_WORD:
return prepareDelete(arguments);

Expand Down
25 changes: 25 additions & 0 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ public void execute_help() throws Exception {
assertCommandBehavior("help", HelpCommand.MESSAGE_ALL_USAGES);
}

@Test
public void execute_sort() throws Exception {
TestDataHelper helper =new TestDataHelper();

Person testSubjectFour = helper.generatePersonWithName("Adam Cole");
Person testSubjectTwo = helper.generatePersonWithName("Brock Lesnar");
Person testSubjectOne = helper.generatePersonWithName("Cesaro");
Person testSubjectThree = helper.generatePersonWithName("Deam Ambrose");

List<Person> listOfTestSubjects = helper.generatePersonList(
testSubjectOne ,testSubjectTwo ,testSubjectThree ,testSubjectFour);

AddressBook expectedAB = helper.generateAddressBook(listOfTestSubjects);
expectedAB.sortList();
List<? extends ReadOnlyPerson> expectedList = expectedAB.getAllPersons().immutableListView();

helper.addToAddressBook(addressBook, listOfTestSubjects);

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

@Test
public void execute_exit() throws Exception {
assertCommandBehavior("exit", ExitCommand.MESSAGE_EXIT_ACKNOWEDGEMENT);
Expand Down
5 changes: 5 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,11 @@ 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