diff --git a/.gitignore b/.gitignore index 2873e189e1..b62c48a477 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,7 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +text-ui-test/a.txt +text-ui-test/b.txt +src/main/java/input.txt +SavedList.txt diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..867684e3cb --- /dev/null +++ b/build.gradle @@ -0,0 +1,59 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + id 'com.github.johnrengelman.shadow' version '5.1.0' + id 'org.openjfx.javafxplugin' version '0.0.13' +} + +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 + enableAssertions = true +} diff --git a/docs/README.md b/docs/README.md index 8077118ebe..c766bbcb87 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,178 @@ +Duke is a desktop tasks lists to allow you to keep track of your tasks and events easily. +It provides an easy to use Graphical User Interface for users to interact with Duke. + # User Guide +1. Ensure that you have `Java 11` or above installed +2. Download the latest `duke.jar` from [here](https://github.com/benedict733/ip/releases) +3. Copy `duke.jar` to the folder you want to use. +4. Double-click the file to start Duke. -## Features +## Features +* Keep track of your tasks easily + * Tasks include todos, tasks with deadlines and events +* Mark tasks as done/not done +* Snooze on tasks with deadlines +* Delete tasks when not needed +* Find tasks by keywords +* Save details into text file for easily retrieval -### Feature-ABC +## Usage -Description of the feature. +### Adding a task: `Todo`, `Event`, `Deadline` -### Feature-XYZ +Adds a task into Skylark. -Description of the feature. +The format for the command is as per follows: +1. `Todo`: `todo DESCRIPTION` +2. `Event`: `event DESCRIPTION /from WHEN /to WHEN` +3. `Deadline`: `deadline DESCRIPTION /by DATETIME` -## Usage +**NOTE** +* There is no particular format for `WHEN` used in `Event` +* The format for `DATETIME` used in `Deadline`: + * `DATETIME`: `yyy-mm-ddThh:mm:ss` -### `Keyword` - Describe action -Describe the action and its outcome. +Example of usage: +* `todo read book` +* `deadline return book /by 2023-03-28T23:59:59` +* `event cs2103 finals /from 26 April 9am /to 11am` -Example of usage: +Expected outcome: +* After running `todo read book` + ``` + Got it. I've added this task: + [T][ ] read book + Now you have 1 tasks in the list. + ``` +* After running `deadline return book /by 2023-03-28T23:59:59` + ``` + Got it. I've added this task: + [D][ ] return book (by:2023-02-28T23:59:59) + Now you have 2 tasks in the list. + ``` +* After running `event cs2103 finals /from 26 April 9am /to 11am` + ``` + Got it. I've added this task: + [E][ ] cs2103 finals (from : 26 April 9am to: 11am) + Now you have 3 tasks in the list. + ``` -`keyword (optional arguments)` +### Lists the current tasks - `list` -Expected outcome: +Shows the list of tasks saved in Skylark -Description of the outcome. +Example of usage: +* `list` +Expected outcome: ``` -expected output +Here are the tasks in your list: +1. [T][ ] borrow book +2. [D][ ] return book (by:2023-02-28T23:59:59) +3. [E][ ] cs2103 finals (from : 26 April 9am to: 11am) ``` + +### Note for the following commands: `delete`, `mark`, `unmark`, `snooze` +1. If the index does not exist: + * After running `delete/mark/unmark/snooze 8` + ``` + Please enter a number from 1 to 2 + ``` +2. If an integer is not entered: + * After running `delete/mark/unmark/snooze six` + ``` + Please enter a valid number + ``` + +### Delete a Task - `delete` + +Delete a particular task + +Example of usage: +* `delete INDEX` + +**NOTE:** The index must be a valid `integer` representing the index of the task + +Expected outcome: +* After running `delete 1` + ``` + Noted. I've removed this task: + [T][ ] borrow book + Now you have 2 tasks in the list + ``` + +### Marking/Unmarking a Task as complete - `unmark`, `mark` + +Marks/Unmarks a particular task from as done/undone + +Example of usage: +* `mark INDEX` +* `unmark INDEX` + +**NOTE:** The index must be a valid `integer` representing the index of the task + +Expected outcome: +* After running `mark 1` + ``` + Noted. I've marked this task as done: + [D][X] return book (by:2023-03-28T23:59:59) + ``` +* After running `unmark 1` + ``` + Noted. I've unmarked this task as done: + [D][ ] return book (by:2023-03-28T23:59:59) + ``` + +### Snoozing a Task - `snooze` + +Snoozes a particular task for 1 day + +Example of usage: +* `snooze INDEX` + +**NOTE:** The index must be a valid `integer` representing the index of the task + +Expected outcome: +* After running `snooze 1` + ``` + Nice!. I've snoozed this task: + [D][ ] return book (by:2023-03-29T23:59:59) + ``` +**NOTE:** If the task at the index is not a `Deadline` task: +* After running `snooze 2` + ``` + Please enter the index of a task which is a deadline + ``` + +### Finding a particular task - `find` + +Finds any task that contains the keyword(s) given by the user + +Example of usage: +* `find KEYWORDS` + +Expected outcome: +* After running `find CS2103` + + ``` + 1. [E][ ] CS2103 finals (from : 26 April 9am to: 11am) + ``` + +**NOTE:** If there are no matching tasks: +* After running `find hehe` + ``` + There are no tasks found with words: + hehe + ``` + +### Exiting the program - `bye` + +Exits the program + +Example of usage: +* `bye` + +### Saving data + +Tasks stored in Duke are automatically saved after any command. \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..e7df4c61fb 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/DialogBox.java b/src/main/java/duke/DialogBox.java new file mode 100644 index 0000000000..1131a63014 --- /dev/null +++ b/src/main/java/duke/DialogBox.java @@ -0,0 +1,60 @@ +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); + } + /** + * 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; + } +} \ 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..c3bc307ed5 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,172 @@ +package duke; + +import duke.command.Command; +import duke.task.TaskList; + +import java.util.Map; +import java.util.HashMap; +import java.nio.file.Path; +import java.nio.file.Paths; + +import javafx.application.Application; +import javafx.fxml.FXML; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; + +/** + * The class that encompasses the main logic of running Duke, the name of the todo list + */ +public class Duke extends Application { + /** + * Minimum length of a string command is given by + * The length of the command +2 (for whitespace and + * at least 1 letter for the command) + */ + static final String HOME_DIRECTORY = System.getProperty("user.dir"); + static final Path DUKE_LIST_DIRECTORY = Paths.get(HOME_DIRECTORY, "SavedList.txt"); + static final String GREETING = "Hello my name is Duke.\nWhat can I do for you?"; + static final String NAME = "DUKE"; + + private Storage storage; + private TaskList tasks; + private Ui ui; + /** + * Returns Duke with the Path specified as filePath and loads the stored tasks + * + * @param filePath Path of where the tasks are stored + */ + public Duke(Path filePath) { + ui = new Ui(); + storage = new Storage(filePath); + try { + tasks = new TaskList(storage.load()); + } catch (DukeException e) { + ui.showLoadingError(); + tasks = new TaskList(); + } + } + + /** + * Returns Duke with the Path as the default path and loads the stored tasks + */ + public Duke() { + this(DUKE_LIST_DIRECTORY); + } + @Override + public void start(Stage stage) { + return; + /* + //Step 1. Formatting the window to look as expected. + + //The container for the content of the chat to scroll. + scrollPane = new ScrollPane(); + dialogContainer = new VBox(); + scrollPane.setContent(dialogContainer); + + userInput = new TextField(); + sendButton = new Button("Send"); + + AnchorPane mainLayout = new AnchorPane(); + mainLayout.getChildren().addAll(scrollPane, userInput, sendButton); + + scene = new Scene(mainLayout); + + stage.setScene(scene); + stage.show(); + + //Step 2. Formatting the window to look as expected + stage.setTitle("Duke"); + stage.setResizable(false); + stage.setMinHeight(600.0); + stage.setMinWidth(400.0); + + mainLayout.setPrefSize(400.0, 600.0); + + scrollPane.setPrefSize(385, 535); + scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); + scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); + + scrollPane.setVvalue(1.0); + scrollPane.setFitToWidth(true); + + // You will need to import `javafx.scene.layout.Region` for this. + dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE); + + userInput.setPrefWidth(325.0); + + sendButton.setPrefWidth(55.0); + + AnchorPane.setTopAnchor(scrollPane, 1.0); + + AnchorPane.setBottomAnchor(sendButton, 1.0); + AnchorPane.setRightAnchor(sendButton, 1.0); + + AnchorPane.setLeftAnchor(userInput , 1.0); + AnchorPane.setBottomAnchor(userInput, 1.0); + + //Part 3. Add functionality to handle user input. +// sendButton.setOnMouseClicked((event) -> { +// handleUserInput(); +// }); +// +// userInput.setOnAction((event) -> { +// handleUserInput(); +// }); + + //Scroll down to the end every time dialogContainer's height changes. + dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0)); + // more code to be added here later + */ + } + /** + * You should have your own function to generate a response to user input. + * Replace this stub with your completed method. + */ + public String getResponse(String input) { + try { + Command c = Parser.parse(input); + return c.execute(tasks, storage); + } catch (DukeException e) { + return e.getMessage(); + } + } + /** + * Runs Duke, allowing the user to use interact with Duke + */ + public void run() { + ui.greet(); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse(fullCommand); + c.execute(tasks, ui, storage); + isExit = c.isExit(); + } catch (DukeException e) { + ui.showError(e.getMessage()); + } finally { + ui.showLine(); + } + } + } + + /** + * Creates Duke with the specified directory where the tasks are stored + * In this case using the default DUKELISTDIRECTORY + * + * @param args String[] of any input + */ + public static void main(String[] args) { + new Duke(DUKE_LIST_DIRECTORY).run(); + } +} diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..a25cca3873 --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,15 @@ +package duke; + +/** + * Exception thrown from Duke's code. + */ +public class DukeException extends Exception { + /** + * Returns a DukeException with errorMessage stored + * + * @param errorMessage String of the error message + */ + public DukeException(String errorMessage) { + super(errorMessage); + } +} \ No newline at end of file diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..1b8f47b613 --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,14 @@ +package duke; + +import duke.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..f1f5b1dda4 --- /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); + fxmlLoader.getController().setStage(stage); + stage.setTitle(Duke.NAME); + 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..ff125d7bf7 --- /dev/null +++ b/src/main/java/duke/MainWindow.java @@ -0,0 +1,67 @@ +package duke; + +import duke.command.ExitCommand; + +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 Stage stage; + 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()); + dialogContainer.getChildren().addAll( + DialogBox.getDukeDialog(Duke.GREETING, dukeImage) + ); + } + + public void setDuke(Duke d) { + duke = d; + } + + public void setStage(Stage stage) { + this.stage = stage; + } + + /** + * 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(); + + if (input.equals(new ExitCommand().toString())) { + this.stage.close(); + } + + String response = duke.getResponse(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 0000000000..aa4a080ed4 --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,83 @@ +package duke; + +import duke.command.Command; +import duke.command.AddTaskCommand; +import duke.command.DeleteCommand; +import duke.command.ExitCommand; +import duke.command.FindCommand; +import duke.command.ListCommand; +import duke.command.MarkCommand; +import duke.command.SnoozeCommand; +import duke.command.UnmarkCommand; + +import java.util.Map; +import java.util.HashMap; + +/** + * The Parser class that is used to take in and parse commands to be used for the correct purpose + */ +public class Parser { + static final String VALID_COMMANDS = "bye, list, mark, unmark, delete, todo, deadline, event, find, snooze"; + static final String INVALID_COMMAND_MESSAGE = "I'm sorry, but I don't know what that means. My valid commands are:\n" + + VALID_COMMANDS; + static final HashMap CORRECT_FORMAT = new HashMap<>(Map.of( + "list", "list", + "bye", "bye", + "todo", "todo THE TASK", + "deadline", "deadline THE TASK /by yyyy-mm-ddThh:mm:ss", + "event", "event THE TASK /from TIME /to TIME", + "mark", "mark INDEX", + "unmark", "unmark INDEX", + "delete", "delete INDEX", + "find", "find WORDS", + "snooze", "snooze INDEX" + )); + + /** + * Returns the correct Command given by the string input + * + * @param fullCommand String containing the input of the full command + * @return cmd the correct type of Command + * @throws DukeException if the command does not exist or is in the wrong format + */ + public static Command parse(String fullCommand) throws DukeException { + String[] words = fullCommand.split(" "); + if (!CORRECT_FORMAT.containsKey(words[0])) { + throw new DukeException(INVALID_COMMAND_MESSAGE); + } + Command cmd = new Command(); + try { + switch (words[0]) { + case "bye": + cmd = new ExitCommand(); + break; + case "list": + cmd = new ListCommand(); + break; + case "mark": + cmd = new MarkCommand(words[1]); + break; + case "unmark": + cmd = new UnmarkCommand(words[1]); + break; + case "delete": + cmd = new DeleteCommand(words[1]); + break; + case "find": + cmd = new FindCommand(words[1]); + break; + case "snooze": + cmd = new SnoozeCommand(words[1]); + break; + default: // for tasks + cmd = new AddTaskCommand(fullCommand); + } + } catch (Exception e) { + throw new DukeException(correctFormat(words[0])); + } + return cmd; + } + private static String correctFormat(String typeOfTask) { + return String.format("Please enter in the correct format:\n%s", CORRECT_FORMAT.get(typeOfTask)); + } +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 0000000000..92518cbd77 --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,140 @@ +package duke; + +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.ToDo; +import duke.task.TaskList; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.ArrayList; + +/** + * The Storage class that is used to store and read the list of Task(s) + */ +public class Storage { + static final String IO_MESSAGE = "File is corrupted"; + static final String FILE_ERROR_MESSAGE = "File format of tasks is wrong."; + static final String HOME_DIRECTORY = System.getProperty("user.dir"); + static final Path DEFAULT_DIRECTORY = Paths.get(HOME_DIRECTORY, "SavedList.txt"); + private final Path filePath; + + /** + * Returns a Storage object with the filePath as the default Path + */ + public Storage() { + this.filePath = DEFAULT_DIRECTORY; + } + + /** + * Returns a Storage object with filePath stored as the Path + * + * @param filePath Path of the stored list + */ + public Storage(Path filePath) { + this.filePath = filePath; + } + + /** + * Returns an ArrayList of Task(s) loaded from the filePath + * + * @return ArrayList list of Task(s) + * @throws DukeException if unable to read the file or the format of a line in the file is wrong + */ + public ArrayList load() throws DukeException { + try { + return this.readSavedFile(this.filePath); + } catch (IOException e) { + throw new DukeException(IO_MESSAGE); + } + } + + /** + * Returns an ArrayList of Task(s) loaded from the Path given + * + * @param path Path directory of stored list of task(s) + * @return ArrayList list of Task(s) + * @throws IOException if unable to read the file + * @throws DukeException if the format of a line in the file is wrong + */ + public ArrayList readSavedFile(Path path) throws IOException, DukeException { + try { + Files.createFile(path); + return new ArrayList<>(); + } catch (FileAlreadyExistsException e) { + ArrayList arr = new ArrayList<>(); + List savedList = Files.readAllLines(path); + for (String task : savedList) { + try { + arr.add(readLineToTask(task)); + } catch (WrongTaskFormatException | ArrayIndexOutOfBoundsException wrongFormat) { + throw new DukeException(FILE_ERROR_MESSAGE); + } + } + return arr; + } + } + + /** + * Reads the line taskDescription and formats it into the correct type of Task + * + * @param taskDescription String description of the task + * @return Task correct type of Task with information + * @throws WrongTaskFormatException if the format of the taskDescription is wrong + */ + public Task readLineToTask(String taskDescription) throws WrongTaskFormatException { + String[] items = taskDescription.split(" \\| "); + boolean isDone = getIsDone(items[1]); + Task task = new Task(taskDescription, isDone); + switch(items[0]) { + case "T": + task = new ToDo(items[2], isDone); + break; + case "D": + task = new Deadline(items[2], items[3], isDone); + break; + case "E": + String[] timeOfEvent = items[3].split("-"); + task = new Event(items[2], timeOfEvent[0], timeOfEvent[1], isDone); + break; + default: + throw new WrongTaskFormatException("Invalid task.Task String Format"); + } + return task; + } + + /** + * Returns true if the doneString is 1 and false otherwise (0) + * + * @param doneString String + * @return boolean + */ + public boolean getIsDone(String doneString) { + return doneString.equals("1"); + } + + /** + * Saves the TaskList into the filePath + * + * @param tasks TaskList list of Tasks + */ + public void saveList(TaskList tasks) { + try { + // no need to deleteIfExists as BufferedWriter automatically clears all prev input + BufferedWriter fileWriter = Files.newBufferedWriter(this.filePath); + for (Task task : tasks.getTasks()) { + fileWriter.write(task.saveString()); + fileWriter.newLine(); + } + fileWriter.close(); + } catch (IOException e) { + // unsure of what cases would throw IOException under write() + } + } +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 0000000000..22fb255bd9 --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,106 @@ +package duke; + +import java.util.Scanner; + +/** + * The Ui class that is used to interact with the user for input and output + */ +public class Ui { + private static final String LINE = "~~~~~~~~~~~~~~~~~~~~"; + private final Scanner sc; + + /** + * Returns a Ui object with the input as the command line + */ + public Ui() { + this.sc = new Scanner(System.in); + } + + /** + * Prints error if there is an issue with loading + */ + public void showLoadingError() { + // error in reading the file + print("error"); + } + + /** + * Prints the String given + * + * @param toBePrinted String + */ + public void print(String toBePrinted) { + System.out.println(toBePrinted); + } + + /** + * Prints the error message given in the format for errors + * + * @param errorMessage String + */ + public void showError(String errorMessage) { + print(errorMsg(errorMessage)); + } + + /** + * Returns a String that formats the error message given + * + * @param error String + * @return String formatted error message + */ + public String errorMsg(String error) { + return String.format("☹ OOPS!!!\n%s :-(", error); + } + /** + * Echoes the input, printing the command that was input + * + * @param command String + */ + public void echo(String command) { + System.out.println(command); + } + + /** + * Returns a String of the name DUKE formatted + * + * @return name String + */ + private String ownName() { + String name = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + return name; + } + + /** + * Prints the greeting message for Duke + */ + public void greet() { + print(String.format("Hello I am:\n%sWhat can I do for you?", ownName())); + } + + /** + * Prints a line used to separate the inputs and outputs + */ + public void showLine() { + System.out.println(LINE); + } + + /** + * Reads the command in the line and returns it as a String + * + * @return String + */ + public String readCommand() { + return this.sc.nextLine(); + } + + /** + * Closes the scanner as it is good practice + */ + public void closeUi() { + this.sc.close(); + } +} diff --git a/src/main/java/duke/WrongTaskFormatException.java b/src/main/java/duke/WrongTaskFormatException.java new file mode 100644 index 0000000000..3876c902c8 --- /dev/null +++ b/src/main/java/duke/WrongTaskFormatException.java @@ -0,0 +1,15 @@ +package duke; + +/** + * Exception thrown if any of the Strings input to Tasks are in the wrong format + */ +public class WrongTaskFormatException extends Exception { + /** + * Returns a WrongTaskFormatException with errorMessage stored + * + * @param errorMessage String of the error message + */ + public WrongTaskFormatException(String errorMessage) { + super(errorMessage); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/AddTaskCommand.java b/src/main/java/duke/command/AddTaskCommand.java new file mode 100644 index 0000000000..01d433fe5a --- /dev/null +++ b/src/main/java/duke/command/AddTaskCommand.java @@ -0,0 +1,105 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; +import duke.task.Task; +import duke.task.ToDo; +import duke.task.Deadline; +import duke.task.Event; + +import java.util.regex.PatternSyntaxException; + +/** + * Represents the command to add the tasks + */ +public class AddTaskCommand extends Command { + private final String typeOfTask; + + /** + * Returns an AddTaskCommand with the command stored + * + * @param command String of the command to be stored + */ + public AddTaskCommand(String command) { + super(command); + this.typeOfTask = command.split(" ")[0]; + } + /** + * Creates the correct type of task and adds the correct task to TaskList + * Display the output via Ui showing the new task created + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the task is in the wrong format + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + try{ + Task task = makeTask(); + tasks.add(task); + ui.print(String.format("Got it. I've added this task:\n\t%s\nNow you have %d tasks in the list.", task, tasks.size())); + storage.saveList(tasks); + } catch (RuntimeException wrongFormat) { + throw new DukeException("Please enter the command in the correct format:\n" + correctFormat()); + } + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + try{ + Task task = makeTask(); + tasks.add(task); + storage.saveList(tasks); + return String.format("Got it. I've added this task:\n\t%s\nNow you have %d tasks in the list.", task, tasks.size()); + } catch (RuntimeException wrongFormat) { + throw new DukeException("Please enter the command in the correct format:\n" + correctFormat()); + } + } + + /** + * Returns the correct Task depending on the command that was entered + * + * @return task Correct version of Task (ToDo, Event, Deadline) + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + */ + private Task makeTask() throws PatternSyntaxException, ArrayIndexOutOfBoundsException { + Task task = new Task(this.command); + String cmd = this.command.replace(typeOfTask + " ", ""); + switch (typeOfTask) { + case "todo": + task = new ToDo(cmd); + break; + case "deadline": + task = new Deadline(cmd); + break; + case "event": + task = new Event(cmd); + break; + } + return task; + } + + /** + * Returns the correct string format for the type of task that is stored in the AddTaskCommand + * + * @return String format + */ + private String correctFormat() { + String format = "THE TASK"; + switch(this.typeOfTask) { + case "todo": + format = "todo [TASK]"; + break; + case "deadline": + format = "deadline [TASK] /by yyyy-mm-ddThh:mm:ss"; + break; + case "event": + format = "event [TASK] /from TIME /to TIME"; + } + return format; + } +} diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 0000000000..0e2a6c7650 --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,77 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the basic Command, from which all other commands inherit from + */ +public class Command { + protected static final String INVALID_NUMBER_MESSAGE = "Please enter a valid number"; + protected static final String EMPTY_LIST_MESSAGE = "You do not have any items in your list!"; + protected final String command; + + /** + * Returns a Command with empty String command + */ + public Command() { + this(""); + } + + /** + * Returns a Command with the command stored + * + * @param command String of the command to be stored + */ + public Command(String command) { + this.command = command; + } + + /** + * Executes the task of the command on TaskList + * Display the output via Ui + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if an issue arises + */ + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + // do the commands purpose for command line + return; + } + + /** + * Returns the String of the output of the execution + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param storage Storage used to save the TaskList to be retrieved in the future + * @return String empty string + */ + public String execute(TaskList tasks, Storage storage) throws DukeException { + // do the commands purpose and returns as String for GUi + return ""; + } + protected String listString(TaskList tasks) throws DukeException { + if (tasks.isEmpty()) { + throw new DukeException(EMPTY_LIST_MESSAGE); + } + String lst = String.format("1. %s", tasks.get(1)); + for (int i = 2; i <= tasks.size(); i++) { + lst += String.format("\n%d. %s", i, tasks.get(i)); + } + return lst; + } + /** + * Returns a boolean: true if the command is an ExitCommand, false otherwise + * + * @return boolean if the command is an ExitCommand + */ + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 0000000000..3e21d014ac --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,60 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to delete a particular task + */ +public class DeleteCommand extends Command { + private final String index; + + /** + * Returns a DeleteCommand with the index stored + * + * @param index Integer as String + */ + public DeleteCommand(String index) { + super("delete"); + this.index = index; + } + /** + * Deletes the task at the target index from TaskList + * Display the output via Ui showing the old task that was deleted + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + try { + int idx = Integer.parseInt(this.index); + String taskDescription = tasks.delete(idx); + ui.print(output(taskDescription, tasks.size())); + storage.saveList(tasks); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + try { + int idx = Integer.parseInt(this.index); + String taskDescription = tasks.delete(idx); + storage.saveList(tasks); + return output(taskDescription, tasks.size()); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + + private String output(String taskDescription, int size) { + return String.format("Noted, I've removed this task: \n\t%s\nNow you have %d tasks in this list.", + taskDescription, size); + } +} diff --git a/src/main/java/duke/command/EchoCommand.java b/src/main/java/duke/command/EchoCommand.java new file mode 100644 index 0000000000..525c570bfe --- /dev/null +++ b/src/main/java/duke/command/EchoCommand.java @@ -0,0 +1,37 @@ +package duke.command; + +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to echo the input of the user + */ +public class EchoCommand extends Command { + private final String input; + + /** + * Returns an EchoCommand with the input stored + * + * @param input + */ + public EchoCommand(String input) { + this.input = input; + } + /** + * Prints the input of the user + * Display the output via Ui showing the input of the user + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + ui.print(input); + } + @Override + public String execute(TaskList tasks, Storage storage) { + return input; + } +} diff --git a/src/main/java/duke/command/ExitCommand.java b/src/main/java/duke/command/ExitCommand.java new file mode 100644 index 0000000000..a816eef2d3 --- /dev/null +++ b/src/main/java/duke/command/ExitCommand.java @@ -0,0 +1,43 @@ +package duke.command; + +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to exit the application + */ +public class ExitCommand extends Command { + /** + * Close the Ui + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + storage.saveList(tasks); + ui.closeUi(); + } + @Override + public String execute(TaskList tasks, Storage storage) { + storage.saveList(tasks); + return "Goodbye. See you again!! :)"; + } + /** + * Returns true as this is an ExitCommand + * + * @return true + */ + @Override + public boolean isExit() { + return true; + } + + @Override + public String toString() { + return "bye"; + } +} diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 0000000000..15463c7aee --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,52 @@ +package duke.command; + +import duke.DukeException; +import duke.Storage; +import duke.Ui; +import duke.task.TaskList; + +/** + * Represents the command used to find tasks containing words or phrases + */ +public class FindCommand extends Command { + /** + * Returns a FindCommand + * + * @param wordsToFind String for word(s) to store to search in TaskList + */ + public FindCommand(String wordsToFind) { + super(wordsToFind); + } + /** + * Finds tasks in TaskList which contains the words stored + * Display the output via Ui showing the tasks found with the words + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + TaskList tasksWithWords = tasks.find(this.command); + if (tasksWithWords.isEmpty()) { + throw new DukeException(noItemsFoundMessage()); + } + + ui.print(listString(tasksWithWords)); + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + TaskList tasksWithWords = tasks.find(this.command); + if (tasksWithWords.isEmpty()) { + throw new DukeException(noItemsFoundMessage()); + } + + return listString(tasksWithWords); + } + + private String noItemsFoundMessage() { + return String.format("There are no tasks found with words:\n%s", this.command); + } +} diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 0000000000..e5f507a0b2 --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,30 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to list out all the tasks + */ +public class ListCommand extends Command { + /** + * Lists the tasks in TaskList + * Display the output via Ui showing the old task that was deleted + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + ui.print(listString(tasks)); + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + return listString(tasks); + } +} diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java new file mode 100644 index 0000000000..ab7794cdcc --- /dev/null +++ b/src/main/java/duke/command/MarkCommand.java @@ -0,0 +1,65 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to mark a task as done at the index + */ +public class MarkCommand extends Command { + private final String index; + + /** + * Returns a MarkCommand with the index stored + * + * @param index Integer as String + */ + public MarkCommand(String index) { + super("mark"); + this.index = index; + } + /** + * Marks the task as done at the target index from TaskList + * Display the output via Ui showing the updated marked task + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + tasks.get(idx).setDone(); + ui.print(output(tasks.get(idx).toString())); + storage.saveList(tasks); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + tasks.get(idx).setDone(); + storage.saveList(tasks); + return output(tasks.get(idx).toString()); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + + private String output(String taskString) { + return String.format("Nice! I've marked this task as done: \n\t%s", taskString); + } +} diff --git a/src/main/java/duke/command/SnoozeCommand.java b/src/main/java/duke/command/SnoozeCommand.java new file mode 100644 index 0000000000..2db96ec1ed --- /dev/null +++ b/src/main/java/duke/command/SnoozeCommand.java @@ -0,0 +1,77 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.Task; +import duke.task.TaskList; + +/** + * Represents the command to mark a task as done at the index + */ +public class SnoozeCommand extends Command { + private static final String NOT_DEADLINE_MESSAGE = "Please enter the index of a task which is a deadline"; + private final String index; + + /** + * Returns a MarkCommand with the index stored + * + * @param index Integer as String + */ + public SnoozeCommand(String index) { + super("snooze"); + this.index = index; + } + /** + * Marks the task as done at the target index from TaskList + * Display the output via Ui showing the updated marked task + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + Task task = tasks.get(idx); + if (task.isDeadline()) { + task.snooze(); + } else { + throw new DukeException(NOT_DEADLINE_MESSAGE); + } + ui.print(output(task)); + storage.saveList(tasks); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + Task task = tasks.get(idx); + if (task.isDeadline()) { + task.snooze(); + } else { + throw new DukeException(NOT_DEADLINE_MESSAGE); + } + storage.saveList(tasks); + return output(task); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + + private String output(Task task) { + return String.format("Nice! I've snoozed this task: \n\t%s", task.toString()); + } +} diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java new file mode 100644 index 0000000000..145c1f1961 --- /dev/null +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -0,0 +1,64 @@ +package duke.command; + +import duke.DukeException; +import duke.Ui; +import duke.Storage; +import duke.task.TaskList; + +/** + * Represents the command to unmark a task as done at the index + */ +public class UnmarkCommand extends Command { + private final String index; + /** + * Returns an UnmarkCommand with the index stored + * + * @param index Integer as String + */ + public UnmarkCommand(String index) { + super("unmark"); + this.index = index; + } + /** + * Unmarks the task as done at the target index from TaskList + * Display the output via Ui showing the updated unmarked task + * Saves the file via Storage + * + * @param tasks TaskList of all the tasks + * @param ui Ui the user interface to interact with the user + * @param storage Storage used to save the TaskList to be retrieved in the future + * @throws DukeException if the String index is not an integer OR if index is not in range of size of TaskList + */ + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + tasks.get(idx).setNotDone(); + ui.print(output(tasks.get(idx).toString())); + storage.saveList(tasks); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + @Override + public String execute(TaskList tasks, Storage storage) throws DukeException { + if (tasks.size() == 0) { + throw new DukeException(Command.EMPTY_LIST_MESSAGE); + } + try { + int idx = Integer.parseInt(this.index); + tasks.get(idx).setNotDone(); + storage.saveList(tasks); + return output(tasks.get(idx).toString()); + } catch (NumberFormatException notANumber) { + throw new DukeException(Command.INVALID_NUMBER_MESSAGE); + } + } + + private String output(String taskString) { + return String.format("Nice! I've unmarked this task as done: \n\t%s", taskString); + } +} diff --git a/src/main/java/duke/input.txt b/src/main/java/duke/input.txt new file mode 100644 index 0000000000..ae02667119 --- /dev/null +++ b/src/main/java/duke/input.txt @@ -0,0 +1,6 @@ +todo borrow book +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +mark 2 +list +bye \ No newline at end of file diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java new file mode 100644 index 0000000000..9484b826c1 --- /dev/null +++ b/src/main/java/duke/task/Deadline.java @@ -0,0 +1,87 @@ +package duke.task; + +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; +import java.util.regex.PatternSyntaxException; + +/** + * Represents a Deadline task + */ +public class Deadline extends Task { + private static final long DEFAULT_SNOOZE_DAYS = 1; + private LocalDateTime deadline; + + /** + * Returns a Deadline stored as not done + * + * @param cmd String with format deadline [TASK] /by yyyy-mm-ddThh:mm:ss + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + * @throws DateTimeParseException if the deadline was not formatted correctly + */ + public Deadline(String cmd) + throws PatternSyntaxException, ArrayIndexOutOfBoundsException, DateTimeParseException { + this(cmd, Task.IS_DONE); + } + + /** + * Returns a Deadline with task and isDone stored + * + * @param cmd String with format deadline [TASK] /by yyyy-mm-ddThh:mm:ss + * @param isDone boolean of if the Deadline is done + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + * @throws DateTimeParseException if the deadline was not formatted correctly + */ + public Deadline(String cmd, boolean isDone) + throws PatternSyntaxException, ArrayIndexOutOfBoundsException, DateTimeParseException { + this(cmd.split(" /by ")[0], + cmd.split(" /by ")[1], + isDone); + } + + /** + * Returns a Deadline with task, deadline and isDone stored + * + * @param task String of Deadline description to be stored + * @param deadlineString String of deadline in format yyyy-mm-ddThh:mm:ss + * @param isDone boolean of if the Deadline is done + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + @throws DateTimeParseException if the deadline was not formatted correctly + */ + public Deadline(String task, String deadlineString, boolean isDone) + throws PatternSyntaxException, ArrayIndexOutOfBoundsException, DateTimeParseException { + super(task, isDone); + this.deadline = LocalDateTime.parse(deadlineString); + } + @Override + public void snooze() { + deadline = deadline.plusDays(DEFAULT_SNOOZE_DAYS); + } + @Override + public boolean isDeadline() { + return true; + } + + /** + * Return the String of the Deadline formatted to be displayed + * + * @return String formatted String + */ + @Override + public String toString() { + return String.format("[D]%s (by:%s)", super.toString(), deadline); + } + + /** + * Return the String of the Deadline used to be saved + * + * @return String formatted String + */ + @Override + public String saveString() { + int done = isDone() ? 1 : 0; + return String.format("D | %d | %s | %s", done, getTask(), deadline); + } +} \ No newline at end of file diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java new file mode 100644 index 0000000000..7d00be67e8 --- /dev/null +++ b/src/main/java/duke/task/Event.java @@ -0,0 +1,72 @@ +package duke.task; + +import java.util.regex.PatternSyntaxException; + +/** + * Represents an Event task + */ +public class Event extends Task { + private final String from; + private final String to; + + /** + * Returns an Event with task stored as not done + * + * @param cmd String with format event [TASK] /from [WHEN] /to [WHEN] + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + */ + public Event(String cmd) throws PatternSyntaxException, ArrayIndexOutOfBoundsException { + this(cmd, Task.IS_DONE); + } + + /** + * Returns an Event with task and isDone stored + * + * @param cmd String with format event [TASK] /from [WHEN] /to [WHEN] + * @param isDone boolean of if the Event is done + * @throws PatternSyntaxException if the command was not formatted correctly + * @throws ArrayIndexOutOfBoundsException if the command was not formatted correctly + */ + public Event(String cmd, boolean isDone) throws PatternSyntaxException, ArrayIndexOutOfBoundsException { + this(cmd.split(" /")[0], + cmd.split(" /")[1].replace("from ", ""), + cmd.split(" /")[2].replace("to ", ""), + isDone); + } + + /** + * Returns an Event with the task, from, to and isDone stored immediately + * + * @param task String of Event description to be stored + * @param from String of when Event starts + * @param to String of when Event ends + * @param isDone boolean of if the Event is done + */ + public Event(String task, String from, String to, boolean isDone) { + super(task, isDone); + this.from = from; + this.to = to; + } + + /** + * Return the String of the Event formatted to be displayed + * + * @return String formatted String + */ + @Override + public String toString() { + return String.format("[E]%s (from: %s to: %s)", super.toString(), from, to); + } + + /** + * Return the String of the Event used to be saved + * + * @return String formatted String + */ + @Override + public String saveString() { + int done = isDone() ? 1 : 0; + return String.format("E | %d | %s | %s-%s", done, getTask(), from, to); + } +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java new file mode 100644 index 0000000000..a42a32a6fc --- /dev/null +++ b/src/main/java/duke/task/Task.java @@ -0,0 +1,90 @@ +package duke.task; + +/** + * Represents a Task, from which all other tasks inherit from + */ +public class Task { + protected static final boolean IS_DONE = false; + + private final String task; + private boolean isDone; + + /** + * Returns a Task with task stored that is not done + * + * @param task String of task to be stored + */ + public Task(String task) { + this(task, IS_DONE); + } + + /** + * Returns a Task with task and isDone stored + * + * @param task String of task to be stored + * @param isDone boolean of if the task is done + */ + public Task(String task, boolean isDone) { + this.task = task; + this.isDone = isDone; + assert task.length() > 0 : "Task description should not be empty!"; + } + + /** + * Returns true if the task is done and false otherwise + * + * @return isDone boolean + */ + public boolean isDone() { + return this.isDone; + } + /** + * Sets task as done + */ + public void setDone() { + this.isDone = true; + } + /** + * Sets task as not done + */ + public void setNotDone() { + this.isDone = false; + } + /** + * Returns a boolean: true if the task is a Deadline, false otherwise + * + * @return boolean if the command is a Deadline + */ + public boolean isDeadline() { return false; } + public void snooze() { + + } + /** + * Returns the String task without any formatting + * + * @return task String + */ + protected String getTask() { + return this.task; + } + /** + * Returns the String of the task formatted to be displayed + * + * @return String formatted String + */ + @Override + public String toString() { + String markedAsDone = isDone ? "X" : " "; + return String.format("[%s] %s", markedAsDone, task); + } + + /** + * Returns the String of the task used to be saved + * + * @return String formatted String + */ + public String saveString() { + int done = isDone() ? 1 : 0; + return String.format("Task | %d | %s", done, task); + } +} diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java new file mode 100644 index 0000000000..b66e2081f2 --- /dev/null +++ b/src/main/java/duke/task/TaskList.java @@ -0,0 +1,119 @@ +package duke.task; + +import duke.DukeException; + +import java.util.ArrayList; + +/** + * Represents the TaskList that is used to store the tasks using 1-based indexing + */ +public class TaskList { + private static final String NO_ITEMS_MESSAGE = "You do not have any items in your list!"; + private final ArrayList tasks; + + /** + * Returns a new TaskList without any tasks + */ + public TaskList() { + this(new ArrayList<>()); + } + + /** + * Returns a new TaskList containing the Tasks in tasks + * + * @param tasks ArrayList of Task(s) + */ + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + private String getIndexOutOfBoundsMessage() { + return String.format("Please enter a number from 1 to %d", this.size()); + } + + /** + * Returns the Task at the index + * + * @param index int used to get the Task at index + * @return Task at the index + * @throws DukeException if index is not in range of size of TaskList + */ + public Task get(int index) throws DukeException { + try { + return tasks.get(index - 1); + } catch (IndexOutOfBoundsException e) { + throw new DukeException(getIndexOutOfBoundsMessage()); + } + } + + /** + * Returns the size of the list + * + * @return size int size of list + */ + public int size() { + return tasks.size(); + } + + /** + * Returns true if the TaskList has no items and false otherwise + * + * @return isEmpty boolean + */ + public boolean isEmpty() { + return tasks.isEmpty(); + } + + /** + * Adds the task given into the TaskList + * + * @param task Task to be added + */ + public void add(Task task) { + this.tasks.add(task); + } + + /** + * Deletes the Task at the index + * + * @param index int used to delete the Task at index + * @return String of the deleted Task toString() + * @throws DukeException if index is not in range of size of TaskList + */ + public String delete(int index) throws DukeException { + if (size() == 0) { + throw new DukeException(NO_ITEMS_MESSAGE); + } + try { + String taskDescription = this.get(index).toString(); + this.tasks.remove(index-1); + return taskDescription; + } catch (IndexOutOfBoundsException badNumber) { + throw new DukeException(getIndexOutOfBoundsMessage()); + } + } + + /** + * Returns the ArrayList which stores the tasks + * + * @return ArrayList of Tasks + */ + public ArrayList getTasks() { + return this.tasks; + } + + /** + * Returns a new TaskList with Tasks that contains the words provided + * + * @param words String words to be found in Tasks + * @return TaskList with only Tasks that contain the words + */ + public TaskList find(String words) { + ArrayList tasksWithWord = new ArrayList<>(); + for (Task task : tasks) { + if (task.getTask().contains(words)) { + tasksWithWord.add(task); + } + } + return new TaskList(tasksWithWord); + } +} diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java new file mode 100644 index 0000000000..a128bfa595 --- /dev/null +++ b/src/main/java/duke/task/ToDo.java @@ -0,0 +1,47 @@ +package duke.task; + +import java.util.regex.PatternSyntaxException; + +/** + * Represents a ToDo task + */ +public class ToDo extends Task { + /** + * Returns a ToDo stored as not done + * + * @param task String of ToDo to be stored + */ + public ToDo(String task) { + super(task); + } + + /** + * Returns a ToDo with task and isDone stored + * + * @param task String of ToDo to be stored + * @param isDone boolean of if the ToDo is done + */ + public ToDo(String task, boolean isDone) { + super(task, isDone); + } + /** + * Return the String of the ToDo formatted to be displayed + * + * @return String formatted String + */ + @Override + public String toString() { + return String.format("[T]%s", super.toString()); + } + + /** + * Return the String of the ToDo used to be saved + * + * @return String formatted String + */ + @Override + public String saveString() { + int done = isDone() ? 1 : 0; + return String.format("T | %d | %s", done, getTask()); + } +} diff --git a/src/main/resources/images/DaDuke.png b/src/main/resources/images/DaDuke.png new file mode 100644 index 0000000000..d893658717 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..3c82f45461 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..832f84382e --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +