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

[W7][T11-2]WU PEI HSUAN #30

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
5 changes: 5 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Examples:
Shows a list of all persons in the address book. +
Format: `list`

== Listing all persons in sorted order : `sort`

Shows a list of all persons sorted in alphabetical order in the address book. +
Format: `sort`

== Finding all persons containing any keyword in their name: `find`

Finds persons whose names contain any of the given keywords. +
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_PERSON_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 @@ -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
31 changes: 31 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.AddressBook;

import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.*;
import java.lang.*;


/**
* Lists all persons in the address book in sorted order to the user.
*/
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 as a list sorted in alphabetical order with index numbers.\n\t"
+ "Example: " + COMMAND_WORD;


@Override

Choose a reason for hiding this comment

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

Missing header comment. All non-trivial methods should have java doc format header comments.

public CommandResult execute() {
addressBook.sorted();

Choose a reason for hiding this comment

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

Be aware that too many empty lines can be a coding violation.

List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForPersonSortShownSummary(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_PERSON_SORTED_OVERVIEW = "%1$d persons sorted in alphabetical order!";
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
6 changes: 6 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException
allPersons.remove(toRemove);
}

/**
* Sorts all persons from the address book.
*/

public void sorted() { allPersons.sort();}

Choose a reason for hiding this comment

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

The placement of your code is violating the coding convention. Please add logic on new line. Refer to java coding conventions for CS2113/T for more information.


/**
* Clears all persons from the address book.
*/
Expand Down
11 changes: 10 additions & 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 implements Comparable<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 Expand Up @@ -61,4 +61,13 @@ public int hashCode() {
return fullName.hashCode();
}


/**compare name for sorting function*/
@Override
public int compareTo(Name name) {
return this.fullName.compareTo(name.fullName);
}



}
14 changes: 14 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,27 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException {
}
}

Choose a reason for hiding this comment

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

Again, we consider too many empty lines as a coding violation, as it reduces code readability.



/**
* Clears all persons in list.
*/
public void clear() {
internalList.clear();
}

/**
* Sorts all persons in list by name.
*/
public void sort(){
Comparator<Person> person = (p1, p2) -> {
Name name1 = p1.getName();
Name name2 = p2.getName();
return name1.compareTo(name2);
};
Collections.sort(internalList, person);
}

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ 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();

case HelpCommand.COMMAND_WORD: // Fallthrough
case HelpCommand.COMMAND_WORD:// Fallthrough
default:
return new HelpCommand();
}
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 @@ -58,6 +58,12 @@ public void listCommand_parsedCorrectly() {
parseAndAssertCommandType(input, ListCommand.class);
}

@Test

Choose a reason for hiding this comment

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

Missing header comment. All non-trivial methods should have java doc format header comments.

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

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