diff --git a/.classpath b/.classpath new file mode 100644 index 0000000000..bfdccfd000 --- /dev/null +++ b/.classpath @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index f69985ef1f..31f49a30b8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ /.idea/ /out/ /*.iml +/.project +/.editorconfig # Gradle build files /.gradle/ @@ -15,3 +17,5 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT +/data/* +/src/main/java/META-INF/ diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000000..bceedc473b --- /dev/null +++ b/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 +gradle.user.home= +java.home=C\:/Program Files/Java/jdk-11.0.9 +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..b36d45a6de --- /dev/null +++ b/build.gradle @@ -0,0 +1,73 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + id 'com.github.johnrengelman.shadow' version '5.1.0' +} + +checkstyle { + toolVersion = '8.29' +} + +repositories { + mavenCentral() +} + +sourceSets { + main { + resources { + srcDirs "src/main/resources" + } + } +} + +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.Launcher" +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +checkstyle { + toolVersion = '8.29' +} + +run{ + standardInput = System.in +} diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..7951f3a6c4 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,398 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 fd44069597..075ed89610 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,20 +1,175 @@ -# User Guide +# **DUKE** - User Guide ## Features +1. todo +2. deadline +3. event +4. list +5. done +6. delete +7. check +8. find +9. help +10. exit -### Feature 1 -Description of feature. +`<>` - required inputs +`{}` - optional inputs +### 1. `todo` - Add todo task +Adds a todo task to the list -## Usage +Format: `todo {task name}` -### `Keyword` - Describe action +#### Usage +Example of usage: `todo read book` -Describe action and its outcome. +Expected response: +```` +Got it. I've added this task: + [T][ ] read book +Now you have 1 task in the list +```` -Example of usage: +### 2. `deadline` - Add deadline task +Adds a deadline task to the list -`keyword (optional arguments)` +Format: `deadline {task name} /by {date in YYYY-MM-DD format}` -Expected outcome: +#### Usage +Example of usage: `deadline submit essay /by 2021-05-03` -`outcome` +Expected response: +```` +Got it. I've added this task: + [D][ ] submit essay (by: May 3 2021) +Now you have 2 tasks in the list +```` + +### 3. `event` - Add event task +Adds a event task to the list + +Format: `event {task name} /at {date in YYYY-MM-DD format}` + +#### Usage +Example of usage: `event workshop /at 2021-03-05` + +Expected response: +```` +Got it. I've added this task: + [E][ ] workshop (at: Mar 5 2021) +Now you have 3 tasks in the list +```` + +### 4. `list` - lists tasks +Lists all current tasks + +Format: `list` + +#### Usage +Example of usage: list + +Expected response: +```` +1. [T][ ] read book +2. [D][ ] submit essay (by: May 3 2021) +3. [E][ ] workshop (at: Mar 5 2021) +```` + +### 5. `done` - Marks task as done +Marks the given task as done + +Format: `done {task number}` + +#### Usage +Example of usage: `done 2` + +Expected response: +```` +Nice! I've marked this taks as done: + [D][X] submit essay (by: May 3 2021) +```` + +### 6. `delete` - Deletes a task +Deletes the given task from the list + +Format: `delete {task number}` + +#### Usage +Example of usage: `delete 2` + +Expected response: +```` +Noted. I've removed this task: + [D][X] submit essay (by: May 3 2021) + Now you have 2 tasks in the list. +```` + +### 7. `check` - Checks tasks on date +Looks for all tasks on a given date + +Format: `check {date in YYYY-MM-DD format}` + +#### Usage +Example of usage: `check 2021-03-05` + +Expected response: +```` +1. [E][ ] workshop (at: Mar 5 2021) +```` + +### 8. `find` - Finds tasks by name +Looks for all tasks whose name contains a given key word + +Format: `find {key work}` + +#### Usage +Example of usage: `find read` + +Expected response: +```` +1. [T][ ] read book +```` + +### 9. `help` - Gives help for a command +Gets help for the list of commands or a specific command + +Format: `help (command)` + +#### Usage +Example of usage: `help` + +Expected response: +```` +List of commands: +1. todo +2. deadline +3. event +4. list +5. done +6. delete +7. find +8. check +9. exit +Type 'help ' in the chat to see the details! +```` + +Example of usage: `help done` + +Expected response: +```` +Marks the task as done + +Format: done +```` + +### 10. `exit` - Exits duke +Prompts duke to say goodbye and closes the programme + +Format: `exit` + +#### Usage +Example of usage: `exit` + +Expected response: +```` +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..d9cf3f6de1 Binary files /dev/null and b/docs/Ui.png differ diff --git a/docs/_config.yml b/docs/_config.yml new file mode 100644 index 0000000000..c4192631f2 --- /dev/null +++ b/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file 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/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/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..2c9a9745c5 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..b8c0f9c373 --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,9 @@ +package duke; + +import javafx.application.Application; + +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/duke/Main.java b/src/main/java/duke/Main.java new file mode 100644 index 0000000000..05dc4c8da1 --- /dev/null +++ b/src/main/java/duke/Main.java @@ -0,0 +1,35 @@ +package duke; + +import java.io.IOException; + +import duke.logic.Duke; +import duke.ui.MainWindow; +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. + */ +public class Main extends Application { + + private Duke duke = new Duke("data/tasks.txt"); + + @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().setStage(stage); + fxmlLoader.getController().greet(); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java new file mode 100644 index 0000000000..e1ad540803 --- /dev/null +++ b/src/main/java/duke/command/AddCommand.java @@ -0,0 +1,42 @@ +package duke.command; + +import java.io.IOException; + +import duke.logic.DukeException; +import duke.logic.Storage; +import duke.task.Task; +import duke.task.TaskList; + +/** + * Represents a command telling duke to add a task to its task list + */ +public class AddCommand implements Command { + private Task taskToAdd; + + /** + * Constructor + * @param task Task to be added + */ + public AddCommand(Task task) { + this.taskToAdd = task; + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + tasks.add(this.taskToAdd); + try { + storage.store(tasks); + } catch (IOException e) { + throw new DukeException("Cannot save tasks. Save file not found"); + } + String numOfTasks = tasks.size() + (tasks.size() > 1 ? " tasks" : " task"); + String addResponse = "Got it. I've added this task:\n " + this.taskToAdd + + "\nNow you have " + numOfTasks + " in the list."; + return addResponse; + } +} diff --git a/src/main/java/duke/command/CheckCommand.java b/src/main/java/duke/command/CheckCommand.java new file mode 100644 index 0000000000..109eca7fff --- /dev/null +++ b/src/main/java/duke/command/CheckCommand.java @@ -0,0 +1,42 @@ +package duke.command; + +import java.time.LocalDate; + +import duke.logic.Storage; +import duke.task.Task; +import duke.task.TaskList; + +/** + * Represetns a command telling to list all tasks on a specified date + */ +public class CheckCommand implements Command { + private LocalDate date; + + /** + * Constructor + * @param date Date to check + */ + public CheckCommand(LocalDate date) { + this.date = date; + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + TaskList filteredTasks = new TaskList(); + + for (int i = 1; i <= tasks.size(); i++) { + Task currTask = tasks.get(i); + if (this.date.equals(currTask.getDate())) { + filteredTasks.add(currTask); + } + } + + String checkResponse = filteredTasks.toString(); + return checkResponse; + } +} diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 0000000000..dc5dd0a06f --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,23 @@ +package duke.command; + +import duke.logic.Storage; +import duke.task.TaskList; + +/** + * Represents a command to duke + */ +public interface Command { + /** + * Checks if the command is an exit command + * @return True if is an exit command, false otherwise + */ + public boolean isExit(); + + /** + * Executes the command and gets the response + * @param tasks List of tasks to operate on + * @param storage Storage manager for loading and saving task files + * @return Duke's response after executing the command + */ + public String execute(TaskList tasks, Storage storage); +} diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 0000000000..9b0ae1b93f --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,48 @@ +package duke.command; + +import java.io.IOException; + +import duke.logic.DukeException; +import duke.logic.Storage; +import duke.task.Task; +import duke.task.TaskList; + +/** + * Represents a command telling duke to delete a task + */ +public class DeleteCommand implements Command { + private int taskNum; + + /** + * Constructor + * @param taskNum The task number of the task to be deleted + */ + public DeleteCommand(int taskNum) { + this.taskNum = taskNum; + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + if (this.taskNum > tasks.size()) { + throw new DukeException("No such task exists!"); + } + Task deletedTask = tasks.get(taskNum); + tasks.delete(taskNum); + + try { + storage.store(tasks); + } catch (IOException e) { + throw new DukeException("Cannot save delete. Save file not found"); + } + + String numOfTasks = tasks.size() + (tasks.size() > 1 ? " tasks" : " task"); + String deleteResponse = "Noted. I've removed this task:\n " + deletedTask + "\nNow you have " + + numOfTasks + " in the list."; + return deleteResponse; + } +} diff --git a/src/main/java/duke/command/DoneCommand.java b/src/main/java/duke/command/DoneCommand.java new file mode 100644 index 0000000000..010595730f --- /dev/null +++ b/src/main/java/duke/command/DoneCommand.java @@ -0,0 +1,44 @@ +package duke.command; + +import java.io.IOException; + +import duke.logic.DukeException; +import duke.logic.Storage; +import duke.task.TaskList; + +/** + * Represents a command telling duke to mark a task as complete + */ +public class DoneCommand implements Command { + private int taskNum; + + /** + * Constructor + * @param taskNum Task number of the task to be marked as complete + */ + public DoneCommand(int taskNum) { + this.taskNum = taskNum; + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + if (this.taskNum > tasks.size()) { + throw new DukeException("No such task exists!"); + } + tasks.done(taskNum); + + try { + storage.store(tasks); + } catch (IOException e) { + throw new DukeException("Cannot save tasks. Save file not found"); + } + + String doneResponse = "Nice! I've marked this task as done:\n " + tasks.get(taskNum).toString(); + return doneResponse; + } +} diff --git a/src/main/java/duke/command/ExitCommand.java b/src/main/java/duke/command/ExitCommand.java new file mode 100644 index 0000000000..07ac6b8a3a --- /dev/null +++ b/src/main/java/duke/command/ExitCommand.java @@ -0,0 +1,21 @@ +package duke.command; + +import duke.logic.Storage; +import duke.task.TaskList; + +/** + * Represents a command telling duke to close + */ +public class ExitCommand implements Command { + + @Override + public boolean isExit() { + return true; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + String goodbyeResponse = "Bye. Hope to see you again soon!"; + return goodbyeResponse; + } +} diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 0000000000..887b2064f5 --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,40 @@ +package duke.command; + +import duke.logic.Storage; +import duke.task.Task; +import duke.task.TaskList; + +/** + * Represents a command telling Duke to find tasks containing a given word + */ +public class FindCommand implements Command { + private String searchString; + + /** + * Constructor + * @param searchString The user input + */ + public FindCommand(String searchString) { + this.searchString = searchString; + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + TaskList filteredTasks = new TaskList(); + + for (int i = 1; i <= tasks.size(); i++) { + Task currTask = tasks.get(i); + if (currTask.getDetail().indexOf(this.searchString) >= 0) { + filteredTasks.add(currTask); + } + } + + String findResponse = filteredTasks.toString(); + return findResponse; + } +} diff --git a/src/main/java/duke/command/HelpCommand.java b/src/main/java/duke/command/HelpCommand.java new file mode 100644 index 0000000000..03a067ba0e --- /dev/null +++ b/src/main/java/duke/command/HelpCommand.java @@ -0,0 +1,123 @@ +package duke.command; + +import duke.logic.Storage; +import duke.task.TaskList; + +/** + * Represents a task telling Duke to help the user + */ +public class HelpCommand implements Command { + private static final String HELP_ALL_RESPONSE = "List of commands:\n" + + "1. todo\n2. deadline\n3. event\n4. list\n5. done\n6. delete\n7. find\n8. check\n9. exit\n" + + "Type 'help ' in the chat to see the details!"; + private static final String HELP_TODO_RESPONSE = "Adds a todo task to the list\n\nFormat: todo "; + private static final String HELP_DEADLINE_RESPONSE = "Adds a deadline task to the list\n\n" + + "Format: deadline /by "; + private static final String HELP_EVENT_RESPONSE = "Adds an event task to the list\n\n" + + "Format: event /at "; + private static final String HELP_LIST_RESPONSE = "Lists all the events currently in the list\n\nFormat: list"; + private static final String HELP_DONE_RESPONSE = "Marks the task as done\n\nFormat: done "; + private static final String HELP_DELETE_RESPONSE = "Deletes the task\n\nFormat: delete "; + private static final String HELP_FIND_RESPONSE = "Finds a task that contains the search term\n\n" + + "Format: find "; + private static final String HELP_CHECK_RESPONSE = "Checks all tasks on a specific date\n\n" + + "Format: check "; + private static final String HELP_EXIT_RESPONSE = "Exits duke\n\nFormat: exit"; + + private String helpResponse; + + private HelpCommand(String helpResponse) { + this.helpResponse = helpResponse; + } + + // static help command constructor + /** + * Static constructor for generic help command + * @return Help command with generic help response + */ + public static HelpCommand getHelpAllCommand() { + return new HelpCommand(HELP_ALL_RESPONSE); + } + + /** + * Static constructor for todo help command + * @return Help command with help regarding the todo task + */ + public static HelpCommand getHelpToDoCommand() { + return new HelpCommand(HELP_TODO_RESPONSE); + } + + /** + * Static constructor for deadline help command + * @return Help command with help regarding the deadline task + */ + public static HelpCommand getHelpDeadlineCommand() { + return new HelpCommand(HELP_DEADLINE_RESPONSE); + } + + /** + * Static constructor for event help command + * @return Help command with help regarding the event task + */ + public static HelpCommand getHelpEventCommand() { + return new HelpCommand(HELP_EVENT_RESPONSE); + } + + /** + * Static constructor for list help command + * @return Help command with help regarding the list command + */ + public static HelpCommand getHelpListCommand() { + return new HelpCommand(HELP_LIST_RESPONSE); + } + + /** + * Static constructor for done help command + * @return Help command with help regarding the done command + */ + public static HelpCommand getHelpDoneCommand() { + return new HelpCommand(HELP_DONE_RESPONSE); + } + + /** + * Static constructor for delete help command + * @return Help command with help regarding the delete command + */ + public static HelpCommand getHelpDeleteCommand() { + return new HelpCommand(HELP_DELETE_RESPONSE); + } + + /** + * Static constructor for find help command + * @return Help command with help regarding the find command + */ + public static HelpCommand getHelpFindCommand() { + return new HelpCommand(HELP_FIND_RESPONSE); + } + + /** + * Static constructor for check help command + * @return Help command with help regarding the check command + */ + public static HelpCommand getHelpCheckCommand() { + return new HelpCommand(HELP_CHECK_RESPONSE); + } + + /** + * Static constructor for exit help command + * @return Help command with help regarding the exit command + */ + public static HelpCommand getHelpExitCommand() { + return new HelpCommand(HELP_EXIT_RESPONSE); + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + return helpResponse; + } +} diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 0000000000..1da2b26c3a --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,21 @@ +package duke.command; + +import duke.logic.Storage; +import duke.task.TaskList; + +/** + * Represents a command telling duke to list all current tasks + */ +public class ListCommand implements Command { + + @Override + public boolean isExit() { + return false; + } + + @Override + public String execute(TaskList tasks, Storage storage) { + String listResponse = tasks.toString(); + return listResponse; + } +} diff --git a/src/main/java/duke/logic/Duke.java b/src/main/java/duke/logic/Duke.java new file mode 100644 index 0000000000..d0a53b0590 --- /dev/null +++ b/src/main/java/duke/logic/Duke.java @@ -0,0 +1,81 @@ +package duke.logic; + +import java.io.IOException; +import java.time.format.DateTimeParseException; + +import duke.command.Command; +import duke.task.TaskList; + +/** + * Represents a task manager + */ +public class Duke { + private Storage storage; + private TaskList tasks; + private boolean isClosed; + private boolean isConfused; + + /** + * Class constructor with specified file path. + * @param filePath The path to load and save from + */ + public Duke(String filePath) { + storage = new Storage(filePath); + isClosed = false; + isConfused = false; + try { + tasks = storage.load(); + } catch (IOException e) { + tasks = new TaskList(); + } catch (DateTimeParseException e) { + tasks = new TaskList(); + } + } + + /** + * Gets duke's response to the user input + * @param input The user input + * @return Duke's response + */ + public String getResponse(String input) { + try { + Command c = Parser.parse(input); + if (c.isExit()) { + isClosed = true; + } else { + isClosed = false; + } + String response = c.execute(tasks, storage); + isConfused = false; + return response; + } catch (DukeException e) { + isConfused = true; + return e.getMessage(); + } + } + + /** + * Gets the welcome string + * @return Welcome string + */ + public String getWelcome() { + String welcomeMessage = "Hello! I'm Duke\nWhat can I do for you?"; + return welcomeMessage; + } + + /** + * Checks if previous command was an exit command + * @return true if previous command was an exit command and false otherwise + */ + public boolean isClosed() { + return this.isClosed; + } + + /** + * Checks duke is confused by the last command + * @return true if duke is confused and false otherwise + */ + public boolean isConfused() { + return this.isConfused; + } +} diff --git a/src/main/java/duke/logic/DukeException.java b/src/main/java/duke/logic/DukeException.java new file mode 100644 index 0000000000..5e4cb8be51 --- /dev/null +++ b/src/main/java/duke/logic/DukeException.java @@ -0,0 +1,9 @@ +package duke.logic; + +public class DukeException extends IllegalArgumentException { + private static final long serialVersionUID = 50572093442410692L; + + public DukeException(String s) { + super(s); + } +} diff --git a/src/main/java/duke/logic/Parser.java b/src/main/java/duke/logic/Parser.java new file mode 100644 index 0000000000..1a87860232 --- /dev/null +++ b/src/main/java/duke/logic/Parser.java @@ -0,0 +1,232 @@ +package duke.logic; + +import java.time.LocalDate; +import java.time.format.DateTimeParseException; + +import duke.command.AddCommand; +import duke.command.CheckCommand; +import duke.command.Command; +import duke.command.DeleteCommand; +import duke.command.DoneCommand; +import duke.command.ExitCommand; +import duke.command.FindCommand; +import duke.command.HelpCommand; +import duke.command.ListCommand; +import duke.task.Deadline; +import duke.task.DeadlineException; +import duke.task.Event; +import duke.task.EventException; +import duke.task.ToDo; +import duke.task.ToDoException; + +/** + * Parser for Duke commands + */ +public class Parser { + /** + * Parses a string input to the relevent command instruction. + * @param input The String to be parsed + * @return Duke's response to the input as a String + * @throws DukeException + */ + public static Command parse(String input) throws DukeException { + switch (input) { + + case "list": + return new ListCommand(); + + case "help": + return HelpCommand.getHelpAllCommand(); + + case "exit": + return new ExitCommand(); + + default: + String[] splitInput = input.split(" ", 2); + + switch (splitInput[0]) { + case "help": + return getHelpCommand(splitInput[1].strip()); + + case "done": + checkDoneFormat(splitInput); + return getDoneCommand(splitInput[1]); + + case "delete": + checkDeleteFormat(splitInput); + return getDeleteCommand(splitInput[1]); + + case "check": + checkCheckFormat(splitInput); + return getCheckCommand(splitInput[1]); + + case "find": + checkFindFormat(splitInput); + return new FindCommand(splitInput[1].strip()); + + case "todo": + checkToDoFormat(splitInput); + ToDo todo = new ToDo(splitInput[1]); + return new AddCommand(todo); + + case "deadline": + checkDeadlineFormat(splitInput); + Deadline deadline = getDeadline(splitInput[1]); + return new AddCommand(deadline); + + case "event": + checkEventFormat(splitInput); + Event event = getEvent(splitInput[1]); + return new AddCommand(event); + + default: + throw new DukeException("I'm sorry :(\nI don't know what that means\n" + + "Type 'help' in the chat to see the list of valid commands"); + } + } + } + + private static void checkDoneFormat(String[] splitInput) throws DukeException { + if (splitInput.length < 2) { + throw new DukeException("Please enter the task to mark as finished!"); + } + } + + private static void checkDeleteFormat(String[] splitInput) throws DukeException { + if (splitInput.length < 2) { + throw new DukeException("Please enter the task to delete!"); + } + } + + private static void checkCheckFormat(String[] splitInput) throws DukeException { + if (splitInput.length < 2) { + throw new DukeException("Please enter the date to check!"); + } + } + + private static void checkFindFormat(String[] splitInput) throws DukeException { + if (splitInput.length < 2) { + throw new DukeException("Please enter the keyword(s) to find!"); + } + } + + private static void checkToDoFormat(String[] splitInput) throws ToDoException { + if (splitInput.length < 2) { + throw new ToDoException("Todo details cannot be empty!"); + } + } + + private static void checkDeadlineFormat(String[] splitInput) throws DeadlineException { + if (splitInput.length < 2) { + throw new DeadlineException("Deadline details cannot be empty!"); + } + } + + private static void checkEventFormat(String[] splitInput) throws EventException { + if (splitInput.length < 2) { + throw new EventException("Event details cannot be empty!"); + } + } + + // get methods + private static Command getHelpCommand(String command) throws DukeException { + switch (command) { + case "todo": + return HelpCommand.getHelpToDoCommand(); + case "deadline": + return HelpCommand.getHelpDeadlineCommand(); + case "event": + return HelpCommand.getHelpEventCommand(); + case "list": + return HelpCommand.getHelpListCommand(); + case "done": + return HelpCommand.getHelpDoneCommand(); + case "delete": + return HelpCommand.getHelpDeleteCommand(); + case "find": + return HelpCommand.getHelpFindCommand(); + case "check": + return HelpCommand.getHelpCheckCommand(); + case "exit": + return HelpCommand.getHelpExitCommand(); + default: + throw new DukeException(command + " is not a valid command!\n" + + "Type 'help' in the chat to see the list of valid commands"); + } + } + + private static Command getDoneCommand(String taskNumString) throws DukeException { + try { + int taskNum = Integer.parseInt(taskNumString); + return new DoneCommand(taskNum); + } catch (NumberFormatException e) { + throw new DukeException("Incorrect input!\nPlease re-enter the command in the format: done "); + } + } + + private static Command getDeleteCommand(String taskNumString) throws DukeException { + try { + int taskNum = Integer.parseInt(taskNumString); + return new DeleteCommand(taskNum); + } catch (NumberFormatException e) { + throw new DukeException( + "Incorrect input!\nPlease re-enter the command in the format: delete "); + } + } + + private static Command getCheckCommand(String dateString) throws DukeException { + try { + LocalDate date = LocalDate.parse(dateString); + return new CheckCommand(date); + } catch (DateTimeParseException e) { + throw new DukeException("Cannot read date!\nPlease re-enter the command in the format: check "); + } + } + + private static Deadline getDeadline(String detail) { + if (detail.indexOf(" /by ") < 0) { + throw new DeadlineException("Missing a due date!"); + } + + String[] splitDetails = detail.split(" /by "); + + if (splitDetails.length < 2) { + throw new DeadlineException("Deadline details cannot be empty!"); + } else if (splitDetails.length > 2) { + throw new DeadlineException("Too many dates given!"); + } else { + assert(splitDetails.length == 2); + + try { + LocalDate date = LocalDate.parse(splitDetails[1]); + return new Deadline(splitDetails[0], date); + } catch (DateTimeParseException e) { + throw new DeadlineException("Cannot read date!"); + } + } + } + + private static Event getEvent(String detail) { + if (detail.indexOf(" /at ") < 0) { + throw new EventException("Missing a due date!"); + } + + String[] splitDetails = detail.split(" /at "); + + if (splitDetails.length < 2) { + throw new EventException("Event details cannot be empty!"); + } else if (splitDetails.length > 2) { + throw new EventException("Too many dates given!"); + } else { + assert(splitDetails.length == 2); + + try { + LocalDate date = LocalDate.parse(splitDetails[1]); + return new Event(splitDetails[0], date); + } catch (DateTimeParseException e) { + throw new EventException("Cannot read date!"); + } + } + } + +} diff --git a/src/main/java/duke/logic/Storage.java b/src/main/java/duke/logic/Storage.java new file mode 100644 index 0000000000..a8f8225560 --- /dev/null +++ b/src/main/java/duke/logic/Storage.java @@ -0,0 +1,141 @@ +package duke.logic; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeParseException; +import java.util.Scanner; + +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.TaskList; +import duke.task.ToDo; + +/** + * Storage for Duke + */ +public class Storage { + private String filePath; + + /** + * Construction with specified file path + * @param filePath the file path to load and save from + */ + public Storage(String filePath) { + this.filePath = filePath; + } + + /** + * Loads task list from the file path given during initialisation + * @return List of saved tasks + * @throws IOException + * @throws DateTimeParseException + */ + public TaskList load() throws IOException, DateTimeParseException { + String fileDir = getFileDir(); + boolean fileDirExists = checkDirPath(fileDir); + + if (!fileDirExists) { + makeFileDir(fileDir); + } + + File f = new File(this.filePath); + + if (!f.exists()) { + f.createNewFile(); + assert(f.exists()); + return new TaskList(); + } else { + TaskList memList = readFromMem(f); + return memList; + } + } + + /** + * Stores task list into file path specified during initialisation + * @param tasks Task list to be saved + * @throws IOException + */ + public void store(TaskList tasks) throws IOException { + File f = new File(this.filePath); + assert(f.exists()); + + FileWriter fw = new FileWriter(this.filePath); + + for (int i = 1; i <= tasks.size(); i++) { + Task currTask = tasks.get(i); + String tag = currTask.getTag(); + int isComplete = currTask.checkIsComplete() ? 1 : 0; + String detail = currTask.getDetail(); + String date = currTask.getDate() == null ? "" : currTask.getDate().toString(); + fw.write(tag + " | " + isComplete + " | " + detail + " | " + date + "\n"); + } + fw.close(); + } + + private String getFileDir() { + int lastDelimiterIndex = this.filePath.lastIndexOf("/"); + if (lastDelimiterIndex < 0) { + return ""; + } + String fileDir = this.filePath.substring(0, lastDelimiterIndex); + return fileDir; + } + + private boolean checkDirPath(String fileDir) { + File dir = new File(fileDir); + return dir.exists(); + } + + private void makeFileDir(String fileDir) { + File dir = new File(fileDir); + dir.mkdirs(); + } + + private TaskList readFromMem(File f) throws FileNotFoundException { + assert(f.exists()); + + TaskList memList = new TaskList(); + + // fill with stored data + Scanner sc = new Scanner(f); + while (sc.hasNext()) { + String taskString = sc.nextLine(); + Task parsedTask = parseTask(taskString); + memList.add(parsedTask); + } + sc.close(); + return memList; + } + + private Task parseTask(String taskString) { + String[] inputs = taskString.split(" \\| "); + String type = inputs[0]; + Task task; + + if (type.equals("T")) { // todo + assert(inputs.length == 3); + task = new ToDo(inputs[2]); + + } else if (type.equals("D")) { // deadline + assert(inputs.length == 4); + LocalDate date = LocalDate.parse(inputs[3]); + task = new Deadline(inputs[2], date); + + } else { // event + assert(inputs.length == 4); + LocalDate date = LocalDate.parse(inputs[3]); + task = new Event(inputs[2], date); + } + + // change status + if (inputs[1].equals("1")) { + task.setCompletion(true); + } + + return task; + } +} diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java new file mode 100644 index 0000000000..6b04767144 --- /dev/null +++ b/src/main/java/duke/task/Deadline.java @@ -0,0 +1,24 @@ +package duke.task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * Represents a deadline task + */ +public class Deadline extends Task { + + /** + * Constructor + * @param message Deadline details + * @param date Due date + */ + public Deadline(String message, LocalDate date) { + super("D", message, date); + } + + @Override + public String toString() { + return super.toString() + " (by: " + this.date.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; + } +} diff --git a/src/main/java/duke/task/DeadlineException.java b/src/main/java/duke/task/DeadlineException.java new file mode 100644 index 0000000000..cdd6e5ddf1 --- /dev/null +++ b/src/main/java/duke/task/DeadlineException.java @@ -0,0 +1,11 @@ +package duke.task; + +import duke.logic.DukeException; + +public class DeadlineException extends DukeException { + private static final long serialVersionUID = 5843754080272552309L; + + public DeadlineException(String s) { + super(s + "\nPlease re-enter the command in the format:\ndeadline /by "); + } +} diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java new file mode 100644 index 0000000000..8e96477e86 --- /dev/null +++ b/src/main/java/duke/task/Event.java @@ -0,0 +1,24 @@ +package duke.task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * Represents an Event + */ +public class Event extends Task { + + /** + * Constructor + * @param message Event details + * @param date Date of the event + */ + public Event(String message, LocalDate date) { + super("E", message, date); + } + + @Override + public String toString() { + return super.toString() + " (at: " + this.date.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; + } +} diff --git a/src/main/java/duke/task/EventException.java b/src/main/java/duke/task/EventException.java new file mode 100644 index 0000000000..622639a0a1 --- /dev/null +++ b/src/main/java/duke/task/EventException.java @@ -0,0 +1,11 @@ +package duke.task; + +import duke.logic.DukeException; + +public class EventException extends DukeException { + private static final long serialVersionUID = 1L; + + public EventException(String s) { + super(s + "\nPlease re-enter the command in the format:\nEvent /at "); + } +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java new file mode 100644 index 0000000000..3a251e8fb8 --- /dev/null +++ b/src/main/java/duke/task/Task.java @@ -0,0 +1,72 @@ +package duke.task; + +import java.time.LocalDate; + +/** + * Represents a task + */ +public abstract class Task { + protected LocalDate date; + private String tag; + private boolean isComplete; + private String detail; + + /** + * Constructor + * @param tag Indentification for the task + * @param detail Details of the task + * @param date Date associated with the task + */ + protected Task(String tag, String detail, LocalDate date) { + this.tag = tag; + this.isComplete = false; + this.detail = detail; + this.date = date; + } + + /** + * Sets the completion of a task + * @param isComplete Boolean representing if the task is done. True for done, False for not. + */ + public void setCompletion(boolean isComplete) { + this.isComplete = isComplete; + } + + /** + * Gets the identifier tag of the task + * @return String representing the identifier tag + */ + public String getTag() { + return this.tag; + } + + /** + * Gets the completion status of the task + * @return Boolean representing the completion status of the task. True if complete, false if not. + */ + public boolean checkIsComplete() { + return this.isComplete; + } + + /** + * Gets the details of the task + * @return String of the details of the task + */ + public String getDetail() { + return this.detail; + } + + /** + * Gets the date of the task + * @return Date associated with the task + */ + public LocalDate getDate() { + return this.date; + } + + @Override + public String toString() { + String status = this.isComplete ? "[X] " : "[ ] "; + return "[" + this.tag + "]" + status + this.detail; + } +} diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java new file mode 100644 index 0000000000..d3edaa965f --- /dev/null +++ b/src/main/java/duke/task/TaskList.java @@ -0,0 +1,67 @@ +package duke.task; + +import java.util.ArrayList; + +/** + * Represents a list of tasks + */ +public class TaskList { + private ArrayList tasks; + + /** + * Constructor + */ + public TaskList() { + this.tasks = new ArrayList<>(); + } + + /** + * Adds task to list + * @param task Task to be added + */ + public void add(Task task) { + this.tasks.add(task); + } + + /** + * Gets the task in the list based on its task number + * @param taskNum Task number of the task to be found + * @return Task with the given task number + */ + public Task get(int taskNum) { + return this.tasks.get(taskNum - 1); + } + + /** + * Marks the task with the given task number as complete + * @param taskNum Task number of the task to be marked as complete + */ + public void done(int taskNum) { + this.tasks.get(taskNum - 1).setCompletion(true); + } + + /** + * Removes the task with the given task number from the list + * @param taskNum Task number of the task to be removed + */ + public void delete(int taskNum) { + this.tasks.remove(taskNum - 1); + } + + /** + * Gets the total number of tasks currently in the list + * @return Number of tasks in the list + */ + public int size() { + return this.tasks.size(); + } + + @Override + public String toString() { + StringBuilder taskListString = new StringBuilder(); + for (int i = 1; i <= this.tasks.size(); i++) { + taskListString.append(i + ". " + this.get(i).toString() + "\n"); + } + return taskListString.toString(); + } +} diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java new file mode 100644 index 0000000000..98384cab9d --- /dev/null +++ b/src/main/java/duke/task/ToDo.java @@ -0,0 +1,10 @@ +package duke.task; + +/** + * Represents a to do task + */ +public class ToDo extends Task { + public ToDo(String message) { + super("T", message, null); + } +} diff --git a/src/main/java/duke/task/ToDoException.java b/src/main/java/duke/task/ToDoException.java new file mode 100644 index 0000000000..8b79533356 --- /dev/null +++ b/src/main/java/duke/task/ToDoException.java @@ -0,0 +1,11 @@ +package duke.task; + +import duke.logic.DukeException; + +public class ToDoException extends DukeException { + private static final long serialVersionUID = -1674176699745504837L; + + public ToDoException(String s) { + super(s + "\nPlease re-enter the command in the format: todo "); + } +} diff --git a/src/main/java/duke/ui/DialogBox.java b/src/main/java/duke/ui/DialogBox.java new file mode 100644 index 0000000000..d2adcd5b83 --- /dev/null +++ b/src/main/java/duke/ui/DialogBox.java @@ -0,0 +1,44 @@ +package duke.ui; + +import java.io.IOException; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.layout.VBox; + +/** + * An example of a custom control using FXML. + * This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label + * containing text from the speaker. + */ +public class DialogBox extends VBox { + @FXML + private Label username; + + private DialogBox(String name, InnerDialogBox idb) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml")); + fxmlLoader.setController(this); + fxmlLoader.setRoot(this); + fxmlLoader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + username.setText(name); + this.getChildren().addAll(idb); + } + + + public static DialogBox getUserDialog(String text, Image img, String name) { + return new DialogBox(name, InnerDialogBox.getInnerUserDialog(text, img)); + } + + public static DialogBox getDukeDialog(String text, Image img, String name) { + DialogBox db = new DialogBox(name, InnerDialogBox.getInnerDukeDialog(text, img)); + db.setAlignment(Pos.TOP_LEFT); + return db; + } +} diff --git a/src/main/java/duke/ui/InnerDialogBox.java b/src/main/java/duke/ui/InnerDialogBox.java new file mode 100644 index 0000000000..27e052f813 --- /dev/null +++ b/src/main/java/duke/ui/InnerDialogBox.java @@ -0,0 +1,60 @@ +package duke.ui; + +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; + +/** + * An example of a custom control using FXML. + * This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label + * containing text from the speaker. + */ +public class InnerDialogBox extends HBox { + @FXML + private Label dialog; + @FXML + private ImageView displayPicture; + + private InnerDialogBox(String text, Image img) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/InnerDialogBox.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); + this.getChildren().setAll(tmp); + setAlignment(Pos.TOP_LEFT); + } + + public static InnerDialogBox getInnerUserDialog(String text, Image img) { + return new InnerDialogBox(text, img); + } + + public static InnerDialogBox getInnerDukeDialog(String text, Image img) { + var db = new InnerDialogBox(text, img); + db.flip(); + return db; + } +} diff --git a/src/main/java/duke/ui/MainWindow.java b/src/main/java/duke/ui/MainWindow.java new file mode 100644 index 0000000000..9f81759afe --- /dev/null +++ b/src/main/java/duke/ui/MainWindow.java @@ -0,0 +1,105 @@ +package duke.ui; + +import duke.logic.Duke; +import javafx.animation.PauseTransition; +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.stage.Stage; +import javafx.util.Duration; + +/** + * Controller for MainWindow. Provides the layout for the other controls. + */ +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 Stage stage; + + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + private Image dukeConfusedImage = new Image(this.getClass().getResourceAsStream("/images/DaDukeConfused.png")); + + private String dukeName = "Duke"; + private String userName = "You"; + + /** + * Initialises MainWindow + */ + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + /** + * Sets the duke used by MainWindow to the given duke + * @param d The duke to be set + */ + public void setDuke(Duke d) { + duke = d; + } + + /** + * Sets the Stage MainWindow is set in + * @param s The stage to be set + */ + public void setStage(Stage s) { + stage = s; + } + + /** + * Tells duke to greet the user + */ + public void greet() { + String welcome = duke.getWelcome(); + dialogContainer.getChildren().addAll( + DialogBox.getDukeDialog(welcome, dukeImage, dukeName) + ); + } + + /** + * 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() throws InterruptedException { + String input = userInput.getText(); + String response = duke.getResponse(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage, userName), + DialogBox.getDukeDialog(response, getDukeImage(), dukeName) + ); + userInput.clear(); + if (duke.isClosed()) { + exit(); + } + } + + private void exit() { + PauseTransition delay = new PauseTransition(Duration.seconds(1.5)); + delay.setOnFinished(event -> { + stage.close(); + }); + delay.play(); + } + + private Image getDukeImage() { + if (duke.isConfused()) { + return dukeConfusedImage; + } else { + return dukeImage; + } + } +} diff --git a/src/main/resources/images/DaDuke.png b/src/main/resources/images/DaDuke.png new file mode 100644 index 0000000000..ba824f73b1 Binary files /dev/null and b/src/main/resources/images/DaDuke.png differ diff --git a/src/main/resources/images/DaDukeConfused.png b/src/main/resources/images/DaDukeConfused.png new file mode 100644 index 0000000000..6568df9c70 Binary files /dev/null and b/src/main/resources/images/DaDukeConfused.png differ diff --git a/src/main/resources/images/DaUser.png b/src/main/resources/images/DaUser.png new file mode 100644 index 0000000000..04a4178b68 Binary files /dev/null and b/src/main/resources/images/DaUser.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..0755283ab6 --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/src/main/resources/view/InnerDialogBox.fxml b/src/main/resources/view/InnerDialogBox.fxml new file mode 100644 index 0000000000..eb94e51e82 --- /dev/null +++ b/src/main/resources/view/InnerDialogBox.fxml @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..64af57d7eb --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +