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 task list command #75

Merged
Merged
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
18 changes: 17 additions & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ Examples:
- `list` followed by `delete 2` deletes the 2nd person in the address book.
- `find Betsy` followed by `delete 1` deletes the 1st person in the results of the `find` command.

### Listing tasks: `task list`

Lists all tasks in the task list. Clears any existing filters.

**Format:**

`task list`

#### Filtering tasks

Tasks can be filtered by completion status and/or tags. Show only completed tasks with `done/`, pending tasks with `undone/` and tasks with a certain tag `TAG` with `tag/TAG`.

**Format:**

`task list [done/] [undone/] [t/TAG]`

### Adding a task: `task add`

Allows the user to add a task to the current database.
Expand All @@ -164,7 +180,7 @@ Each task has a compulsory title field, and textual description, timestamp, and

**Format:**

`task add TITLE [d/DESCRIPTION] [ts/TIMESTAMP] [t/TAG}`
`task add TITLE [d/DESCRIPTION] [ts/TIMESTAMP] [t/TAG]`

**Example:**

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.address.logic.commands.task;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_UNDONE;

import java.util.List;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.TaskCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.task.filters.TaskFilters.TaskFilter;

/**
* Completes an existing task in the task list.
*/
public class ListTaskCommand extends TaskCommand {
public static final String COMMAND_WORD = "list";
public static final String FULL_COMMAND_WORD = TaskCommand.COMMAND_WORD + " " + COMMAND_WORD;
public static final String MESSAGE_SUCCESS = "Task list updated";
public static final String MESSAGE_USAGE = FULL_COMMAND_WORD
+ ": Lists tasks matching the given search conditions.\n"
+ "Parameters: "
+ "[" + PREFIX_DONE + "] "
+ "[" + PREFIX_UNDONE + "] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + FULL_COMMAND_WORD + " " + PREFIX_UNDONE + " " + PREFIX_TAG + "important";

private final List<TaskFilter> taskFilters;

public ListTaskCommand(List<TaskFilter> taskFilters) {
this.taskFilters = taskFilters;
}

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

model.setTaskFilters(taskFilters);
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public boolean equals(Object o) {
return this == o
|| (o instanceof ListTaskCommand
&& taskFilters.equals(((ListTaskCommand) o).taskFilters));
}

@Override
public int hashCode() {
return taskFilters.hashCode();
}
}
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class CliSyntax {
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
public static final Prefix PREFIX_TIMESTAMP = new Prefix("ts/");
public static final Prefix PREFIX_TITLE = new Prefix("ti/");
public static final Prefix PREFIX_DONE = new Prefix("done/");
public static final Prefix PREFIX_UNDONE = new Prefix("undone/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import seedu.address.logic.commands.task.DeleteTaskCommand;
import seedu.address.logic.commands.task.DoneTaskCommand;
import seedu.address.logic.commands.task.EditTaskCommand;
import seedu.address.logic.commands.task.ListTaskCommand;
import seedu.address.logic.commands.task.PurgeTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.task.AddTaskCommandParser;
import seedu.address.logic.parser.task.DeleteTaskCommandParser;
import seedu.address.logic.parser.task.DoneTaskCommandParser;
import seedu.address.logic.parser.task.EditTaskCommandParser;
import seedu.address.logic.parser.task.ListTaskCommandParser;

/**
* Parses all task-related commands (those starting with "task") and returns a TaskCommand.
Expand Down Expand Up @@ -50,6 +52,9 @@ public TaskCommand parse(String userInput) throws ParseException {
case EditTaskCommand.COMMAND_WORD:
return new EditTaskCommandParser().parse(arguments);

case ListTaskCommand.COMMAND_WORD:
return new ListTaskCommandParser().parse(arguments);

case PurgeTaskCommand.COMMAND_WORD:
return new PurgeTaskCommand();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.parser.task;

import static seedu.address.logic.parser.CliSyntax.PREFIX_DONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_UNDONE;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.logic.commands.task.ListTaskCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.filters.TaskFilters;
import seedu.address.model.task.filters.TaskFilters.TaskFilter;

public class ListTaskCommandParser implements Parser<ListTaskCommand> {
/**
* Parses the given {@code String} of arguments in the context of the EditTaskCommand
* and returns an EditTaskCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ListTaskCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_DONE, PREFIX_UNDONE, PREFIX_TAG);

Set<Tag> tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
List<TaskFilter> taskFilters = tags.stream().map(TaskFilters.FILTER_TAG).collect(Collectors.toList());

if (argMultimap.getValue(PREFIX_DONE).isPresent()) {
taskFilters.add(TaskFilters.FILTER_DONE);
}

if (argMultimap.getValue(PREFIX_UNDONE).isPresent()) {
taskFilters.add(TaskFilters.FILTER_DONE.invert());
}

if (!argMultimap.getPreamble().isEmpty()) {
throw new ParseException(ListTaskCommand.MESSAGE_USAGE);
}

return new ListTaskCommand(taskFilters);
}
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public ModelManager() {
this(new AddressBook(), new TaskList(), new UserPrefs());
}

/**
* Returns a new ModelManager initialize from the given {@link Model}.
* @param model The model to initialize the {@link ModelManager} from
* @return The initialized ModelManager
*/
public static ModelManager from(Model model) {
return new ModelManager(
new AddressBook(model.getAddressBook()), new TaskList(model.getTaskList()) , new UserPrefs());
}

//=========== UserPrefs ==================================================================================

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.address.logic.commands.task;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.task.ListTaskCommand.MESSAGE_SUCCESS;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalTasks.getTypicalTaskList;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.Command;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.filters.TaskFilters;
import seedu.address.model.task.filters.TaskFilters.TaskFilter;

public class ListTaskCommandTest {
private final Model model = new ModelManager(getTypicalAddressBook(), getTypicalTaskList(), new UserPrefs());

@Test
void execute_noFilter_showsEntireList() {
Command command = new ListTaskCommand(new ArrayList<>());
Model expectedModel = ModelManager.from(model);
expectedModel.setTaskFilters(new ArrayList<>());

assertCommandSuccess(command, model, MESSAGE_SUCCESS, expectedModel);
}

@Test
void execute_tagsFilter_showsTasksWithTag() {
Tag tag = new Tag("important");
List<TaskFilter> taskFilters = List.of(TaskFilters.FILTER_TAG.apply(tag));
Command command = new ListTaskCommand(taskFilters);
Model expectedModel = ModelManager.from(model);
expectedModel.setTaskFilters(taskFilters);

assertCommandSuccess(command, model, MESSAGE_SUCCESS, expectedModel);
}

@Test
void execute_multipleFilters_showsCorrectTasks() {
Tag tag = new Tag("important");
List<TaskFilter> taskFilters = List.of(
TaskFilters.FILTER_TAG.apply(tag),
TaskFilters.FILTER_DONE
);
Command command = new ListTaskCommand(taskFilters);
Model expectedModel = ModelManager.from(model);
expectedModel.setTaskFilters(taskFilters);

assertCommandSuccess(command, model, MESSAGE_SUCCESS, expectedModel);
}

@Test
void execute_filterThenClear_showsAllTasks() {
Tag tag = new Tag("important");
List<TaskFilter> taskFilters = List.of(
TaskFilters.FILTER_TAG.apply(tag),
TaskFilters.FILTER_DONE
);
Command command = new ListTaskCommand(taskFilters);
Model expectedModel = ModelManager.from(model);

expectedModel.setTaskFilters(taskFilters);
assertCommandSuccess(command, model, MESSAGE_SUCCESS, expectedModel);

Command clearCommand = new ListTaskCommand(List.of());
expectedModel.setTaskFilters(List.of());
assertCommandSuccess(clearCommand, model, MESSAGE_SUCCESS, expectedModel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser.task;

import static seedu.address.logic.commands.task.ListTaskCommand.MESSAGE_USAGE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_UNDONE;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.task.ListTaskCommand;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.filters.TaskFilters;
import seedu.address.model.task.filters.TaskFilters.TaskFilter;

public class ListTaskCommandParserTest {
private final ListTaskCommandParser parser = new ListTaskCommandParser();
@Test
void parse_emptyArguments_noTaskFilters() {
assertParseSuccess(parser, "", new ListTaskCommand(new ArrayList<>()));
}

@Test
void parse_showDone_showDoneTasks() {
assertParseSuccess(parser, " " + PREFIX_DONE, new ListTaskCommand(List.of(TaskFilters.FILTER_DONE)));
}

@Test
void parse_showUndone_showUndoneTasks() {
assertParseSuccess(parser, " " + PREFIX_UNDONE, new ListTaskCommand(List.of(TaskFilters.FILTER_DONE.invert())));
}

@Test
void parse_showTag_showTaggedTasks() {
String tagName = "important";
Tag tag = new Tag(tagName);
List<TaskFilter> taskFilters = List.of(TaskFilters.FILTER_TAG.apply(tag));
assertParseSuccess(parser, " " + PREFIX_TAG + tagName,
new ListTaskCommand(taskFilters));
}

@Test
void parse_withPreamble_failure() {
assertParseFailure(parser, " preamble", MESSAGE_USAGE);
}
}
21 changes: 19 additions & 2 deletions src/test/java/seedu/address/testutil/TypicalTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
public class TypicalTasks {
public static final Task BUY_GROCERIES = new TaskBuilder().build();
public static final Task DO_HOMEWORK = new TaskBuilder()
.withTitle("Do homework").withDescription("Math, physics and chemistry").build();
.withTitle("Do homework")
.withDescription("Math, physics and chemistry")
.withTags("important", "homework")
.build();
public static final Task CLEAN_ROOM = new TaskBuilder()
.withTitle("Clean my room")
.withTags("important")
.build();
public static final Task ARRANGE_MEETING = new TaskBuilder()
.withTitle("Arrange meeting")
.withTags("work", "important")
.withDone(true)
.build();

/**
* Returns an {@code AddressBook} with all the typical tasks.
Expand All @@ -24,6 +36,11 @@ public static TaskList getTypicalTaskList() {
}

public static List<Task> getTypicalTasks() {
return new ArrayList<>(Arrays.asList(BUY_GROCERIES, DO_HOMEWORK));
return new ArrayList<>(Arrays.asList(
BUY_GROCERIES,
DO_HOMEWORK,
CLEAN_ROOM,
ARRANGE_MEETING
));
}
}