diff --git a/README.md b/README.md index 8715d4d915..afb375d36c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# duke project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/duke.java` file, right-click it, and choose `Run duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..915b159781 --- /dev/null +++ b/build.gradle @@ -0,0 +1,61 @@ +plugins { + id 'java' + id 'application' + id 'com.github.johnrengelman.shadow' version '5.1.0' + id 'checkstyle' +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0' + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0' + + String javaFxVersion = '11' + + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "duke.main.Launcher" +} + +checkstyle { + toolVersion = '10.2' +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +run{ + standardInput = System.in +} diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..e8ee76467b --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,429 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml new file mode 100644 index 0000000000..135ea49ee0 --- /dev/null +++ b/config/checkstyle/suppressions.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 8077118ebe..fedaa4a335 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,147 @@ -# User Guide +# User Guide for DUKE ## Features -### Feature-ABC +### Display task list -Description of the feature. +View your task list -### Feature-XYZ +### Add Tasks -Description of the feature. +Add up to 3 types of tasks + + +**[Todo]** + +A todo task denoted by [T] + +_Input format: **"todo"** + task description_ + + +**[Deadline]** + +A deadline task with a deadline to be done by, denoted by [D] + +_Input format: **"deadline"** + task description + **"/by"** + deadline_ + + +**[Event]** + +An event task with a time period for the event to be carried out, denoted by [E] + +_Input format: **"event"** + task description + **"/at"** + time period_ + + +### Mark / Unmark tasks as done + +Marks tasks as done with an "X" +or undone without an "X" +to keep track of completion progression + + +### Undo + +Undoes the previous command + + +### Exit + +Exits Duke ## Usage -### `Keyword` - Describe action +### list - Displays current task list + +Example of usage: +> list + +Expected outcome: +``` +[T][X] Read book +[D][ ] Return Book (by: Monday 2pm) +[E][ ] Meeting (at: Monday 2pm-4pm) +``` + +### todo - Adds todo task to task list + +Example of usage: +> todo Read Book + +Expected outcome: +``` +[T][ ] Read book +``` + +### deadline - Adds Deadline task to task list + +Example of usage: +> deadline Return Book /by Monday 2pm + +Expected outcome: +``` +[D][ ] Return Book (by: Monday 2pm) +``` + +### event - Adds Event task to task list -Describe the action and its outcome. +Example of usage: +> event Meeting /at Monday 2pm-4pm -Example of usage: +Expected outcome: +``` +[E][ ] Meeting (at: Monday 2pm-4pm) +``` + +### mark - Marks task as done -`keyword (optional arguments)` +Example of usage: +> mark 1 Expected outcome: +``` +[T][X] Read book +``` -Description of the outcome. +### unmark - Marks task as undone +Example of usage: +> unmark 1 + +Expected outcome: ``` -expected output +[T][ ] Read book +``` + +### undo - Undoes the previous command + +Example of usage: +> undo + +Expected outcome: +``` +[T][X] Read book +[D][ ] Return Book (by: Monday 2pm) +[E][ ] Meeting (at: Monday 2pm-4pm) + +unmark 1 + +[T][ ] Read book +[D][ ] Return Book (by: Monday 2pm) +[E][ ] Meeting (at: Monday 2pm-4pm) + +undo + +[T][X] Read book +[D][ ] Return Book (by: Monday 2pm) +[E][ ] Meeting (at: Monday 2pm-4pm) +``` + +### bye - Exits Duke + +Example of usage: +> bye + +Expected outcome: ``` +Bye. Hope to see you again soon! +``` \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..69ae6ad484 Binary files /dev/null and b/docs/Ui.png differ diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..f3d88b1c2f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..b7c8c5dbf5 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000000..2fe81a7d95 --- /dev/null +++ b/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..62bd9b9cce --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,103 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/data/prevTaskList.txt b/src/main/data/prevTaskList.txt new file mode 100644 index 0000000000..f899b96796 --- /dev/null +++ b/src/main/data/prevTaskList.txt @@ -0,0 +1,3 @@ + +T|1|read book +D|0|return book| Monday 2pm \ No newline at end of file diff --git a/src/main/data/tasks.txt b/src/main/data/tasks.txt new file mode 100644 index 0000000000..5ef4a47a9e --- /dev/null +++ b/src/main/data/tasks.txt @@ -0,0 +1,3 @@ + +T|0|read book +D|0|return book| Monday 2pm \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334cc..0000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/duke/commands/AddDeadlineCommand.java b/src/main/java/duke/commands/AddDeadlineCommand.java new file mode 100644 index 0000000000..a5482a1212 --- /dev/null +++ b/src/main/java/duke/commands/AddDeadlineCommand.java @@ -0,0 +1,72 @@ +package duke.commands; + +import java.time.LocalDateTime; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.Deadline; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * AddDeadlineCommand implements method for adding Deadline to task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class AddDeadlineCommand extends Command { + + private final String input; + + /** + * Creates new AddDeadlineCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public AddDeadlineCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + if (!input.contains("/by")) { + throw new DukeException(" ☹ OOPS!!! Please enter in the format : \n" + + " deadline {task description} /by {day / date : YYYY-MM-DD / time}"); + } + this.input = input; + } + + /** + * Adds a new Deadline task to the task list. + * + * @param taskList the destination task list for the deadline to be added + * @param ui the ui to display message after the task is added + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + String taskDesc = input.substring(9, input.indexOf('/') - 1); + StringBuilder date = new StringBuilder(input.substring(input.indexOf('/') + 3)); + String[] dateArray = date.toString().split(" "); + if (isDate(dateArray[0])) { + LocalDateTime ld = LocalDateTime.parse(dateArray[0]); + date = new StringBuilder(ld.format(DTF) + " " + ld.getDayOfWeek()); + for (int i = 1; i < dateArray.length; i++) { + date.append(" ").append(dateArray[i]); + } + } + Deadline deadline = new Deadline(taskDesc, date.toString()); + taskList.addTask(deadline); + this.response = ui.addTaskMessage(deadline, taskList.getSize()); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/AddEventCommand.java b/src/main/java/duke/commands/AddEventCommand.java new file mode 100644 index 0000000000..b27f04ab7d --- /dev/null +++ b/src/main/java/duke/commands/AddEventCommand.java @@ -0,0 +1,72 @@ +package duke.commands; + +import java.time.LocalDateTime; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.Event; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * AddEventCommand implements method for adding Event to task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class AddEventCommand extends Command { + + private final String input; + + /** + * Creates new AddEventCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public AddEventCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! The description of a event cannot be empty."); + } + if (!input.contains("/at")) { + throw new DukeException(" ☹ OOPS!!! Please enter in the format : \n" + + " event {task description} /at {day / date : YYYY-MM-DD / time : no spaces}"); + } + this.input = input; + } + + /** + * Adds a new Event task to the task list. + * + * @param taskList the destination task list for the event to be added + * @param ui the ui to display message after the task is added + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + String taskDesc = input.substring(6, input.indexOf('/') - 1); + StringBuilder deadline = new StringBuilder(input.substring(input.indexOf('/') + 3)); + String[] deadlineArray = deadline.toString().split(" "); + if (isDate(deadlineArray[0])) { + LocalDateTime ld = LocalDateTime.parse(deadlineArray[0]); + deadline = new StringBuilder(ld.format(DTF) + " " + ld.getDayOfWeek()); + for (int i = 1; i < deadlineArray.length; i++) { + deadline.append(" ").append(deadlineArray[i]); + } + } + Event event = new Event(taskDesc, deadline.toString()); + taskList.addTask(event); + this.response = ui.addTaskMessage(event, taskList.getSize()); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/AddTodoCommand.java b/src/main/java/duke/commands/AddTodoCommand.java new file mode 100644 index 0000000000..7f6c728e80 --- /dev/null +++ b/src/main/java/duke/commands/AddTodoCommand.java @@ -0,0 +1,58 @@ +package duke.commands; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.tasks.Todo; +import duke.ui.UI; + +/** + * AddTodoCommand implements method for adding Todo to task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class AddTodoCommand extends Command { + + private final String input; + + /** + * Creates new AddTodoCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public AddTodoCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } + this.input = input; + } + + /** + * Adds a new Todo task to the task list. + * + * @param taskList the destination task list for the todo to be added + * @param ui the ui to display message after the task is added + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + // Index of beginning of Todo description after the "todo" keyword in command. + String taskDesc = input.substring(5); + Todo todo = new Todo(taskDesc); + taskList.addTask(todo); + this.response = ui.addTaskMessage(todo, taskList.getSize()); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/Command.java b/src/main/java/duke/commands/Command.java new file mode 100644 index 0000000000..09f002b46d --- /dev/null +++ b/src/main/java/duke/commands/Command.java @@ -0,0 +1,55 @@ +package duke.commands; + +import java.time.format.DateTimeFormatter; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * Abstract Command class implements methods for various commands recognised by Duke. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public abstract class Command { + + // Formatter to standardize the display of dates + protected static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("MMM d yyyy"); + + public String response = ""; + + /** + * Executes the given command. + * + * @param tasks the TaskList to be operated on + * @param ui the ui to display messages + * @param storage the Storage to store changes to the TaskList + */ + public abstract void execute(TaskList tasks, UI ui, Storage storage); + + public abstract boolean isExit(); + + /** + * Checks if the input is a Date or a Day. + * + * @param input the input to be checked + * + * @return True if the input is a Date + */ + public static boolean isDate(String input) { + return input.matches(".[0-9].*"); + } + + /** + * Checks that the input String is a valid command + * + * @param input the input String to be checked + * + * @return True if the input is valid + */ + public static boolean checkValid(String input) { + String[] str = input.split(" "); + return ((str.length > 1) && ((!input.equals("list")) && (!input.equals("exit")))); + } +} diff --git a/src/main/java/duke/commands/DeleteAllCommand.java b/src/main/java/duke/commands/DeleteAllCommand.java new file mode 100644 index 0000000000..4197469338 --- /dev/null +++ b/src/main/java/duke/commands/DeleteAllCommand.java @@ -0,0 +1,39 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * DeleteAllCommand implements method for clearing the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class DeleteAllCommand extends Command { + + /** + * Clears out the entire task list. + * + * @param taskList the task list to be cleared + * @param ui the ui to display message after the task list has been cleared + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + taskList.deleteAll(); + this.response = ui.deleteAllMessage(); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} + diff --git a/src/main/java/duke/commands/DeleteTaskCommand.java b/src/main/java/duke/commands/DeleteTaskCommand.java new file mode 100644 index 0000000000..a1548a13e2 --- /dev/null +++ b/src/main/java/duke/commands/DeleteTaskCommand.java @@ -0,0 +1,54 @@ +package duke.commands; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * DeleteTaskCommand implements method for deleting a task from the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class DeleteTaskCommand extends Command { + + private final int taskId; + + /** + * Creates new DeleteTaskCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public DeleteTaskCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! Please enter task to delete."); + } + this.taskId = Integer.parseInt(input.substring(7)) - 1; + } + + /** + * Deletes a specific task in the task list. + * + * @param taskList the task list from which the task is to be deleted + * @param ui the ui to display message after the task is deleted + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + this.response = ui.deleteTaskMessage(taskList.getTask(taskId), taskList.getSize()); + taskList.deleteTask(taskId); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/ExitCommand.java b/src/main/java/duke/commands/ExitCommand.java new file mode 100644 index 0000000000..184274bf83 --- /dev/null +++ b/src/main/java/duke/commands/ExitCommand.java @@ -0,0 +1,37 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * ExitCommand implements method for exiting Duke. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class ExitCommand extends Command { + + /** + * Exits the program + * + * @param taskList placeholder to match parameters defined in parent abstract class Command + * @param ui the ui to display Goodbye message + * @param storage placeholder to match parameters defined in parent abstract class Command + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + this.response = ui.goodbyeMessage(); + storage.store(taskList); + } + + /** + * Terminates the program in Duke.run(). + * + * @return True as this is the 'exit' command + */ + @Override + public boolean isExit() { + return true; + } +} diff --git a/src/main/java/duke/commands/FindTaskCommand.java b/src/main/java/duke/commands/FindTaskCommand.java new file mode 100644 index 0000000000..799b4dd315 --- /dev/null +++ b/src/main/java/duke/commands/FindTaskCommand.java @@ -0,0 +1,42 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * FindTaskCommand implements method for finding a task in the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class FindTaskCommand extends Command { + + private final String keywords; + + /** + * Creates new FindTaskCommand object. + * + * @param input the input string from the user + */ + public FindTaskCommand(String input) { + this.keywords = input.substring(5); + } + + /** + * Prints all the tasks in the task list that matches the keywords searched for by the user. + * + * @param tasks the TaskList to be search in + * @param ui the ui to display messages + * @param storage placeholder to match parameters defined in parent abstract class Command + */ + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + this.response = ui.findMessage() + "\n" + tasks.findTask(this.keywords); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/IncorrectCommand.java b/src/main/java/duke/commands/IncorrectCommand.java new file mode 100644 index 0000000000..22cc4409d2 --- /dev/null +++ b/src/main/java/duke/commands/IncorrectCommand.java @@ -0,0 +1,36 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * IncorrectCommand implements method for unrecognised commands by Duke. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class IncorrectCommand extends Command { + + /** + * Informs the user that the command is not recognised. + * + * @param tasks placeholder to match parameters defined in parent abstract class Command + * @param ui the ui to display incorrect command message + * @param storage placeholder to match parameters defined in parent abstract class Command + */ + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + this.response = ui.incorrectCommandMessage(); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/MarkTaskCommand.java b/src/main/java/duke/commands/MarkTaskCommand.java new file mode 100644 index 0000000000..310fd9f358 --- /dev/null +++ b/src/main/java/duke/commands/MarkTaskCommand.java @@ -0,0 +1,54 @@ +package duke.commands; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * MarkTaskCommand implements method for marking a task in the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class MarkTaskCommand extends Command { + + private final int taskId; + + /** + * Creates new MarkTaskCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public MarkTaskCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! Please enter task to unmark."); + } + this.taskId = Integer.parseInt(input.substring(5)) - 1; + } + + /** + * Marks the specified task in the task list. + * + * @param taskList the task list which contains the task + * @param ui the ui to display message after the task is marked + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + taskList.markTask(this.taskId); + this.response = ui.markTaskMessage(taskList.getTask(this.taskId)); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/PrintListCommand.java b/src/main/java/duke/commands/PrintListCommand.java new file mode 100644 index 0000000000..d76189cdd5 --- /dev/null +++ b/src/main/java/duke/commands/PrintListCommand.java @@ -0,0 +1,37 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * PrintListCommand implements method for printing out the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class PrintListCommand extends Command { + + /** + * Prints out the current task list. + * + * @param taskList the task list to be printed + * @param ui the ui to display message after the task list is printed + * @param storage placeholder to match parameters defined in parent abstract class Command + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + this.response = taskList.printList() + "\n" + + ui.printListMessage(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/UndoCommand.java b/src/main/java/duke/commands/UndoCommand.java new file mode 100644 index 0000000000..40a1a80bf7 --- /dev/null +++ b/src/main/java/duke/commands/UndoCommand.java @@ -0,0 +1,40 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * UndoCommand implements method for undoing the previous command. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class UndoCommand extends Command { + + /** + * Undoes the previous command. + * + * @param taskList the task list + * @param ui the ui to display message after the command is undone + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + taskList.undo(); + this.response = ui.undoMessage() + "\n" + + taskList.printList() + "\n" + + ui.printListMessage(taskList); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/commands/UnmarkTaskCommand.java b/src/main/java/duke/commands/UnmarkTaskCommand.java new file mode 100644 index 0000000000..e655a43447 --- /dev/null +++ b/src/main/java/duke/commands/UnmarkTaskCommand.java @@ -0,0 +1,53 @@ +package duke.commands; + +import duke.exceptions.DukeException; +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; +/** + * UnmarkTaskCommand implements method for unmarking a task in the task list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class UnmarkTaskCommand extends Command { + + private final int taskId; + + /** + * Creates new UnmarkTaskCommand object. + * + * @param input the input string from the user + * @throws DukeException to handle if the input string is invalid. + */ + public UnmarkTaskCommand(String input) throws DukeException { + if (!checkValid(input)) { + throw new DukeException(" ☹ OOPS!!! Please enter task to unmark."); + } + this.taskId = Integer.parseInt(input.substring(7)) - 1; + } + + /** + * Unmarks the specified task in the task list. + * + * @param taskList the task list which contains the task + * @param ui the ui to display message after the task is unmarked + * @param storage the storage to handle storing of the new task list + */ + @Override + public void execute(TaskList taskList, UI ui, Storage storage) { + taskList.unmarkTask(this.taskId); + this.response = ui.unmarkTaskMessage(taskList.getTask(this.taskId)); + storage.store(taskList); + } + + /** + * Prevents the program from terminating in Duke.run(). + * + * @return False as this is not the 'exit' command + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/exceptions/DukeException.java b/src/main/java/duke/exceptions/DukeException.java new file mode 100644 index 0000000000..2011ed3d69 --- /dev/null +++ b/src/main/java/duke/exceptions/DukeException.java @@ -0,0 +1,19 @@ +package duke.exceptions; + +/** + * DukeException implements method for exceptions in Duke. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class DukeException extends Exception { + + /** + * Creates a new DukeException object. + * + * @param message the error message to be displayed + */ + public DukeException(String message) { + super(" " + message + "\n"); + } +} diff --git a/src/main/java/duke/main/DialogBox.java b/src/main/java/duke/main/DialogBox.java new file mode 100644 index 0000000000..d329372d42 --- /dev/null +++ b/src/main/java/duke/main/DialogBox.java @@ -0,0 +1,84 @@ +package duke.main; + +import java.io.IOException; +import java.util.Collections; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +/** + * DialogBox implements method for dialog boxes in GUI. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class DialogBox extends HBox { + @FXML + private Label dialog; + @FXML + private ImageView displayPicture; + + /** + * Constructs a new DialogBox object. + * + * @param text input from user / output from Duke + * @param img profile picture + */ + private DialogBox(String text, Image img) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml")); + fxmlLoader.setController(this); + fxmlLoader.setRoot(this); + fxmlLoader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + + dialog.setText(text); + displayPicture.setImage(img); + } + + /** + * Flips the dialog box such that the ImageView is on the left and text on the right. + */ + private void flip() { + ObservableList tmp = FXCollections.observableArrayList(this.getChildren()); + Collections.reverse(tmp); + getChildren().setAll(tmp); + setAlignment(Pos.TOP_LEFT); + } + + /** + * Creates a new Dialog box with input and user's profile picture. + * + * @param text input from user + * @param img profile picture + * + * @return a new dialog box + */ + public static DialogBox getUserDialog(String text, Image img) { + return new DialogBox(text, img); + } + + /** + * Creates a new Dialog box with output and Duke's profile picture. + * + * @param text output from Duke + * @param img profile picture + * + * @return a new dialog box + */ + public static DialogBox getDukeDialog(String text, Image img) { + var db = new DialogBox(text, img); + db.flip(); + return db; + } +} diff --git a/src/main/java/duke/main/Duke.java b/src/main/java/duke/main/Duke.java new file mode 100644 index 0000000000..c781746133 --- /dev/null +++ b/src/main/java/duke/main/Duke.java @@ -0,0 +1,65 @@ +package duke.main; + +import duke.commands.Command; +import duke.exceptions.DukeException; +import duke.parser.Parser; +import duke.storage.Storage; +import duke.tasks.TaskList; +import duke.ui.UI; + +/** + * Duke implements the Duke bot, functions as a simple todo-list. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ + +public class Duke { + + private final Storage storage; + private TaskList tasks; + private final UI ui; + + /** + * Creates new Duke object. + * + * @param filePath the file path to store teh task list + */ + public Duke(String filePath) { + ui = new UI(); + storage = new Storage(filePath); + try { + tasks = new TaskList(storage.load()); + } catch (DukeException e) { + System.out.println(e.getMessage()); + tasks = new TaskList(); + } + } + + /** + * Returns the greeting message when Duke is opened + * + * @return Greeting message. + */ + public String greeting() { + return ui.welcomeMessage(); + } + + /** + * Returns the response from Duke + * + * @param input input from the user + * + * @return the response from Duke + */ + public String getResponse(String input) { + try { + Command c = Parser.parse(input); + c.execute(tasks, ui, storage); + return c.response; + } catch (DukeException e) { + return e.getMessage(); + } + } + +} diff --git a/src/main/java/duke/main/Launcher.java b/src/main/java/duke/main/Launcher.java new file mode 100644 index 0000000000..6ace5a7ae2 --- /dev/null +++ b/src/main/java/duke/main/Launcher.java @@ -0,0 +1,15 @@ +package duke.main; + +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/duke/main/Main.java b/src/main/java/duke/main/Main.java new file mode 100644 index 0000000000..229fa16468 --- /dev/null +++ b/src/main/java/duke/main/Main.java @@ -0,0 +1,40 @@ +package duke.main; + +import java.io.IOException; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +/** + * A GUI for Duke using FXML. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Main extends Application { + + private final Duke duke = new Duke("data/tasks.txt"); + + /** + * Runs the main logic of the window displayed by GUI. + * + * @param stage the stage provided during execution + */ + @Override + public void start(Stage stage) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = fxmlLoader.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + fxmlLoader.getController().setDuke(duke); + fxmlLoader.getController().greeting(); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/main/MainWindow.java b/src/main/java/duke/main/MainWindow.java new file mode 100644 index 0000000000..93619d5637 --- /dev/null +++ b/src/main/java/duke/main/MainWindow.java @@ -0,0 +1,76 @@ +package duke.main; + +import javafx.animation.PauseTransition; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; +import javafx.util.Duration; + +import java.util.Objects; + +/** + * Controller for MainWindow. Provides the layout for the other controls. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class MainWindow extends AnchorPane { + @FXML + private ScrollPane scrollPane; + @FXML + private VBox dialogContainer; + @FXML + private TextField userInput; + @FXML + private Button sendButton; + + private Duke duke; + + private final Image userImage = new Image(Objects.requireNonNull(this.getClass().getResourceAsStream("/images/User.png"))); + private final Image dukeImage = new Image(Objects.requireNonNull(this.getClass().getResourceAsStream("/images/Duke.png"))); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void setDuke(Duke d) { + duke = d; + } + + @FXML + public void greeting() { + dialogContainer.getChildren().addAll(DialogBox.getDukeDialog(duke.greeting(), dukeImage)); + } + + /** + * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to + * the dialog container. Clears the user input after processing. + */ + @FXML + private void handleUserInput() { + String input = userInput.getText(); + String response = duke.getResponse(input); + dialogContainer.getChildren().addAll(DialogBox.getUserDialog(input, userImage)); + if (input.equals("bye")) { + PauseTransition delay = new PauseTransition(Duration.seconds(1.0)); + delay.setOnFinished(scene -> { + Platform.exit(); + System.exit(0); + }); + delay.play(); + userInput.setDisable(true); + sendButton.setDisable(true); + dialogContainer.getChildren().add(DialogBox.getDukeDialog(response, dukeImage)); + } else { + dialogContainer.getChildren().add(DialogBox.getDukeDialog(response, dukeImage)); + } + + userInput.clear(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 0000000000..c145fd6d25 --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,84 @@ +package duke.parser; + +import duke.commands.*; +import duke.exceptions.DukeException; + +/** + * AddDeadlineCommand implements method for parsing inputs by the user. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Parser { + + /** + * Parses the input and directs Duke to execute the appropriate command. + * + * @param input input String from the user + * + * @return Command to be executed in Duke.run() + * + * @throws DukeException to handle inappropriate inputs + */ + public static Command parse(String input) throws DukeException { + //Fetch and display taskList + if (input.equals("list")) { + return new PrintListCommand(); + } + + // Mark a task + if (input.startsWith("mark")) { + return new MarkTaskCommand(input); + } + + // Unmark a task + if (input.startsWith("unmark")) { + return new UnmarkTaskCommand(input); + } + + // Deletes all tasks from the list + if (input.equals("delete all")) { + return new DeleteAllCommand(); + } + + // Delete a task + if (input.startsWith("delete")) { + return new DeleteTaskCommand(input); + } + + // Add todo to taskList + if (input.startsWith("todo")) { + return new AddTodoCommand(input); + } + + // Add deadline to taskList + if (input.startsWith("deadline")) { + return new AddDeadlineCommand(input); + } + + // Add event to taskList + if (input.startsWith("event")) { + return new AddEventCommand(input); + } + + // Finds the tasks matching the given keywords + if (input.startsWith("find")) { + return new FindTaskCommand(input); + } + + // Undoes the previous command + if (input.startsWith("undo")) { + return new UndoCommand(); + } + + // Exits program + if (input.startsWith("bye")) { + return new ExitCommand(); + } + + // In case of an unrecognised command + return new IncorrectCommand(); + } +} + + diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 0000000000..e6113971c0 --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,110 @@ +package duke.storage; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import duke.tasks.Task; +import duke.tasks.TaskList; + +/** + * Storage implements method for storing and fetching the task list from hard drive. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Storage { + + /** Default file path used if the user does not provide the file name. */ + private static final String PATH = Paths.get(".").toAbsolutePath().toString(); + private static final String RELATIVE_FILEPATH = PATH.substring(0, PATH.length() - 1) + "src/main/"; + + private String filePath = ""; + + /** + * Saves file to a default path if one was not provided by the user. + */ + public Storage() { + this.filePath = RELATIVE_FILEPATH + "data/tasks.txt"; + } + + /** + * Saves the file to the file path specified by the user. + * + * @param filePath path for the file to be stored + */ + @SuppressWarnings("ResultOfMethodCallIgnored") + public Storage(String filePath) { + try { + this.filePath = RELATIVE_FILEPATH + filePath; + File file = new File(this.filePath); + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Stores the given task list to the given path. + * + * @param toStore the task list to be stored + */ + public void store(TaskList toStore) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(this.filePath)); + List encodedTaskList = TaskListEncoder.encodeTaskList(toStore); + for (String task : encodedTaskList) { + writer.write("\n" + task); + } + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Stores the given task list (as ArrayList) to the given path. + * + * @param toStore the task list to be stored + */ + public void store(ArrayList toStore) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(this.filePath)); + List encodedTaskList = TaskListEncoder.encodeTaskList(toStore); + for (String task : encodedTaskList) { + writer.write("\n" + task); + } + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Loads the stored task list from the specified file path. + * + * @return an ArrayList to be passed as parameters for the TaskList constructor + */ + public ArrayList load() { + ArrayList result = new ArrayList<>(); + try { + BufferedReader reader = new BufferedReader(new FileReader(this.filePath)); + List encodedTaskList = new ArrayList<>(); + String line; + while ((line = reader.readLine()) != null) { + encodedTaskList.add(line); + } + reader.close(); + result = TaskListDecoder.decodeTaskList(encodedTaskList); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } +} diff --git a/src/main/java/duke/storage/TaskListDecoder.java b/src/main/java/duke/storage/TaskListDecoder.java new file mode 100644 index 0000000000..16a570054e --- /dev/null +++ b/src/main/java/duke/storage/TaskListDecoder.java @@ -0,0 +1,78 @@ +package duke.storage; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import duke.exceptions.DukeException; +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; + +/** + * Decodes the storage data file into a {@code TaskList} object. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class TaskListDecoder { + + /** + * Decodes a List into a TaskList containing the decoded tasks. + * + * @param encodedTaskList the task list to be decoded + * + * @return the decoded task list + */ + public static ArrayList decodeTaskList(List encodedTaskList) { + ArrayList decodedTaskList = new ArrayList<>(); + for (String encodedTask : encodedTaskList) { + decodedTaskList.add(decodeTaskFromString(encodedTask)); + } + decodedTaskList.remove(null); + return decodedTaskList; + } + + /** + * Decodes the given String into a task. + * + * @param encodedTask the String to be decoded + * + * @return the task represented by the encoded String + */ + public static Task decodeTaskFromString(String encodedTask) { + Task result = null; + String[] str = encodedTask.split("\\|"); + switch (str[0]) { + case "T": + Todo todo = new Todo(str[2]); + if (Objects.equals(str[1], "1")) { + todo.mark(); + } + result = todo; + break; + case "D": + Deadline deadline = new Deadline(str[2], str[3]); + if (Objects.equals(str[1], "1")) { + deadline.mark(); + } + result = deadline; + break; + case "E": + Event event = new Event(str[2], str[3]); + if (Objects.equals(str[1], "1")) { + event.mark(); + } + result = event; + break; + default: + try { + throw new DukeException(" OOPS!!! Unrecognised task"); + } catch (DukeException e) { + System.out.println(e.getMessage()); + } + } + return result; + } +} diff --git a/src/main/java/duke/storage/TaskListEncoder.java b/src/main/java/duke/storage/TaskListEncoder.java new file mode 100644 index 0000000000..5aa2e15780 --- /dev/null +++ b/src/main/java/duke/storage/TaskListEncoder.java @@ -0,0 +1,59 @@ +package duke.storage; + +import java.util.ArrayList; +import java.util.List; + +import duke.tasks.Task; +import duke.tasks.TaskList; + +/** + * Encode the {@code TaskList} object into a data file for storage. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class TaskListEncoder { + + /** + * Encodes a task list into a List containing the decoded tasks. + * + * @param toEncode the task list to be encoded + * + * @return the encoded task list + */ + public static List encodeTaskList(TaskList toEncode) { + final List encodedTaskList = new ArrayList<>(); + for (Task task : toEncode.getAllTasks()) { + encodedTaskList.add(encodeTaskToString(task)); + } + return encodedTaskList; + } + + /** + * Encodes a ArrayList into a List containing the decoded tasks. + * + * @param toEncode the task list to be encoded + * + * @return the encoded task list + */ + public static List encodeTaskList(ArrayList toEncode) { + final List encodedTaskList = new ArrayList<>(); + for (Task task : toEncode) { + encodedTaskList.add(encodeTaskToString(task)); + } + return encodedTaskList; + } + + /** + * Encodes the given task into a template format to be stored. + * + * @param task the task to be encoded + * + * @return a String representing the encoded task + */ + public static String encodeTaskToString(Task task) { + return task.getTaskType() + "|" + + (task.isisCompleted() ? 1 : 0) + "|" + + task.getDesc(); + } +} diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java new file mode 100644 index 0000000000..5d02e4624e --- /dev/null +++ b/src/main/java/duke/tasks/Deadline.java @@ -0,0 +1,49 @@ +package duke.tasks; + +/** + * Deadline implements methods for Deadline objects. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Deadline extends Task { + + private final String date; + private final String taskDesc; + + /** + * Creates a new Deadline object. + * + * @param taskDesc description of the deadline task + * @param date deadline of the deadline task + */ + public Deadline(String taskDesc, String date) { + super(taskDesc); + this.taskDesc = taskDesc; + this.date = date; + } + + /** + * Fetches the type of task for identification during encoding and decoding for storage. + * + * @return a Character 'D' representing Deadline + */ + @Override + public char getTaskType() { + return 'D'; + } + + /** + * Fetches the description of the task. + * + * @return the description of the task + */ + @Override + public String getDesc() { + return this.taskDesc + "|" + this.date; + } + + public String toString() { + return "[D]" + super.toString() + " (by: " + date + ")"; + } +} diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java new file mode 100644 index 0000000000..91fb963f33 --- /dev/null +++ b/src/main/java/duke/tasks/Event.java @@ -0,0 +1,49 @@ +package duke.tasks; + +/** + * Event implements methods for Event objects. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Event extends Task { + + private final String deadline; + private final String taskDesc; + + /** + * Creates a new Event object. + * + * @param taskDesc description of the event task + * @param date deadline of the event task + */ + public Event(String taskDesc, String deadline) { + super(taskDesc); + this.taskDesc = taskDesc; + this.deadline = deadline; + } + + /** + * Fetches the type of task for identification during encoding and decoding for storage. + * + * @return a Character 'E' representing Event + */ + @Override + public char getTaskType() { + return 'E'; + } + + /** + * Fetches the description of the task. + * + * @return the description of the task + */ + @Override + public String getDesc() { + return this.taskDesc + "|" + this.deadline; + } + + public String toString() { + return "[E]" + super.toString() + " (at: " + deadline + ")"; + } +} diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java new file mode 100644 index 0000000000..044190a6ac --- /dev/null +++ b/src/main/java/duke/tasks/Task.java @@ -0,0 +1,70 @@ +package duke.tasks; + +/** + * Event implements methods for Task objects. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public abstract class Task { + + private final String taskDesc; + private boolean isCompleted; + + /** + * Creates a new Task object. + * + * @param taskDesc description of the task + */ + public Task(String taskDesc) { + this.taskDesc = taskDesc; + this.isCompleted = false; + } + + /** + * Fetches the description of the task. + * + * @return the description of the task + */ + public String getDesc() { + return this.taskDesc; + } + + /** + * Checks if the task has been marked as completed. + * + * @return True if the task is completed + */ + public boolean isisCompleted() { + return this.isCompleted; + } + + /** + * Fetches the type of task for identification during encoding and decoding for storage. + * + * @return a Character representing the type of task + */ + public abstract char getTaskType(); + + /** + * Marks the task as done. + */ + public void mark() { + this.isCompleted = true; + } + + /** + * Marks the task as not done. + */ + public void unmark() { + this.isCompleted = false; + } + + public String toString() { + if (isCompleted) { + return "[X] " + taskDesc; + } else { + return "[ ] " + taskDesc; + } + } +} diff --git a/src/main/java/duke/tasks/TaskList.java b/src/main/java/duke/tasks/TaskList.java new file mode 100644 index 0000000000..61b65d903e --- /dev/null +++ b/src/main/java/duke/tasks/TaskList.java @@ -0,0 +1,182 @@ +package duke.tasks; + +import java.util.ArrayList; + +import duke.exceptions.DukeException; +import duke.storage.Storage; + +/** + * TaskList implements methods for TaskList objects. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class TaskList { + + private static ArrayList taskList; + private final Storage prevStorage = new Storage("data/prevTaskList.txt"); + + /** + * Constructs new TaskList using the given ArrayList. + * + * @param taskArrayList the ArrayList to construct the TaskList from + * + * @throws DukeException to handle if the given ArrayList is empty or invalid + */ + public TaskList(ArrayList taskArrayList) throws DukeException { + taskList = taskArrayList; + } + + /** + * Constructs an empty TaskList if no ArrayList is given. + */ + public TaskList() { + taskList = new ArrayList<>(); + } + + /** + * Checks if the task list is empty. + * + * @return true if the task list is empty + */ + public boolean isEmpty() { + return taskList.size() == 0; + } + + /** + * Fetches the current number of tasks. + * + * @return an Integer representing the current number of tasks + */ + public int getSize() { + return taskList.size(); + } + + /** + * Prints out the current task list. + */ + public String printList() { + StringBuilder res = new StringBuilder(); + try { + if (isEmpty()) { + throw new DukeException(" ☹ OOPS!!! Seems like your list is empty."); + } else { + for (int i = 0; i < taskList.size(); i++) { + res.append(" ").append(i + 1).append(". ") + .append(taskList.get(i).toString()) + .append("\n"); + } + } + } catch (DukeException e) { + System.out.println(e.getMessage()); + } + return res.toString(); + } + + /** + * Prints out list of tasks that contains the keyword searched for by the user + * + * @param input the keyword to be searched for by the user + */ + public String findTask(String input) { + boolean noMatches = true; + ArrayList res = new ArrayList<>(); + for (int i = 0; i < taskList.size(); i++) { + Task task = taskList.get(i); + if (task.getDesc().contains(input)) { + noMatches = false; + res.add( " " + (i + 1) + ". " + task); + } + } + if (noMatches) { + try { + throw new DukeException(" ☹ OOPS!!! Seems like there are no tasks matching this description."); + } catch (DukeException e) { + System.out.println(e.getMessage()); + } + } + return res.toString(); + } + + /** + * Marks the task at the given index as done. + * + * @param taskIndex the index of the task to be marked + */ + public void markTask(int taskIndex) { + saveTaskList(); + taskList.get(taskIndex).mark(); + } + + /** + * Marks the task at the given index as not done. + * + * @param taskIndex the index of the task to be marked + */ + public void unmarkTask(int taskIndex) { + saveTaskList(); + taskList.get(taskIndex).unmark(); + } + + /** + * Adds given task to the task list. + * + * @param task the task to be added + */ + public void addTask(Task task) { + saveTaskList(); + taskList.add(task); + } + + /** + * Deletes the task at the given index from the list. + * + * @param index index of the task to be deleted + */ + public void deleteTask(int index) { + saveTaskList(); + taskList.remove(index); + } + + /** + * Fetches the task at the given index of the list. + * + * @param taskId index of the task to be fetched + * + * @return the task at the given index + */ + public Task getTask(int taskId) { + return taskList.get(taskId); + } + + /** + * Clears out the task list. + */ + public void deleteAll() { + saveTaskList(); + taskList = new ArrayList<>(); + } + + /** + * Fetches all the tasks in the task list. + * + * @return an ArrayList containing all the tasks in the task list + */ + public ArrayList getAllTasks() { + return taskList; + } + + /** + * Saves the taskList into src/main/data/prevTaskList.txt. + */ + public void saveTaskList() { + prevStorage.store(taskList); + } + + /** + * Updates the taskList as the previously saved taskList. + */ + public void undo() { + taskList = prevStorage.load(); + } +} diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java new file mode 100644 index 0000000000..4627c074ee --- /dev/null +++ b/src/main/java/duke/tasks/Todo.java @@ -0,0 +1,33 @@ +package duke.tasks; + +/** + * Todo implements methods for Todo objects. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class Todo extends Task { + + /** + * Creates a new Todo object. + * + * @param task description of the deadline task + */ + public Todo(String task) { + super(task); + } + + /** + * Fetches the type of task for identification during encoding and decoding for storage. + * + * @return a Character 'T' representing Todo + */ + @Override + public char getTaskType() { + return 'T'; + } + + public String toString() { + return "[T]" + super.toString(); + } +} diff --git a/src/main/java/duke/ui/UI.java b/src/main/java/duke/ui/UI.java new file mode 100644 index 0000000000..17731ec6f3 --- /dev/null +++ b/src/main/java/duke/ui/UI.java @@ -0,0 +1,70 @@ +package duke.ui; + +import duke.tasks.Task; +import duke.tasks.TaskList; + +/** + * Event implements methods for displaying messages in Duke. + * + * @author Isaac Li Haoyang + * @version v0.2 + */ +public class UI { + + public String welcomeMessage() { + return " Hello! I'm Duke\n What can I do for you?"; + } + + public String goodbyeMessage() { + return " Bye. Hope to see you again soon!"; + } + + public boolean checkValid(String input) { + String[] str = input.split(" "); + return (str.length > 1); + } + + public String printListMessage(TaskList taskList) { + return " " + " You have " + taskList.getSize() + " tasks in the list."; + } + + public String findMessage() { + return " Here are the matching tasks in your list: "; + } + + public String addTaskMessage(Task task, int listSize) { + return " " + " Got it. I've added this task: \n" + + " " + task + "\n" + + " " + " Now you have " + listSize + " tasks in the list."; + } + + public String deleteTaskMessage(Task task, int listSize) { + return " " + " Got it. I've removed this task: \n" + + " " + task + "\n" + + " " + " Now you have " + listSize + " tasks in the list."; + } + + public String deleteAllMessage() { + return " " + " Got it. I've removed all tasks\n" + + " " + " Now you have 0 tasks in the list."; + } + + public String markTaskMessage(Task task) { + return " Nice! I've marked this task as done:\n" + + " " + task; + } + + public String unmarkTaskMessage(Task task) { + return " OK, I've marked this task as not done yet:\n" + + " " + task; + } + + public String incorrectCommandMessage() { + return " ☹ OOPS!!! I'm sorry, but I don't know what that means :-(."; + } + + public String undoMessage() { + return " OK, I've undone the previous command.\n" + + " Here is your task list:"; + } +} diff --git a/src/main/resources/images/Duke.png b/src/main/resources/images/Duke.png new file mode 100644 index 0000000000..3941155449 Binary files /dev/null and b/src/main/resources/images/Duke.png differ diff --git a/src/main/resources/images/User.png b/src/main/resources/images/User.png new file mode 100644 index 0000000000..bd2f69d3d3 Binary files /dev/null and b/src/main/resources/images/User.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..6a4f69e65f --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..9323e6b51e --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +