Skip to content

Commit

Permalink
Merge pull request #11 from gowgos5/B-ViewSchedules
Browse files Browse the repository at this point in the history
[CS2113-T14-1]-gowgos5-B-ViewSchedules
  • Loading branch information
aquohn authored Sep 23, 2019
2 parents db69b7a + 701b14a commit 70babd9
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/duke/command/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public Command getCommand() {
return new FindCommand();
}
},
VIEW("view") {
public Command getCommand() {
return new ViewCommand();
}
},
SNOOZE("snooze") {
public Command getCommand() {
return new SnoozeCommand();
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/duke/command/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package duke.command;

import duke.DukeContext;
import duke.exception.DukeException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Class responsible for executing Command to view the scheduled Tasks on a specified date.
*
* @author Pang Jia Jun Vernon
*/
public class ViewCommand extends ArgCommand {
private static final DateTimeFormatter PAT_DATE = DateTimeFormatter.ofPattern("d/M/yyyy");

private LocalDate date;

/**
* Creates a new Command object that can be executed to view the scheduled Tasks on a specified date.
*/
public ViewCommand() {
emptyArgMsg = "Please give me a date to work with!";
}

@Override
public void parse(String inputStr) throws DukeException {
super.parse(inputStr);

try {
date = LocalDate.parse(arg, PAT_DATE);
} catch (DateTimeParseException excp) {
throw new DukeException("Date must be given as e.g. " + LocalDateTime.now().format(PAT_DATE) + ".");
}
}

@Override
public void execute(DukeContext ctx) throws DukeException {
String scheduleStr = "Here are your tasks for " + arg + ":";
ctx.ui.print(scheduleStr + ctx.taskList.listSchedule(date));
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void setName(String name) {
this.name = name;
}

public Boolean isDone() {
return isDone;
}

/**
* Formats the data about the task for display to the user.
*
Expand Down
55 changes: 50 additions & 5 deletions src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import duke.exception.DukeFatalException;
import duke.exception.DukeResetException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TaskList {
// TSV files will have one entry per line, tabs disallowed in input
Expand Down Expand Up @@ -150,8 +154,9 @@ public int getTaskIdx(String idxStr) throws DukeException {

/**
* Reports the addition of a number of tasks.
* @param addStr The descriptions of the tasks, formatted with two spaces behind each task and a leading line
* separator.
*
* @param addStr The descriptions of the tasks, formatted with two spaces behind each task and a leading line
* separator.
* @param taskCount Number of tasks added.
* @return A String reporting the addition of one or more tasks.
*/
Expand All @@ -163,8 +168,9 @@ public String getAddReport(String addStr, long taskCount) {

/**
* Reports the deletion of a number of tasks.
* @param delStr The descriptions of the tasks, formatted with two spaces behind each task and a leading line
* separator.
*
* @param delStr The descriptions of the tasks, formatted with two spaces behind each task and a leading line
* separator.
* @param taskCount Number of tasks added.
* @return A String reporting the deletion of one or more tasks.
*/
Expand All @@ -185,6 +191,45 @@ private String getTaskCountStr() {
return "Now you have " + taskCountStr + " in the list.";
}

/**
* Reports the schedule of the user on a specified date.
*
* @param date The specified date.
* @return Concatenated data representations of all scheduled tasks on the specified date.
* @throws DukeException If the user has no scheduled tasks on the specified date.
*/
public String listSchedule(LocalDate date) throws DukeException {
List<TimedTask> timedTaskList = new ArrayList<>();

for (Task currTask : taskArrList) {
// TODO: Code smell
if (!currTask.isDone() && currTask instanceof TimedTask) {
LocalDate taskDate = ((TimedTask) currTask).getDateTime().toLocalDate();

if (taskDate.isEqual(date)) {
timedTaskList.add((TimedTask) currTask);
}
}
}
Collections.sort(timedTaskList);

StringBuilder scheduleBuilder = new StringBuilder();
int scheduleCount = 0;

for (Task timedTask : timedTaskList) {
scheduleCount = scheduleCount + 1;
scheduleBuilder.append(System.lineSeparator()).append(scheduleCount).append(".")
.append(timedTask.toString());
}

if (scheduleCount == 0) {
throw new DukeException("You have no tasks due on " + date.format(DateTimeFormatter.ofPattern("d/M/yyyy"))
+ "!");
}

return scheduleBuilder.toString();
}

/**
* Returns a string that indicates if snooze was successful.
* @param index the tasks to be snoozed index in the list of all tasks
Expand All @@ -196,7 +241,7 @@ public String snooze(int index, LocalDateTime datetime) throws DukeException {
taskArrList.get(index).changeTime(datetime);
return "The task have been snoozed;\n\t" + taskArrList.get(index);
}

/**
* Sets a reminder for a task in the list.
*
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/duke/task/TimedTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public abstract class TimedTask extends Task {
private static final DateTimeFormatter PAT_DATETIME = DateTimeFormatter.ofPattern("d/M/y HHmm");
public abstract class TimedTask extends Task implements Comparable<TimedTask> {
private static final DateTimeFormatter PAT_DATETIME = DateTimeFormatter.ofPattern("d/M/yyyy HHmm");
private static final DateTimeFormatter PAT_DATETIME_DISPLAY = DateTimeFormatter.ofPattern("eee, d MMM yyyy h:mm a");

private LocalDateTime time;
Expand Down Expand Up @@ -41,6 +41,15 @@ public void setTime(LocalDateTime time) {
this.time = time;
}

public LocalDateTime getDateTime() {
return time;
}

@Override
public int compareTo(TimedTask o) {
return getDateTime().compareTo(o.getDateTime());
}

@Override
public void changeTime(LocalDateTime newTime) {
time = newTime;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/TaskListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -141,6 +142,35 @@ public void markDone_validIdx_tasksMarkedDone() {
}
}

/**
* Tests listSchedule() with an empty list. Expect an exception to be thrown.
*/
@Test
public void listSchedule_emptyList_exceptionThrown() {
TaskList list = new TaskList();
assertThrows(DukeException.class, () -> list.listSchedule(LocalDate.now()));
}

/**
* Tests listSchedule() with a list of unscheduled and scheduled Tasks.
* Expect the uncompleted scheduled Tasks to be returned.
*
* @throws DukeException If there are no uncompleted scheduled Tasks present in the user's original Task list.
*/
@Test
public void listSchedule_scheduledTasks_scheduledTasksReturned() throws DukeException {
LocalDate date = LocalDateTime.parse("12/09/2019 1400", TimedTask.getPatDatetime()).toLocalDate();
String expectedScheduleStr = System.lineSeparator() + "1.[E][N] tutorial (at: Thu, 12 Sep 2019 2:00 PM "
+ "- Thu, 12 Sep 2019 2:00 PM)" + System.lineSeparator()
+ "2.[D][N] submission (by: Thu, 12 Sep 2019 2:00 PM)";
assertEquals(expectedScheduleStr, taskList.listSchedule(date));

taskList.markDone("3");
expectedScheduleStr = System.lineSeparator() + "1.[E][N] tutorial (at: Thu, 12 Sep 2019 2:00 PM "
+ "- Thu, 12 Sep 2019 2:00 PM)";
assertEquals(expectedScheduleStr, taskList.listSchedule(date));
}

/**
* Compares the output returned by setReminder() with the correct output.
* Expect them to be equal if validIdx is given.
Expand Down

0 comments on commit 70babd9

Please sign in to comment.