From 74c73642f809bbd7b1e95dceb42408e69c48629a Mon Sep 17 00:00:00 2001 From: tzw0 Date: Sat, 14 Sep 2019 21:44:52 +0800 Subject: [PATCH] B-Snooze --- src/main/java/command/RescheduleCommand.java | 21 ++++++++++++++++++++ src/main/java/process/Parser.java | 12 ++++++++++- src/main/java/process/Ui.java | 15 +++++++++++++- src/main/java/task/Event.java | 2 +- src/main/java/task/TaskList.java | 11 +++++++++- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/main/java/command/RescheduleCommand.java diff --git a/src/main/java/command/RescheduleCommand.java b/src/main/java/command/RescheduleCommand.java new file mode 100644 index 0000000000..d3c12a6943 --- /dev/null +++ b/src/main/java/command/RescheduleCommand.java @@ -0,0 +1,21 @@ +package command; + +import process.DukeException; +import process.Storage; +import process.Ui; +import task.TaskList; + +public class RescheduleCommand extends Command{ + private int index; + private String datetime; + public RescheduleCommand(int index_, String datetime_) { + index = index_; + datetime = datetime_; + } + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + if (this.index >= tasks.size()) throw new DukeException("index error"); + tasks.rescheduleTask(index, datetime); + storage.save(tasks); + return ui.showTaskRescheduled(tasks.get(index).toString()); + } +} diff --git a/src/main/java/process/Parser.java b/src/main/java/process/Parser.java index 23eaff76a4..b8d65e613b 100644 --- a/src/main/java/process/Parser.java +++ b/src/main/java/process/Parser.java @@ -16,7 +16,7 @@ public class Parser { * @throws DukeException if Duke cannot make sense of the input */ public static Command parse(String input) throws DukeException { //input validation - ArrayList command_list = new ArrayList(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event")); + ArrayList command_list = new ArrayList(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule")); String operation; String date; int index =-1; @@ -66,6 +66,16 @@ public static Command parse(String input) throws DukeException { //input validat return new ExitCommand(); } else if (operation.equals("list")) { return new ListCommand(); + } else if (operation.equals("reschedule")) { + try { + index = Integer.parseInt(operation_list[1]) - 1; + } catch (Exception e) { + throw new DukeException("index error"); + } + int to_index = input.indexOf(" /to "); + date = input.substring(to_index + 5).trim(); + if (to_index < 0 || date.isBlank()) throw new DukeException("datetime"); + return new RescheduleCommand(index, date); } } throw new DukeException("unknown"); diff --git a/src/main/java/process/Ui.java b/src/main/java/process/Ui.java index 28c2f96ebe..0adf126017 100644 --- a/src/main/java/process/Ui.java +++ b/src/main/java/process/Ui.java @@ -78,6 +78,8 @@ public String showError(String error_msg) { out_ = "You have no tasks in your list"; } else if (error_msg.equals("empty task")) { out_ ="OOPS!!! The description of a task cannot be empty."; + } else if (error_msg.equals("reschedule")) { + out_ = "OOPS!!! A Todo task cannot be rescheduled"; } else out_ = ("OOPS!!! " + error_msg); System.out.println(out_); @@ -94,7 +96,7 @@ public String showLoadingError() { } /** * Shows and - * @return the given taska as done + * @return the given task as done * @param task to be displayed as done */ public String showTaskDone(String task) { @@ -103,6 +105,17 @@ public String showTaskDone(String task) { System.out.println(out_); return out_; } + /** + * Shows and + * @return the given task as rescheduled + * @param task to be displayed as rescheduled + */ + public String showTaskRescheduled(String task) { +// String out_ = "Nice! I've marked this task as done:\n" + "[✓] " + task; + String out_ = "Nice! I've rescheduled this task:\n" + task; + System.out.println(out_); + return out_; + } /** * Shows and * @return the given task as deleted diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 2161428454..92750ab729 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -29,7 +29,7 @@ public Event(String description, String at, boolean b) throws DukeException { SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy hhmm"); Date date = sdf.parse(at); this.at = date; - super.tt = "D"; + super.tt = "E"; super.extra = at; } catch (ParseException e){ throw new DukeException("datetime"); diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index b69db837de..40057e1c72 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -75,7 +75,16 @@ public void deleteTask(int index) { public void doneTask(int index) { this.get(index).done(true); } - + public void rescheduleTask(int index, String datetime) throws DukeException{ + String description = this.get(index).description; + String tasktype = this.get(index).tt; + if (tasktype.equals("D")) { + this.add(index, new Deadline(description, datetime, false)); + } else if (tasktype.equals("E")) { + this.add(index, new Event(description, datetime, false)); + } else throw new DukeException("reschedule"); + this.remove(index + 1); + } /** * Searches for a task with the given keyword * @param keyword to be found