diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index 502d798da7..ca039a3ddb 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -15,7 +15,7 @@ public class AddCommand extends Command { * @param task task object created from user input. */ public AddCommand(Task task) { - super(task, null, null); + super(task); } /** diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 33c47906de..2ab8ee7062 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -8,19 +8,12 @@ */ public abstract class Command { protected Task task; - protected Integer index; - protected String word; - /** * Constructor of Command class. * @param task task object created from user input. - * @param number an indicator to the index of the taskList in TaskList class. - * @param word keyword used to find similar tasks in taskList of TaskList class. */ - public Command(Task task, Integer number, String word) { + public Command(Task task) { this.task = task; - this.index = number; - this.word = word; } /** diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index f88294b061..3319702527 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -9,12 +9,14 @@ */ public class DeleteCommand extends Command { + private int index; /** * Constructor for DeleteCommand class. * @param number an indicator to the index of the taskList in TaskList class. */ public DeleteCommand(Integer number) { - super(null, number, null); + super(null); + this.index = number; } /** @@ -24,8 +26,7 @@ public DeleteCommand(Integer number) { */ @Override public String execute(TaskList tasks) { - int index = super.index; - assert index > 0 : "Index provided should be greater then 0"; + assert this.index > 0 : "Index provided should be greater then 0"; Task deletedTask = tasks.deleteTask(index); String message = "Noted. I've removed this task:\n"; return message + deletedTask.toString() + "\n" diff --git a/src/main/java/duke/command/ExitCommand.java b/src/main/java/duke/command/ExitCommand.java index ae1f301d75..79827ed33c 100644 --- a/src/main/java/duke/command/ExitCommand.java +++ b/src/main/java/duke/command/ExitCommand.java @@ -11,7 +11,7 @@ public class ExitCommand extends Command { * Constructor for ExitCommand class. */ public ExitCommand() { - super(null, null, null); + super(null); } /** diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java index a1091330cb..9eafab7598 100644 --- a/src/main/java/duke/command/FindCommand.java +++ b/src/main/java/duke/command/FindCommand.java @@ -7,13 +7,14 @@ * in the taskList of TaskList class. */ public class FindCommand extends Command { - + private String keyword; /** * Constructor of FindCommand. * @param word keyword used to find similar tasks in taskList of TaskList class. */ public FindCommand(String word) { - super(null, null, word); + super(null); + this.keyword = word; } /** @@ -23,7 +24,7 @@ public FindCommand(String word) { */ @Override public String execute(TaskList tasks) { - TaskList newTaskList = tasks.findWord(super.word); + TaskList newTaskList = tasks.findWord(this.keyword); String message = "Here are the matching tasks in your list:\n"; int counter = 1; for (int i = 0; i < newTaskList.getListSize(); i++) { diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index b6e7f9ff11..b990db2554 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -7,13 +7,14 @@ * Represents the mark command. A MarkCommand object allows users to set the corresponding task as done. */ public class MarkCommand extends Command { - + private int index; /** * Constructor for MarkCommand class. * @param number an indicator to the index of the taskList in TaskList class. */ public MarkCommand(Integer number) { - super(null, number, null); + super(null); + this.index = number; } /** @@ -23,7 +24,6 @@ public MarkCommand(Integer number) { */ @Override public String execute(TaskList tasks) { - int index = super.index; assert index > 0 : "Index provided should be greater then 0"; Task markedTask = tasks.markTask(index); String message = "Nice! I've marked this task as done:\n"; diff --git a/src/main/java/duke/command/PrintCommand.java b/src/main/java/duke/command/PrintCommand.java index 5c7d1b60f8..589d927f35 100644 --- a/src/main/java/duke/command/PrintCommand.java +++ b/src/main/java/duke/command/PrintCommand.java @@ -11,7 +11,7 @@ public class PrintCommand extends Command { * Constructor for PrintCommand class. */ public PrintCommand() { - super(null, null, null); + super(null); } /** diff --git a/src/main/java/duke/command/ScheduleCommand.java b/src/main/java/duke/command/ScheduleCommand.java index 7c73542ec9..39c42efa05 100644 --- a/src/main/java/duke/command/ScheduleCommand.java +++ b/src/main/java/duke/command/ScheduleCommand.java @@ -1,2 +1,52 @@ -package duke.command;public class ScheduleCommand { +package duke.command; + +import java.time.LocalDate; + +import duke.functionality.TaskList; + +/** + * Represents the schedule command. A ScheduleCommand object corresponds to finding similar tasks + * in the taskList of TaskList class. + */ +public class ScheduleCommand extends Command { + private LocalDate date; + + /** + * Constructor of FindCommand. + * @param date date used to find similar tasks in taskList of TaskList class. + */ + public ScheduleCommand(LocalDate date) { + super(null); + this.date = date; + } + + /** + * Returns a string which contains all the task after the execution of findSameSchedule in the TaskList class. + * @param tasks an object of TaskList, used to access public methods in TaskList class. + * @return crafted message after calling findSameSchedule in the TaskList class. + */ + @Override + public String execute(TaskList tasks) { + TaskList newTaskList = tasks.findSameSchedule(this.date); + String message = "Here is your Schedule for " + this.date + "\n"; + int counter = 1; + for (int i = 0; i < newTaskList.getListSize(); i++) { + String output = counter + "." + newTaskList.getTask(i); + counter++; + message += output + "\n"; + } + if (counter == 1) { + message = "You have no Schedule on " + this.date + "\n"; + } + return message; + } + + /** + * Returns false as the Command is not an ExitCommand. + * @return false. + */ + @Override + public boolean isExit() { + return false; + } } diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index 01327348a6..8e832725d2 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -9,12 +9,14 @@ */ public class UnmarkCommand extends Command { + private int index; /** * Constructor for the UnmarkCommand class. * @param number an indicator to the index of the taskList in TaskList class. */ public UnmarkCommand(Integer number) { - super(null, number, null); + super(null); + this.index = number; } /** @@ -25,7 +27,6 @@ public UnmarkCommand(Integer number) { @Override public String execute(TaskList tasks) { String message = "OK, I've marked this task as not done yet:\n"; - int index = super.index; assert index > 0 : "Index provided should be greater then 0"; Task unMarkedTask = tasks.unmarkTask(index); return message + unMarkedTask; diff --git a/src/main/java/duke/functionality/Parser.java b/src/main/java/duke/functionality/Parser.java index bd91abfd1a..7e74844d5a 100644 --- a/src/main/java/duke/functionality/Parser.java +++ b/src/main/java/duke/functionality/Parser.java @@ -12,6 +12,7 @@ import duke.command.FindCommand; import duke.command.MarkCommand; import duke.command.PrintCommand; +import duke.command.ScheduleCommand; import duke.command.UnmarkCommand; import duke.exception.BlankCommandException; import duke.exception.DukeException; @@ -29,6 +30,7 @@ public class Parser { private static final int EVENT_OFFSET = 5; private static final int TODO_OFFSET = 4; private static final int DEADLINE_OFFSET = 8; + private static final int SCHEDULE_OFFSET = 8; private static final int INPUT_OFFSET = 3; private static final int FIRST_INPUT = 0; private static final int SECOND_INPUT = 1; @@ -126,6 +128,19 @@ private static Command handleTodo(String input, String command) throws DukeExcep return new AddCommand(new Todo(description)); } + private static Command handleSchedule(String input, String command) throws DukeException { + String[] inputSplit = input.split(" "); + String description = input.substring(SCHEDULE_OFFSET).trim(); + checkDescriptionLength(description, command); + + try { + LocalDate date = formatDate(inputSplit[SECOND_INPUT]); + return new ScheduleCommand(date); + } catch (DateTimeParseException e) { + throw new DukeException("The expected input form is schedule yyyy-mm-dd"); + } + } + /** * Returns the respective command from user input. * @param input user input. Eg, "todo run". @@ -162,6 +177,9 @@ public static Command parse(String input) throws DukeException { } else if (command.equals("find")) { return new FindCommand(inputSplit[SECOND_INPUT]); + } else if (command.equals("schedule")) { + return handleSchedule(input, command); + } else if (command.equals("")) { throw new BlankCommandException(); diff --git a/src/main/java/duke/functionality/TaskList.java b/src/main/java/duke/functionality/TaskList.java index 11c3ef8745..b057f73abd 100644 --- a/src/main/java/duke/functionality/TaskList.java +++ b/src/main/java/duke/functionality/TaskList.java @@ -1,7 +1,10 @@ package duke.functionality; +import java.time.LocalDate; import java.util.ArrayList; +import duke.task.Deadline; +import duke.task.Event; import duke.task.Task; /** @@ -88,6 +91,31 @@ public TaskList findWord(String word) { return newTaskList; } + /** + * Returns a new TaskList which contains all the Task that matches the specified date. + * @param date date input from user + * @return a new TaskList containing all Task that contains the specified date. + */ + public TaskList findSameSchedule(LocalDate date) { + TaskList newTaskList = new TaskList(); + + for (int i = 0; i < getListSize(); i++) { + Task task = taskList.get(i); + LocalDate taskDate = null; + if (task instanceof Event) { + taskDate = ((Event) task).getDate(); + + } else if (task instanceof Deadline) { + taskDate = ((Deadline) task).getDate(); + } + + if (taskDate != null && taskDate.equals(date)) { + newTaskList.addToList(task); + } + } + return newTaskList; + } + public int getListSize() { return taskList.size(); }