From 189169e257e22d40d5dc57976b9bb1f84bfd209f Mon Sep 17 00:00:00 2001 From: limsiying <42372947+limsiying@users.noreply.github.com> Date: Fri, 4 Oct 2019 19:51:47 +0800 Subject: [PATCH] Add week view for ViewScheduleCommand (#55) * test * test * test * test * test * test * test * image * image * image * image * update * update * update * update * update * update * refactor Schedule to TimeFrame * Add implements Comparable * 092919 ui fix (#2) * udate * udate * refactor Schedule to TimeFrame * update * update * update change in schedule command * fix merge conflict and bug * minor code change * add academic context to date * modify ViewScheduleCommand to accept date range * add academic context to date * update to latest (#5) * merge to latest (#3) * refactor Schedule to TimeFrame * Add implements Comparable * 092919 ui fix (#2) * udate * udate * refactor Schedule to TimeFrame * Refactor Schedule class to TimeFrame (#40) * Refactor TimeFrame class (#42) * Reimplement TimeFrameClass * Refactor ViewScheduleCommand * Allow for instantaneous tasks * Implement printing of schedule * update * update * update change in schedule command * fix merge conflict and bug * minor code change * Add date context (#4) * refactor Schedule to TimeFrame * Add implements Comparable * 092919 ui fix (#2) * udate * udate * refactor Schedule to TimeFrame * Refactor Schedule class to TimeFrame (#40) * Refactor TimeFrame class (#42) * Reimplement TimeFrameClass * Refactor ViewScheduleCommand * Allow for instantaneous tasks * Implement printing of schedule * update * update * update change in schedule command * fix merge conflict and bug * minor code change * add academic context to date * modify ViewScheduleCommand to accept date range * add academic context to date * add week view * improve code quality * refactor Date to LocalDateTime, fix bug causing incorrect date resets * Add week view * Update javadoc * remove time run * remove time run * Merge branch 'master' of https://github.com/AY1920S1-CS2113T-T09-2/main into add_weekView # Conflicts: # src/main/java/Duke.java # src/main/java/duchess/logic/commands/ViewScheduleCommand.java * fix checkstyle indentation --- .gitignore | 1 + data.json | 13 +++-- src/main/java/Duke.java | 2 +- .../logic/commands/AddDeadlineCommand.java | 6 +-- .../logic/commands/AddEventCommand.java | 2 +- .../logic/commands/AddTodoCommand.java | 6 +-- .../duchess/logic/commands/ByeCommand.java | 2 +- .../java/duchess/logic/commands/Command.java | 2 +- .../duchess/logic/commands/DeleteCommand.java | 4 +- .../duchess/logic/commands/DoneCommand.java | 2 +- .../duchess/logic/commands/FindCommand.java | 4 +- .../duchess/logic/commands/ListCommand.java | 2 +- .../duchess/logic/commands/SnoozeCommand.java | 2 +- .../logic/commands/ViewScheduleCommand.java | 54 +++++++++++++------ src/main/java/duchess/model/task/Task.java | 2 - .../java/duchess/model/task/TaskList.java | 1 - src/main/java/duchess/storage/Storage.java | 4 +- src/main/java/duchess/ui/Ui.java | 4 +- src/test/java/ReminderCommandTest.java | 4 +- 19 files changed, 71 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index c010949308..e049ea3874 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ data.json /data.json text_file +/data.json diff --git a/data.json b/data.json index 2856e9c60f..4f875b6325 100644 --- a/data.json +++ b/data.json @@ -1,3 +1,10 @@ -[ "duchess.storage.Store", { - "taskList" : [ "java.util.ArrayList", [ [ "duchess.model.task.Todo", { - "description" : "work" \ No newline at end of file +[ + "duchess.storage.Store", + { + "taskList": [ + "java.util.ArrayList", + [ + [ + "duchess.model.task.Todo", + { + "description": "work" \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 13bf3df014..b114f04f52 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,10 @@ import duchess.logic.commands.Command; +import duchess.logic.commands.exceptions.DukeException; import duchess.logic.parser.Parser; import duchess.model.task.DuchessLog; import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; -import duchess.logic.commands.exceptions.DukeException; public class Duke { diff --git a/src/main/java/duchess/logic/commands/AddDeadlineCommand.java b/src/main/java/duchess/logic/commands/AddDeadlineCommand.java index 3f32c40c0a..5c05425aa5 100644 --- a/src/main/java/duchess/logic/commands/AddDeadlineCommand.java +++ b/src/main/java/duchess/logic/commands/AddDeadlineCommand.java @@ -1,9 +1,9 @@ package duchess.logic.commands; -import duchess.storage.Storage; -import duchess.model.task.Task; -import duchess.model.task.Deadline; import duchess.logic.commands.exceptions.DukeException; +import duchess.model.task.Deadline; +import duchess.model.task.Task; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/AddEventCommand.java b/src/main/java/duchess/logic/commands/AddEventCommand.java index 8386399dc7..32e602bafa 100644 --- a/src/main/java/duchess/logic/commands/AddEventCommand.java +++ b/src/main/java/duchess/logic/commands/AddEventCommand.java @@ -1,8 +1,8 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; import duchess.model.task.Event; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/AddTodoCommand.java b/src/main/java/duchess/logic/commands/AddTodoCommand.java index 01c133d56a..b4da42503b 100644 --- a/src/main/java/duchess/logic/commands/AddTodoCommand.java +++ b/src/main/java/duchess/logic/commands/AddTodoCommand.java @@ -1,9 +1,9 @@ package duchess.logic.commands; -import duchess.storage.Storage; -import duchess.model.task.Todo; -import duchess.model.task.Task; import duchess.logic.commands.exceptions.DukeException; +import duchess.model.task.Task; +import duchess.model.task.Todo; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/ByeCommand.java b/src/main/java/duchess/logic/commands/ByeCommand.java index d217860b1d..c0422faaa2 100644 --- a/src/main/java/duchess/logic/commands/ByeCommand.java +++ b/src/main/java/duchess/logic/commands/ByeCommand.java @@ -1,7 +1,7 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/Command.java b/src/main/java/duchess/logic/commands/Command.java index db99cee5ef..62bab36b8e 100644 --- a/src/main/java/duchess/logic/commands/Command.java +++ b/src/main/java/duchess/logic/commands/Command.java @@ -1,7 +1,7 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/DeleteCommand.java b/src/main/java/duchess/logic/commands/DeleteCommand.java index db3e7dbf51..8b10a8ea5a 100644 --- a/src/main/java/duchess/logic/commands/DeleteCommand.java +++ b/src/main/java/duchess/logic/commands/DeleteCommand.java @@ -1,8 +1,8 @@ package duchess.logic.commands; -import duchess.storage.Storage; -import duchess.model.task.Task; import duchess.logic.commands.exceptions.DukeException; +import duchess.model.task.Task; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/DoneCommand.java b/src/main/java/duchess/logic/commands/DoneCommand.java index 11d0f9707a..af1c7eda7a 100644 --- a/src/main/java/duchess/logic/commands/DoneCommand.java +++ b/src/main/java/duchess/logic/commands/DoneCommand.java @@ -1,8 +1,8 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; import duchess.model.task.Task; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/FindCommand.java b/src/main/java/duchess/logic/commands/FindCommand.java index f91fdcdfc9..3c68e9d9d4 100644 --- a/src/main/java/duchess/logic/commands/FindCommand.java +++ b/src/main/java/duchess/logic/commands/FindCommand.java @@ -1,8 +1,8 @@ package duchess.logic.commands; -import duchess.storage.Storage; -import duchess.model.task.Task; import duchess.logic.commands.exceptions.DukeException; +import duchess.model.task.Task; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/ListCommand.java b/src/main/java/duchess/logic/commands/ListCommand.java index a1c2528e69..797f12ca00 100644 --- a/src/main/java/duchess/logic/commands/ListCommand.java +++ b/src/main/java/duchess/logic/commands/ListCommand.java @@ -1,7 +1,7 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/SnoozeCommand.java b/src/main/java/duchess/logic/commands/SnoozeCommand.java index a0e9541ec0..965522c6ec 100644 --- a/src/main/java/duchess/logic/commands/SnoozeCommand.java +++ b/src/main/java/duchess/logic/commands/SnoozeCommand.java @@ -1,8 +1,8 @@ package duchess.logic.commands; -import duchess.storage.Storage; import duchess.logic.commands.exceptions.DukeException; import duchess.model.task.Task; +import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; diff --git a/src/main/java/duchess/logic/commands/ViewScheduleCommand.java b/src/main/java/duchess/logic/commands/ViewScheduleCommand.java index bfc9d6c4c2..09f7ce4518 100644 --- a/src/main/java/duchess/logic/commands/ViewScheduleCommand.java +++ b/src/main/java/duchess/logic/commands/ViewScheduleCommand.java @@ -7,16 +7,20 @@ import duchess.storage.Store; import duchess.ui.Ui; +import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.format.ResolverStyle; +import java.time.temporal.TemporalAdjusters; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class ViewScheduleCommand extends Command { private List words; + private String date; + private String view; private TimeFrame timeFrame; /** @@ -27,32 +31,52 @@ public class ViewScheduleCommand extends Command { */ public ViewScheduleCommand(List words) throws DukeException { this.words = words; - + this.date = processString("/for"); + this.view = processString("view"); + if (!view.equals("day") && !view.equals("week")) { + throw new DukeException("Invalid view. Please choose either day or week view."); + } LocalDateTime start = processDate(" 0000"); LocalDateTime end = processDate(" 2359"); this.timeFrame = new TimeFrame(start, end); } + private String processString(String keyword) throws DukeException { + int index = words.indexOf(keyword); + if (index == -1) { + throw new DukeException("Format for viewing schedule: schedule /for view "); + } + return words.get(index + 1); + } + /** - * Creates LocalDateTime object from string input in dd/MM/yyyy HHmm. + * Process date by setting time of LocalDateTime to either 0000 or 2359. + * Also sets LocalDateTime to nearest previous or same Monday date/ nearest + * next or same Friday date if user desires week view. * - * @return The LocalDateTime instance - * @throws DukeException Exception thrown for invalid or missing date time + * @param time either " 0000" or " 2359" to indicate timeframe + * @return the LocalDateTime instance + * @throws DukeException Thrown for invalid or missing date time and command format */ private LocalDateTime processDate(String time) throws DukeException { try { - int index = words.indexOf("/for"); - if (index == -1) { - throw new DukeException("Format for viewing schedule: schedule /for "); - } - String dateString = words.get(index + 1) + time; + boolean isWeek = view.equals("week"); + boolean isStartOfDay = time.equals(" 0000"); + boolean isStartOfWeek = isWeek && isStartOfDay; + boolean isEndOfWeek = isWeek && !isStartOfDay; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HHmm") .withResolverStyle(ResolverStyle.STRICT); - return LocalDateTime.parse(dateString, formatter); + LocalDateTime localDateTime = LocalDateTime.parse(date + time, formatter); + if (isStartOfWeek) { + localDateTime = localDateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); + } else if (isEndOfWeek) { + localDateTime = localDateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); + } + return localDateTime; } catch (DateTimeParseException e) { - throw new DukeException("Invalid datetime. Correct format: dd/mm/yyyy hhmm"); + throw new DukeException("Invalid date format. Please follow dd/MM/yyyy."); } catch (IndexOutOfBoundsException e) { - throw new DukeException("Format for viewing schedule: schedule /for "); + throw new DukeException("Format for viewing schedule: schedule /for view "); } } @@ -62,12 +86,12 @@ public void execute(Store store, Ui ui, Storage storage) throws DukeException { store.getTaskList().stream() .filter(task -> task.getTimeFrame().fallsWithin(this.timeFrame)) .collect(Collectors.toList()); - if (tasksForToday.size() <= 0) { throw new DukeException("There are no tasks in the schedule."); + } else if (view.equals("week")) { + date = "the week with " + date; } - Collections.sort(tasksForToday); - ui.showScheduleResult(tasksForToday, words.get(words.indexOf("/for") + 1)); + ui.showScheduleResult(tasksForToday, date); } } \ No newline at end of file diff --git a/src/main/java/duchess/model/task/Task.java b/src/main/java/duchess/model/task/Task.java index 89760ef0f1..7253e7d9cc 100644 --- a/src/main/java/duchess/model/task/Task.java +++ b/src/main/java/duchess/model/task/Task.java @@ -8,8 +8,6 @@ import duchess.model.Module; import duchess.model.TimeFrame; - -import java.util.List; import java.util.Optional; public abstract class Task implements Comparable { diff --git a/src/main/java/duchess/model/task/TaskList.java b/src/main/java/duchess/model/task/TaskList.java index ba5e4d5760..4e3153b945 100644 --- a/src/main/java/duchess/model/task/TaskList.java +++ b/src/main/java/duchess/model/task/TaskList.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonSetter; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/duchess/storage/Storage.java b/src/main/java/duchess/storage/Storage.java index 17b92e7147..09514fcc34 100644 --- a/src/main/java/duchess/storage/Storage.java +++ b/src/main/java/duchess/storage/Storage.java @@ -1,12 +1,10 @@ package duchess.storage; -import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; -import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import duchess.logic.commands.exceptions.DukeException; -import duchess.model.task.TaskList; + import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; diff --git a/src/main/java/duchess/ui/Ui.java b/src/main/java/duchess/ui/Ui.java index 2bb11451fb..eb63aa88e0 100644 --- a/src/main/java/duchess/ui/Ui.java +++ b/src/main/java/duchess/ui/Ui.java @@ -1,7 +1,6 @@ package duchess.ui; -import duchess.logic.commands.Command; import duchess.model.task.Task; import java.util.List; @@ -95,9 +94,8 @@ public void showSearchResult(List tasks) { /** * Displays schedule of a single day to user. * Informs user if there are ongoing events. - * * @param tasks List of tasks to show - * @param date Date of choice + * @param date Date */ public void showScheduleResult(List tasks, String date) { printIndented("Here is your schedule for " + date + ":"); diff --git a/src/test/java/ReminderCommandTest.java b/src/test/java/ReminderCommandTest.java index 9f4e83aef8..cf5fbdd6ae 100644 --- a/src/test/java/ReminderCommandTest.java +++ b/src/test/java/ReminderCommandTest.java @@ -1,12 +1,12 @@ import duchess.logic.commands.AddDeadlineCommand; import duchess.logic.commands.ReminderCommand; +import duchess.logic.commands.exceptions.DukeException; import duchess.logic.parser.Parser; import duchess.model.task.DuchessLog; +import duchess.model.task.TaskList; import duchess.storage.Storage; import duchess.storage.Store; import duchess.ui.Ui; -import duchess.logic.commands.exceptions.DukeException; -import duchess.model.task.TaskList; import org.junit.jupiter.api.Test; import java.util.Arrays;