Skip to content

Commit

Permalink
Add code to list tasks that occurs at a specific date
Browse files Browse the repository at this point in the history
  • Loading branch information
xzynos committed Sep 27, 2022
1 parent 9b4301e commit 4b74e9a
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/main/java/duke/commands/ConsoleCommandDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ConsoleCommandDelete extends ConsoleCommand {
/**
* Initializes object with arguments for delete command.
*
* @param taskNumber Task number of the task as listed by the function {@link duke.data.task.TaskManager#printTasks()}.
* @param taskNumber Task number of the task as listed by the function {@link duke.userinterface.ConsoleInterface#executeCommandList(ConsoleCommandList)}.
*/
public ConsoleCommandDelete(int taskNumber) {
this.taskNumber = taskNumber;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/duke/commands/ConsoleCommandList.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
package duke.commands;

import java.time.LocalDate;

/**
* Stores arguments for list command.
*/
public class ConsoleCommandList extends ConsoleCommand {
private LocalDate date;

/**
* Initializes object for list command.
*/
public ConsoleCommandList() {
}

/**
* Initializes object with arguments for list command.
*
* @param date Date.
*/
public ConsoleCommandList(LocalDate date) {
this.date = date;
}

public LocalDate getDate() {
return date;
}
}
2 changes: 1 addition & 1 deletion src/main/java/duke/commands/ConsoleCommandMark.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ConsoleCommandMark extends ConsoleCommand {
/**
* Initializes object with arguments for mark command.
*
* @param taskNumber Task number of the task as listed by the function {@link duke.data.task.TaskManager#printTasks()}.
* @param taskNumber Task number of the task as listed by the function {@link duke.userinterface.ConsoleInterface#executeCommandList(ConsoleCommandList)}.
*/
public ConsoleCommandMark(int taskNumber) {
this.taskNumber = taskNumber;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/commands/ConsoleCommandUnmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ConsoleCommandUnmark extends ConsoleCommand {
/**
* Initializes object with arguments for unmark command.
*
* @param taskNumber Task number of the task as listed by the function {@link duke.data.task.TaskManager#printTasks()}.
* @param taskNumber Task number of the task as listed by the function {@link duke.userinterface.ConsoleInterface#executeCommandList(ConsoleCommandList)}.
*/
public ConsoleCommandUnmark(int taskNumber) {
this.taskNumber = taskNumber;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/common/Configurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public class Configurations {
public static final String LOCAL_STORAGE_TASKS_DIRECTORY_PATH = "./data/";
public static final String LOCAL_STORAGE_TASKS_FILENAME = "tasks.txt";
public static final String CONSOLE_INTERFACE_DATE_FORMAT = "dd MMM yyyy";
}
7 changes: 5 additions & 2 deletions src/main/java/duke/common/Messages.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.common;

import duke.data.task.Task;

/**
* Defines messages used by program.
*/
Expand All @@ -8,10 +10,11 @@ public class Messages {
public static final String CONSOLE_MESSAGE_GOODBYE = "Bye. Hope to see you again soon!";
public static final String CONSOLE_ERROR_ARGUMENT_NOT_INTEGER = "The argument provided is not a valid integer.";
public static final String CONSOLE_ERROR_COMMAND_NOT_FOUND = "I'm sorry, but I don't know what that means :-(";
public static final String CONSOLE_ERROR_COMMAND_LIST_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: list [" + Task.DATE_FORMAT + "]";
public static final String CONSOLE_ERROR_COMMAND_TODO_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: todo DESCRIPTION";
public static final String CONSOLE_ERROR_COMMAND_TODO_FORBIDDEN_CHARACTERS = "The arguments provided contain forbidden characters.";
public static final String CONSOLE_ERROR_COMMAND_DEADLINE_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: deadline DESCRIPTION /by " + Task.DATE_TIME_FORMAT;
public static final String CONSOLE_ERROR_COMMAND_DEADLINE_FORBIDDEN_CHARACTERS = "The arguments provided contain forbidden characters.";
public static final String CONSOLE_ERROR_COMMAND_EVENT_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: event DESCRIPTION /at " + Task.DATE_TIME_FORMAT + " " + Task.DATE_TIME_FORMAT;
public static final String CONSOLE_ERROR_COMMAND_EVENT_FORBIDDEN_CHARACTERS = "The arguments provided contain forbidden characters.";
public static final String CONSOLE_ERROR_COMMAND_DEADLINE_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: deadline DESCRIPTION /by dd/MM/yyyy HHmm";
public static final String CONSOLE_ERROR_COMMAND_EVENT_INVALID_SYNTAX = "The arguments are invalid. SYNTAX: event DESCRIPTION /at dd/MM/yyyy HHmm dd/MM/yyyy HHmm";
}
1 change: 1 addition & 0 deletions src/main/java/duke/data/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Stores tasks.
*/
public class Task {
public static final String DATE_FORMAT = "dd/MM/yyyy";
public static final String DATE_TIME_FORMAT = "dd/MM/yyyy HHmm";

protected String description;
Expand Down
80 changes: 52 additions & 28 deletions src/main/java/duke/data/task/TaskManager.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package duke.data.task;

import duke.common.Configurations;
import duke.exceptions.TaskManagerException;
import duke.storage.LocalStorage;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;

/**
* Stores tasks and manages task-list.
*/
@SuppressWarnings({"FieldMayBeFinal", "unused"})
@SuppressWarnings({"FieldMayBeFinal", "unused", "PatternVariableCanBeUsed", "StatementWithEmptyBody"})
public class TaskManager {
public final String DEFAULT_TASKS_PATH = "./data/";
public final String DEFAULT_TASKS_FILENAME = "tasks.txt";

private ArrayList<Task> tasks;
private String tasksPath;
private String tasksFilename;
Expand All @@ -23,8 +22,8 @@ public class TaskManager {
*/
public TaskManager() {
this.tasks = new ArrayList<>();
this.tasksPath = DEFAULT_TASKS_PATH;
this.tasksFilename = DEFAULT_TASKS_FILENAME;
this.tasksPath = Configurations.LOCAL_STORAGE_TASKS_DIRECTORY_PATH;
this.tasksFilename = Configurations.LOCAL_STORAGE_TASKS_FILENAME;
}

/**
Expand All @@ -40,16 +39,45 @@ public TaskManager(String tasksPath, String tasksFilename) {
}

/**
* Prints all tasks in list to standard out.
* Returns list of task in task manager.
*
* @return List of tasks.
*/
public void printTasks() {
for (int i = 0; i < tasks.size(); i++) {
int taskNumber = i + 1;
System.out.print(taskNumber + ".");
public ArrayList<Task> getTasks() {
return new ArrayList<>(tasks);
}

Task task = tasks.get(i);
task.print();
/**
* Returns list of tasks in task manager that occurs at a certain date.
*
* @return List of tasks.
*/
public ArrayList<Task> getTasks(LocalDate date) {
if (date == null) {
return getTasks();
}

ArrayList<Task> tasksWithDate = new ArrayList<>();
for (Task task : tasks) {
if (task instanceof Deadline) {
Deadline deadline = (Deadline) task;
LocalDate byDate = deadline.getBy().toLocalDate();
if (date.equals(byDate)) {
tasksWithDate.add(deadline);
}
} else if (task instanceof Event) {
Event event = (Event) task;
LocalDate startAtDate = event.getStartAt().toLocalDate();
LocalDate endAtDate = event.getEndAt().toLocalDate();
if (!date.isBefore(startAtDate) && !date.isAfter(endAtDate)) {
tasksWithDate.add(event);
}
} else {
// Do nothing if the current task is not an instance of the objects in the clauses above;
}
}

return tasksWithDate;
}

/**
Expand All @@ -73,12 +101,11 @@ public void addTask(Task task) {
/**
* Gets a task from list of tasks.
*
* @param taskNumber Task number of task as shown by the function {@link #printTasks()}.
* @param taskIndex Task index.
* @return Task.
* @throws TaskManagerException.TaskNotFoundException If task is not the task manager.
*/
public Task getTask(int taskNumber) throws TaskManagerException.TaskNotFoundException {
int taskIndex = taskNumber - 1;
public Task getTask(int taskIndex) throws TaskManagerException.TaskNotFoundException {
try {
return tasks.get(taskIndex);
} catch (IndexOutOfBoundsException e) {
Expand All @@ -90,21 +117,20 @@ public Task getTask(int taskNumber) throws TaskManagerException.TaskNotFoundExce
* Gets task number for task in list of tasks.
*
* @param task Task.
* @return Task number.
* @return Task index.
*/
public int getTaskNumber(Task task) {
return tasks.indexOf(task) + 1;
public int getTaskIndex(Task task) {
return tasks.indexOf(task);
}

/**
* Deletes a task from list of tasks.
*
* @param taskNumber Task number of task as shown by the function {@link #printTasks()}.
* @param taskIndex Task index.
* @return Task.
* @throws TaskManagerException.TaskNotFoundException If task is not the task manager.
*/
public Task deleteTask(int taskNumber) throws TaskManagerException.TaskNotFoundException {
int taskIndex = taskNumber - 1;
public Task deleteTask(int taskIndex) throws TaskManagerException.TaskNotFoundException {
try {
Task task = tasks.get(taskIndex);
tasks.remove(taskIndex);
Expand Down Expand Up @@ -136,11 +162,10 @@ public ArrayList<Task> findTask(String description) {
/**
* Marks a task as completed.
*
* @param taskNumber Task number of task as shown by the function {@link #printTasks()}.
* @param taskIndex Task index.
* @throws TaskManagerException.TaskNotFoundException If task is not the task manager.
*/
public void markTaskAsCompleted(int taskNumber) throws TaskManagerException.TaskNotFoundException {
int taskIndex = taskNumber - 1;
public void markTaskAsCompleted(int taskIndex) throws TaskManagerException.TaskNotFoundException {
try {
Task task = tasks.get(taskIndex);
task.setComplete(true);
Expand All @@ -152,11 +177,10 @@ public void markTaskAsCompleted(int taskNumber) throws TaskManagerException.Task
/**
* Marks a task as uncompleted.
*
* @param taskNumber Task number of task as shown by the function {@link #printTasks()}.
* @param taskIndex Task index.
* @throws TaskManagerException.TaskNotFoundException If task is not the task manager.
*/
public void markTaskAsUncompleted(int taskNumber) throws TaskManagerException.TaskNotFoundException {
int taskIndex = taskNumber - 1;
public void markTaskAsUncompleted(int taskIndex) throws TaskManagerException.TaskNotFoundException {
try {
Task task = tasks.get(taskIndex);
task.setComplete(false);
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/duke/exceptions/ConsoleInputParserException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package duke.exceptions;

import duke.userinterface.ConsoleInputParser;

/**
* Define exceptions thrown by {@link ConsoleInputParser#}.
* Define exceptions thrown by {@link duke.userinterface.ConsoleInputParser#}.
*/
@SuppressWarnings("unused")
public class ConsoleInputParserException {
Expand All @@ -19,6 +17,18 @@ public CommandNotFoundException(String message) {
}
}

/**
* Thrown when command list is not valid.
*/
public static class InvalidCommandListException extends DukeException {
public InvalidCommandListException() {
}

public InvalidCommandListException(String message) {
super(message);
}
}

/**
* Thrown when command mark is not valid.
*/
Expand Down Expand Up @@ -105,7 +115,7 @@ public InvalidCommandFindException(String message) {
}

/**
* Thrown when forbidden characters are found in command todo
* Thrown when forbidden characters are found in command todo.
*/
public static class ForbiddenCharactersCommandTodoException extends DukeException {
public ForbiddenCharactersCommandTodoException() {
Expand All @@ -118,7 +128,7 @@ public ForbiddenCharactersCommandTodoException(String message) {
}

/**
* Thrown when forbidden characters are found in command deadline
* Thrown when forbidden characters are found in command deadline.
*/
public static class ForbiddenCharactersCommandDeadlineException extends DukeException {
public ForbiddenCharactersCommandDeadlineException() {
Expand All @@ -131,7 +141,7 @@ public ForbiddenCharactersCommandDeadlineException(String message) {
}

/**
* Thrown when forbidden characters are found in command event
* Thrown when forbidden characters are found in command event.
*/
public static class ForbiddenCharactersCommandEventException extends DukeException {
public ForbiddenCharactersCommandEventException() {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/duke/exceptions/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package duke.exceptions;

import duke.Duke;

/**
* Define exceptions thrown by {@link Duke#}.
* Define exceptions thrown by {@link duke.Duke#}.
*/
public abstract class DukeException extends Exception {
private String message;
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/duke/exceptions/TaskManagerException.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package duke.exceptions;

import duke.data.task.TaskManager;

/**
* Define exceptions thrown by {@link TaskManager#}.
* Define exceptions thrown by {@link duke.data.task.TaskManager#}.
*/
@SuppressWarnings("unused")
public class TaskManagerException {
/**
* Thrown when a task cannot be found in {@link TaskManager#}.
* Thrown when a task cannot be found in {@link duke.data.task.TaskManager#}.
*/
public static class TaskNotFoundException extends DukeException {
public TaskNotFoundException() {
Expand All @@ -20,7 +18,7 @@ public TaskNotFoundException(String message) {
}

/**
* Thrown when a task cannot be saved by {@link TaskManager#}.
* Thrown when a task cannot be saved by {@link duke.data.task.TaskManager#}.
*/
public static class TasksFileIOException extends DukeException {
public TasksFileIOException() {
Expand Down
Loading

0 comments on commit 4b74e9a

Please sign in to comment.