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][W09-A3]ZHAO PENGYU #915

Open
wants to merge 2 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
6 changes: 5 additions & 1 deletion doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ Examples:

## Clearing all entries : `clear`
Clears all entries from the address book.<br>
Format: `clear`
Format: `clear`

## Sorts all entries:'sort'
Sorts all entries in the address book.<br>
Format:'sort'

## Exiting the program : `exit`
Exits the program.<br>
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> personDisplayed){
return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW,personDisplayed.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
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 com.sun.org.apache.regexp.internal.RE;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
Copy link

Choose a reason for hiding this comment

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

don't import everything with * import classes explicitly


public class SortCommand extends Command {
Copy link

Choose a reason for hiding this comment

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

need header comments for the class

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Sort the address book.\n\t"
+ "Example:" + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "sort successfually!";

public CommandResult execute() {
addressBook.sort();
List<ReadOnlyPerson> allpersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(String.format(MESSAGE_SUCCESS), 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_PERSONS_SORTED_OVERVIEW = "%1$d persons listed!";
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
3 changes: 3 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public UniqueTagList getAllTags() {
return new UniqueTagList(allTags);
}

public void sort(){
allPersons.sort();
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
18 changes: 18 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public List<ReadOnlyPerson> immutableListView() {
return Collections.unmodifiableList(internalList);
}

public List<Person> getInternalList() {
return internalList;
}

/**
* Checks if the list contains an equivalent person as the given argument.
Expand Down Expand Up @@ -113,6 +116,21 @@ public void clear() {
internalList.clear();
}

/**
* Sorts all persons in list.
*/
public void sort(){
Collections.sort(internalList,compare);
}

public Comparator<Person> compare = new Comparator<Person>(){
public int compare(Person First, Person Second){
String firstName= First.getName().fullName;
String secondName= Second.getName().fullName;
return firstName.compareTo(secondName);
}
};

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
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 @@ -69,6 +69,9 @@ public Command parseCommand(String userInput) {
case FindCommand.COMMAND_WORD:
return prepareFind(arguments);

case SortCommand.COMMAND_WORD:
return new SortCommand();

case ListCommand.COMMAND_WORD:
return new ListCommand();

Expand Down
18 changes: 18 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,24 @@ public void execute_find_matchesIfAnyKeywordPresent() throws Exception {
expectedList);
}

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

Person first=helper.generatePersonWithName("Peng");
Person second= helper.generatePersonWithName("Zhao");
List<Person> Persons=helper.generatePersonList(first,second);
AddressBook unsorted=helper.generateAddressBook(Persons);
helper.addToAddressBook(addressBook, Persons);

unsorted.sort();
List<? extends ReadOnlyPerson> sorted= unsorted.getAllPersons().immutableListView();
assertCommandBehavior("sort",
String.format(SortCommand.MESSAGE_SUCCESS),
unsorted,
true,
sorted);
}
/**
* 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 @@ -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