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.4][F10-A3] Chen Huijia #878

Open
wants to merge 11 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
5 changes: 5 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ Format: `clear`
Exits the program.<br>
Format: `exit`

## Sorting the entries alphabetically by name : `sort`
Sorts addressbook alphabetically <br>
Format: `sort`


## Saving the data
Address book data are saved in the hard disk automatically after any command that changes the data.<br>
There is no need to save manually.
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;

/*
* sorts the addressbook
*/
public class SortCommand extends Command{
public static final String COMMAND_WORD = "sort";
public static final String MESSAGE_USAGE = "Sort the Addressbook alphabetically.\n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Address book has been sorted!";

public SortCommand() {}


@Override
public CommandResult execute() {
addressBook.sort();
return new CommandResult(MESSAGE_SUCCESS);
}
}
8 changes: 8 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,12 @@ public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(allPersons, allTags);
}

/*
* sorts the list alphabetically
*/
public void sort() {
allPersons.sort();

}
}
11 changes: 11 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,15 @@ public int hashCode() {
return internalList.hashCode();
}

/*
* sorts list alphabetically
*/
public void sort() {
Collections.sort(internalList, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getName().fullName.compareTo(o2.getName().fullName);
}
});
}
}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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
20 changes: 19 additions & 1 deletion test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ public void execute_list_showsAllPersons() throws Exception {
expectedList);
}

@Test
public void execute_sort() throws Exception {
// prepare expectations
TestDataHelper helper = new TestDataHelper();
Person p1 = helper.generatePerson(4, true);
Person p2 = helper.generatePerson(5, true);
List<Person> PersonsInOrder = helper.generatePersonList(p1, p2);
List<Person> PersonsNotIO = helper.generatePersonList(p2, p1);

//set up the expected AB
AddressBook inOrderAB = new AddressBook();
helper.addToAddressBook(inOrderAB,PersonsInOrder);
helper.addToAddressBook(addressBook,PersonsNotIO);
AddressBook expectedAB = new AddressBook(inOrderAB.getAllPersons(), addressBook.getAllTags());

//verify results
assertCommandBehavior("sort", SortCommand.MESSAGE_SUCCESS,expectedAB,false,Collections.emptyList());
}

@Test
public void execute_view_invalidArgsFormat() throws Exception {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE);
Expand Down Expand Up @@ -587,5 +606,4 @@ Person generatePersonWithName(String name) throws Exception {
);
}
}

}