From 02e18c63b8ec21c653e59853a372a2e110aabbf4 Mon Sep 17 00:00:00 2001 From: lijiayu980606 Date: Thu, 19 Sep 2019 22:06:33 +0800 Subject: [PATCH] Completed B-Snooze: snooze/postpone/reschedule a task --- data/duke.txt | Bin 244 -> 220 bytes src/main/java/command/Parser.java | 112 +++++++++++++++++++++++++++++- src/main/java/task/Deadline.java | 3 +- src/main/java/task/Event.java | 3 +- src/main/java/task/Task.java | 35 ++++++++++ src/main/java/task/Todo.java | 1 + src/main/java/ui/Ui.java | 30 ++++++++ 7 files changed, 180 insertions(+), 4 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index a3b51ff92306161732478c15f775a9c3a450d70f..a7304ed93459dffbf3cc161907df591dd34369f0 100644 GIT binary patch delta 160 zcmeyuc!zO9#3pyb*6w}hA6 zy!C>KfhmfCEwk7qKQGmXfjcF&IJqdZpd>RtuY^I!Co8cmQ9ma!FI_*lq$o2l-MXTH zp@f0Cs5Gy*kfDx&3CLk7Pt3^%TFG1D46@TDH8CY8GcVP9pHH?-6@u+%p)Ftk)KFfcZ#007({J#hd4 diff --git a/src/main/java/command/Parser.java b/src/main/java/command/Parser.java index 458c1646c2..0d03a0dd36 100644 --- a/src/main/java/command/Parser.java +++ b/src/main/java/command/Parser.java @@ -7,6 +7,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; /** @@ -61,6 +62,15 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor } else if (isWithinPeriodTask(input)) { processWithin(input, tasklist, ui); storage.save(tasklist.returnArrayList()); + }else if (isSnooze(input)) { + processSnooze(input, tasklist, ui); + storage.save(tasklist.returnArrayList()); + }else if (isPostpone(input)) { + processPostpone(input, tasklist, ui); + storage.save(tasklist.returnArrayList()); + }else if (isReschedule(input)) { + processReschedule(input, tasklist, ui); + storage.save(tasklist.returnArrayList()); } else { throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } @@ -70,8 +80,6 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor return false; } - - /** * Processes the find command and outputs a list of tasks containing the word. * @param input Input from the user. @@ -222,6 +230,94 @@ private static void processWithin(String input, TaskList tasklist, Ui ui) { } } + /** + * Process the snooze command and automatically postpone the selected deadline task by 1 hour. + * @param input Input from the user. + * @param tasklist Tasklist of the user. + * @param ui Ui that interacts with the user. + */ + private static void processSnooze(String input, TaskList tasklist, Ui ui) { + try { + String[] arr = input.split(" ", 2); + int nsnooze = Integer.parseInt(arr[1]) - 1; + if(tasklist.get(nsnooze).getType().equals("D")){ + String taskTime = tasklist.get(nsnooze).getBy(); + Date formattedtime = dataformat.parse(taskTime); + java.util.Calendar calendar = java.util.Calendar.getInstance(); + calendar.setTime(formattedtime); + calendar.add(Calendar.HOUR_OF_DAY,1); + Date newDate = calendar.getTime(); + tasklist.get(nsnooze).setBy(dataformat.format(newDate)); + ui.printSnoozeMessage(tasklist.get(nsnooze)); + } else { + ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline type task to snooze."); + } + + } catch (ArrayIndexOutOfBoundsException e) { + ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to snooze."); + }catch (ParseException e) { + ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong."); + } + } + /** + * Process the postpone command and postpone the selected deadline task by required number of hours. + * @param input Input from the user. + * @param tasklist Tasklist of the user. + * @param ui Ui that interacts with the user. + */ + private static void processPostpone(String input, TaskList tasklist, Ui ui) { + try { + String[] splitspace = input.split(" ", 2); + String[] splittime = splitspace[1].split(" ", 2); + int npostpone = Integer.parseInt(splittime[0]) - 1; + int delaytime = Integer.parseInt(splittime[1]); + if(tasklist.get(npostpone).getType().equals("D")){ + String taskTime = tasklist.get(npostpone).getBy(); + Date formattedtime = dataformat.parse(taskTime); + java.util.Calendar calendar = java.util.Calendar.getInstance(); + calendar.setTime(formattedtime); + calendar.add(Calendar.HOUR_OF_DAY,delaytime); + Date newDate = calendar.getTime(); + tasklist.get(npostpone).setBy(dataformat.format(newDate)); + ui.printPostponeMessage(tasklist.get(npostpone)); + } else { + ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline type task to postpone."); + } + + } catch (ArrayIndexOutOfBoundsException e) { + ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to postpone. Format:'postpone '"); + }catch (ParseException e) { + ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong. Format:'postpone "); + } + } + + private static void processReschedule(String input, TaskList tasklist, Ui ui) { + try { + String[] splitspace = input.split(" ", 2); + String[] splittime = splitspace[1].split(" ", 2); + int nreschedule = Integer.parseInt(splittime[0]) - 1; + String delay = splittime[1]; + if(tasklist.get(nreschedule).getType().equals("D")){ + Date formattedtime = dataformat.parse(delay); + String newschedule = dataformat.format(formattedtime); + tasklist.get(nreschedule).setBy(newschedule); + ui.printRescheduleMessage(tasklist.get(nreschedule)); + } else if(tasklist.get(nreschedule).getType().equals("E")){ + Date formattedtime = dataformat.parse(delay); + String newschedule = dataformat.format(formattedtime); + tasklist.get(nreschedule).setAt(newschedule); + ui.printRescheduleMessage(tasklist.get(nreschedule)); + } else { + ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline or event type task to reschedule."); + } + + } catch (ArrayIndexOutOfBoundsException e) { + ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to reschedule. Format:'postpone '"); + }catch (ParseException e) { + ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong. Format:'postpone "); + } + } + private static boolean isBye(String input) { return input.equals("bye"); @@ -258,4 +354,16 @@ private static boolean isFind(String input) { private static boolean isWithinPeriodTask(String input) { return input.startsWith("within"); } + + private static boolean isSnooze(String input) { + return input.startsWith("snooze"); + } + + private static boolean isPostpone(String input) { + return input.startsWith("postpone"); + } + + private static boolean isReschedule(String input) { + return input.startsWith("reschedule"); + } } diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index 5a0cbc8b34..2f10ba6fa7 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -7,7 +7,7 @@ */ public class Deadline extends Task implements Serializable { - protected String by; + //protected String by; /** * Creates a Deadline instance and initialises the required attributes. @@ -17,6 +17,7 @@ public class Deadline extends Task implements Serializable { public Deadline(String description, String by) { super(description); this.by = by; + this.type = "D"; } /** diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 50498e932e..b2cf3c166f 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -6,7 +6,7 @@ * Task containing information of a deadline. */ public class Event extends Task implements Serializable { - protected String at; + //protected String at; /** * Creates an Event instance and initialises the required attributes. @@ -16,6 +16,7 @@ public class Event extends Task implements Serializable { public Event(String description, String at) { super(description); this.at = at; + this.type = "E"; } /** diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index d48663fac6..cff1530d9c 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -8,6 +8,9 @@ public class Task implements Serializable { protected String description; protected boolean isDone; + protected String type; + protected String by; + protected String at; /** * Creates a Task instance and initialises the required attributes. @@ -41,6 +44,20 @@ public void setDone() { this.isDone = true; } + /** + * Sets a new value to the by attribute. + */ + public void setBy(String by) { + this.by = by; + } + + /** + * Sets a new value to the at attribute. + */ + public void setAt(String at) { + this.at = at; + } + /** * Gets the description of the Task. * @return Task Description. @@ -48,4 +65,22 @@ public void setDone() { public String getDescription() { return this.description; } + + /** + * Gets the type of the Task. + * @return Task type. + */ + public String getType(){ return this.type; } + + /** + * Gets the deadline of the Task. + * @return Task by. + */ + public String getBy(){ return this.by; } + + /** + * Gets the period of the Task. + * @return Task at. + */ + public String getAt(){ return this.at; } } diff --git a/src/main/java/task/Todo.java b/src/main/java/task/Todo.java index 5831894637..144108418c 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/task/Todo.java @@ -13,6 +13,7 @@ public class Todo extends Task implements Serializable { */ public Todo(String description) { super(description); + this.type = "T"; } /** diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index eb309e74c3..c42a009572 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -104,6 +104,36 @@ public void printAddedMessage(Task task, TaskList tasklist) { System.out.print(line); } + /** + * Prints message to indicate a Task being snoozed. + * @param task Task to be snoozed. + */ + public void printSnoozeMessage(Task task) { + System.out.print(line + " Got it. I've snoozed this task: \n"); + System.out.print(" " + task.giveTask() + "\n"); + System.out.print(line); + } + + /** + * Prints message to indicate a Task being postponed. + * @param task Task to be postponed. + */ + public void printPostponeMessage(Task task) { + System.out.print(line + " Got it. I've postponed this task: \n"); + System.out.print(" " + task.giveTask() + "\n"); + System.out.print(line); + } + + /** + * Prints message to indicate a Task being rescheduled. + * @param task Task to be snoozed. + */ + public void printRescheduleMessage(Task task) { + System.out.print(line + " Got it. I've rescheduled this task: \n"); + System.out.print(" " + task.giveTask() + "\n"); + System.out.print(line); + } + /** * Prints the message for the exception thrown. * @param message Exception message.