Skip to content

Commit

Permalink
Merge pull request #101 from pra-navi/branch-taskTag
Browse files Browse the repository at this point in the history
feat: Add tag to task
  • Loading branch information
freshcabbage123 authored Oct 25, 2023
2 parents d79a756 + 97c3620 commit 21bf265
Show file tree
Hide file tree
Showing 25 changed files with 420 additions and 115 deletions.
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public static String format(Task task) {
.append("; Note: ")
.append(task.getNote())
.append("; Status: ")
.append(task.getStatus());
.append(task.getStatus())
.append("; Tags: ");
task.getTags().forEach(builder::append);
return builder.toString();
}

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

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_TITLE;

Expand All @@ -19,10 +20,13 @@ public class AddTaskCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a task to the address book. "
+ "Parameters: "
+ PREFIX_TASK_TITLE + "TITLE "
+ PREFIX_TASK_NOTE + "NOTE\n"
+ PREFIX_TASK_NOTE + "NOTE "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_TASK_TITLE + "Get flowers "
+ PREFIX_TASK_NOTE + "Wedding Anniversary";
+ PREFIX_TASK_NOTE + "Wedding Anniversary "
+ PREFIX_TAG + "finance "
+ PREFIX_TAG + "class ";

public static final String MESSAGE_SUCCESS = "New task added: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the address book";
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditTaskCommand.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_TITLE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Note;
import seedu.address.model.task.Task;
import seedu.address.model.task.Title;
Expand All @@ -31,7 +36,8 @@ public class EditTaskCommand extends Command {
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_TASK_TITLE + "TITLE] "
+ "[" + PREFIX_TASK_NOTE + "NOTE]\n"
+ "[" + PREFIX_TASK_NOTE + "NOTE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_TASK_TITLE + "Prepare Agenda "
+ PREFIX_TASK_NOTE + "To book venue";
Expand Down Expand Up @@ -85,8 +91,9 @@ private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTas

Title updatedTitle = editTaskDescriptor.getTitle().orElse(taskToEdit.getTitle());
Note updatedNote = editTaskDescriptor.getNote().orElse(taskToEdit.getNote());
Set<Tag> updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags());

return new Task(updatedTitle, updatedNote);
return new Task(updatedTitle, updatedNote, updatedTags);
}

@Override
Expand Down Expand Up @@ -120,6 +127,7 @@ public String toString() {
public static class EditTaskDescriptor {
private Title title;
private Note note;
private Set<Tag> tags;

public EditTaskDescriptor() {
}
Expand All @@ -130,13 +138,14 @@ public EditTaskDescriptor() {
public EditTaskDescriptor(EditTaskDescriptor toCopy) {
setTitle(toCopy.title);
setNote(toCopy.note);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(title, note);
return CollectionUtil.isAnyNonNull(title, note, tags);
}

public void setTitle(Title title) {
Expand All @@ -155,6 +164,23 @@ public Optional<Note> getNote() {
return Optional.ofNullable(note);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -168,14 +194,16 @@ public boolean equals(Object other) {

EditTaskDescriptor otherEditTaskDescriptor = (EditTaskDescriptor) other;
return Objects.equals(title, otherEditTaskDescriptor.title)
&& Objects.equals(note, otherEditTaskDescriptor.note);
&& Objects.equals(note, otherEditTaskDescriptor.note)
&& Objects.equals(tags, otherEditTaskDescriptor.tags);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("title", title)
.add("note", note)
.add("tags", tags)
.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_TITLE;

import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Note;
import seedu.address.model.task.Task;
import seedu.address.model.task.Title;
Expand All @@ -25,7 +28,8 @@ public class AddTaskCommandParser implements Parser<AddTaskCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddTaskCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TASK_TITLE, PREFIX_TASK_NOTE);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_TASK_TITLE, PREFIX_TASK_NOTE, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_TASK_TITLE, PREFIX_TASK_NOTE)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -35,8 +39,9 @@ public AddTaskCommand parse(String args) throws ParseException {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TASK_TITLE, PREFIX_TASK_NOTE);
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TASK_TITLE).get());
Note note = ParserUtil.parseNote(argMultimap.getValue(PREFIX_TASK_NOTE).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Task task = new Task(title, note);
Task task = new Task(title, note, tagList);

return new AddTaskCommand(task);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_TASK_NOTE = new Prefix("n/");
public static final Prefix PREFIX_TASK_TITLE = new Prefix("t/");
public static final Prefix PREFIX_TASK_TITLE = new Prefix("T/");

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_TITLE;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditTaskCommand;
import seedu.address.logic.commands.EditTaskCommand.EditTaskDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new EditTaskCommand object
Expand All @@ -23,7 +30,8 @@ public class EditTaskCommandParser implements Parser<EditTaskCommand> {
*/
public EditTaskCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TASK_TITLE, PREFIX_TASK_NOTE);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_TASK_TITLE, PREFIX_TASK_NOTE, PREFIX_TAG);

Index index;

Expand All @@ -45,10 +53,27 @@ public EditTaskCommand parse(String args) throws ParseException {
editTaskDescriptor.setNote(ParserUtil.parseNote(argMultimap.getValue(PREFIX_TASK_NOTE).get()));
}

parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editTaskDescriptor::setTags);

if (!editTaskDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditTaskCommand.MESSAGE_NOT_EDITED);
}

return new EditTaskCommand(index, editTaskDescriptor);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
}
}
38 changes: 28 additions & 10 deletions src/main/java/seedu/address/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;

/**
* Represents a Task in the task list.
Expand All @@ -16,17 +20,19 @@ public class Task {
private final Title title;
private final Note note;
private Status status;
private final Set<Tag> tags = new HashSet<>();

/**
* A Task consists of a title and a note.
* Both fields must be present and not null.
* A Task consists of a title, a note, a status and tags.
* All fields must be present and not null.
* Tasks will be default not done when created.
*/
public Task(Title title, Note note) {
requireAllNonNull(title, note);
public Task(Title title, Note note, Set<Tag> tags) {
requireAllNonNull(title, note, tags);
this.title = title;
this.note = note;
this.status = new Status(Status.TaskStatus.NOT_DONE);
this.tags.addAll(tags);
}

/**
Expand All @@ -35,12 +41,14 @@ public Task(Title title, Note note) {
* @param title The title of the task. Must not be null.
* @param note The note associated with the task. Must not be null.
* @param status The status of the task. Must not be null.
* @param tags The tags linked to the task. Must not be null.
*/
public Task(Title title, Note note, Status status) {
public Task(Title title, Note note, Status status, Set<Tag> tags) {
requireAllNonNull(title, note);
this.title = title;
this.note = note;
this.status = status;
this.tags.addAll(tags);
}

public Title getTitle() {
Expand All @@ -59,14 +67,22 @@ public Status getStatus() {
* Updates the Status of the Task as Done.
*/
public Task markDone() {
return new Task(title, note, new Status(Status.TaskStatus.DONE));
return new Task(title, note, new Status(Status.TaskStatus.DONE), tags);
}

/**
* Updates the Status of the Task as not Done.
*/
public Task unmarkDone() {
return new Task(title, note, new Status(Status.TaskStatus.NOT_DONE));
return new Task(title, note, new Status(Status.TaskStatus.NOT_DONE), tags);
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}

/**
Expand All @@ -84,7 +100,7 @@ public boolean isSameTask(Task otherTask) {
}

/**
* Returns true if both tasks have the same title, note and status.
* Returns true if both tasks have the same title, note, status and tags.
*/
@Override
public boolean equals(Object other) {
Expand All @@ -100,13 +116,14 @@ public boolean equals(Object other) {
Task otherTask = (Task) other;
return title.equals(otherTask.title)
&& note.equals(otherTask.note)
&& status.equals(otherTask.status);
&& status.equals(otherTask.status)
&& tags.equals(otherTask.tags);
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(title, note, status);
return Objects.hash(title, note, status, tags);
}

@Override
Expand All @@ -115,6 +132,7 @@ public String toString() {
.add("title", title)
.add("note", note)
.add("status", status)
.add("tags", tags)
.toString();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static Person[] getSamplePersons() {

public static Task[] getSampleTasks() {
return new Task[] {
new Task(new Title("Buy Flowers"), new Note("from Gina")),
new Task(new Title("Buy Milk"), new Note("from NTUC"))
new Task(new Title("Find caterer"), new Note("for 221 students"), getTagSet("caterer")),
new Task(new Title("Create budget"), new Note("for CS2102"), getTagSet("finance", "class"))
};
}

Expand Down
Loading

0 comments on commit 21bf265

Please sign in to comment.