From 6c9940b938f5645b83585147999fb22379a11dd2 Mon Sep 17 00:00:00 2001 From: Exprescode Date: Sun, 15 Sep 2019 16:15:38 +0800 Subject: [PATCH 1/5] src/main/java/task/FixedTask.java: Add FixedTask object. --- src/main/java/task/FixedTask.java | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/task/FixedTask.java diff --git a/src/main/java/task/FixedTask.java b/src/main/java/task/FixedTask.java new file mode 100644 index 0000000000..92c78344a5 --- /dev/null +++ b/src/main/java/task/FixedTask.java @@ -0,0 +1,36 @@ +package task; + +import process.DukeException; + +public class FixedTask extends Task { + int durationHour, durationMinute; + + public FixedTask(String desc, boolean check, int durationHour, int durationMinute) throws DukeException { + super(desc, check); + checkHour(durationHour); + this.durationHour = durationHour; + checkMinute(durationMinute); + this.durationMinute = durationMinute; + } + + private void checkHour(int hour) throws DukeException { + if (hour < 0) { + throw new DukeException("FixedTask argument hour must be a positive neutral number."); + } + } + + private void checkMinute(int minute) throws DukeException { + if (minute >= 60) { + throw new DukeException("FixedTask argument minute can be simplified to hours."); + } + if (minute < 0) { + throw new DukeException("FixedTask argument minute must be a positive neutral number."); + } + } + + public String toString() { + String printHour = durationHour > 0 ? durationHour + " Hour" + (durationHour > 1 ? "s" : "") : ""; + String printMinute = durationMinute > 0 ? durationMinute + " Minute" + (durationMinute > 1 ? "s" : "" ) : ""; + return "[F]" + super.toString() + " (Requires: " + printHour + (durationHour > 0 && durationMinute > 0 ? " " : "") + printMinute + ")"; + } +} From fc8cfa87496a634438ffba8c2c116ded5245e7c7 Mon Sep 17 00:00:00 2001 From: Exprescode Date: Sun, 15 Sep 2019 16:29:12 +0800 Subject: [PATCH 2/5] src/main/java/command/AddCommand.java: Add task command. --- src/main/java/command/AddCommand.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/command/AddCommand.java b/src/main/java/command/AddCommand.java index 4597b20280..226e80847b 100644 --- a/src/main/java/command/AddCommand.java +++ b/src/main/java/command/AddCommand.java @@ -2,10 +2,7 @@ import process.*; import process.DukeException; -import task.Deadline; -import task.Event; -import task.TaskList; -import task.Todo; +import task.*; /** * Represents a command that adds an item to tasks @@ -14,6 +11,7 @@ public class AddCommand extends Command { private String description; private String tasktype; private String datetime; + private int durationHour, durationMinute; /** * Creates a new AddCommand object with the given type of task and description * @param tasktype The task type @@ -34,6 +32,13 @@ public AddCommand(String tasktype, String description, String datetime) { this.tasktype = tasktype; this.datetime = datetime; } + + public AddCommand(String description, int durationHour, int durationMinute ){ + this.tasktype = "task"; + this.description = description; + this.durationHour = durationHour; + this.durationMinute = durationMinute; + } /** * Executes the AddCommand and saves changes to storage * @param tasks the task list @@ -45,6 +50,7 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti if (tasktype.equals("todo")) tasks.add(new Todo(description, false)); else if (tasktype.equals("deadline")) tasks.add(new Deadline(description, datetime, false)); else if (tasktype.equals("event")) tasks.add(new Event(description, datetime, false)); + else if (tasktype.equals("task")) tasks.add(new FixedTask(description, false, durationHour, durationMinute)); else throw new DukeException("add error"); storage.save(tasks); return ui.showTaskAdded(tasks.get(tasks.size()-1).toString(), tasks.size()); From 123f7f648a8c1c7083b3bc396a93f184e02af39f Mon Sep 17 00:00:00 2001 From: Exprescode Date: Sun, 15 Sep 2019 17:53:50 +0800 Subject: [PATCH 3/5] src/main/java/process/Parser.java: Add task command parse function. --- src/main/java/process/Parser.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/process/Parser.java b/src/main/java/process/Parser.java index b8d65e613b..f334952368 100644 --- a/src/main/java/process/Parser.java +++ b/src/main/java/process/Parser.java @@ -5,6 +5,9 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Represents a parser to make sense of user input and translate it to commands for Duke */ @@ -62,6 +65,14 @@ public static Command parse(String input) throws DukeException { //input validat date = input.substring(at_index + 5).trim(); if (date.isBlank()) throw new DukeException("datetime"); return new AddCommand("event", arg1, date); + } else if (operation.equals("task")) { + int delimiterIndex = input.indexOf(" /need "); + if (delimiterIndex == -1) throw new DukeException("Missing task delimiter. (/need)"); + arg1 = input.substring(4, delimiterIndex).trim(); + if (arg1.isBlank()) throw new DukeException("arg1 error "+ operation); + Matcher m = Pattern.compile("^(\\d+) (\\d+)$").matcher(input.substring(delimiterIndex + 7).trim()); + if (m.find()) throw new DukeException("Invalid task argument need."); + return new AddCommand(arg1, m.group(0), m.group(1)); } else if (operation.equals("bye")) { return new ExitCommand(); } else if (operation.equals("list")) { From 23a0996922681dc3d69d6d45dfb8be7fb0bf48e7 Mon Sep 17 00:00:00 2001 From: Exprescode Date: Sun, 15 Sep 2019 18:24:36 +0800 Subject: [PATCH 4/5] src/main/java/process/Parser.java: Fix task command not found issue. --- src/main/java/process/Parser.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/process/Parser.java b/src/main/java/process/Parser.java index f334952368..b62b4c4a79 100644 --- a/src/main/java/process/Parser.java +++ b/src/main/java/process/Parser.java @@ -19,7 +19,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","reschedule")); + ArrayList command_list = new ArrayList(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule","task")); String operation; String date; int index =-1; @@ -69,10 +69,10 @@ public static Command parse(String input) throws DukeException { //input validat int delimiterIndex = input.indexOf(" /need "); if (delimiterIndex == -1) throw new DukeException("Missing task delimiter. (/need)"); arg1 = input.substring(4, delimiterIndex).trim(); - if (arg1.isBlank()) throw new DukeException("arg1 error "+ operation); + if (arg1.isBlank()) throw new DukeException("Missing task description."); Matcher m = Pattern.compile("^(\\d+) (\\d+)$").matcher(input.substring(delimiterIndex + 7).trim()); - if (m.find()) throw new DukeException("Invalid task argument need."); - return new AddCommand(arg1, m.group(0), m.group(1)); + if (!m.find()) throw new DukeException("Invalid task argument need.\ntask /need "); + return new AddCommand(arg1, Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2))); } else if (operation.equals("bye")) { return new ExitCommand(); } else if (operation.equals("list")) { From 6566fc26045262836b34fabbf04f8aa8e247e1f7 Mon Sep 17 00:00:00 2001 From: Exprescode Date: Sun, 15 Sep 2019 18:38:17 +0800 Subject: [PATCH 5/5] src/main/java/task/FixedTask.java: Add javadoc. --- src/main/java/task/FixedTask.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/task/FixedTask.java b/src/main/java/task/FixedTask.java index 92c78344a5..8cf213efd9 100644 --- a/src/main/java/task/FixedTask.java +++ b/src/main/java/task/FixedTask.java @@ -5,6 +5,14 @@ public class FixedTask extends Task { int durationHour, durationMinute; + /** + * Creates FixedTask object that represents a fixed duration task. + * @param desc Task description. + * @param check Mark task as done. + * @param durationHour Duration require to complete task. (Hours) + * @param durationMinute Duration require to complete task. (Minutes) + * @throws DukeException + */ public FixedTask(String desc, boolean check, int durationHour, int durationMinute) throws DukeException { super(desc, check); checkHour(durationHour); @@ -13,12 +21,22 @@ public FixedTask(String desc, boolean check, int durationHour, int durationMinut this.durationMinute = durationMinute; } + /** + * Checks for valid hour representation. + * @param hour Integer hour to validate. + * @throws DukeException Hour is not a positive neutral number. + */ private void checkHour(int hour) throws DukeException { if (hour < 0) { throw new DukeException("FixedTask argument hour must be a positive neutral number."); } } + /** + * Checks for valid minute representation. + * @param minute Integer minute to validate. + * @throws DukeException Minute is not a positive neutral number less than 60. + */ private void checkMinute(int minute) throws DukeException { if (minute >= 60) { throw new DukeException("FixedTask argument minute can be simplified to hours."); @@ -28,6 +46,10 @@ private void checkMinute(int minute) throws DukeException { } } + /** + * Formats object variables to text form. + * @return Printable text representation of object. + */ public String toString() { String printHour = durationHour > 0 ? durationHour + " Hour" + (durationHour > 1 ? "s" : "") : ""; String printMinute = durationMinute > 0 ? durationMinute + " Minute" + (durationMinute > 1 ? "s" : "" ) : "";