diff --git a/.gitignore b/.gitignore index f69985ef1f..0d8e8e0fb9 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT +src/Test.java +tasks.txt +src/META-INF/MANIFEST.MF diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..8035f320c0 --- /dev/null +++ b/build.gradle @@ -0,0 +1,61 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + id 'com.github.johnrengelman.shadow' version '5.1.0' +} + +repositories { + mavenCentral() +} + +dependencies { + 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' + + 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' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "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..4c001417ae --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,403 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml new file mode 100644 index 0000000000..39efb6e4ac --- /dev/null +++ b/config/checkstyle/suppressions.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/docs/README.md b/docs/README.md index fd44069597..49d863979c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,20 +1,169 @@ # User Guide +Duke is a Personal Assistant Chatbot that helps users to manage their tasks. ## Features -### Feature 1 -Description of feature. +### Add new tasks +You can add 4 different types of tasks in Duke: +* **Todo** - *Tasks that can be done anytime* +* **Deadline** - *Tasks that must be completed before a specific date/time* +* **Event** - *Events that are occurring at a specific date/time* +* **Fixed Duration** - *Tasks that take a fixed amount of time to complete but do not have a fixed start/end time* + +### Delete tasks +You can delete existing tasks in Duke. + +### Mark tasks as completed +You can mark existing tasks as completed in Duke. + +### List tasks +You can list all existing tasks in Duke. + +### Find tasks +You can search through all tasks in Duke and find tasks matching the keyword(s). + +### Save and load tasks +All tasks will be automatically saved into a local file when you exit Duke, and are loaded when you start +the application. ## Usage +**Note:** +Words in `UPPER_CASE` are the parameters to be supplied by the user. +*e.g. in `t DESCRIPTION`, `DESCRIPTION` is a parameter which can be used as `t Read book`.* + +### `t` - Add a new Todo task +Adds a new Todo task. + +Format: `t DESCRIPTION` + +Example of usage: +`t Read book` + +Expected outcome: +
+Got it! I've added this task:
+    [T][] Read book
+Now you have 1 task in the list.
+
+ +### `d` - Add a new Deadline task +Adds a new Deadline task. +Input date should be in **yyyy-mm-dd format**. + +Format: `d DESCRIPTION /by DATE` + +Example of usage: +`d Return book /by 2021-03-05` + +Expected outcome: +
+Got it! I've added this task:
+    [D][] Return book (by: Mar 05 2021)
+Now you have 2 tasks in the list.
+
+ +### `e` - Add a new Event task +Adds a new Event task. + +Format: `e DESCRIPTION /at DATE` + +Example of usage: +`e Project meeting /at Mon 5pm` + +Expected outcome: +
+Got it! I've added this task:
+    [E][] Project meeting (at: Mon 5pm)
+Now you have 3 tasks in the list.
+
+ +### `fd` - Add a new Fixed Duration task +Adds a new Fixed Duration task. + +Format: `fd DESCRIPTION /dur DURATION` -### `Keyword` - Describe action +Example of usage: +`fd Fix bicycle /dur 1 hour` + +Expected outcome: +
+Got it! I've added this task:
+    [FD][] Fix bicycle (time required: 1 hour)
+Now you have 4 tasks in the list.
+
+ +### `delete` - Delete an existing task +Deletes the task with the specified **index**. + +Format: `delete INDEX` + +Example of usage: +`delete 2` + +Expected outcome: +
+Noted! I've removed this task:
+    [D][] Return book (by: Mar 05 2021)
+Now you have 3 tasks in the list.
+
+ +### `done` - Mark an existing task as completed +Marks the task with the specified **index** as completed. + +Format: `done INDEX` + +Example of usage: +`done 1` + +Expected outcome: +
+Nice! I've marked this task as done:
+    [T][X] Read book
+
+ +### `list` - List all existing tasks +Lists all existing tasks. +The type and status icon of the task will be shown. +The status icon will be `[X]` for completed tasks and `[]` otherwise. + +Format: `list` + +Example of usage: +`list` + +Expected outcome: +
+Here are the tasks in your list:
+    1. [T][X] Read book
+    2. [E][] Project meeting (at: Mon 5pm)
+    3. [FD][] Fix bicycle (time required: 1 hour)
+
+ +### `find` - Search tasks +Finds all existing tasks matching the **keyword(s)**. + +Format: `find KEYWORDS` + +Example of usage: +`find book` + +Expected outcome: +
+Here are the matching tasks in your list:
+    1. [T][X] Read book
+
-Describe action and its outcome. +### `bye` - Save tasks +Saves all existing tasks to the hard disk. +**The user may now close the app.** -Example of usage: +Format: `bye` -`keyword (optional arguments)` +Example of usage: +`bye` Expected outcome: +
+Bye! Hope to see you again soon!
+
-`outcome` diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..bb43d1514b 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..3fbdea7562 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Feb 22 15:32:10 SGT 2021 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME 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/DialogBox.java b/src/main/java/DialogBox.java new file mode 100644 index 0000000000..f578c16b2c --- /dev/null +++ b/src/main/java/DialogBox.java @@ -0,0 +1,59 @@ +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 DialogBox extends HBox { + @FXML + private Label dialog; + @FXML + private ImageView displayPicture; + + 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); + } + + public static DialogBox getUserDialog(String text, Image img) { + return new DialogBox(text, img); + } + + 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.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/Launcher.java b/src/main/java/Launcher.java new file mode 100644 index 0000000000..43d64c26fb --- /dev/null +++ b/src/main/java/Launcher.java @@ -0,0 +1,10 @@ +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + */ +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000000..d7e5bf104b --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,32 @@ +import java.io.IOException; + +import duke.Duke; +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("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.setTitle("Duke"); + stage.setResizable(false); + stage.setScene(scene); + fxmlLoader.getController().setDuke(duke); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/MainWindow.java b/src/main/java/MainWindow.java new file mode 100644 index 0000000000..d4d0cf8511 --- /dev/null +++ b/src/main/java/MainWindow.java @@ -0,0 +1,51 @@ +import duke.Duke; +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; + +/** + * 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 Image userImage = new Image(this.getClass().getResourceAsStream("/images/charmander.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/squirtle.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void setDuke(Duke d) { + duke = d; + } + + /** + * 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), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + } +} diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java new file mode 100644 index 0000000000..1142b49b00 --- /dev/null +++ b/src/main/java/duke/Deadline.java @@ -0,0 +1,50 @@ +package duke; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * Represents a task that is to be completed by a deadline. + */ +public class Deadline extends Task { + private final String by; + private final LocalDate date; + + /** + * Creates a new Deadline object with a description and deadline. + * If deadline is not properly formatted in yyyy-mm-dd format, the date attribute will be null. + * + * @param description Description of the task. + * @param by Deadline that the task is to be completed by (in yyyy-mm-dd format). + */ + public Deadline(String description, String by) { + super(description); + this.by = by; + date = parseStringToDateTime(by); + } + + private LocalDate parseStringToDateTime(String by) { + if (by.matches("\\d{4}-\\d{2}-\\d{2}")) { + return LocalDate.parse(by); + } else { + return null; + } + } + + @Override + public String toString() { + String deadline; + if (date != null) { + DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy"); + deadline = date.format(myFormatter); + } else { + deadline = by; + } + return "[D]" + super.toString() + " (by: " + deadline + ")"; + } + + @Override + public String getTaskCommand() { + return "d " + getDescription() + " /by " + by; + } +} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..256cd70aa2 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,60 @@ +package duke; + +/** + * A Personal Assistant Chatbot that helps the user to keep track of various things. + */ +public class Duke { + private Storage storage; + private Ui ui; + private Parser parser; + private TaskList tasks; + + /** + * Creates a new Duke object. + * Existing tasks previously saved, if any, will be loaded from the file specified by filePath. + * + * @param filePath Path of the file that tasks are to be loaded from and saved in. + */ + public Duke(String filePath) { + assert filePath.length() > 0 : "filePath cannot be empty"; + storage = new Storage(filePath); + ui = new Ui(); + parser = new Parser(); + try { + tasks = new TaskList(storage.load()); + } catch (LoadTasksException e) { + tasks = new TaskList(); + } + } + + /** + * Returns the response message that is to be displayed to the user depending + * on the input of the user. + * + * @param input User input + * @return Response message. + */ + public String getResponse(String input) { + try { + return parser.handleInput(storage, ui, tasks, input); + } catch (EmptyDescriptionException e) { + return ui.getEmptyDescriptionErrorMsg(); + } catch (InvalidCommandException e) { + return ui.getInvalidCommandErrorMsg(); + } catch (WriteTasksException e) { + return ui.getWritingErrorMsg(); + } + } +} + + + + + + + + + + + + diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..753e998c68 --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,7 @@ +package duke; + +/** + * Represents exceptions that are specific to the Duke program. + */ +public class DukeException extends Exception { +} diff --git a/src/main/java/duke/EmptyDescriptionException.java b/src/main/java/duke/EmptyDescriptionException.java new file mode 100644 index 0000000000..a488d5db2d --- /dev/null +++ b/src/main/java/duke/EmptyDescriptionException.java @@ -0,0 +1,7 @@ +package duke; + +/** + * An exception that is thrown when the user tries to create a task with no description. + */ +public class EmptyDescriptionException extends DukeException { +} diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java new file mode 100644 index 0000000000..6ccff69b4b --- /dev/null +++ b/src/main/java/duke/Event.java @@ -0,0 +1,29 @@ +package duke; + +/** + * Represents an event that is occurring at a given date/time. + */ +public class Event extends Task { + private final String at; + + /** + * Creates a new Event object with a description and date/time of occurrence. + * + * @param description Description of the event. + * @param at Date/time that the event is occurring at. + */ + public Event(String description, String at) { + super(description); + this.at = at; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + at + ")"; + } + + @Override + public String getTaskCommand() { + return "e " + getDescription() + " /at " + at; + } +} diff --git a/src/main/java/duke/FixedDurationTask.java b/src/main/java/duke/FixedDurationTask.java new file mode 100644 index 0000000000..b244f0a9d1 --- /dev/null +++ b/src/main/java/duke/FixedDurationTask.java @@ -0,0 +1,30 @@ +package duke; + +/** + * Represents a task that takes a fixed amount of time to complete but does not have a fixed + * start/end time. + */ +public class FixedDurationTask extends Task { + private final String duration; + + /** + * Creates a new FixedDurationTask object with a description and duration. + * + * @param description Description of the task. + * @param duration Duration needed to complete the task. + */ + public FixedDurationTask(String description, String duration) { + super(description); + this.duration = duration; + } + + @Override + public String toString() { + return "[FD]" + super.toString() + " (time required: " + duration + ")"; + } + + @Override + public String getTaskCommand() { + return "fd " + getDescription() + " /dur " + duration; + } +} diff --git a/src/main/java/duke/InvalidCommandException.java b/src/main/java/duke/InvalidCommandException.java new file mode 100644 index 0000000000..e8d5a10b21 --- /dev/null +++ b/src/main/java/duke/InvalidCommandException.java @@ -0,0 +1,7 @@ +package duke; + +/** + * An exception that is thrown when the user enters an invalid command. + */ +public class InvalidCommandException extends DukeException { +} diff --git a/src/main/java/duke/LoadTasksException.java b/src/main/java/duke/LoadTasksException.java new file mode 100644 index 0000000000..e6f86ecdcd --- /dev/null +++ b/src/main/java/duke/LoadTasksException.java @@ -0,0 +1,8 @@ +package duke; + +/** + * An exception that is thrown when an error is encountered while trying to load tasks + * from the hard disk. + */ +public class LoadTasksException extends DukeException { +} diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 0000000000..eb2804f0db --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,56 @@ +package duke; + +/** + * A parser that deals with making sense of the user command. + */ +public class Parser { + /** + * Identifies the type of user command and calls the relevant method to perform the action. + * Commands accepted are list, done, delete, find, todo, deadline, event, fixedDur and bye. + * + * @param storage Storage object that deals with loading and saving tasks. + * @param ui Ui object that deals with interactions with the user. + * @param tasks TaskList that contains the list of Task objects. + * @param cmd Command entered by the user. + * @return Message to be displayed to the user after the command has been performed. + * @throws EmptyDescriptionException If the user tries to create a task without a description. + * @throws InvalidCommandException If the user enters an invalid command. + * @throws WriteTasksException If an error is encountered when trying to write tasks to the hard disk. + */ + public String handleInput(Storage storage, Ui ui, TaskList tasks, String cmd) throws + EmptyDescriptionException, InvalidCommandException, WriteTasksException { + if (cmd.equals("list")) { + return ui.getListOfTasks(tasks.getPrintableTasks()); + } else if (cmd.equals("bye")) { + storage.writeTasksToDisk(tasks); + return ui.getExitMsg(); + } else { + String[] arr = cmd.split(" ", 2); + switch (arr[0]) { + case "find": + return ui.getListOfFoundTasks(tasks.getPrintableTasksWithKeyword(arr[1])); + case "done": + int doneId = Integer.parseInt(arr[1]); + tasks.markTaskAsDone(doneId); + return ui.getMarkTaskAsDoneMsg(tasks.getTaskString(doneId)); + case "delete": + int deleteId = Integer.parseInt(arr[1]); + String taskString = tasks.getTaskString(deleteId); + tasks.deleteTask(deleteId); + return ui.getDeleteTaskMsg(taskString, tasks.getNumOfTasks()); + case "t": + // Fallthrough + case "d": + // Fallthrough + case "e": + // Fallthrough + case "fd": + tasks.addTask(cmd); + int numOfTasks = tasks.getNumOfTasks(); + return ui.getAddTaskMsg(tasks.getTaskString(numOfTasks), numOfTasks); + default: + throw new InvalidCommandException(); + } + } + } +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 0000000000..413e31423b --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,70 @@ +package duke; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +/** + * Deals with loading tasks from a file and saving tasks to the file. + */ +public class Storage { + private final String filePath; + + /** + * Creates a new Storage object with a file path. + * + * @param filePath File path. + */ + public Storage(String filePath) { + this.filePath = filePath; + } + + /** + * Loads the task commands from the file specified by filePath and stores them in an ArrayList. + * If the file does not exist, a new file is created in that path. + * + * @return ArrayList of Strings representing the task commands. + * @throws LoadTasksException If an error is encountered when trying to load tasks from the file. + */ + public ArrayList load() throws LoadTasksException { + ArrayList taskCommands = new ArrayList<>(); + File file = new File(filePath); + try { + if (file.exists()) { + BufferedReader br = new BufferedReader(new FileReader(file)); + String taskCommand = br.readLine(); + while (taskCommand != null) { + taskCommands.add(taskCommand); + taskCommand = br.readLine(); + } + } else { + file.createNewFile(); + } + } catch (IOException e) { + throw new LoadTasksException(); + } + return taskCommands; + } + + /** + * Writes the task commands of current tasks in the TaskList to the file specified by filePath. + * + * @param tasks TaskList containing the current tasks. + * @throws WriteTasksException If an error is encountered when trying to write tasks to the file. + */ + public void writeTasksToDisk(TaskList tasks) throws WriteTasksException { + try { + ArrayList writableTasks = tasks.getWritableTasks(); + FileWriter myWriter = new FileWriter(filePath); + for (String writableTask : writableTasks) { + myWriter.write(writableTask + "\n"); + } + myWriter.close(); + } catch (IOException e) { + throw new WriteTasksException(); + } + } +} diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java new file mode 100644 index 0000000000..f05b2129bd --- /dev/null +++ b/src/main/java/duke/Task.java @@ -0,0 +1,76 @@ +package duke; + +/** + * Abstract class that represents a task. + */ +public abstract class Task { + private final String description; + private boolean isDone; + + /** + * Creates a new Task object with a description. + * + * @param description Task description. + */ + public Task(String description) { + this.description = description; + this.isDone = false; + } + + /** + * Returns the status icon of the task. + * The status icon is "X" if the task is completed and " " otherwise. + * + * @return Status icon. + */ + public String getStatusIcon() { + return (isDone ? "X" : " "); + } + + @Override + public String toString() { + return "[" + getStatusIcon() + "] " + description; + } + + /** + * Marks the task as completed. + */ + public void setDone() { + isDone = true; + } + + /** + * Returns the user command required to create this Task object. + * + * @return User command. + */ + public abstract String getTaskCommand(); + + /** + * Returns the description of the task. + * + * @return Task description. + */ + public String getDescription() { + return description; + } + + /** + * Returns true if the task is completed and false otherwise. + * + * @return True if the task is completed. + */ + public boolean isDone() { + return isDone; + } + + /** + * Returns true if the task description contains the keyword. + * + * @param keyword Keyword + * @return True if the task description contains the keyword. + */ + public boolean descriptionContains(String keyword) { + return description.contains(keyword); + } +} diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java new file mode 100644 index 0000000000..d4fe37ca6f --- /dev/null +++ b/src/main/java/duke/TaskList.java @@ -0,0 +1,177 @@ +package duke; + +import java.util.ArrayList; + +/** + * Represents a list of tasks. + */ +public class TaskList { + private ArrayList tasks; + + /** + * Creates a new TaskList object with an ArrayList of tasks. + * + * @param taskCommands ArrayList of Strings representing task commands. + */ + public TaskList(ArrayList taskCommands) { + try { + tasks = initialiseTasks(taskCommands); + } catch (EmptyDescriptionException e) { + tasks = new ArrayList<>(); + } + } + + /** + * Creates a new TaskList object with an empty list of tasks. + */ + public TaskList() { + tasks = new ArrayList<>(); + } + + private ArrayList initialiseTasks(ArrayList taskCommands) throws EmptyDescriptionException { + ArrayList tasks = new ArrayList<>(); + for (String taskCommand : taskCommands) { + Task task; + if (taskCommand.charAt(0) == '#') { + taskCommand = taskCommand.substring(1); + task = createTask(taskCommand); + task.setDone(); + } else { + task = createTask(taskCommand); + } + tasks.add(task); + } + return tasks; + } + + private Task createTask(String taskCommand) throws EmptyDescriptionException { + String[] arr = taskCommand.split(" ", 2); + if (arr.length == 1) { + throw new EmptyDescriptionException(); + } + String type = arr[0]; + String rest = arr[1]; + Task task; + if (type.equals("t")) { + task = new Todo(rest); + } else if (type.equals("d")) { + String[] temp = rest.split(" /by "); + String description = temp[0]; + String by = temp[1]; + task = new Deadline(description, by); + } else if (type.equals("e")) { + String[] temp = rest.split(" /at "); + String description = temp[0]; + String at = temp[1]; + task = new Event(description, at); + } else { + String[] temp = rest.split(" /dur "); + String description = temp[0]; + String duration = temp[1]; + task = new FixedDurationTask(description, duration); + } + return task; + } + + /** + * Creates and adds a new task to the list of tasks. + * + * @param taskCommand Command entered by the user to create a new task. + * @throws EmptyDescriptionException If the task does not contain a description. + */ + public void addTask(String taskCommand) throws EmptyDescriptionException { + Task task = createTask(taskCommand); + tasks.add(task); + } + + /** + * Deletes the task with taskId from the list of tasks. + * + * @param taskId ID of the task. + */ + public void deleteTask(int taskId) { + tasks.remove(taskId - 1); + } + + /** + * Returns an ArrayList of Strings representing the current tasks in the list of tasks. + * The tasks are represented in a format that is to be displayed to the user. + * + * @return ArrayList of Strings representing the tasks in printable format. + */ + public ArrayList getPrintableTasks() { + ArrayList printableTasks = new ArrayList<>(); + int id = 1; + for (Task task : tasks) { + printableTasks.add(id + ". " + task); + id++; + } + return printableTasks; + } + + /** + * Returns an ArrayList of Strings representing the current tasks in the list of tasks whose + * description contains the key word specified by the user. + * The tasks are represented in a format that is to be displayed to the user. + * + * @param keyword Keyword + * @return ArrayList of Strings representing the tasks in printable format. + */ + public ArrayList getPrintableTasksWithKeyword(String keyword) { + ArrayList printableTasks = new ArrayList<>(); + int id = 1; + for (Task task : tasks) { + if (task.descriptionContains(keyword)) { + printableTasks.add(id + ". " + task); + id++; + } + } + return printableTasks; + } + + /** + * Returns an ArrayList of Strings representing the current tasks in the list of tasks. + * The tasks are represented in a format that is to be stored in a file. + * + * @return ArrayList of Strings representing the tasks in writable format. + */ + public ArrayList getWritableTasks() { + ArrayList writableTasks = new ArrayList<>(); + for (Task task : tasks) { + if (task.isDone()) { + writableTasks.add("#" + task.getTaskCommand()); + } else { + writableTasks.add(task.getTaskCommand()); + } + } + return writableTasks; + } + + /** + * Marks the task with taskId as completed. + * + * @param taskId ID of the task. + */ + public void markTaskAsDone(int taskId) { + tasks.get(taskId - 1).setDone(); + } + + /** + * Returns the string representation of the task with taskId in the list of tasks. + * + * @param taskId ID of the task. + * @return String representation of the task. + */ + public String getTaskString(int taskId) { + return tasks.get(taskId - 1).toString(); + } + + /** + * Returns the number of tasks in the list of tasks. + * + * @return Number of tasks. + */ + public int getNumOfTasks() { + return tasks.size(); + } +} diff --git a/src/main/java/duke/Todo.java b/src/main/java/duke/Todo.java new file mode 100644 index 0000000000..1f8f00eac8 --- /dev/null +++ b/src/main/java/duke/Todo.java @@ -0,0 +1,25 @@ +package duke; + +/** + * Represents a task with a description. + */ +public class Todo extends Task { + /** + * Creates a new Todo object with a description. + * + * @param description Task description. + */ + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } + + @Override + public String getTaskCommand() { + return "t " + getDescription(); + } +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 0000000000..23b7be7836 --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,117 @@ +package duke; + +import java.util.ArrayList; + +/** + * Generates messages to display to the user. + */ +public class Ui { + /** + * Returns an exit message to be displayed when the user exits the program. + * + * @return Message. + */ + public String getExitMsg() { + return "Bye! Hope to see you again soon!"; + } + + /** + * Returns a message confirming that a new task has been added. + * + * @param taskString String representation of the task. + * @param numOfTasks Number of tasks in the list of tasks. + * @return Message. + */ + public String getAddTaskMsg(String taskString, int numOfTasks) { + return "Got it! I've added this task:\n " + taskString + "\nNow you have " + + numOfTasks + " task" + (numOfTasks == 1 ? "" : "s") + " in the list."; + } + + /** + * Returns a message confirming that a task has been marked as completed. + * + * @param taskString String representation of the task. + * @return Message. + */ + public String getMarkTaskAsDoneMsg(String taskString) { + return "Nice! I've marked this task as done:\n " + taskString; + } + + /** + * Returns a message confirming that a task has been deleted. + * + * @param taskString String representation of the task. + * @param numOfTasksRemaining Number of tasks remaining in the list of tasks. + * @return Message. + */ + public String getDeleteTaskMsg(String taskString, int numOfTasksRemaining) { + return "Noted! I've removed this task:\n " + taskString + "\nNow you have " + + numOfTasksRemaining + " task" + (numOfTasksRemaining == 1 ? "" : "s") + " in the list."; + } + + /** + * Returns a String that lists out all tasks in the list of tasks. + * + * @param printableTasks ArrayList of Strings representing the tasks. + * @return String representing the list of tasks. + */ + public String getListOfTasks(ArrayList printableTasks) { + if (printableTasks.size() == 0) { + return "You have no tasks currently."; + } else { + StringBuilder listOfTasks = new StringBuilder("Here are the tasks in your list:"); + for (String printableTask : printableTasks) { + listOfTasks.append("\n ").append(printableTask); + } + return listOfTasks.toString(); + } + } + + /** + * Returns a String that lists out all tasks in the list of tasks whose description + * contains the keyword specified by the user. + * + * @param printableTasks ArrayList of Strings representing the tasks. + * @return String representing the list of tasks containing the keyword. + */ + public String getListOfFoundTasks(ArrayList printableTasks) { + if (printableTasks.size() == 0) { + return "Found no matching tasks."; + } else { + StringBuilder listOfTasks = new StringBuilder("Here are the matching tasks in your list:"); + for (String printableTask : printableTasks) { + listOfTasks.append("\n ").append(printableTask); + } + return listOfTasks.toString(); + } + } + + /** + * Returns an error message to be displayed when the user tries to create a new + * task without a description. + * + * @return Error message. + */ + public String getEmptyDescriptionErrorMsg() { + return "The description of a task cannot be empty!"; + } + + /** + * Returns an error message to be displayed when the user enters an invalid command. + * + * @return Error message. + */ + public String getInvalidCommandErrorMsg() { + return "Invalid command! Please try again."; + } + + /** + * Returns an error message to be displayed when an error is encountered while trying + * to save tasks to the hard disk. + * + * @return Error message. + */ + public String getWritingErrorMsg() { + return "Encountered an error when trying to write tasks to the hard disk."; + } +} diff --git a/src/main/java/duke/WriteTasksException.java b/src/main/java/duke/WriteTasksException.java new file mode 100644 index 0000000000..7f48728ecb --- /dev/null +++ b/src/main/java/duke/WriteTasksException.java @@ -0,0 +1,8 @@ +package duke; + +/** + * An exception that is thrown when an error is encountered while trying to write tasks to the + * hard disk. + */ +public class WriteTasksException extends DukeException { +} diff --git a/src/main/resources/images/charmander.png b/src/main/resources/images/charmander.png new file mode 100644 index 0000000000..aba2c7d4a1 Binary files /dev/null and b/src/main/resources/images/charmander.png differ diff --git a/src/main/resources/images/squirtle.png b/src/main/resources/images/squirtle.png new file mode 100644 index 0000000000..42502a7e15 Binary files /dev/null and b/src/main/resources/images/squirtle.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..93e33ba868 --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ 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..0143ddaff2 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,23 @@ + + + + + + + + + + +