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

Branch c update #5

Merged
merged 3 commits into from
Sep 13, 2024
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
24 changes: 24 additions & 0 deletions src/main/java/gopher/Gopher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.format.DateTimeParseException;

import gopher.exception.EmptyTaskDescriptionException;
import gopher.exception.InvalidTokenException;
import gopher.exception.MissingTokenException;
import gopher.exception.UnknownCommandException;
import gopher.parser.Parser;
Expand Down Expand Up @@ -133,6 +134,27 @@ public static String executeCreateTaskCommand(String userInput)
}
}

/**
* Executes the relevant actions when user input update task command.
*
* @param userInput command input by the user
* @return response by gopher after successful action
*/
public static String executeUpdateTaskCommand(String userInput) {
try {
String[] tokens = userInput.split(" ");
if (tokens.length < 2) {
return "Update command cannot be empty";
}
taskList.update(tokens);
return "Hi I have updated this task for you already!";
} catch (DateTimeParseException e) {
return UI.getInvalidDateWarning();
} catch (InvalidTokenException e) {
return e.getMessage();
}
}

/**
* Start the main event loop.
*/
Expand All @@ -152,6 +174,8 @@ public static String getResponse(String userInput)
return executeFindTaskCommand(userInput);
} else if (Parser.isValidTaskType(userInput.split(" ")[0])) {
return executeCreateTaskCommand(userInput);
} else if (userInput.toLowerCase().startsWith("update")) {
return executeUpdateTaskCommand(userInput);
} else {
throw new UnknownCommandException(userInput);
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/gopher/exception/InvalidTokenException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gopher.exception;

/**
* Thrown if the given command contains tokens that cannot be accepted
* by the task that is involved in the operation.
*/
public class InvalidTokenException extends Exception {
private String taskType;
private String token;

/**
* Constructor for InvalidTokenException.
*
* @param taskType type of task involved in the operation
* @param token token that triggers the exception
*/
public InvalidTokenException(String taskType, String token) {
this.taskType = taskType;
this.token = token;
}

@Override
public String getMessage() {
return String.format("Sorry, %s task cannot accept %s token as part of command\n"
+ "Tokens supported by %s are: //to be done",
this.taskType,
this.token,
this.taskType);
}
}
149 changes: 149 additions & 0 deletions src/main/java/gopher/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import gopher.exception.EmptyTaskDescriptionException;
import gopher.exception.FileCorruptedException;
import gopher.exception.InvalidTokenException;
import gopher.exception.MissingTokenException;
import gopher.exception.UnknownCommandException;
import gopher.task.Deadline;
Expand Down Expand Up @@ -230,6 +231,154 @@ public static Task parseCreateTaskCommand(String command)
}
}

/**
* Parses update todo task command.
*
* @param tokens tokens within the given update command
* @return new name to be updated
*/
public static String parseUpdateTodoTaskCommand(String[] tokens)
throws InvalidTokenException {
StringBuilder taskName = new StringBuilder();
for (int i = 2; i < tokens.length; i++) {
// If other tasks tokens are used, remind user that
// he/she may be updating the undesired task
if (tokens[i].startsWith("/")) {
throw new InvalidTokenException("todo", tokens[i]);
}

// Append the token if everything is fine
taskName.append(tokens[i]);
if (i < tokens.length - 1) {
taskName.append(" ");
}
}

return taskName.toString();
}

/**
* Parses update deadline task command.
*
* @param tokens tokens within the given command
* @return a String array of size 2, the first element is the task name,
* the second element is the due date in date input format
* If a certain field is not provided in the given command, the
* corresponding element in the array will be empty String
*/
public static String[] parseUpdateDeadlineTaskCommand(String[] tokens)
throws InvalidTokenException {
String[] result = new String[]{"", ""};
// Determine the position of /by token in update command tokens
// Check for any invalid tokens as well
int byTokenIndex = -1;
for (int i = 2; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("/by")) {
byTokenIndex = i;
} else if (tokens[i].startsWith("/")) {
throw new InvalidTokenException("deadline", tokens[i]);
}
}

// Update task name based on the position of /by token
StringBuilder taskName = new StringBuilder();
int taskNameIndexLimit = byTokenIndex != -1 ? byTokenIndex : tokens.length;
for (int i = 2; i < taskNameIndexLimit; i++) {
taskName.append(tokens[i]);
if (i < taskNameIndexLimit - 1) {
taskName.append(" ");
}
}
result[0] = taskName.toString();

// Update due date if /by token is found in command tokens
if (byTokenIndex != -1) {
StringBuilder dueDateString = new StringBuilder();
for (int i = byTokenIndex + 1; i < tokens.length; i++) {
dueDateString.append(tokens[i]);
if (i < tokens.length - 1) {
dueDateString.append(" ");
}
}
result[1] = dueDateString.toString();
}

return result;
}

/**
* Parses update event task command.
*
* @param tokens tokens within the given update command
* @return a String array of size 3, the first element is the task name,
* the second element is the start date in date input format,
* the third element is the end date in date input format
* If the update command does not contain a certain part, the
* corresponding element would be empty String
*/
public static String[] parseUpdateEventTaskCommand(String[] tokens)
throws InvalidTokenException {
String[] result = new String[]{"", "", ""};

// Determine the positions of from & to tokens if they exist in the command tokens
// Check for any invalid token as well
int fromTokenIndex = -1;
int toTokenIndex = -1;
for (int i = 2; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("/from")) {
fromTokenIndex = i;
} else if (tokens[i].equalsIgnoreCase("/to")) {
toTokenIndex = i;
} else if (tokens[i].startsWith("/")) {
throw new InvalidTokenException("event", tokens[i]);
}
}

// Update task name based on the position of /from and /to token
StringBuilder taskName = new StringBuilder();
int taskNameIndexLimit = fromTokenIndex != -1
? fromTokenIndex
: toTokenIndex != -1
? toTokenIndex
: tokens.length;
for (int i = 2; i < taskNameIndexLimit; i++) {
taskName.append(tokens[i]);
if (i < taskNameIndexLimit - 1) {
taskName.append(" ");
}
}
result[0] = taskName.toString();

// Update start date if /from token exists in the command tokens
if (fromTokenIndex != -1) {
StringBuilder startDateString = new StringBuilder();
int startDateIndexLimit = toTokenIndex != -1
? toTokenIndex
: tokens.length;
for (int i = taskNameIndexLimit + 1; i < startDateIndexLimit; i++) {
startDateString.append(tokens[i]);
if (i < startDateIndexLimit - 1) {
startDateString.append(" ");
}
}
result[1] = startDateString.toString();
}

// Update end date if /to token exists in the command tokens
if (toTokenIndex != -1) {
StringBuilder endDateString = new StringBuilder();
for (int i = toTokenIndex + 1; i < tokens.length; i++) {
endDateString.append(tokens[i]);
if (i < tokens.length - 1) {
endDateString.append(" ");
}
}
result[2] = endDateString.toString();
}

return result;
}

/**
* Parses saved task data to ArrayList of Task
*
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/gopher/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDateTime;

import gopher.exception.InvalidTokenException;
import gopher.parser.Parser;

/**
Expand All @@ -22,6 +23,22 @@ public Deadline(String name, String dueDate) {
this.dueDate = Parser.parseDateString(dueDate);
}

@Override
public void update(String[] tokens) throws InvalidTokenException {
String[] parsedResult = Parser.parseUpdateDeadlineTaskCommand(tokens);

String taskName = parsedResult[0];
String dueDateString = parsedResult[1];

if (!taskName.isEmpty()) {
this.name = taskName;
}

if (!dueDateString.isEmpty()) {
this.dueDate = Parser.parseDateString(dueDateString);
}
}

@Override
public String getSaveMessage() {
return String.format("D | %s | %s | %s",
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/gopher/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDateTime;

import gopher.exception.InvalidTokenException;
import gopher.parser.Parser;

/**
Expand All @@ -25,6 +26,27 @@ public Event(String name, String startDate, String endDate) {
this.endDate = Parser.parseDateString(endDate);
}

@Override
public void update(String[] tokens) throws InvalidTokenException {
String[] parsedResult = Parser.parseUpdateEventTaskCommand(tokens);

String taskName = parsedResult[0];
String startDateString = parsedResult[1];
String endDateString = parsedResult[2];

if (!taskName.isEmpty()) {
this.name = taskName;
}

if (!startDateString.isEmpty()) {
this.startDate = Parser.parseDateString(startDateString);
}

if (!endDateString.isEmpty()) {
this.endDate = Parser.parseDateString(endDateString);
}
}

@Override
public String getSaveMessage() {
return String.format("E | %s | %s | %s | %s",
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/gopher/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gopher.task;

import gopher.exception.EmptyTaskDescriptionException;
import gopher.exception.InvalidTokenException;
import gopher.exception.MissingTokenException;
import gopher.exception.UnknownCommandException;
import gopher.parser.Parser;
Expand Down Expand Up @@ -42,6 +43,14 @@ public static Task of(String command) throws UnknownCommandException,
return Parser.parseCreateTaskCommand(command);
}

/**
* Updates the relevant detail in the specified task.
*
* @param tokens tokens within the given update task command
* @throws InvalidTokenException if an invalid token exists in the given command
*/
public abstract void update(String[] tokens) throws InvalidTokenException;

/**
* Gets the save file message representation of this task.
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gopher/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.regex.Pattern;

import gopher.exception.InvalidTokenException;
import gopher.storage.TaskManager;

/**
Expand Down Expand Up @@ -39,6 +40,18 @@ public void add(Task task) {
TaskManager.saveTasks(tasks);
}

/**
* Updates the task with the given task number with the relevant information.
*
* @param tokens tokens from the update command
*/
public void update(String[] tokens)
throws InvalidTokenException {
int taskNumber = Integer.parseInt(tokens[1]);
this.getTask(taskNumber).update(tokens);
TaskManager.saveTasks(tasks);
}

/**
* Deletes the task with the given task number from the task list.
* Triggers the TaskManager to update the local saved tasks.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/gopher/task/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gopher.task;

import gopher.exception.InvalidTokenException;
import gopher.parser.Parser;

/**
* Represents the todo Task.
* Only includes task description.
Expand All @@ -14,6 +17,15 @@ public ToDo(String name) {
super(name);
}

@Override
public void update(String[] tokens) throws InvalidTokenException {
// Extract task name from the given tokens
String taskName = Parser.parseUpdateTodoTaskCommand(tokens);
if (!taskName.isEmpty()) {
this.name = taskName;
}
}

@Override
public String getSaveMessage() {
return String.format("T | %s | %s",
Expand Down
Loading