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

Add sort command for tutorial 2 #2

Closed
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
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.model.Model;

/**
* Lists all persons in the address book to the user, sorted by
* alphabetical order of names.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_SUCCESS = "List sorted in alphabetical order of names";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.sortByName();
return new CommandResult(MESSAGE_SUCCESS);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -77,6 +78,9 @@
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand();

Check warning on line 82 in src/main/java/seedu/address/logic/parser/AddressBookParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddressBookParser.java#L82

Added line #L82 was not covered by tests

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import java.util.Comparator;
import java.util.List;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -127,4 +128,13 @@ public boolean equals(Object other) {
public int hashCode() {
return persons.hashCode();
}

/**
* Sorts the list of persons using the provided comparator.
*
* @param comparator The comparator to use for sorting.
*/
public void sortNames(Comparator<Person> comparator) {
persons.sort(comparator);
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Sorts the list of persons in alphabetical order of names.
*/
void sortByName();
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.Comparator;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -145,4 +146,10 @@ public boolean equals(Object other) {
&& filteredPersons.equals(otherModelManager.filteredPersons);
}

@Override
public void sortByName() {
Comparator<Person> nameComp = Comparator.comparing(person -> person.getName().toString());
addressBook.sortNames(nameComp);
}

}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model;

import java.util.Comparator;

import javafx.collections.ObservableList;
import seedu.address.model.person.Person;

Expand All @@ -14,4 +16,6 @@ public interface ReadOnlyAddressBook {
*/
ObservableList<Person> getPersonList();

void sortNames(Comparator<Person> comparator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -147,4 +148,9 @@ private boolean personsAreUnique(List<Person> persons) {
}
return true;
}

public void sort(Comparator<Person> comparator) {
internalList.sort(comparator);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public ObservableList<Person> getFilteredPersonList() {
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortByName() {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/seedu/address/logic/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Comparator;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;

/**
* Contains integration tests (interaction with the Model) and unit tests for SortCommand.
*/
public class SortCommandTest {

private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
}

@Test
public void execute_sortByName_success() {
Comparator<Person> nameComp = Comparator.comparing(person -> person.getName().toString());
expectedModel.getAddressBook().sortNames(nameComp);
assertCommandSuccess(new SortCommand(), model, SortCommand.MESSAGE_SUCCESS, expectedModel);
}
}
6 changes: 6 additions & 0 deletions src/test/java/seedu/address/model/AddressBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -103,6 +104,11 @@ private static class AddressBookStub implements ReadOnlyAddressBook {
public ObservableList<Person> getPersonList() {
return persons;
}

@Override
public void sortNames(Comparator<Person> comparator) {
persons.sort(comparator);
}
}

}
Loading