Skip to content

Commit

Permalink
Task update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojj2209 committed Sep 7, 2020
1 parent a696175 commit 8222f50
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/duke/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum ActionType {
MARK_DONE,
EDIT,
DELETE,
ADD_TODO,
ADD_DEADLINE,
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duke.command.AddCommand;
import duke.command.Command;
import duke.command.DeleteCommand;
import duke.command.EditCommand;
import duke.command.ExitCommand;
import duke.command.FindCommand;
import duke.command.ListCommand;
Expand All @@ -24,6 +25,10 @@ private static boolean isFindCommand(String input) {
return input.length() >= 4 && input.substring(0, 4).equalsIgnoreCase("find");
}

private static boolean isEditCommand(String input) {
return input.length() >= 4 && input.substring(0, 4).equalsIgnoreCase("edit");
}

private static boolean isDeleteCommand(String input) {
return input.length() >= 6 && input.substring(0, 6).equalsIgnoreCase("delete");
}
Expand Down Expand Up @@ -51,6 +56,10 @@ public static Command parse(String input) throws DukeException {
} else if (isFindCommand(input)) {
String search = input.substring(5);
return new FindCommand(search);
} else if (isEditCommand(input)) {
int taskIndex = Character.getNumericValue(input.charAt(5)) - 1;
String editInput = input.substring(7);
return new EditCommand(taskIndex, editInput);
} else if (isDeleteCommand(input)) {
int taskIndex = Integer.parseInt(input.substring(7)) - 1;
return new DeleteCommand(taskIndex);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public String printTask(Task task, ActionType action) {
case MARK_DONE:
taskString = "Task marked complete: \n";
break;
case EDIT:
taskString = "Task edited: \n";
break;
case DELETE:
taskString = "Task deleted: \n";
break;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/duke/command/EditCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package duke.command;

import duke.ActionType;
import duke.DukeException;
import duke.Storage;
import duke.Ui;
import duke.tasks.Task;
import duke.tasks.TaskList;

/**
* Command to edit a task
*/
public class EditCommand extends Command {
private final int taskIndex;
private final String editInput;
/**
* EditCommand constructor
*
* @param taskIndex Index of task to be edited
*/
public EditCommand(int taskIndex, String editInput) {
super(false);
this.taskIndex = taskIndex;
this.editInput = editInput;
}

/**
* Edits task, then prints its info
*
* @param tasks List of tasks
* @param ui User interface to print task
* @param storage File storage object
* @return Info on edited task
* @throws DukeException if exception encountered
*/
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
if (taskIndex >= tasks.size()) {
throw new DukeException("Task does not exist _(´ཀ`」 ∠)_");
} else {
Task editedTask = tasks.edit(taskIndex, editInput);
storage.updateFile(tasks);
return ui.printTask(editedTask, ActionType.EDIT);
}
}
}
16 changes: 15 additions & 1 deletion src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package duke.tasks;

import duke.DukeException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Deadline extends Task {
private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MMM dd yyyy");
private final LocalDate deadline;
private LocalDate deadline;

/**
* Deadline constructor
Expand All @@ -19,6 +21,18 @@ public Deadline(String description, LocalDate deadline, boolean isDone) {
this.deadline = deadline;
}

public void edit(String editInput) throws DukeException {
String editType = editInput.substring(0, 4);
String editText = editInput.substring(5);
if (editType.equalsIgnoreCase("desc")) {
super.description = editText;
} else if (editType.equalsIgnoreCase("time")) {
deadline = LocalDate.parse(editText);
} else {
throw new DukeException("Edit command error _(´ཀ`」 ∠)_");
}
}

@Override
public String toString() {
return "[D][" + super.getStatusIcon() + "] " + super.description + " (by: " + this.deadline.format(FORMATTER) + ")";
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package duke.tasks;

import duke.DukeException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Event extends Task {
private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MMM dd yyyy");
private final LocalDate eventTime;
private LocalDate eventTime;

/**
* Event constructor
Expand All @@ -19,6 +21,18 @@ public Event(String description, LocalDate eventTime, boolean isDone) {
this.eventTime = eventTime;
}

public void edit(String editInput) throws DukeException {
String editType = editInput.substring(0, 4);
String editText = editInput.substring(5);
if (editType.equalsIgnoreCase("desc")) {
super.description = editText;
} else if (editType.equalsIgnoreCase("time")) {
eventTime = LocalDate.parse(editText);
} else {
throw new DukeException("Edit command error _(´ཀ`」 ∠)_");
}
}

@Override
public String toString() {
return "[E][" + super.getStatusIcon() + "] " + super.description + " (at: " + this.eventTime.format(FORMATTER) + ")";
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/duke/tasks/Task.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package duke.tasks;

public class Task {
protected final String description;
import duke.DukeException;

public abstract class Task {
protected String description;
protected boolean isDone;

/**
Expand All @@ -22,4 +24,6 @@ public String getStatusIcon() {
public void markAsDone() {
this.isDone = true;
}

public abstract void edit(String editInput) throws DukeException;
}
16 changes: 16 additions & 0 deletions src/main/java/duke/tasks/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.tasks;

import duke.DukeException;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -35,6 +37,7 @@ public void addTask(Task task) {
* Marks task as done, then returns it
*
* @param index Index of task to be completed
* @return Completed task
*/
public Task markDone(int index) {
Task doneTask = list.get(index);
Expand All @@ -46,10 +49,23 @@ public Task markDone(int index) {
* Deletes task, then returns it
*
* @param index Index of task to be deleted
* @return Deleted task
*/
public Task delete(int index) {
Task deletedTask = list.get(index);
list.remove(index);
return deletedTask;
}

/**
* Edits task, then returns it
*
* @param index Index of task to be edited
* @return Edited task
*/
public Task edit(int index, String editInput) throws DukeException {
Task editedTask = list.get(index);
editedTask.edit(editInput);
return editedTask;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/tasks/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public ToDo(String description, boolean isDone) {
super(description, isDone);
}

public void edit(String editInput) {
assert editInput.substring(0, 4) == "desc";
super.description = editInput.substring(5);
}

@Override
public String toString() {
return "[T][" + super.getStatusIcon() + "] " + super.description;
Expand Down

0 comments on commit 8222f50

Please sign in to comment.