Skip to content

Commit

Permalink
extension: tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
bbryant824 committed Sep 13, 2024
1 parent 414e01c commit d67dc23
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 113 deletions.
9 changes: 6 additions & 3 deletions data/kobee.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
T | 0 | shoot
D | 0 | eatshiet | 2002-11-11 2020
T | 1 | 777
T | 0 | shoot | fun
D | 0 | eatshiet | 2002-11-11 2020 | 2002-11-11 2020
T | 1 | 777 | lousy
T | 0 | read book
T | 0 | eat fun
T | 0 | ryan eat dog
19 changes: 5 additions & 14 deletions src/main/java/kobe/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
package kobe.command;

import kobe.task.Task;
import kobe.task.TaskList;
import kobe.util.Storage;
import kobe.util.Ui;

/**
* Represents a command to find tasks based on a keyword in the task list of the Duke chatbot application.
* Represents a command to find tasks based on a keyword or tag.
*/
public class FindCommand extends Command {
private final String keyword;

/**
* Constructs a FindCommand with the specified keyword.
*
* @param keyword The keyword to search for in task descriptions.
*/
public FindCommand(String keyword) {
this.keyword = keyword;
}

/**
* Executes the find command, which displays tasks that match the keyword.
*
* @param tasks The TaskList object containing all tasks.
* @param ui The Ui object responsible for user interactions.
* @param storage The Storage object responsible for saving and loading tasks (not used in this command).
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
TaskList matchingTasks = tasks.findTasks(keyword);
TaskList matchingTasks = keyword.startsWith("#")
? tasks.findTasksByTag(keyword.substring(1))
: tasks.findTasks(keyword);
ui.setResponse("Here are the matching tasks in your list:\n" + matchingTasks.getAllTasksAsString());
}
}
3 changes: 2 additions & 1 deletion src/main/java/kobe/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public Deadline(String name, LocalDateTime byWhen) {
@Override
public String toFileFormat() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
return "D | " + (isDone ? "1" : "0") + " | " + name + " | " + byWhen.format(formatter);
return "D | " + (isDone ? "1" : "0") + " | " + name + " | " + byWhen.format(formatter)
+ (getTags().isEmpty() ? "" : " | " + String.join(",", getTags()));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/kobe/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public Event(String name, LocalDateTime from, LocalDateTime to) {
@Override
public String toFileFormat() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
return "E | " + (isDone ? "1" : "0") + " | " + name + " | " + from.format(formatter) + " | " + to.format(formatter);
return "E | " + (isDone ? "1" : "0") + " | " + name + " | " + from.format(formatter) + " | " + to.format(formatter)
+ (getTags().isEmpty() ? "" : " | " + String.join(",", getTags()));
}

/**
Expand Down
59 changes: 47 additions & 12 deletions src/main/java/kobe/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package kobe.task;

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

/**
* Represents a general task in the Duke chatbot application.
* A task has a name and can be marked as done or not done.
*/
public class Task {
/** The name or description of the task. */
public final String name;

/** Indicates whether the task is done. */
public boolean isDone;
private List<String> tags;

/**
* Constructs a Task with the specified name and sets the task as not done by default.
Expand All @@ -19,6 +20,7 @@ public class Task {
public Task(String name) {
this.name = name;
this.isDone = false; // Default is not done
this.tags = new ArrayList<>();
}

/**
Expand All @@ -36,31 +38,64 @@ public void markAsNotDone() {
}

/**
* Returns a string representation indicating whether the task is done or not.
* Adds a tag to the task.
*
* @return "[X]" if the task is done, "[ ]" otherwise.
* @param tag The tag to be added.
*/
public String showDoneOrNot() {
return (isDone ? "[X]" : "[ ]"); // Return X or space depending on isDone
public void addTag(String tag) {
if (!tags.contains(tag)) {
tags.add(tag);
}
}

/**
* Returns the string representation of the task for saving to a file.
* This method provides a basic file format that differentiates between different types of tasks.
* Gets the list of tags for the task.
*
* @return The list of tags.
*/
public List<String> getTags() {
return tags;
}

/**
* Checks if the task contains a specific tag.
*
* @param tag The tag to check for.
* @return true if the tag exists, false otherwise.
*/
public boolean hasTag(String tag) {
return tags.contains(tag);
}

/**
* Returns a string representation of the task for saving to a file.
*
* @return A formatted string representing the task in a file-friendly format.
*/
public String toFileFormat() {
return (this instanceof Todo ? "T" : (this instanceof Deadline ? "D" : "E")) + " | " + (isDone ? "1" : "0") + " | " + name;
return (this instanceof Todo ? "T" : (this instanceof Deadline ? "D" : "E"))
+ " | " + (isDone ? "1" : "0")
+ " | " + name
+ " | " + String.join(",", tags); // Include tags in the file format
}

/**
* Returns the string representation of the task, including its completion status.
* Returns the string representation of the task, including its completion status and tags.
*
* @return A formatted string representing the task.
*/
@Override
public String toString() {
return showDoneOrNot() + " " + name;
String tagsString = tags.isEmpty() ? "" : " #" + String.join(" #", tags);
return showDoneOrNot() + " " + name + tagsString;
}

/**
* Returns a string representation indicating whether the task is done or not.
*
* @return "[X]" if the task is done, "[ ]" otherwise.
*/
public String showDoneOrNot() {
return (isDone ? "[X]" : "[ ]"); // Return X or space depending on isDone
}
}
78 changes: 23 additions & 55 deletions src/main/java/kobe/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,117 +3,85 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Represents a list of tasks in the Kobe chatbot application.
* Provides methods to manipulate tasks in the list.
* Provides methods to manipulate tasks in the list, including searching by tags.
*/
public class TaskList {
private final List<Task> tasks;

/**
* Constructs an empty TaskList.
*/
public TaskList() {
this.tasks = new ArrayList<>();
}

/**
* Constructs a TaskList with the specified list of tasks.
*
* @param tasks The list of tasks to initialize the TaskList with.
*/
public TaskList(List<Task> tasks) {
this.tasks = tasks;
}

/**
* Adds multiple tasks to the task list using varargs.
*
* @param tasksToAdd The tasks to be added.
*/
public void addTask(Task... tasksToAdd) {
for (Task t : tasksToAdd) {
tasks.add(t);
}
}

/**
* Removes the task at the specified index from the task list.
*
* @param index The index of the task to be removed.
* @return The task that was removed.
*/
public Task removeTask(int index) {
return tasks.remove(index);
}

/**
* Returns the task at the specified index in the task list.
*
* @param index The index of the task to return.
* @return The task at the specified index.
*/
public Task getTask(int index) {
return tasks.get(index);
}

/**
* Returns the number of tasks in the task list.
*
* @return The number of tasks in the list.
*/
public int size() {
return tasks.size();
}

/**
* Checks if the task list is empty.
*
* @return {@code true} if the task list is empty; {@code false} otherwise.
*/
public boolean isEmpty() {
return tasks.isEmpty();
}

/**
* Returns the list of tasks.
*
* @return The list of tasks.
*/
public List<Task> getTasks() {
return tasks;
}

/**
* Finds tasks that contain the specified keyword.
* Finds tasks that contain the specified keyword or tag.
*
* @param keyword The keyword to search for.
* @param keyword The keyword or tag to search for.
* @return A TaskList containing the matching tasks.
*/
public TaskList findTasks(String keyword) {
List<Task> matchingTasks = new ArrayList<>();
for (Task task : tasks) {
if (task.name.contains(keyword)) {
matchingTasks.add(task);
}
}
List<Task> matchingTasks = tasks.stream()
.filter(task -> task.name.contains(keyword) || task.hasTag(keyword))
.collect(Collectors.toList());
return new TaskList(matchingTasks);
}

/**
* Returns all tasks as a string.
* Finds tasks that contain the specified tag.
*
* @return The string representation of all tasks.
* This method filters the tasks in the task list and returns a new TaskList
* containing only the tasks that have the given tag.
*
* @param tag The tag to search for in the tasks.
* @return A TaskList containing the tasks that have the specified tag.
*/
public TaskList findTasksByTag(String tag) {
List<Task> matchingTasks = tasks.stream()
.filter(task -> task.hasTag(tag))
.collect(Collectors.toList());
return new TaskList(matchingTasks);
}



public String getAllTasksAsString() {
if (tasks.isEmpty()) {
return "Your task list is currently empty.";
}
return IntStream.range(0, tasks.size())
.mapToObj(i -> (i + 1) + ". " + tasks.get(i).toString())
return tasks.stream()
.map(task -> (tasks.indexOf(task) + 1) + ". " + task.toString())
.collect(Collectors.joining("\n"));
}

}
2 changes: 1 addition & 1 deletion src/main/java/kobe/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Todo(String name) {
*/
@Override
public String toFileFormat() {
return "T | " + (isDone ? "1" : "0") + " | " + name;
return "T | " + (isDone ? "1" : "0") + " | " + name + (getTags().isEmpty() ? "" : " | " + String.join(",", getTags()));
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/kobe/util/Parser.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package kobe.util;

import kobe.KobeException;
import kobe.command.*;
import kobe.KobeException;

/**
* Parses user input and converts it into a command to be executed by the Kobe chatbot application.
* The Parser class helps interpret the user's commands and instantiate the appropriate Command objects.
*/
public class Parser {

/**
* Parses the user input into a specific Command object.
*
* @param fullCommand The full user input string to parse.
* @return A Command object corresponding to the user's input.
* @throws Exception If the user input is invalid or cannot be parsed into a known command.
*/
public static Command parse(String fullCommand) throws Exception {
String[] words = fullCommand.split(" ", 2);
String commandWord = words[0];
Expand All @@ -32,13 +24,17 @@ public static Command parse(String fullCommand) throws Exception {
case "delete":
return new DeleteCommand(Integer.parseInt(words[1]));
case "todo":
return new AddCommand(fullCommand);
case "deadline":
return new AddCommand(fullCommand);
case "event":
return new AddCommand(fullCommand);
case "find":
return new FindCommand(words[1]);
case "tag":
String[] tagParts = words[1].split(" ", 2);
if (tagParts.length != 2) {
throw new KobeException("Invalid tag command. Use: tag <task number> <#tag>");
}
return new TagCommand(Integer.parseInt(tagParts[0]), tagParts[1].replace("#", ""));
default:
throw new KobeException("I'm sorry, but I don't know what that means :-(");
}
Expand Down
Loading

0 comments on commit d67dc23

Please sign in to comment.