diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..6a2349bbbb --- /dev/null +++ b/build.gradle @@ -0,0 +1,56 @@ +plugins { + id 'java' + id 'application' + id 'com.github.johnrengelman.shadow' version '5.1.0' +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0' + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0' + + String javaFxVersion = '11' + + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "duke.Launcher" +} + +shadowJar { + archiveBaseName = "Duke" + archiveClassifier = null +} + +run{ + standardInput = System.in +} diff --git a/docs/README.md b/docs/README.md index 8077118ebe..85b1c9d2a6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,28 +2,152 @@ ## Features -### Feature-ABC +### Keep track of all your important tasks! -Description of the feature. +Use Duke to manage all your todos, deadlines and event. View their dates, times as well as completion status at a glance. -### Feature-XYZ +### Find tasks -Description of the feature. +Duke can help you search specific task(s) that was previously stored based on keyword. ## Usage -### `Keyword` - Describe action +### `todo ` - Creates a todo -Describe the action and its outcome. +Adds a new todo task into the list. Example of usage: -`keyword (optional arguments)` +`todo read book` Expected outcome: -Description of the outcome. +``` +Got it. I've added this task: +[T][] read book +Now you have 1 task(s) in your list. +``` + +### `deadline /by ` - Creates a deadline + +Adds a new deadline task into the list. + +Example of usage: + +`deadline return book /by 2022-09-19T14:30:00` + +Expected outcome: + +``` +Got it. I've added this task: +[D][] return book (by: Sep 19 2022 02:30:00) +Now you have 2 task(s) in your list. +``` + +### `event /at ` - Creates an event + +Adds a new event task into the list. + +Example of usage: + +`event zoom meeting /by 2022-09-19T19:30:00` + +Expected outcome: + +``` +Got it. I've added this task: +[E][] zoom meeting (by: Sep 19 2022 07:30:00) +Now you have 3 task(s) in your list. +``` + +### `list` - Display all current tasks + +Shows all current task(s) in the list. + +Example of usage: + +`list` + +Expected outcome: + +``` +Here are the task(s) in your list: +1. [T][] read book +2. [D][] return book (by: Sep 19 2022 02:30:00) +3. [E][] zoom meeting (at: Sep 19 2022 07:30:00) +``` + +### `mark ` - Marks a task as done + +Marks a task in the list as done. + +Example of usage: + +`mark 1` + +Expected outcome: ``` -expected output +Nice! You've completed this task: +[T][X] read book +``` + +### `unmark ` - Marks a task as not done + +Marks a task in the list as not done. + +Example of usage: + +`unmark 1` + +Expected outcome: + +``` +Oh no! Try to complete this task ASAP: +[T][] read book +``` + +### `delete ` - Deletes a task + +Remove a task from your list. + +Example of usage: + +`delete 1` + +Expected outcome: + +``` +Task eliminated: +[T][] read book +Now you have 2 tasks in your list. +``` + +### `find ` - Search for specific task(s) + +Searches for matching task(s) using a keyword. + +Example of usage: + +`find zoom` + +Expected outcome: + +``` +Here are the task(s) you are looking for: +1. [E][] zoom meeting (at: Sep 19 2022 07:30:00) +``` + +### `bye` - Exits the app + +Exits the application. + +Example of usage: + +`bye` + +Expected outcome: + ``` +Keep moving forward until you finish all your tasks. Goodbye. +``` \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..0ab95f7c0e Binary files /dev/null and b/docs/Ui.png differ diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..f3d88b1c2f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..b7c8c5dbf5 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000000..2fe81a7d95 --- /dev/null +++ b/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..62bd9b9cce --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,103 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334cc..0000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java new file mode 100644 index 0000000000..968ccfe5d0 --- /dev/null +++ b/src/main/java/duke/Deadline.java @@ -0,0 +1,35 @@ +package duke; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents a task which contains a deadline specified by user. + */ +public class Deadline extends Task { + + protected LocalDateTime by; + + /** + * Class constructor. + * + * @param description Description of deadline task. + * @param by Date and time of the deadline task. + */ + public Deadline(String description, String by) { + super(description); + this.by = LocalDateTime.parse(by); //Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30. + } + + @Override + public String saveString() { + return "D | " + (this.isDone ? "1 | " : "0 | ") + this.description + " | " + this.by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + + " (by: " + this.by.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss")) + ")"; + } + +} diff --git a/src/main/java/duke/DialogBox.java b/src/main/java/duke/DialogBox.java new file mode 100644 index 0000000000..c5a3e2f44d --- /dev/null +++ b/src/main/java/duke/DialogBox.java @@ -0,0 +1,74 @@ +package duke; + +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); + + } + + /** + * Sets the dialog box's background colour to purple. + */ + private void setPurpleBackground() { + dialog.setStyle("-fx-background-color: mediumpurple; "); + } + + /** + * 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) { + var db = new DialogBox(text, img); + db.setPurpleBackground(); + return db; + } + + public static DialogBox getDukeDialog(String text, Image img) { + var db = new DialogBox(text, img); + db.flip(); + db.setPurpleBackground(); + return db; + } + +} \ No newline at end of file diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..2eb2ee24a3 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,182 @@ +package duke; + +import java.io.IOException; +import java.util.ArrayList; + + +/** + * Is an interactive chatbot that keep tracks of tasks inputted by the user. + */ +public class Duke { + + private Storage storage; + private TaskList tasks; + private Ui ui; + + /** + * Class constructor. + */ + public Duke() { + ui = new Ui(); + storage = new Storage("src/main/data/duke.txt"); + try { + tasks = new TaskList(storage.load()); + } catch (DukeException e) { + ui.showLoadingError(); + tasks = new TaskList(); + } catch (IOException e) { + System.out.println("I am unable to create a new file."); + } + } + + /** + * Returns a string response based on user's input. + * + * @param input String input entered by user. + * @param duke Duke object. + * @return String response associated with the input. + */ + public String getResponse(String input, Duke duke) { + Parser parser = new Parser(duke); + return parser.initialise(input); + } + + + //Solution adapted from https://github.com/nus-cs2103-AY2223S1/ip/commit/1425eadf831d33ce6909694a2c3d5d58670aacd9 + + /** + * Returns the list of all current tasks. + */ + public String showList() { + return ui.printList(tasks); + } + + + /** + * Handles the addition of a new Todo task inputted by the user. + * + * @param description String description of Todo task. + * @return String response corresponding to user's todo input. + * @throws DukeException + */ + public String setToDo(String description) throws DukeException { + if (description.isEmpty()) { + throw new DukeException("The description of todo cannot be empty"); + } + Task toDo = new ToDo(description); + tasks.addTask(toDo); + int size = tasks.getSize(); + storage.save(tasks); + return ui.printToDo(toDo, size); + } + + /** + * Handles the addition of a new Deadline task inputted by the user. + * + * @param description String description of Deadline task. + * @param by String representation of date and time of the Deadline task. + * @return String response corresponding to user's deadline input + */ + public String setDeadLine(String description, String by) { + Task deadLine = new Deadline(description, by); + tasks.addTask(deadLine); + int size = tasks.getSize(); + storage.save(tasks); + return ui.printDeadLine(deadLine, size); + } + + /** + * Handles the addition of a new Event task inputted by the user. + * + * @param description String description of Event task. + * @param at String representation of date and time of the Event task. + * @return String response corresponding to user's event input. + */ + public String setEvent(String description, String at) { + Task event = new Event(description, at); + tasks.addTask(event); + int size = tasks.getSize(); + storage.save(tasks); + return ui.printEvent(event, size); + } + + + /** + * Calls TaskList and Ui objects to handle the marking of a task. + * + * @param index Index of the task to be marked. + * @return String response corresponding to user's mark input. + * @throws DukeException + */ + public String mark(int index) throws DukeException { + tasks.markTask(index); + Task markedTask = tasks.getTask(index); + storage.save(tasks); + return ui.printMark(markedTask); + } + + /** + * Calls TaskList and Ui objects to handle the unmarking of a task. + * + * @param index Index of the task to be unmarked. + * @return String response corresponding to user's unmark input. + * @throws DukeException + */ + public String unmark(int index) throws DukeException { + tasks.unMarkTask(index); + Task unmarkedTask = tasks.getTask(index); + storage.save(tasks); + return ui.printUnMark(unmarkedTask); + } + + + /** + * Calls TaskList and Ui objects to handle the deletion of a task. + * + * @param index Index of the task to be deleted. + * @return String response corresponding to the user's delete input. + * @throws DukeException + */ + public String delete(int index) throws DukeException { + Task taskToDelete = tasks.deleteTask(index); + int size = tasks.getSize(); + storage.save(tasks); + return ui.printDelete(taskToDelete, size); + } + + /** + * Filters the task list based on the specified keyword and display the list of filtered task(s). + * + * @param keyword Keyword to be used for filtering the list. + * @return String response corresponding to the user's find input. + */ + public String find(String keyword) { + ArrayList taskList = tasks.getAllTasks(); + ArrayList filteredTasks = new ArrayList<>(); + for (Task task : taskList) { + String description = task.description; + if (description.contains(keyword)) { + filteredTasks.add(task); + } + } + return ui.printFind(filteredTasks); + } + + /** + * Returns goodbye message to the user. + * + * @return String of goodbye message. + */ + public String sayBye() { + return ui.goodBye(); + } + + /** + * Returns a list of all available commands usable in Duke to the user. + * + * @return String representation of help list. + */ + public String sendHelp() { + return ui.printHelp(); + } +} diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..1bbfa63d04 --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,17 @@ +package duke; + +/** + * Exception class for Duke. + */ +public class DukeException extends Exception { + + /** + * Class constructor. + * + * @param errorMessage String representation of error message. + */ + public DukeException(String errorMessage) { + super(errorMessage); + } + +} diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java new file mode 100644 index 0000000000..1cc1dbdfc8 --- /dev/null +++ b/src/main/java/duke/Event.java @@ -0,0 +1,34 @@ +package duke; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents a event task. + */ +public class Event extends Task { + + protected LocalDateTime at; + + /** + * Class constructor. + * + * @param description Description of event task. + * @param at Date and time of the event task. + */ + public Event(String description, String at) { + super(description); + this.at = LocalDateTime.parse(at); + } + + @Override + public String saveString() { + return "E | " + (this.isDone ? "1 | " : "0 | ") + this.description + " | " + this.at; + } + + @Override + public String toString() { + return "[E]" + super.toString() + + " (at: " + this.at.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss")) + ")"; + } +} diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..e4ef6b4628 --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,12 @@ +package duke; + +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/duke/Main.java b/src/main/java/duke/Main.java new file mode 100644 index 0000000000..db593707b5 --- /dev/null +++ b/src/main/java/duke/Main.java @@ -0,0 +1,31 @@ +package duke; + +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(); + + @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.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java new file mode 100644 index 0000000000..cdc1633409 --- /dev/null +++ b/src/main/java/duke/MainWindow.java @@ -0,0 +1,64 @@ +package 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; +import javafx.stage.Stage; + +/** + * 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/mikasa.jpg")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/eren.jpg")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + dialogContainer.getChildren() + .addAll(DialogBox + .getDukeDialog("To all Subjects of Ymir. My name is Eren Yeager. How can I help you?", 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, duke); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + + //Solution adapted from https://github.com/24Donovan24/ip/blob/master/src/main/java/duke/MainWindow.java + + if (response.contains("Goodbye")) { + Stage stage = (Stage) sendButton.getScene().getWindow(); + stage.close(); + } + } + +} diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 0000000000..25af61b983 --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,117 @@ +package duke; + +import java.util.Scanner; + +//Solution below adapted from https://github.com/nus-cs2103-AY2223S1/ip/commit/da76395fec6c4ab68f6c849a0431d7297e9fb97f + +/** + * Represents a parser that handles input entered by the user. + */ +public class Parser { + + private Duke duke; + + /** + * Class constructor. + * + * @param duke Duke object. + */ + public Parser(Duke duke) { + this.duke = duke; + } + + /** + * Parses the different type of user input. + * + * @param input String representation of user's input. + * @return String response corresponding to the user's input. + */ + public String initialise(String input) { + String[] splitInput = input.split(" ", 2); + try { + switch (splitInput[0]) { + + case "list": { + return duke.showList(); + } + + case "mark": { + checkForMissingDescription(splitInput); + int index = Integer.parseInt(splitInput[1]) - 1; + return duke.mark(index); + } + + case "unmark": { + checkForMissingDescription(splitInput); + int index = Integer.parseInt(splitInput[1]) - 1; + return duke.unmark(index); + } + + case "todo": { + checkForMissingDescription(splitInput); + String description = splitInput[1].trim(); + return duke.setToDo(description); + } + + case "deadline": { + checkForMissingDescription(splitInput); + String string = splitInput[1].trim(); + String description = string.substring(0, string.indexOf("/") - 1); + String by = string.substring(string.indexOf("/") + 4); + return duke.setDeadLine(description, by); + } + + case "event": { + checkForMissingDescription(splitInput); + String string = splitInput[1].trim(); + String description = string.substring(0, string.indexOf("/") - 1); + String at = string.substring(string.indexOf("/") + 4); + return duke.setEvent(description, at); + } + + case "delete": { + checkForMissingDescription(splitInput); + int index = Integer.parseInt(splitInput[1]) - 1; + return duke.delete(index); + } + + case "find": { + checkForMissingDescription(splitInput); + String string = splitInput[1].trim(); + return duke.find(string); + } + + case "bye": { + return duke.sayBye(); + } + + case "help": { + return duke.sendHelp(); + } + + default: + throw new DukeException("I'm sorry, but I don't know what that means."); + } + } catch (DukeException exception) { + Ui ui = new Ui(); + return ui.printErrorMessage(exception.getMessage()); + } + } + + //Solution below adapted from https://github.com/24Donovan24/ip/blob/master/src/main/java/duke/Parser.java + + /** + * Checks for missing arguments/description in the user's input. + * + * @param input Array of String of the user's input. + * @throws DukeException + */ + public void checkForMissingDescription(String[] input) throws DukeException { + if (input.length == 1) { + throw new DukeException("Hey! What are you trying to do? " + + "Please refer to the user guide if you need help using the commands!"); + } + } + + +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 0000000000..c253588c90 --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,110 @@ +package duke; + +import java.io.FileNotFoundException; +import java.util.Scanner; +import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +/** + * Represents a storage that handles saving of the task list into a file and loading from the file. + */ +public class Storage { + + private File listOfAllTasks; + + /** + * Class constructor. + * + * @param listOfTasks String representation of file's path. + */ + public Storage(String listOfTasks) { + this.listOfAllTasks = new File(listOfTasks); + } + + //Solution adapted from https://github.com/24Donovan24/ip/tree/d036dd6c7e3bdaace325d1a365de12c676f55340 + + /** + * Loads task list from the file. + * + * @return ArrayList of tasks loaded from a saved file if the file exists or a new ArrayList otherwise. + * @throws IOException + * @throws DukeException + */ + public ArrayList load() throws IOException, DukeException{ + ArrayList list = new ArrayList<>(); + Scanner sc = null; + if (!listOfAllTasks.exists()) { + System.out.println("No existing file! Let me create a new one for you."); + try { + listOfAllTasks.getParentFile().mkdirs(); + listOfAllTasks.createNewFile(); + } catch (IOException e) { + System.out.println("I encountered a problem!"); + } + } + try { + sc = new Scanner(listOfAllTasks); + } catch (FileNotFoundException e) { + throw new DukeException("I cannot read your file"); + } + while (sc.hasNextLine()) { + String taskInFile = sc.nextLine(); + String[] taskInArray = taskInFile.split(" \\| "); //The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). + // It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning" + String taskType = taskInArray[0]; + assert taskType.equals("T") || taskType.equals("D") || taskType.equals("E"); + switch (taskType) { + case "T": { + Task task = new ToDo(taskInArray[2]); + list.add(task); + if (taskInArray[1].equals("1")) { + task.markAsDone(); + } + break; + } + + case "D": { + Task task = new Deadline(taskInArray[2], taskInArray[3]); + list.add(task); + if (taskInArray[1].equals("1")) { + task.markAsDone(); + } + break; + } + + case "E": { + Task task = new Event(taskInArray[2], taskInArray[3]); + list.add(task); + if (taskInArray[1].equals("1")) { + task.markAsDone(); + } + break; + } + default: + throw new DukeException("I cannot find a valid task type."); + } + } + return list; + } + + /** + * Saves changes made to tasklist to the file. + * + * @param taskList TaskList object consisting of list of tasks. + */ + public void save(TaskList taskList) { + try { + FileWriter writer = new FileWriter(listOfAllTasks.getPath()); + for (Task task : taskList.getAllTasks()) { + writer.write(task.saveString() + System.lineSeparator()); + } + writer.close(); + } catch (IOException exception) { + System.out.println("I cannot save your tasks in the file."); + } + } + +} diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java new file mode 100644 index 0000000000..5002207605 --- /dev/null +++ b/src/main/java/duke/Task.java @@ -0,0 +1,59 @@ +package duke; + +/** + * Represents a task. + */ +public abstract class Task { + protected String description; + protected boolean isDone; + + /** + * Class constructor. + * + * @param description Description of task. + */ + public Task(String description) { + this.description = description; + this.isDone = false; + } + + /** + * Checks if a task is marked as done or not done. + * + * @return String representing the status of the task's completion status. + */ + public String getStatusIcon() { + return (isDone ? "X" : " "); + } + + /** + * Marks a task as done. + */ + public void markAsDone() { + this.isDone = true; + } + + /** + * Marks a task as not done. + */ + public void markAsNotDone() { + this.isDone = false; + } + + /** + * Creates a string to be saved in save file. + * @return String to be displayed in save file. + */ + abstract String saveString(); + + /** + * Creates a string to represent the task. + * + * @return String representation of task. + */ + @Override + public String toString() { + return "[" + getStatusIcon() + "] " + this.description; + } + +} diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java new file mode 100644 index 0000000000..9fdb3bf683 --- /dev/null +++ b/src/main/java/duke/TaskList.java @@ -0,0 +1,117 @@ +package duke; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents the list of tasks. + */ +public class TaskList { + + private ArrayList tasks; + + private final String HORIZONTAL_LINE_BREAK = "-------------------------"; + + /** + * Default class constructor. + */ + public TaskList() { + this.tasks = new ArrayList<>(); + } + + /** + * Class constructor. + * + * @param list ArrayList of tasks. + */ + public TaskList(ArrayList list) { + this.tasks = list; + } + + /** + * Returns the list of tasks. + * + * @return ArrayList of all tasks. + */ + public ArrayList getAllTasks() { + return tasks; + } + + /** + * Returns a specified task corresponding to the index. + * + * @param index Index of tasks in the list. + * @return Task object. + */ + public Task getTask(int index) { + return tasks.get(index); + } + + /** + * Returns the number of tasks currently in the list. + * + * @return Integer representing the size of the list. + */ + public int getSize() { + return tasks.size(); + } + + /** + * Adds task into the list. + * + * @param task Task object to be added into the list. + */ + public void addTask(Task task) { + tasks.add(task); + } + + /** + * Marks a specified task using index. + * + * @param index Integer representing the index of task to be marked. + * @throws DukeException + */ + public void markTask(int index) throws DukeException { + int size = tasks.size(); + if (index >= size || index < 0) { + throw new DukeException("The index of the task does not exists."); + } else { + Task completedTask = tasks.get(index); + completedTask.markAsDone(); + } + } + + /** + * Unmarks a specified task using index. + * + * @param index Integer representing the index of task to be unmarked. + * @throws DukeException + */ + public void unMarkTask(int index) throws DukeException{ + int size = tasks.size(); + if (index >= size || index < 0) { + throw new DukeException("The index of the task does not exists."); + } else { + Task unfinishedTask = tasks.get(index); + unfinishedTask.markAsNotDone(); + } + } + + /** + * Deletes a specified task using index. + * + * @param index Integer representing the index of task to be deleted. + * @return Task to be deleted. + * @throws DukeException + */ + public Task deleteTask(int index) throws DukeException { + int size = tasks.size(); + if (index >= size || index < 0) { + throw new DukeException("The index of the task does not exists."); + } else { + Task toBeDeleted = tasks.get(index); + tasks.remove(index); + return toBeDeleted; + } + } +} diff --git a/src/main/java/duke/ToDo.java b/src/main/java/duke/ToDo.java new file mode 100644 index 0000000000..d531a2cf8d --- /dev/null +++ b/src/main/java/duke/ToDo.java @@ -0,0 +1,28 @@ +package duke; + +/** + * Represents a todo task. + */ +public class ToDo extends Task { + + /** + * Class constructor. + * + * @param description Description of todo task. + */ + public ToDo(String description) { + super(description); + } + + + @Override + public String saveString() { + return "T | " + (this.isDone ? "1 | " : "0 | ") + this.description; + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } + +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 0000000000..32d603899a --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,165 @@ +package duke; + +import java.util.ArrayList; + +/** + * Represents an Ui that handles user interaction. + */ +public class Ui { + + /** + * Displays goodbye message. + * + * @return String representing the goodbye message. + */ + public String goodBye() { + return String.format("\tKeep moving forward until you finish all your tasks. Goodbye.\n"); + } + + /** + * Displays file loading error message. + */ + public void showLoadingError() { + System.out.println("I cannot load your file!"); + } + + //Solution adapted from https://github.com/24Donovan24/ip/blob/master/src/main/java/duke/Ui.java + + /** + * Displays the current list of tasks. + * + * @param tasks List of task. + * @return String representation of task list. + */ + public String printList(TaskList tasks) { + StringBuilder builder = new StringBuilder(); + builder.append("\tHere are the task(s) in your list:"); + for (int i = 0; i < tasks.getSize(); i++) { + System.out.println((i + 1) + ". " + tasks.getTask(i)); + Task task = tasks.getTask(i); + builder.append("\n\t"); + builder.append((i + 1) + ". " + task.toString()); + } + return builder.toString(); + } + + /** + * Displays the addition of todo task. + * + * @param todo todo task specified by the user. + * @param size Current number of tasks in the list. + * @return String indicating the addition of todo task. + */ + public String printToDo(Task todo, int size) { + return String.format("\tGot it. I've added this task:\n\t%s\n\tNow you have %d task(s) in your list.", todo, size); + } + + /** + * Displays the addition of deadline task. + * + * @param deadline deadline task specified by the user. + * @param size Current number of tasks in the list. + * @return String indicating the addition of deadline task. + */ + public String printDeadLine(Task deadline, int size) { + return String.format("\tGot it. I've added this task:\n\t%s\n\tNow you have %d task(s) in your list.", deadline, size); + } + + /** + * Displays the addition of event task. + * + * @param event event task specified by the user. + * @param size Current number of tasks in the list. + * @return String indicating the addition of event task. + */ + public String printEvent(Task event, int size) { + return String.format("\tGot it. I've added this task:\n\t%s\n\tNow you have %d task(s) in your list.", event, size); + } + + /** + * Displays message when a task has been marked as done. + * + * @param task Task to be marked as done. + * @return String indicating the marking of the task. + */ + public String printMark(Task task) { + return String.format("\tNice! You've completed this task:\n\t%s", task); + + } + + /** + * Displays message when a task has been unmarked. + * + * @param task Task to be unmarked. + * @return String indicating the unmarking of the task. + */ + public String printUnMark(Task task) { + return String.format("\tOh no! Try to complete this task ASAP:\n\t%s", task); + } + + /** + * Displays message when a task has been deleted. + * + * @param task Task to be deleted. + * @param size Number of tasks left in the list after deletion. + * @return String indicating the deletion of task. + */ + public String printDelete(Task task, int size) { + return String.format("\tTask eliminated:\n\t%s\n\tNow you have %d tasks in your list.", task, size); + } + + /** + * Displays list of tasks based on keyword entered by user. + * + * @param list Arraylist of filtered task(s). + * @return String representation of filtered task list. + */ + public String printFind(ArrayList list) { + StringBuilder builder = new StringBuilder(); + builder.append("Here are the task(s) you are looking for:"); + for (int i = 0; i < list.size(); i++) { + Task task = list.get(i); + builder.append((i + 1) + ". " + task.toString()); + } + return builder.toString(); + } + + /** + * Displays error message. + * + * @param msg String representing message to be displayed. + * @return Error message. + */ + public String printErrorMessage(String msg) { + return msg; + } + + /** + * Displays all usable commands available in Duke. + * + * @return String representation of all commands and their description. + */ + public String printHelp() { + StringBuilder builder = new StringBuilder(); + builder.append("These are the commands available for use:"); + builder.append("\n\t"); + builder.append("todo : Add a ToDo type task to the list."); + builder.append("\n\t"); + builder.append("deadline : Add a Deadline type task to the list."); + builder.append("\n\t"); + builder.append("event : Add a Event type task to the list."); + builder.append("\n\t"); + builder.append("list : Display the current list of tasks."); + builder.append("\n\t"); + builder.append("mark : Mark the task as done."); + builder.append("\n\t"); + builder.append("unmark : Mark the task as not done yet."); + builder.append("\n\t"); + builder.append("delete : Remove a task from the list."); + builder.append("\n\t"); + builder.append("find : Find task(s) with the specified keyword."); + builder.append("\n\t"); + builder.append("bye : Exit the program."); + return builder.toString(); + } +} diff --git a/src/main/resources/images/eren.jpg b/src/main/resources/images/eren.jpg new file mode 100644 index 0000000000..f60f72d8ab Binary files /dev/null and b/src/main/resources/images/eren.jpg differ diff --git a/src/main/resources/images/mikasa.jpg b/src/main/resources/images/mikasa.jpg new file mode 100644 index 0000000000..24712934c4 Binary files /dev/null and b/src/main/resources/images/mikasa.jpg differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..9d1d2f109b --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..1b9aa5f9d2 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +