Skip to content

Commit

Permalink
Implement basic update task feature to fulfill C-Update extension
Browse files Browse the repository at this point in the history
User can now use update [task number] taskName [tokens] field value to
specify the fields within the task that they want to update.

For example: update 4 Hello /by 2024-05-11 changes the 4-th task's name
in the task list to Hello. If the task is Deadline, then change its
due date to May 11 2024, else a warning message will be prompted by
gopher
  • Loading branch information
MAOXIONGKAI committed Sep 13, 2024
1 parent 9af2dae commit 29854b2
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/gopher/Gopher.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ 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";
}
int taskNumber = Integer.parseInt(tokens[1]);
taskList.update(taskNumber, tokens);
return "Hi I have updated this task for you already!";
} catch (DateTimeParseException e) {
return UI.getInvalidDateWarning();
}
}

/**
* Start the main event loop.
*/
Expand All @@ -152,6 +172,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
36 changes: 36 additions & 0 deletions src/main/java/gopher/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,42 @@ public Deadline(String name, String dueDate) {
this.dueDate = Parser.parseDateString(dueDate);
}

@Override
public void update(String[] tokens) {
// Determine the position of /by token in update command tokens
int byTokenIndex = -1;
for (int i = 2; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("/by")) {
byTokenIndex = 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(" ");
}
}
if (!taskName.isEmpty()) {
this.name = 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(" ");
}
}
this.dueDate = Parser.parseDateString(dueDateString.toString());
}
}

@Override
public String getSaveMessage() {
return String.format("D | %s | %s | %s",
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/gopher/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,65 @@ public Event(String name, String startDate, String endDate) {
this.endDate = Parser.parseDateString(endDate);
}

@Override
public void update(String[] tokens) {
// Determine the positions of from & to tokens if they exist in the command tokens
int fromTokenIndex = -1;
int toTokenIndex = -1;
for (int i = 2; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("/from")) {
fromTokenIndex = i;
}
if (tokens[i].equalsIgnoreCase("/to")) {
toTokenIndex = 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(" ");
}
}
if (!taskName.isEmpty()) {
this.name = 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(" ");
}
}
this.startDate = Parser.parseDateString(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(" ");
}
}
this.endDate = Parser.parseDateString(endDateString.toString());
}
}

@Override
public String getSaveMessage() {
return String.format("E | %s | %s | %s | %s",
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/gopher/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public static Task of(String command) throws UnknownCommandException,
return Parser.parseCreateTaskCommand(command);
}

/**
* Updates the relevant detail in the specified task.
*/
public abstract void update(String[] tokens);

/**
* Gets the save file message representation of this task.
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/gopher/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public void add(Task task) {
TaskManager.saveTasks(tasks);
}

/**
* Updates the task with the given task number with the relevant information.
*
* @param taskNumber number of the task to be updated
* @param tokens tokens from the update command
*/
public void update(int taskNumber, String[] tokens) {
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
Expand Up @@ -14,6 +14,18 @@ public ToDo(String name) {
super(name);
}

@Override
public void update(String[] tokens) {
StringBuilder taskName = new StringBuilder();
for (int i = 2; i < tokens.length; i++) {
taskName.append(tokens[i]);
if (i < tokens.length - 1) {
taskName.append(" ");
}
}
this.name = taskName.toString();
}

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

0 comments on commit 29854b2

Please sign in to comment.