Skip to content

Commit

Permalink
Implement the Sort Command for People
Browse files Browse the repository at this point in the history
Following additions of two new classes:
* SortCommand.java
* SortCommandParser.java

And changes made to existing classes to support the sorting of people,

* We are now able to sort the lists of people displayed by the
lexicographical order of their names (e.g. Amanda would appear higher
up in the person list as compared to Ben after sorting because the
letter A appears earlier than B following alphabetical orders.
  • Loading branch information
bennyLCK committed Mar 25, 2024
1 parent bb2e7dd commit 8973b0e
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 2 deletions.
39 changes: 39 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,39 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.model.Model;

/**
* Sorts all persons in the address book by an attribute of persons.
*/
public class SortCommand extends PersonCommand {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_SUCCESS = "sorted all persons by name";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": sorts people according to an attribute and displays the sorted person list.\n"
+ "Parameters: PREFIX (corresponds to each person's attribute e.g. /n for Name)\n"
+ "Example: " + COMMAND_WORD + " n/";

private final String prefix;

/**
* @param prefix referring to an attribute of persons to sort by
*/
public SortCommand(String prefix) {
requireNonNull(prefix);
this.prefix = prefix;
}


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.sortAddressBook(prefix);

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 @@ -75,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
return new SortCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/SortCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new SortCommand object
*/
public class SortCommandParser implements Parser<SortCommand> {

/**
* Parses the given {@code String} of arguments in the context of the SortCommand
* and returns an SortCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public SortCommand parse(String args) throws ParseException {
String prefix = args.trim();
if (prefix.isEmpty() || !PREFIX_NAME.getPrefix().equals(prefix)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
}

return new SortCommand(prefix);
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public void removePerson(Person key) {
persons.remove(key);
}

/**
* Sorts the persons in the address book by the attribute derived from a given prefix.
*/
public void sortAddressBook(String prefix) {
persons.sortPersons(prefix);
}

//// util methods

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public interface Model {
*/
void setPerson(Person target, Person editedPerson);

void sortAddressBook(String prefix);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

@Override
public void sortAddressBook(String prefix) {
addressBook.sortAddressBook(prefix);
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down Expand Up @@ -169,7 +174,7 @@ public void setArticle(Article target, Article editedArticle) {
articleBook.setArticle(target, editedArticle);
}

//=========== Filtered Person List Accessors =============================================================
//=========== Filtered Article List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Article} backed by the internal list of
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
Expand Down Expand Up @@ -44,6 +44,11 @@ public String toString() {
return fullName;
}

@Override
public int compareTo(Name other) {
return this.fullName.compareTo(other.fullName);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

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

Expand Down Expand Up @@ -97,6 +99,15 @@ public void setPersons(List<Person> persons) {
internalList.setAll(persons);
}

/**
* Sorts the list of persons by the given prefix.
*/
public void sortPersons(String prefix) {
if (PREFIX_NAME.getPrefix().equals(prefix)) {
internalList.sort(Comparator.comparing(Person::getName));
}
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public void setPerson(Person target, Person editedPerson) {
throw new AssertionError("This method should not be called.");
}

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

@Override
public ObservableList<Person> getFilteredPersonList() {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit 8973b0e

Please sign in to comment.