Skip to content

Commit

Permalink
Merge pull request #49 from ketweeen/branch-ListCommand
Browse files Browse the repository at this point in the history
Branch list command
  • Loading branch information
ketweeen committed Oct 15, 2023
2 parents 0bef066 + e9e888a commit ae37f7b
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Comparator;

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

/**
* Lists all persons in the address book to the user.
Expand All @@ -14,10 +17,41 @@ public class ListCommand extends Command {

public static final String MESSAGE_SUCCESS = "Listed all persons";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Lists all persons. "
+ "Parameters: [s/ATTRIBUTE]\n"
+ "Optional: ATTRIBUTE can be 'name' or other attributes for sorting.\n"
+ "Example: " + COMMAND_WORD + " s/name";

private final Comparator<Person> sortingComparator;


/**
* Creates a ListCommand with no sorting.
*/
public ListCommand() {
// Default constructor for no sorting
this.sortingComparator = null;
}

/**
* Creates a ListCommand with the specified sorting comparator.
*
* @param sortingComparator The comparator to be used for sorting the person list.
*/
public ListCommand(Comparator<Person> sortingComparator) {
this.sortingComparator = sortingComparator;
}

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

if (sortingComparator != null) {
// If a sorting comparator is provided, sort the list using it
model.sortPersonList(sortingComparator);
}

System.out.println(model.getFilteredPersonList());
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(MESSAGE_SUCCESS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Command parseCommand(String userInput) throws ParseException {
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();
return new ListCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class CliSyntax {

public static final Prefix PREFIX_REMARK = new Prefix("r/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_STATUS = new Prefix("s/");


public static final Prefix PREFIX_SORT = new Prefix("so/");
public static final Prefix PREFIX_STATUS = new Prefix("st/");
}
44 changes: 44 additions & 0 deletions src/main/java/seedu/address/logic/parser/ListCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.Comparator;

import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Person;

/**
* Parses input arguments and creates a new ListCommand object with sorting options.
*/
public class ListCommandParser implements Parser<ListCommand> {

@Override
public ListCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_SORT);

if (!argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE));
}

String sortingAttribute = argMultimap.getValue(CliSyntax.PREFIX_SORT).orElse(null);

if (sortingAttribute == null) {
return new ListCommand();
}

// Create the sorting comparator based on the sorting attribute
Comparator<Person> sortingComparator = createSortingComparator(sortingAttribute);

return new ListCommand(sortingComparator);
}

private Comparator<Person> createSortingComparator(String sortingAttribute) {
if ("name".equalsIgnoreCase(sortingAttribute)) {
return Comparator.comparing(Person::getName);
} else {
// Default: no sorting (you can change this behavior as needed)
return null;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model;

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

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -85,9 +86,12 @@ public interface Model {
*/
void updateFilteredPersonList(Predicate<Person> predicate);

void sortPersonList(Comparator<Person> comparator);

/**
* Updates the filter of the filtered person list to filter by the given {@code predicates}.
* @throws NullPointerException if any of the {@code predicates} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate1, Predicate<Person> predicate2);

}
16 changes: 16 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,9 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

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

Expand Down Expand Up @@ -128,6 +131,19 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

// TODO: fix the sorting
@Override
public void sortPersonList(Comparator<Person> comparator) {
requireNonNull(comparator);

List<Person> sortedList = new ArrayList<>(getFilteredPersonList());
sortedList.sort(comparator);

// Update the filtered list
Predicate<Person> predicate = sortedList::contains;
updateFilteredPersonList(predicate);
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate1, Predicate<Person> predicate2) {
requireNonNull(predicate1);
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 @@ -64,4 +64,9 @@ public int hashCode() {
return fullName.hashCode();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -158,9 +159,14 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortPersonList(Comparator<Person> comparator) {
throw new AssertionError("This method should not be called");

@Override
public void updateFilteredPersonList(Predicate<Person> predicate1, Predicate<Person> predicate2) {
throw new AssertionError("This method should not be called.");

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void parseCommand_help() throws Exception {
@Test
public void parseCommand_list() throws Exception {
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD) instanceof ListCommand);
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
// assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
}

@Test
Expand Down

0 comments on commit ae37f7b

Please sign in to comment.