Skip to content

Commit

Permalink
Add EditCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Leb14 committed Sep 18, 2023
1 parent 146ba84 commit 6bff02a
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 7 deletions.
7 changes: 4 additions & 3 deletions data/TaskList.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
T | 0 | sleep nia
T | 1 | sleeeeeeep la la
T | 0 | byebye
T | 0 | gogogo
D | 0 | sleep | 23 Aug 2023 6:00PM
E | 0 | sleep la sia | 23 Aug 2023 7:00PM | 23 Aug 2023 8:00PM
D | 0 | sleep meh | 23 Aug 2024 5:00PM
E | 0 | sleep la sia | 23 Aug 2025 7:00PM | 23 Aug 2023 8:00PM
T | 0 | maybe just do nothing
39 changes: 35 additions & 4 deletions src/main/java/Parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package parser;

import java.util.Arrays;

import command.AddDeadlineCommand;
import command.AddEventCommand;
import command.AddTodoCommand;
import command.ByeCommand;
import command.Command;
import command.DeleteCommand;
import command.EditCommand;
import command.EmptyCommand;
import command.ErrorCommand;
import command.FindCommand;
Expand Down Expand Up @@ -46,6 +49,8 @@ public static Command parse(String str) {
return new ByeCommand();
} else if (str.startsWith("find")) {
return parseFindCommand(str);
} else if (str.startsWith("edit")) {
return parseEditCommand(str);
} else {
return new MiscCommand();
}
Expand All @@ -61,14 +66,13 @@ private static Command parseTodoCommand(String str) {
try {
String[] split = str.split(" ");
if (split.length < 2) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("Hey! I think you forget to enter the todo description!");
} else {
return new AddTodoCommand(str.substring(split[0].length()).trim());
}
} catch (IllegalArgumentException e) {
System.out.println("Hey! I think you forget to enter the todo description "
+ "or leave a space after the command!");
return new ErrorCommand("Hey! I think you forget to enter the todo description!");
System.out.println(e.getMessage());
return new ErrorCommand(e.getMessage());
}
}

Expand Down Expand Up @@ -183,6 +187,33 @@ private static Command parseFindCommand(String str) {
return new FindCommand(split[1].trim());
}

/**
* Parses a user input string as a Edit command.
*
* @param str The user input string to be parsed.
* @return A command object representing the parsed edit command.
*/
private static Command parseEditCommand(String str) {
String[] split = str.split(" ");
if (split.length < 4) {
return new ErrorCommand("Hey! Please enter a valid edit command!\n"
+ "eg. edit 1 taskdesc sleep");
}
int index = Integer.parseInt(split [1]);
String itemType = split[2].trim();

if (itemType.equals("duedate") || itemType.equals("fromdate") || itemType.equals("todate")) {
String[] newItemArray = Arrays.copyOfRange(split, 3, split.length);
String newItem = String.join(" ", newItemArray);
String formattedDate = parseAndFormatDateTime(newItem.trim());
return new EditCommand(index, itemType, formattedDate);
}

String[] newItemArray = Arrays.copyOfRange(split, 3, split.length);
String newItem = String.join(" ", newItemArray);
return new EditCommand(index, itemType, newItem);
}

/**
* Parses and formats a date and time string into a specific format.
*
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/TaskManager/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,23 @@ public boolean isMatch(String keyword) {
return TaskMatcher.isMatch(keyword, taskDesc);
}

/**
* Update the taskDesc
*
* @param newTaskDesc The new taskDesc.
*/
public void editTaskDesc(String newTaskDesc) {
this.taskDesc = newTaskDesc;
}

/**
* Update the dueDateStr.
*
* @param newDueDateStr The new dueDateStr.
*/
public void editDueDate(String newDueDateStr) {
this.dueDateStr = newDueDateStr;
this.dueDate = LocalDateTime.parse(dueDateStr, DATE_TIME_FORMATTER);
}

}
29 changes: 29 additions & 0 deletions src/main/java/TaskManager/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,33 @@ public boolean equals(Object o) {
public boolean isMatch(String keyword) {
return TaskMatcher.isMatch(keyword, taskDesc);
}

/**
* Update the taskDesc
*
* @param newTaskDesc The new taskDesc.
*/
public void editTaskDesc(String newTaskDesc) {
this.taskDesc = newTaskDesc;
}

/**
* Update the new fromDateStr
*
* @param newFromDateStr The new fromDateStr.
*/
public void editFromDate(String newFromDateStr) {
this.fromDateStr = newFromDateStr;
this.fromDate = LocalDateTime.parse(fromDateStr, DATE_TIME_FORMATTER);
}

/**
* Update the new toDateStr
*
* @param newToDateStr The new toDateStr.
*/
public void editToDate(String newToDateStr) {
this.toDateStr = newToDateStr;
this.toDate = LocalDateTime.parse(toDateStr, DATE_TIME_FORMATTER);
}
}
8 changes: 8 additions & 0 deletions src/main/java/TaskManager/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ public boolean isMatch(String keyword) {
return TaskMatcher.isMatch(keyword, taskDesc);
}

/**
* Update the taskDesc
*
* @param newTaskDesc The new taskDesc.
*/
public void editTaskDesc(String newTaskDesc) {
this.taskDesc = newTaskDesc;
}
}
141 changes: 141 additions & 0 deletions src/main/java/command/EditCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package command;

import duke.Ui;
import storage.FileHandler;
import storage.TaskList;
import taskmanager.Deadline;
import taskmanager.Event;
import taskmanager.ToDo;

/**
* A command to edit a task.
*/
public class EditCommand extends Command {

private int index;
private String newItem;
private String itemType;


/**
* Constructs a `MarkCommand` object with the specified task index.
*
* @param index The index of the task to mark as done.
* @param itemType The item type to edit.
* @param newItem The new item to update.
* @throws IllegalArgumentException If the provided index is not positive (greater than 0)
* and if itemType and newItem is null.
* */
public EditCommand(int index, String itemType, String newItem) {
assert index > 0 : "index cannot be negative or zero";
assert itemType != null : "Item type for editing cannot be empty";
assert newItem != null : "New item for editing cannot be empty";
this.index = index;
this.itemType = itemType;
this.newItem = newItem;
}

/**
* Edits the specified task.
*
* @param t The task list containing the tasks.
* @param ui The user interface to display the result.
* @param f The file handler (not used in this command).
*
* @return A string representation of update message.
*/
@Override
public String execute(TaskList t, Ui ui, FileHandler f) {
try {
if (t.get(index - 1) instanceof ToDo) {
return editTodo((ToDo) t.get(index - 1), t);
}
if (t.get(index - 1) instanceof Event) {
return editEvent((Event) t.get(index - 1), t);
}
if (t.get(index - 1) instanceof Deadline) {
return editDeadline((Deadline) t.get(index - 1), t);
}
return "Invalid";
} catch (IndexOutOfBoundsException e) {
return "Please enter the correct task's index number.";
}
}

/**
* Edits the ToDo type task.
*
* @param todo The Todo task to update.
* @param t The task list to update with the new Todo.
*
* @return A string representation of update message.
*/
public String editTodo(ToDo todo, TaskList t) {
if (itemType.equals("taskdesc")) {
todo.editTaskDesc(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ todo.toString();
}
return "Please provide a valid item type for todo (taskdesc)";
}

/**
* Edits the Event type task.
*
* @param event The Event task to update.
* @param t The task list to update with the new Event.
*
* @return A string representation of update message.
*/
public String editEvent(Event event, TaskList t) {
if (itemType.equals("taskdesc")) {
event.editTaskDesc(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ event.toString();
}
if (itemType.equals("fromdate")) {
event.editFromDate(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ event.toString();
}
if (itemType.equals("todate")) {
event.editToDate(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ event.toString();
}
return "Please provide a valid item type for event! (taskdesc, fromdate and todate)";
}

/**
* Edits the Event type task.
*
* @param deadline The Event task to update.
* @param t The task list to update with the new deadline.
*
* @return A string representation of update message.
*/
public String editDeadline(Deadline deadline, TaskList t) {
if (itemType.equals("taskdesc")) {
deadline.editTaskDesc(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ deadline.toString();
}
if (itemType.equals("duedate")) {
deadline.editDueDate(newItem);
FileHandler.writeTasksToFile(t);
return "The event has been updated!\n"
+ deadline.toString();
}
return "Please provide a valid item type for event! (taskdesc and duedate)";
}
@Override
public boolean isExit() {
return false;
}
}

1 change: 1 addition & 0 deletions src/main/java/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ private void handleUserInput() {
);
userInput.clear();
}

}

0 comments on commit 6bff02a

Please sign in to comment.