Skip to content

Commit

Permalink
B-Reminders
Browse files Browse the repository at this point in the history
Add reminders command to show nearest deadline or event.
  • Loading branch information
iyioon committed Sep 13, 2023
1 parent c81d617 commit ae14c8a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import duke.command.FindCommand;
import duke.command.ListCommand;
import duke.command.MarkCommand;
import duke.command.ReminderCommand;
import duke.command.UnmarkCommand;
import duke.exception.DukeException;

Expand Down Expand Up @@ -41,6 +42,8 @@ public static Command parse(String userInput) throws DukeException {
return new DeleteCommand();
case "find":
return new FindCommand();
case "reminders":
return new ReminderCommand();
default:
throw new DukeException("Unknown Command!");
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.ArrayList;

import duke.exception.DukeException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;


Expand Down Expand Up @@ -145,4 +147,22 @@ public String find(String keyword) {
}
return stringBuilder.toString();
}

/**
* Find the nearest deadline or event.
*
* @return list of the nearest task.
* @throws DukeException
*/
public String findNearest() {

StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i) instanceof Deadline || tasks.get(i) instanceof Event) {
stringBuilder.append(i + 1).append(".").append(tasks.get(i).toString());
break;
}
}
return stringBuilder.toString();
}
}
29 changes: 29 additions & 0 deletions src/main/java/duke/command/ReminderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package duke.command;

import duke.TaskList;
import duke.Ui;
import duke.exception.DukeException;

/**
* FindCommand to search for a task in the taskList.
*/
public class ReminderCommand implements Command {

/**
* Find the nearest dated task.
* @param tasks The task list that may be modified by the command.
* @param ui The user interface for analyzing chat history.
* @return false as it should not exit.
* @throws DukeException when error occurs.
*/
@Override
public boolean execute(TaskList tasks, Ui ui) throws DukeException {
String output = tasks.findNearest();
if (output.trim().length() == 0) {
ui.respond("You don't have any events or deadlines.");
} else {
ui.respond("Reminder: your upcoming task:" + "\n" + tasks.findNearest());
}
return false;
}
}

0 comments on commit ae14c8a

Please sign in to comment.