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][W10-B2]Ma Yuqian #771

Open
wants to merge 1 commit 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 doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Examples:
Shows a list of all persons in the address book.<br>
Format: `list`

## Sorting all persons : `sort`
Sorts all the entries in address book based on people's full name in alphabetical order.<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
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
21 changes: 21 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.addressbook.commands;
import seedu.addressbook.common.Messages;

/**
* based on first names, sort entries in the address book in order.
*/
public class SortCommand extends Command{

public static final String COMMAND_WORD = "sort";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Sorts entries in the address book according to first name.\n"
+ "Example: " + COMMAND_WORD;


@Override
public CommandResult execute() {
this.addressBook.sort();
return new CommandResult(Messages.MESSAGE_SORTED);
}
}

1 change: 1 addition & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,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 = "Address Book is sorted successfully!";
}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,11 @@ public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(allPersons, allTags);
}

/**
* Based on full names, sort all persons in the list in order.
*/
public void sort(){
allPersons.sort();
}
}
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 @@ -131,4 +131,11 @@ public int hashCode() {
return internalList.hashCode();
}

/**
* Sorts all persons in the list according to their full name
*/
public void sort(){
internalList.sort((personA, personB) -> (personA.getName().fullName.toLowerCase().compareTo(personB.getName().fullName.toLowerCase())));
}

}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public Command parseCommand(String userInput) {
case ViewAllCommand.COMMAND_WORD:
return prepareViewAll(arguments);

case SortCommand.COMMAND_WORD:
return new SortCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
44 changes: 44 additions & 0 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,50 @@ public void execute_find_matchesIfAnyKeywordPresent() throws Exception {
expectedList);
}

@Test
public void execute_sort_nonEmptyAddressBook() throws Exception {
TestDataHelper testDatahelper = new TestDataHelper();
Person target1 = testDatahelper.generatePersonWithName("A");
Person target2 = testDatahelper.generatePersonWithName("B");
Person target3 = testDatahelper.generatePersonWithName("C");
Person target4 = testDatahelper.generatePersonWithName("D");
Person person1 = testDatahelper.generatePersonWithName("D");
Person person2 = testDatahelper.generatePersonWithName("B");
Person person3 = testDatahelper.generatePersonWithName("C");
Person person4 = testDatahelper.generatePersonWithName("A");
//get an unsorted list
List<Person> unsortedList = testDatahelper.generatePersonList(person1,person2,person3,person4);
//Expect sorted list
List<Person> expectedList = testDatahelper.generatePersonList(target1,target2,target3,target4);
//No last shown list so expect empty
List<Person> emptyList = new ArrayList<>();
//Sorted AddressBook expected
AddressBook expectedAB = testDatahelper.generateAddressBook(expectedList);
//add unsorted list into AddressBook
testDatahelper.addToAddressBook(addressBook, unsortedList);
assertCommandBehavior("sort",
Messages.MESSAGE_SORTED,
expectedAB,
false,
emptyList);
}

@Test
public void execute_sort_emptyAddressBook() throws Exception {
TestDataHelper testDatahelper = new TestDataHelper();
List<Person> emptyList = new ArrayList<>();
List<Person> expectedList = new ArrayList<>();
//Empty AddressBook expected
AddressBook expectedAB = testDatahelper.generateAddressBook(expectedList);
//add empty list into AddressBook
testDatahelper.addToAddressBook(addressBook, emptyList);
assertCommandBehavior("sort",
Messages.MESSAGE_SORTED,
expectedAB,
false,
expectedList);
}

/**
* A utility class to generate test data.
*/
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 @@ -52,6 +52,11 @@ public void clearCommand_parsedCorrectly() {
final String input = "clear";
parseAndAssertCommandType(input, ClearCommand.class);
}
@Test
public void sortCommand_parsedCorrectly() {
final String input = "sort";
parseAndAssertCommandType(input, SortCommand.class);
}

@Test
public void listCommand_parsedCorrectly() {
Expand Down