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()); diff --git a/src/main/java/process/Parser.java b/src/main/java/process/Parser.java index b8d65e613b..b62b4c4a79 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 */ @@ -16,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; @@ -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("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.\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")) { diff --git a/src/main/java/task/FixedTask.java b/src/main/java/task/FixedTask.java new file mode 100644 index 0000000000..8cf213efd9 --- /dev/null +++ b/src/main/java/task/FixedTask.java @@ -0,0 +1,58 @@ +package task; + +import process.DukeException; + +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); + this.durationHour = durationHour; + checkMinute(durationMinute); + 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."); + } + if (minute < 0) { + throw new DukeException("FixedTask argument minute must be a positive neutral number."); + } + } + + /** + * 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" : "" ) : ""; + return "[F]" + super.toString() + " (Requires: " + printHour + (durationHour > 0 && durationMinute > 0 ? " " : "") + printMinute + ")"; + } +}