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.7][T16-4]Rezky Arizaputra #427

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ Views the 2nd person in the address book.
`view 1` +
Views the 1st person in the results of the `find` command.

== Sorting all persons : `sort`

Sorts the list of all people in address book lexicographically. Shows sorted list next time `list` is entered.
Format: `sort`


== View all details of a person : `viewall`

Displays all details (including private details) of the specified person. +
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 @@ -20,6 +20,7 @@ public CommandResult execute() {
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + SortingCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE
Expand Down
18 changes: 18 additions & 0 deletions src/seedu/addressbook/commands/SortingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.addressbook.commands;

/**
* Sort the list of people in address book
*/
public class SortCommand extends Command {
public static final String COMMAND_WORD = "sort";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ "Sorts all people in address book. \n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SORTED = "Address book is sorted";
@Override
public CommandResult execute() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation for this method is inconsistent with the lines before it. Java unlike languages like Ruby or Python compiles fine with inconsistent indentation. But it is still important to have consistent proper indentation to make it easy for the user to read and mke your code neat in general

addressBook.sort();
return new CommandResult(MESSAGE_SORTED);
}
}
86 changes: 86 additions & 0 deletions src/seedu/addressbook/commands/SortingCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package seedu.addressbook.commands;

import org.junit.Before;
import org.junit.Test;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using * for imports. Because then the reader can explicitly see what was imported/used in this file. It is also better practice to import only things that you need instead of importing the entire library/module

import seedu.addressbook.util.TestUtil;

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

import static org.junit.Assert.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no space between this import line and the public class SortCommandTest { line but there is a line gap before this line. Just try to standardise line breaks :) Look at Addressbook-4 for inspiration.

public class SortCommandTest {

private AddressBook addressBook;

private List<ReadOnlyPerson> sortedList;

public void setUp() throws Exception {
Person SamAl = new Person(new Name("Sam Al"), new Phone("65234567", false),
new Email("[email protected]", false), new Address("395C Bukit Road", false), Collections.emptySet());
Person SamUl = new Person(new Name("Sam Ul"), new Phone("95234567", false),
new Email("[email protected]", false), new Address("33G Bata Road", false), Collections.emptySet());
Person SamIl = new Person(new Name("Sam Il"), new Phone("65345566", false),
new Email("[email protected]", false), new Address("55G Paku Road", false), Collections.emptySet());
Person SamEl = new Person(new Name("Sam El"), new Phone("65121122", false),
new Email("[email protected]", false), new Address("44H Buana Road", false),
Collections.emptySet());

addressBook = TestUtil.createAddressBook(SamAl, SamUl, SamIl, SamEl);


sortedList = TestUtil.createList(SamAl, SamEl, SamIl, SamUl);
}

@Test
public void execute_addressBookIsSorted() {
assertSortSuccessful(addressBook, sortedList);
}

/**
* Creates a new sorting command.
*
*/
private SortingCommand createSortingCommand(AddressBook addressBook,
List<ReadOnlyPerson> displayList) {

SortingCommand command = new SortingCommand();
command.setData(addressBook, displayList);

return command;
}
/**
* Asserts that the address book is successfully sorted.
*
* The addressBook passed in will not be modified (no side effects).
*/
private void assertSortingSuccessful(AddressBook addressBook,
List<ReadOnlyPerson> displayList) {
AddressBook expectedAddressBook = TestUtil.clone(addressBook);
expectedAddressBook.sort();
String expectedMessage = SortingCommand.MESSAGE_SORTED;

AddressBook actualAddressBook = TestUtil.clone(addressBook);

SortingCommand command = createSortingCommand(actualAddressBook, displayList);
assertCommandBehaviour(command, expectedMessage, expectedAddressBook, actualAddressBook);

}

/**
* Executes the command, and checks that the execution was what we had expected.
*/
private void assertCommandBehaviour(SortingCommand sortCommand, String expectedMessage,
AddressBook expectedAddressBook, AddressBook actualAddressBook) {

CommandResult result = sortCommand.execute();

assertEquals(expectedMessage, result.feedbackToUser);
assertEquals(expectedAddressBook.getAllPersons(), actualAddressBook.getAllPersons());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think might be good to not have so many blank lines at the end of the class. Also, assertCommandBehaviour does not have a blank line before its closing brace while assertSortingSuccessful does.




}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public void clear() {
public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

/**
* Sorts all persons in address book.
*/
public void sort() {allPersons.sort(); }

@Override
public boolean equals(Object other) {
Expand Down
11 changes: 11 additions & 0 deletions src/seedu/addressbook/data/person/CompareName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package seedu.addressbook.data.person;

import java.util.Comparator;

class CompareName implements Comparator<Person>{
@Override
public int compare(Person p1, Person p2) {
return p1.getName().toString()
.compareToIgnoreCase(p2.getName().toString());
}
}
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 @@ -128,6 +128,11 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException {
public void clear() {
internalList.clear();
}

/**
* Sorts all people in the list.
*/
public void sort() {Collections.sort(internalList, new CompareName()); }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no space after { but space before }


@Override
public Iterator<Person> iterator() {
Expand All @@ -141,3 +146,5 @@ public boolean equals(Object other) {
&& this.internalList.equals(((UniquePersonList) other).internalList));
}
}


4 changes: 4 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.commands.SortingCommand;
import seedu.addressbook.data.exception.IllegalValueException;

/**
Expand Down Expand Up @@ -90,6 +91,9 @@ public Command parseCommand(String userInput) {

case ViewCommand.COMMAND_WORD:
return prepareView(arguments);

case SortingCommand.COMMAND_WORD:
return new SortingCommand();

case ViewAllCommand.COMMAND_WORD:
return prepareViewAll(arguments);
Expand Down