Skip to content

Commit

Permalink
Add view schedule functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanWeiJie committed Feb 9, 2022
1 parent 3927d4e commit 227f6b5
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/duke/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ExitCommand extends Command {
* Constructor for ExitCommand class.
*/
public ExitCommand() {
super(null, null, null);
super(null);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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++) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/duke/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* Represents the mark command. A <code>MarkCommand</code> 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;
}

/**
Expand All @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/command/PrintCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PrintCommand extends Command {
* Constructor for PrintCommand class.
*/
public PrintCommand() {
super(null, null, null);
super(null);
}

/**
Expand Down
52 changes: 51 additions & 1 deletion src/main/java/duke/command/ScheduleCommand.java
Original file line number Diff line number Diff line change
@@ -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 <code>ScheduleCommand</code> 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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/duke/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/duke/functionality/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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".
Expand Down Expand Up @@ -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();

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/duke/functionality/TaskList.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 227f6b5

Please sign in to comment.