diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..c79c597607 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: main.java.Launcher + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..76b3389d1d --- /dev/null +++ b/build.gradle @@ -0,0 +1,41 @@ +plugins { + id 'java' + id 'application' + 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' +} + + +mainClassName = "main.java.Launcher" + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +run{ + standardInput = System.in +} + + +version = "0.2" diff --git a/command.txt b/command.txt new file mode 100644 index 0000000000..532563c916 --- /dev/null +++ b/command.txt @@ -0,0 +1,4 @@ +D|1| play |2020-09-08 +D|0| read this |2020-09-08 +F|1| read that |4.00 +F|0| read book |2.00 \ No newline at end of file diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docs/README.md b/docs/README.md index fd44069597..6abb0737ec 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,20 +1,145 @@ # User Guide -## Features - -### Feature 1 -Description of feature. +### Feature +#### Duke +A robot named Duke who helps you to manage your tasks. +You can simply type in your command, ask Duke to add a task, set a deadline or record a event timing. ## Usage -### `Keyword` - Describe action +### `list` - list all the tasks in your todo list +Duke will list all the tasks in your todo list. + +Example of usage: + +`list` + +Expected outcome: + +`Here are the tasks in your list:` + +`1.[T][✓] read book` + +`2.[D][✓] return book (by: Oct 10 2020)` + +`3.[E][✘] project meeting (at: Aug 6th 2-4pm)` + + +### `done` - mark your corresponding tasks as done +Duke will mark your corresponding tasks as done + +Example of usage: + +`done 3` + +Expected outcome: + +`Nice! I've marked this task as done:` + +`[E][✓] project meeting (at: Aug 6th 2-4pm)` + + +### `delete` - delete a task from your todo list +Duke will delete a task from your todo list + +Example of usage: + +`delete 3` + +Expected outcome: + +`Noted! I've removed this task:` + +`[E][✘] project meeting (at: Aug 6th 2-4pm)` + +`Now you have 3 tasks in the list.` + + +### `todo` - simply add a task +Duke will simply add a task + +Example of usage: + +`todo read book` + +Expected outcome: + +`Got it. I've added this task:` + + `[T][✘] read book` + +`Now you have 1 tasks in the list.` + + +### `deadline` - add a task with a specific deadline +Duke will add a task with a specific deadline + +Example of usage: + +`deadline return book /by 2020-10-10` + +Expected outcome: + +`Got it. I've added this task:` + +`[D][✘] return book (by: Oct 10 2020)` + +`Now you have 2 tasks in the list.` + + +### `event` - add a task that happens at the time you give +Duke will add a task that happens at the time you give + +Example of usage: + +`event return book /at tonight` + +Expected outcome: + +`Got it. I've added this task:` + +`[E][✘] return book (at: tonight)` + +`Now you have 2 tasks in the list.` + + +### `FDTask` - add a task with a fixed duration +Duke will add a task with a fixed duration + +Example of usage: + +`FDTask read book /for 2` + +Expected outcome: + +`Got it. I've added this task:` + +`[F][✘] read book (needs 2.00 hours)` + +`Now you have 2 tasks in the list.` + + +### `find` - find the task with the given keyword +Duke will find the task with the given keyword + +Example of usage: + +`find book` + +Expected outcome: + +`Here are the matching tasks in your list:` + +`1.[F][✘] read book (needs 2.00 hours)` + -Describe action and its outcome. +### `bye` - say goodbye to Duke. +Duke will update you background memory with your current todo list Example of usage: -`keyword (optional arguments)` +`bye` Expected outcome: -`outcome` +`Bye. Hope to see you again soon!` diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..8858fdae57 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..2f7efbeab5 --- /dev/null +++ b/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-minimal \ 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..cc4fdc293d 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..94920145f3 --- /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.0.1-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 100755 index 0000000000..24467a141f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,100 @@ +@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 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/setting.gradle b/setting.gradle new file mode 100644 index 0000000000..e69de29bb2 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/main/java/Deadline.java b/src/main/java/main/java/Deadline.java new file mode 100644 index 0000000000..6723fd614b --- /dev/null +++ b/src/main/java/main/java/Deadline.java @@ -0,0 +1,41 @@ +package main.java; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * A kind of the tasks, which has a specific deadline + */ +public class Deadline extends Task { + protected LocalDate deadline; + + public Deadline(String description, LocalDate deadline) { + super(description); + this.deadline = deadline; + } + + public Deadline(String description, LocalDate deadline, boolean isDone) { + super(description, isDone); + this.deadline = deadline; + this.isDone = isDone; + } + + /** + * gives the String representing the deadline + * @return a String representing the deadline + */ + private String getDeadline() { + return deadline.format(DateTimeFormatter.ofPattern(" MMM dd yyyy")); + } + + @Override + public String getDescription() { + return "[D][" + getStatusIcon() + + "]" + description + "(by:" + getDeadline() + ")"; + } + + @Override + public void printDescription() { + System.out.println(getDescription()); + } +} diff --git a/src/main/java/main/java/DialogBox.java b/src/main/java/main/java/DialogBox.java new file mode 100644 index 0000000000..172e0570ac --- /dev/null +++ b/src/main/java/main/java/DialogBox.java @@ -0,0 +1,64 @@ +package main.java; + +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; + +//@@author {Ringo1225}-reused +//{reused code from https://se-education.org/guides/tutorials/javaFx.html} +/** + * 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; + } +} +//@@author \ No newline at end of file diff --git a/src/main/java/main/java/Duke.java b/src/main/java/main/java/Duke.java new file mode 100644 index 0000000000..c2bfbb0935 --- /dev/null +++ b/src/main/java/main/java/Duke.java @@ -0,0 +1,28 @@ +package main.java; + +/** + * Represents a robot who can help the user to make todo list. + * A Duke object is an instance of such robots. + */ +public class Duke { + private Storage storage; + private TaskList tasks; + private Ui ui; + + public Duke() { + ui = new Ui(); + storage = new Storage("./command.txt"); + try { + tasks = new TaskList(storage.load()); + } catch (DukeException e) { + ui.showLoadingError(); + tasks = new TaskList(); + } + } + + public String getResponse(String input) { + Parser parser = new Parser(tasks, true); + return parser.handleCommand(input); + } +} + diff --git a/src/main/java/main/java/DukeException.java b/src/main/java/main/java/DukeException.java new file mode 100644 index 0000000000..ede4a9fb21 --- /dev/null +++ b/src/main/java/main/java/DukeException.java @@ -0,0 +1,10 @@ +package main.java; + +/** + * Exceptions which are specific to Duke + */ +public class DukeException extends Exception { + public DukeException(String ErrorMessage) { + super(ErrorMessage); + } +} diff --git a/src/main/java/main/java/Event.java b/src/main/java/main/java/Event.java new file mode 100644 index 0000000000..81a67f92f7 --- /dev/null +++ b/src/main/java/main/java/Event.java @@ -0,0 +1,29 @@ +package main.java; + +/** + * A kind of the tasks, which has a specific time period + */ +public class Event extends Task { + protected String time; + + public Event(String description, String time) { + super(description); + this.time = time; + } + + public Event(String description, String time, boolean isDone) { + super(description, isDone); + this.time = time; + } + + @Override + public String getDescription() { + return "[E][" + getStatusIcon() + + "]" + description + "(at:" + time + ")"; + } + + @Override + public void printDescription() { + System.out.println(getDescription()); + } +} diff --git a/src/main/java/main/java/FixedDurationTasks.java b/src/main/java/main/java/FixedDurationTasks.java new file mode 100644 index 0000000000..6af6c30f46 --- /dev/null +++ b/src/main/java/main/java/FixedDurationTasks.java @@ -0,0 +1,38 @@ +package main.java; + +/** + * A kind of the tasks, which has a specific deadline + */ +public class FixedDurationTasks extends Task { + protected double hours; + + public FixedDurationTasks(String description, double hours) { + super(description); + this.hours = hours; + } + + public FixedDurationTasks(String description, double hours, boolean isDone) { + super(description, isDone); + this.hours = hours; + this.isDone = isDone; + } + + /** + * gives a String representing the time needed for the task + * @return a String representing the time needed for the task + */ + String getHours() { + return String.format("%.2f", hours); + } + + @Override + public String getDescription() { + return "[F][" + getStatusIcon() + + "]" + description + "(needs " + getHours() + " hours)"; + } + + @Override + public void printDescription() { + System.out.println(getDescription()); + } +} diff --git a/src/main/java/main/java/Launcher.java b/src/main/java/main/java/Launcher.java new file mode 100644 index 0000000000..8b17438077 --- /dev/null +++ b/src/main/java/main/java/Launcher.java @@ -0,0 +1,12 @@ +package main.java; + +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); + } +} \ No newline at end of file diff --git a/src/main/java/main/java/Main.java b/src/main/java/main/java/Main.java new file mode 100644 index 0000000000..af99c647df --- /dev/null +++ b/src/main/java/main/java/Main.java @@ -0,0 +1,36 @@ +package main.java; + +import java.io.IOException; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +/** + * A GUI for Duke using FXML. + */ +public class Main extends Application { + + private Duke duke = new Duke(); + + //@@author {Ringo1225}-reused + //{reused code from https://se-education.org/guides/tutorials/javaFx.html} + @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); + stage.setTitle("Duke"); + stage.setResizable(false); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + //@@author +} \ No newline at end of file diff --git a/src/main/java/main/java/MainWindow.java b/src/main/java/main/java/MainWindow.java new file mode 100644 index 0000000000..883c03026b --- /dev/null +++ b/src/main/java/main/java/MainWindow.java @@ -0,0 +1,61 @@ +package main.java; + +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +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 main.java.DialogBox; +import main.java.Duke; + +//@@author {Ringo1225}-reused +//{reused code from https://se-education.org/guides/tutorials/javaFx.html} +/** + * 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/DaUser.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + Ui ui = new Ui(); + String greet = ui.greet(); + dialogContainer.getChildren().addAll(DialogBox.getDukeDialog(greet, dukeImage)); + } + + 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("ME: " + input + " ", userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + } +} +//@@author \ No newline at end of file diff --git a/src/main/java/main/java/Parser.java b/src/main/java/main/java/Parser.java new file mode 100644 index 0000000000..6b66af4e9a --- /dev/null +++ b/src/main/java/main/java/Parser.java @@ -0,0 +1,354 @@ +package main.java; + +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Scanner; + +/** + * Represents a handler of the commands. + * It parses the command the user types in and handles the instructions given + */ +public class Parser { + Scanner sc; + TaskList taskList; + boolean flag; + + public Parser(TaskList taskList, boolean flag) { + this.sc = new Scanner(System.in); + this.taskList = taskList; + this.flag = flag; + } + + /** + * parses anf handles the command the user passes into duke + * @param input + * @return the String to print out + */ + public String handleCommand(String input) { + sc = new Scanner(input); + String command = sc.next(); + + switch (command) { + case "bye": + return handleBye(); + + case "list": + return handleList(); + + case "done": + try { + return handleDone(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "delete": + try { + return handleDelete(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "todo": + try { + return handleTodo(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "deadline": + try { + return handleDeadline(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "FDTask": + try { + return handleFDTask(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "event": + try { + return handleEvent(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + case "find": + try { + return handleFind(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + + default: + try { + return handleDefault(); + } catch (DukeException e) { + return " " + e.getMessage(); + } + } + } + + /** + * handles instruction "bye" + */ + public String handleBye() { + flag = false; + try { + this.updateFile(); + } catch (IOException e) { + System.out.println(e); + System.out.println("IOException from FileWriter!!"); + } + return " Bye. Hope to see you again soon!"; + } + + /** + * handles instruction "list" + */ + public String handleList() { + String listOutput = ""; + listOutput += " Here are the tasks in your list:\n"; + for (int i = 0; i < taskList.size(); i++) { + listOutput += " " + (i + 1) + "." + taskList.get(i).getDescription() + "\n"; + } + return listOutput; + } + + /** + * handles instruction "done" + */ + public String handleDone() throws DukeException { + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be done!"); + } + assert sc.hasNextLine(); + String doneCommand = sc.nextLine(); + int index = 0; + if (doneCommand.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be done!"); + } + try { + index = Integer.parseInt(doneCommand.stripLeading()); + } catch (NumberFormatException e) { + return " \u2639 Please enter a valid integer!!"; + } + if (index > taskList.size()) { + throw new DukeException("\u2639 Your number is too large!!"); + } + Task currentTask = taskList.get(index - 1); + + String doneOutput = ""; + currentTask.markAsDone(); + doneOutput += " Nice! I've marked this task as done:\n "; + doneOutput += currentTask.getDescription(); + return doneOutput; + } + + /** + * handles instruction "delete" + */ + public String handleDelete() throws DukeException { + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be deleted!"); + } + assert sc.hasNextLine(); + String deleteCommand = sc.nextLine(); + int index = 0; + if (deleteCommand.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be deleted!"); + } + try { + index = Integer.parseInt(deleteCommand.stripLeading()); + } catch (NumberFormatException e) { + return " \u2639 Please enter a valid integer!!"; + } + if (index > taskList.size()) { + throw new DukeException("\u2639 Your number is too large!!"); + } + assert index <= taskList.size(); + Task currentTask = taskList.get(index - 1); + taskList.remove(index - 1); + String deleteOutput = ""; + deleteOutput += " Noted! I've removed this task:\n "; + deleteOutput += currentTask.getDescription(); + deleteOutput += "\n Now you have " + taskList.size() + " tasks in the list."; + return deleteOutput; + } + + /** + * handles instruction "find" + */ + public String handleFind() throws DukeException { + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! I need to know the keyword!!"); + } + assert sc.hasNextLine(); + String input = sc.nextLine(); + if (input.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! I need to know the keyword!!"); + } + String keyword = input.replaceFirst(" ", ""); + String output = ""; + output += " Here are the matching tasks in your list:\n"; + for (int i = 0, count = 1; i < taskList.size(); i++) { + if (taskList.get(i).description.contains(keyword)) { + output += " " + count + "." + taskList.get(i).getDescription() + "\n"; + count++; + } + } + return output; + } + + /** + * handles instruction "todo" + */ + public String handleTodo() throws DukeException { + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! The description of a todo cannot be empty."); + } + assert sc.hasNextLine(); + String todoDescription = sc.nextLine(); + if (todoDescription.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! The description of a todo cannot be empty."); + } + Todo todo = new Todo(todoDescription); + taskList.add(todo); + String output = ""; + output += "Got it. I've added this task: \n" + todo.getDescription(); + output += "\nNow you have " + taskList.size() + " tasks in the list."; + return output; + } + + /** + * handles instruction "deadline" + */ + public String handleDeadline() throws DukeException { + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! The description of a deadline cannot be empty."); + } + assert sc.hasNextLine(); + String deadlineCommand = sc.nextLine(); + if (deadlineCommand.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! The description of a deadline cannot be empty."); + } + if (!deadlineCommand.contains("/by")) { + throw new DukeException("\u2639 OOPS!!! The timing of a deadline cannot be empty."); + } + String[] strings = deadlineCommand.split("/by"); + String deadlineDescription = strings[0]; + String time = strings[1]; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(" yyyy-MM-dd"); + LocalDate date = LocalDate.parse(time, formatter); + Deadline deadline = new Deadline(deadlineDescription, date); + taskList.add(deadline); + String output = ""; + output += "Got it. I've added this task: \n" + deadline.getDescription(); + output += "\nNow you have " + taskList.size() + " tasks in the list."; + return output; + } + + /** + * handles instruction "FDTask" + */ + public String handleFDTask() throws DukeException{ + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! The description of a FDTask cannot be empty."); + } + String FDCommand = sc.nextLine(); + if (FDCommand.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! The description of a FDTask cannot be empty."); + } + if (!FDCommand.contains("/for")) { + throw new DukeException("\u2639 OOPS!!! The duration of a FDTask cannot be empty."); + } + String[] strings = FDCommand.split("/for"); + String FDDescription = strings[0]; + String period = strings[1]; + double hours = Double.valueOf(period); + FixedDurationTasks fixedDurationTasks = new FixedDurationTasks(FDDescription, hours); + taskList.add(fixedDurationTasks); + String output = ""; + output += "Got it. I've added this task: \n" + fixedDurationTasks.getDescription(); + output += "\nNow you have " + taskList.size() + " tasks in the list."; + return output; + } + + /** + * handles instruction "event" + */ + public String handleEvent() throws DukeException{ + if (!sc.hasNextLine()) { + throw new DukeException("\u2639 OOPS!!! The description of a event cannot be empty."); + } + assert sc.hasNextLine(); + String eventCommand = sc.nextLine(); + if (eventCommand.isEmpty()) { + throw new DukeException("\u2639 OOPS!!! The description of an event cannot be empty."); + } + if (!eventCommand.contains("/at")) { + throw new DukeException("\u2639 OOPS!!! The timing of an event cannot be empty."); + } + String[] strings = eventCommand.split("/at"); + String eventDescription = strings[0]; + String date = strings[1]; + Event event = new Event(eventDescription, date); + taskList.add(event); + String output = ""; + output += "Got it. I've added this task: \n"; + output += event.getDescription() + "Now you have " + taskList.size() + " tasks in the list."; + return output; + } + + /** + * throws exceptions when the command is wrongly typed + * @throws DukeException + */ + public String handleDefault() throws DukeException { + throw new DukeException("\u2639 OOPS!!! I'm sorry, but I don't know what that means :-("); + } + + /** + * updates the task list in the local hard disk + * @throws IOException + */ + public void updateFile() throws IOException { + FileWriter fw = new FileWriter("./command.txt"); + FileWriter fwAppend = new FileWriter("./command.txt", true); + for (int i = 0; i < taskList.size(); i++) { + Task task = taskList.get(i); + String line; + if(task instanceof Todo) { + int done = task.isDone? 1:0; + line = "T|" + done + "|" + task.description; + } else if(task instanceof Deadline) { + int done = task.isDone? 1:0; + line = "D|" + done + "|" + task.description + "|" + + ((Deadline) task).deadline.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + } else if(task instanceof FixedDurationTasks) { + int done = task.isDone? 1:0; + line = "F|" + done + "|" + task.description + "|" + ((FixedDurationTasks) task).getHours(); + } else { + int done = task.isDone? 1:0; + line = "T|" + done + "|" + task.description + "|" + ((Event) task).time; + } + + if (i == 0) { + fw.write(line + '\n'); + } else if (i < taskList.size() - 1) { + fwAppend.write(line + '\n'); + } else { + fwAppend.write(line); + } + } + fw.close(); + fwAppend.close(); + } + +} \ No newline at end of file diff --git a/src/main/java/main/java/Storage.java b/src/main/java/main/java/Storage.java new file mode 100644 index 0000000000..698d812e3c --- /dev/null +++ b/src/main/java/main/java/Storage.java @@ -0,0 +1,86 @@ +package main.java; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.io.File; +import java.util.Scanner; +import java.io.FileNotFoundException; + +/** + * Represents the local storage of the task list + */ +public class Storage { + + ArrayList taskArrayList; + String filePath; + + public Storage(String filePath) { + this.taskArrayList = new ArrayList<>(); + this.filePath = filePath; + } + + /** + * fills the arraylist with the tasks in the hard disk + * @throws IOException + */ + public void fill() throws IOException { + File f = new File(filePath); + Scanner sc; + try { + sc = new Scanner(f); + } catch (FileNotFoundException e) { + f.createNewFile(); + sc = new Scanner(f); + } + + while (sc.hasNext()) { + String next = sc.nextLine(); + String[] token = next.split("\\|"); + String first = token[0]; + boolean isDone = (Integer.parseInt(token[1])) == 1? true: false; + + switch(first) { + case "T": { + String description = token[2]; + taskArrayList.add(new Todo(description, isDone)); + break; + } + case "D": { + String description = token[2]; + String time = token[3]; + LocalDate date = LocalDate.parse(time); + taskArrayList.add(new Deadline(description, date, isDone)); + break; + } + case "E": { + String description = token[2]; + String time = token[3]; + taskArrayList.add(new Event(description, time, isDone)); + break; + } + case "F": { + String description = token[2]; + String period = token[3]; + double hours = Double.valueOf(period); + taskArrayList.add(new FixedDurationTasks(description, hours, isDone)); + break; + } + } + } + } + + /** + * loads the task list from local hard disk + * @return the arraylist that contains the tasks loaded from the local hard disk + * @throws DukeException + */ + public ArrayList load() throws DukeException { + try{ + fill(); + } catch (IOException e) { + throw new DukeException("Cannot find the file!!"); + } + return taskArrayList; + } +} diff --git a/src/main/java/main/java/Task.java b/src/main/java/main/java/Task.java new file mode 100644 index 0000000000..28ecf385fa --- /dev/null +++ b/src/main/java/main/java/Task.java @@ -0,0 +1,45 @@ +package main.java; + +/** + * Tasks that Duke can do for the users + */ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public Task(String description, boolean isDone) { + this.description = description; + this.isDone = isDone; + } + + /** + * Visualizes the isDone() to a String + * @return the icon that represents whether the task is done + */ + public String getStatusIcon() { + return (isDone ? "\u2713" : "\u2718"); + } + + /** + * marks the task as done + */ + public void markAsDone() { + this.isDone = true; + } + + /** + * prints the task in a easy-read manner + */ + public void printDescription() { + System.out.println("[" + getStatusIcon() + "] " + description); + } + + public String getDescription() { + return "[" + getStatusIcon() + "] " + description; + } +} diff --git a/src/main/java/main/java/TaskList.java b/src/main/java/main/java/TaskList.java new file mode 100644 index 0000000000..bd35fbfc48 --- /dev/null +++ b/src/main/java/main/java/TaskList.java @@ -0,0 +1,51 @@ +package main.java; + +import java.util.ArrayList; + +/** + * Represents a container that holds the tasks + */ +public class TaskList { + ArrayList tasks; + + TaskList() { + this.tasks = new ArrayList<>(); + } + + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + + /** + * gives the size of the arraylist + * @return the size of the task list + */ + public int size() { + return tasks.size(); + } + + /** + * returns a task corresponding to the index given + * @param i index of the wanted task + * @return the task corresponding to the index i + */ + public Task get(int i) { + return tasks.get(i); + } + + /** + * adds a new task to the task list + * @param task a new task to be added to the task list + */ + protected void add(Task task) { + tasks.add(task); + } + + /** + * Deletes a task from the task list + * @param i index of the task to be removed + */ + protected void remove(int i) { + tasks.remove(i); + } +} diff --git a/src/main/java/main/java/Todo.java b/src/main/java/main/java/Todo.java new file mode 100644 index 0000000000..e79fd71915 --- /dev/null +++ b/src/main/java/main/java/Todo.java @@ -0,0 +1,24 @@ +package main.java; + +/** + * A kind of the tasks, which contains simply a description + */ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + public Todo(String description, boolean isDone) { + super(description, isDone); + } + + @Override + public void printDescription() { + System.out.println("[T][" + getStatusIcon() + "]" + description); + } + + @Override + public String getDescription() { + return "[T][" + getStatusIcon() + "]" + description; + } +} diff --git a/src/main/java/main/java/Ui.java b/src/main/java/main/java/Ui.java new file mode 100644 index 0000000000..dad669fbaa --- /dev/null +++ b/src/main/java/main/java/Ui.java @@ -0,0 +1,39 @@ +package main.java; + +//@@author {Ringo1225}-reused +//{reused code from nus-cs2103-AY2021S1/ip +// url: https://github.com/nus-cs2103-AY2021S1/ip} +/** + * The User Interface of the robot + * It prints the greeting message and remind user to input + */ +public class Ui { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____\n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + +//@@author + + /** + * prints out the logo and greeting message + */ + public void start() { + System.out.println("Hello from\n" + logo); + System.out.println(" ____________________________________________________________"); + System.out.println(" Hello! I'm Duke\n What can I do for you?"); + System.out.println(" ____________________________________________________________\n"); + } + + public String greet() { + return "Hello from\n" + logo + " Hello! I'm Duke\n What can I do for you?"; + } + + /** + * prints out the loading error message + */ + protected void showLoadingError() { + System.out.println("Oops!! There is a Loading error!!"); + } +} diff --git a/src/main/resources/images/DaDuke.png b/src/main/resources/images/DaDuke.png new file mode 100644 index 0000000000..2f230894c3 Binary files /dev/null and b/src/main/resources/images/DaDuke.png differ diff --git a/src/main/resources/images/DaUser.png b/src/main/resources/images/DaUser.png new file mode 100644 index 0000000000..8e976cfe8b 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..e433809947 --- /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..0590cc8563 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +