diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000000..391c46b4fe --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,34 @@ +name: Java CI + +on: [push, pull_request] + +jobs: + build: + strategy: + matrix: + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + + steps: + - name: Set up repository + uses: actions/checkout@master + + - name: Set up repository + uses: actions/checkout@master + with: + ref: master + + - name: Merge to master + run: git checkout --progress --force ${{ github.sha }} + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Setup JDK 11 + uses: actions/setup-java@v1 + with: + java-version: '11' + java-package: jdk+fx + + - name: Build and check with Gradle + run: ./gradlew check diff --git a/.gitignore b/.gitignore index f69985ef1f..3ed3ce3bf4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ /out/ /*.iml +# vscode files +/.vscode/ + # Gradle build files /.gradle/ /build/ @@ -14,4 +17,9 @@ src/main/resources/docs/ bin/ /text-ui-test/ACTUAL.txt +/text-ui-test/data/ text-ui-test/EXPECTED-UNIX.TXT +/data/ +/notes/ + +*.class diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..eb57e4eeea --- /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..211bc5dc62 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,237 @@ # User Guide -## Features +## Features -### Feature-ABC +### Manage tasks -Description of the feature. +Adds, deletes, marks as done, and finds tasks in the task list. -### Feature-XYZ +Tasks are automatically saved to a file on your computer. -Description of the feature. +### Manage notes + +Adds and deletes notes in the list of saved text files ## Usage -### `Keyword` - Describe action +### `todo` - Adding a todo task + +Adds a todo task to the task list. + +Example of usage: + +`todo TASK_DESCRIPTION` + +Expected outcome: + +A single todo task should be added into the list with `TASK_DESCRIPTION` as the description. + +``` +Task has been added!: [T][] TASK_DESCRIPTION +Total tasks: NUMBER_OF_TOTAL_TASKS +``` + +### `deadline` - Adding a deadline task + +Adds a deadline task to the task list. + +Example of usage: + +`todo TASK_DESCRIPTION /by DATE` + +Expected outcome: + +A single deadline task should be added into the list with `TASK_DESCRIPTION` as the description and `DATE` as the deadline. + +``` +Task has been added!: [D][] TASK_DESCRIPTION (by: DATE) +Total tasks: NUMBER_OF_TOTAL_TASKS +``` + +### `event` - Adding a event task + +Adds a event task to the task list. + +Example of usage: + +`event TASK_DESCRIPTION /at DATE` + +Expected outcome: + +A single event task should be added into the list with `TASK_DESCRIPTION` as the description and `DATE` as the date. + +``` +Task has been added!: [E][] TASK_DESCRIPTION (at: DATE) +Total tasks: NUMBER_OF_TOTAL_TASKS +``` + +### `list` - Listing all tasks + +Lists all tasks in the task list. + +Example of usage: + +`list` + +Expected outcome: + +All tasks in the task list should be listed. + +``` +1) [T][] TASK_DESCRIPTION +2) [D][] TASK_DESCRIPTION (by: DATE) +3) [E][] TASK_DESCRIPTION (at: DATE) +... +``` + +### `reminder` - Listing all tasks with deadlines that are not done + +Lists all tasks with deadlines that are not done in the task list sorted by their deadlines. + +Example of usage: + +`reminder` + +Expected outcome: + +All tasks with deadlines that are not done in the task list should be listed. + +``` +1) [D][] TASK_DESCRIPTION (by: DATE) +... +``` + +### `sort` - Sorting all tasks + +Lexicographically sorts all tasks in the task list by their descriptions. + +Example of usage: + +`sort` + +Expected outcome: + +All tasks in the task list should be sorted. + +``` +1) [T][] TASK_DESCRIPTION +2) [D][] TASK_DESCRIPTION (by: DATE) +3) [E][] TASK_DESCRIPTION (at: DATE) +... +``` + +### `find` - Finding tasks + +Finds all tasks in the task list that contain the keyword. + +Example of usage: + +`find KEYWORD` + +Expected outcome: + +All tasks in the task list that contain `KEYWORD` in their descriptions should be listed. + +``` +1) [T][] TASK_DESCRIPTION +2) [D][] TASK_DESCRIPTION (by: DATE) +3) [E][] TASK_DESCRIPTION (at: DATE) +... +``` + +### `mark` - Marking a task as done + +Marks a task as done in the task list. + +Example of usage: + +`mark TASK_NUMBER` + +Expected outcome: + +The task with `TASK_NUMBER` as the index should be marked as done. + +``` +Task TASK_NUMBER is marked as done! +``` + +### `unmark` - Marking a task as not done + +Marks a task as not done in the task list. + +Example of usage: + +`unmark TASK_NUMBER` + +Expected outcome: + +The task with `TASK_NUMBER` as the index should be marked as not done. + +``` +Task TASK_NUMBER is marked as not done! +``` + +### `delete` - Deleting a task + +Deletes a task from the task list. + +Example of usage: + +`delete TASK_NUMBER` + +Expected outcome: + +The task with `TASK_NUMBER` as the index should be deleted. + +``` +Task TASK_NUMBER is deleted! +``` + +### `add_note` - Adding a note + +Adds a note to the list of saved text files. + +Example of usage: + +`add_note NOTE_NAME /note NOTE_CONTENT` + +Expected outcome: + +A single note should be added into the list with `NOTE_NAME` as the name and `NOTE_CONTENT` as the content. + +``` +Note added. +``` + +### `load_note` - Loading a note + +Loads a note from the list of saved text files. + +Example of usage: + +`load_note NOTE_NAME` + +Expected outcome: + +The note with `NOTE_NAME` as the name should be loaded. + +``` +Here is your note: +NOTE_CONTENT +``` + +### `delete_note` - Deleting a note -Describe the action and its outcome. +Deletes a note from the list of saved text files. -Example of usage: +Example of usage: -`keyword (optional arguments)` +`delete_note NOTE_NAME` Expected outcome: -Description of the outcome. +The note with `NOTE_NAME` as the name should be deleted. ``` -expected output +Note deleted successfully! ``` diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..a2cad7a411 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/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..89c617d39a --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,75 @@ +package duke; + +import duke.command.Command; +import duke.common.DukeException; +import duke.parser.Parser; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Main class of the application + * + * @author Pontakorn Prasertsuk + */ +public class Duke { + + private static final String FILE_PATH = "data/tasks.txt"; + private static final String NOTE_PATH = "notes"; + + private final Ui ui; + private final Storage storage; + private TaskList taskList; + + /** + * Constructs a new Duke instance. + * + * @param filePath the path to the file that stores the tasks + */ + public Duke(String filePath, String notePath) { + ui = new Ui(); + storage = new Storage(filePath, notePath); + try { + taskList = new TaskList(storage.load()); + } catch (DukeException e) { + ui.showError(e.getMessage()); + taskList = new TaskList(); + } + } + + /** + * Runs the Duke application. + */ + public void run() { + ui.showGreeting(); + boolean isExit = false; + while (!isExit) { + ui.showInput(); + try { + String fullCommand = Parser.readCommand(); + ui.showLine(); + Command command = Parser.parse(fullCommand); + command.execute(taskList, ui, storage); + isExit = command.isExit(); + } catch (DukeException e) { + ui.showError(e.getMessage()); + } finally { + ui.showLine(); + } + } + } + + public String getResponse(String input) { + try { + Command command = Parser.parse(input); + + return command.execute(taskList, ui, storage); + } catch (DukeException e) { + return "Error: " + e.getMessage(); + } + } + + public static void main(String[] args) { + new Duke(FILE_PATH, NOTE_PATH).run(); + } +} diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..08cd46827a --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,14 @@ +package duke; + +import javafx.application.Application; + +/** + * A launcher for Duke GUI + * + * @author Pontakorn Prasertsuk + */ +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..e3f9015109 --- /dev/null +++ b/src/main/java/duke/Main.java @@ -0,0 +1,42 @@ +package duke; + +import duke.controller.MainWindow; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +import java.io.IOException; + +/** + * GUI application entry point + * + * @author Pontakorn Prasertsuk + */ +public class Main extends Application { + private static final String FILE_PATH = "data/tasks.txt"; + private static final String NOTE_PATH = "notes"; + + private final Duke duke = new Duke(FILE_PATH, NOTE_PATH); + + /** + * Starts the GUI application + * + * @param stage JavaFX stage + */ + @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); + stage.setTitle("Bob the Bot"); + fxmlLoader.getController().setDuke(duke); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java new file mode 100644 index 0000000000..3084121616 --- /dev/null +++ b/src/main/java/duke/command/AddCommand.java @@ -0,0 +1,56 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.Task; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to add a task to the task list + * + * @author Pontakorn Prasertsuk + */ +public class AddCommand extends Command { + + private Task task; + + /** + * Constructs a new AddCommand instance + * + * @param task the task to be added + */ + public AddCommand(Task task) { + this.task = task; + } + + /** + * Executes the AddCommand. + * + * @param taskList the task list to be added to + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + taskList.add(task); + ui.showOutput("Task has been added!: " + task.toString()); + ui.showOutput("Total tasks: " + taskList.getTaskList().size()); + storage.save(taskList.getTaskList()); + + return "Task has been added!: " + task.toString() + "\n" + "Total tasks: " + + taskList.getTaskList().size(); + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/AddNoteCommand.java b/src/main/java/duke/command/AddNoteCommand.java new file mode 100644 index 0000000000..e9f8e40080 --- /dev/null +++ b/src/main/java/duke/command/AddNoteCommand.java @@ -0,0 +1,55 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to add new note + * + * @author Pontakorn Prasertsuk + */ +public class AddNoteCommand extends Command { + + private String key; + private String note; + + /** + * Constructs a new AddNoteCommand instance. + * + * @param key note name + * @param note note content + */ + public AddNoteCommand(String key, String note) { + this.key = key; + this.note = note; + } + + /** + * Executes the AddNoteCommand + * + * @param taskList not being used + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + storage.saveNote(key, note); + ui.showOutput("Note added."); + + return "Note added."; + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 0000000000..f53805bde6 --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,32 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command input by user + * + * @author Pontakorn Prasertsuk + */ +public abstract class Command { + + /** + * Executes the Command + * + * @param taskList the task list to be mutated + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + public abstract String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException; + + /** + * Returns false as this is not the exit command + * + * @return true if exiting the application after executing this command + */ + public abstract boolean isExit(); +} diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 0000000000..86b122da03 --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,53 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to delete a task to the task list + * + * @author Pontakorn Prasertsuk + */ +public class DeleteCommand extends Command { + + private int index; + + /** + * Constructs a new DeleteCommand instance. + * + * @param index the index of the task to be deleted + */ + public DeleteCommand(int index) { + this.index = index; + } + + /** + * Executes the DeleteCommand + * + * @param taskList the task list to be mutated + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + taskList.delete(index); + ui.showOutput("Task " + (index + 1) + " has been deleted!"); + storage.save(taskList.getTaskList()); + + return "Task " + (index + 1) + " has been deleted!"; + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/DeleteNoteCommand.java b/src/main/java/duke/command/DeleteNoteCommand.java new file mode 100644 index 0000000000..dcd902881d --- /dev/null +++ b/src/main/java/duke/command/DeleteNoteCommand.java @@ -0,0 +1,52 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to delete note file + * + * @author Pontakorn Prasertsuk + */ +public class DeleteNoteCommand extends Command { + + private String key; + + /** + * Constructs a new DeleteNoteCommand instance. + * + * @param key note name + */ + public DeleteNoteCommand(String key) { + this.key = key; + } + + /** + * Executes the DeleteNoteCommand + * + * @param taskList not being used + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + storage.deleteNote(key); + ui.showOutput("Note deleted successfully!"); + + return "Note deleted successfully!"; + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/ExitCommand.java b/src/main/java/duke/command/ExitCommand.java new file mode 100644 index 0000000000..4f604fd964 --- /dev/null +++ b/src/main/java/duke/command/ExitCommand.java @@ -0,0 +1,40 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to exit the application + * + * @author Pontakorn Prasertsuk + */ +public class ExitCommand extends Command { + + /** + * Executes the ExitCommand + * + * @param taskList not being used + * @param ui the user interface to be used + * @param storage not being used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + ui.showOutput("Goodbye!"); + + return "Goodbye!"; + } + + /** + * Returns true as this is the exit command + * + * @return true + */ + @Override + public boolean isExit() { + return true; + } +} diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 0000000000..e77db67e4f --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,59 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to search keyword + * + * @author Pontakorn Prasertsuk + */ +public class FindCommand extends Command { + + private String keyword; + + /** + * Constructs a new FindCommand instance. + * + * @param keyword the keyword to search + */ + public FindCommand(String keyword) { + this.keyword = keyword; + } + + /** + * Executes the FindCommand + * + * @param taskList the task list to be searched + * @param ui the user interface to be used + * @param storage not being used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + TaskList filtered = taskList.filter(keyword); + if (filtered.getTaskList().isEmpty()) { + ui.showOutput("No matching task found!"); + + return "No matching task found!"; + } else { + ui.showOutput("Here are the matching tasks in your list:"); + ui.showOutput(filtered.toString()); + + return "Here are the matching tasks in your list:\n" + filtered.toString(); + } + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 0000000000..6d48952212 --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,40 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to list all tasks in the task list + * + * @author Pontakorn Prasertsuk + */ +public class ListCommand extends Command { + + /** + * Executes the ListCommand + * + * @param taskList the task list to be shown + * @param ui the user interface to be used + * @param storage not being used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + ui.showOutput(taskList.toString()); + + return taskList.toString(); + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/LoadNoteCommand.java b/src/main/java/duke/command/LoadNoteCommand.java new file mode 100644 index 0000000000..7e1a94e705 --- /dev/null +++ b/src/main/java/duke/command/LoadNoteCommand.java @@ -0,0 +1,52 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to load note from the file + * + * @author Pontakorn Prasertsuk + */ +public class LoadNoteCommand extends Command { + + private String key; + + /** + * Constructs a new LoadNoteCommand instance. + * + * @param key note name + */ + public LoadNoteCommand(String key) { + this.key = key; + } + + /** + * Executes the AddNoteCommand + * + * @param taskList not being used + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + ui.showOutput("Here is your note:"); + ui.showOutput(storage.loadNote(key)); + + return "Here is your note:\n" + storage.loadNote(key); + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java new file mode 100644 index 0000000000..0acb73ef63 --- /dev/null +++ b/src/main/java/duke/command/MarkCommand.java @@ -0,0 +1,53 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to mark a task as done + * + * @author Pontakorn Prasertsuk + */ +public class MarkCommand extends Command { + + private int index; + + /** + * Constructs a new MarkCommand instance + * + * @param index the index of the task to be marked + */ + public MarkCommand(int index) { + this.index = index; + } + + /** + * Executes the MarkCommand + * + * @param taskList the task list to be mutated + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + taskList.mark(index, true); + ui.showOutput("Task " + (index + 1) + " is marked as done!"); + storage.save(taskList.getTaskList()); + + return "Task " + (index + 1) + " is marked as done!"; + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/ReminderCommand.java b/src/main/java/duke/command/ReminderCommand.java new file mode 100644 index 0000000000..e0e7d47d4b --- /dev/null +++ b/src/main/java/duke/command/ReminderCommand.java @@ -0,0 +1,41 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to list all unfinished deadline tasks sorted by date + * + * @author Pontakorn Prasertsuk + */ +public class ReminderCommand extends Command { + + /** + * Executes the ReminderCommand + * + * @param taskList the task list to be used for the reminder + * @param ui the user interface to be used + * @param storage not being used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + ui.showOutput("Here are your upcoming deadlines:"); + ui.showOutput(taskList.getDeadlineList().toString()); + + return "Here are your upcoming deadlines:\n" + taskList.getDeadlineList().toString(); + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/SortCommand.java b/src/main/java/duke/command/SortCommand.java new file mode 100644 index 0000000000..e2b103b7c1 --- /dev/null +++ b/src/main/java/duke/command/SortCommand.java @@ -0,0 +1,41 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to sort all tasks by status then by title + * + * @author Pontakorn Prasertsuk + */ +public class SortCommand extends Command { + + /** + * Executes the ReminderCommand + * + * @param taskList the task list to be sorted + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + ui.showOutput(taskList.sort().toString()); + storage.save(taskList.getTaskList()); + + return taskList.sort().toString(); + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java new file mode 100644 index 0000000000..31065a21ba --- /dev/null +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -0,0 +1,53 @@ +package duke.command; + +import duke.common.DukeException; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +/** + * Command to mark a task as not done + * + * @author Pontakorn Prasertsuk + */ +public class UnmarkCommand extends Command { + + private int index; + + /** + * Constructs a new UnmarkCommand instance + * + * @param index the index of the task to be unmarked + */ + public UnmarkCommand(int index) { + this.index = index; + } + + /** + * Executes the UnmarkCommand + * + * @param taskList the task list to be mutated + * @param ui the user interface to be used + * @param storage the storage to be used + * @return output to be shown + * @throws DukeException if an error occurs + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + taskList.mark(index, false); + ui.showOutput("Task " + (index + 1) + " is marked as not done!"); + storage.save(taskList.getTaskList()); + + return "Task " + (index + 1) + " is marked as not done!"; + } + + /** + * Returns false as this is not the exit command + * + * @return false + */ + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/common/DukeException.java b/src/main/java/duke/common/DukeException.java new file mode 100644 index 0000000000..63e7df9d54 --- /dev/null +++ b/src/main/java/duke/common/DukeException.java @@ -0,0 +1,25 @@ +package duke.common; + +/** + * Exception thrown by the application + * + * @author Pontakorn Prasertsuk + */ +public class DukeException extends Exception { + + /** + * Constructs a new default DukeException instance + */ + public DukeException() { + super("Sorry, something went wrong!"); + } + + /** + * Constructs a new DukeException instance + * + * @param message the message to be shown + */ + public DukeException(String message) { + super(message); + } +} diff --git a/src/main/java/duke/controller/DialogBox.java b/src/main/java/duke/controller/DialogBox.java new file mode 100644 index 0000000000..c88fb21247 --- /dev/null +++ b/src/main/java/duke/controller/DialogBox.java @@ -0,0 +1,83 @@ +package duke.controller; + +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.*; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +import java.io.IOException; +import java.util.Collections; + +/** + * Controller for the dialog box + * + * @author Pontakorn Prasertsuk + */ +public class DialogBox extends HBox { + @FXML + private Label dialog; + @FXML + private ImageView displayPicture; + + /** + * Constructs a new DialogBox instance + * + * @param text the text to be displayed + * @param img the image of the dialog's owner + */ + 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); + } + + /** + * Creates a new DialogBox instance for user + * + * @param text the text to be displayed + * @param img the image of the user + * @return the new DialogBox instance + */ + public static DialogBox getUserDialog(String text, Image img) { + return new DialogBox(text, img); + } + + /** + * Creates a new DialogBox instance for bot + * + * @param text the text to be displayed + * @param img the image of the bot + * @return the new DialogBox instance + */ + public static DialogBox getDukeDialog(String text, Image img) { + var db = new DialogBox(text, img); + db.flip(); + + return db; + } +} diff --git a/src/main/java/duke/controller/MainWindow.java b/src/main/java/duke/controller/MainWindow.java new file mode 100644 index 0000000000..b2b0c741b1 --- /dev/null +++ b/src/main/java/duke/controller/MainWindow.java @@ -0,0 +1,64 @@ +package duke.controller; + +import duke.Duke; +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.scene.image.Image; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; + +/** + * Controller for the main window + * + * @author Pontakorn Prasertsuk + */ +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/user.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/robot.png")); + + /** + * Initializes the main window on rendered by the JavaFX application + */ + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + dialogContainer.getChildren() + .add(DialogBox.getDukeDialog("Hello from\n" + "______ _ \n" + + "| ___ \\ | | \n" + "| |_/ / ___ | |__ \n" + + "| ___ \\/ _ \\| '_ \\ \n" + "| |_/ / (_) | |_) |\n" + + "\\____/ \\___/|_.__/ \n" + "What can I do for you?", dukeImage)); + } + + /** + * Sets the Duke instance for the application + * + * @param duke the Duke instance + */ + public void setDuke(Duke duke) { + this.duke = duke; + } + + /** + * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and + * then appends them to the dialog container. Clears the user input after processing. + */ + @FXML + private void handleUserInput() { + String input = userInput.getText(); + String response = duke.getResponse(input); + + dialogContainer.getChildren().addAll(DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage)); + userInput.clear(); + } +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 0000000000..97a006e82a --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,82 @@ +package duke.parser; + +import duke.command.*; +import duke.common.DukeException; +import duke.task.*; + +import java.time.format.DateTimeParseException; +import java.util.Scanner; + +/** + * Read user inputs & parse into commands + * + * @author Pontakorn Prasertsuk + */ +public class Parser { + + private static final Scanner scanner = new Scanner(System.in); + + /** + * Reads user input + * + * @return the user input + */ + public static String readCommand() { + return scanner.nextLine(); + } + + /** + * Parses the user input into a command + * + * @param fullCommand the user input + * @return the parsed command + * @throws DukeException if an error occurs + */ + public static Command parse(String fullCommand) throws DukeException { + String[] inputs = fullCommand.trim().split("\\s+", 2); + + try { + switch (inputs[0]) { + case "bye": + return new ExitCommand(); + case "list": + return new ListCommand(); + case "reminder": + return new ReminderCommand(); + case "sort": + return new SortCommand(); + case "find": + return new FindCommand(inputs[1]); + case "mark": + return (new MarkCommand(Integer.parseInt(inputs[1]) - 1)); + case "unmark": + return (new UnmarkCommand(Integer.parseInt(inputs[1]) - 1)); + case "delete": + return (new DeleteCommand(Integer.parseInt(inputs[1]) - 1)); + case "todo": + return new AddCommand(new Todo(inputs[1], false)); + case "deadline": + String[] deadlineArgs = inputs[1].split("\\s+/by\\s+", 2); + return new AddCommand(new Deadline(deadlineArgs[0], false, deadlineArgs[1])); + case "event": + String[] eventArgs = inputs[1].split("\\s+/at\\s+", 2); + return new AddCommand(new Event(eventArgs[0], false, eventArgs[1])); + case "add_note": + String[] addNoteArgs = inputs[1].split("\\s+/note\\s+", 2); + return new AddNoteCommand(addNoteArgs[0], addNoteArgs[1]); + case "load_note": + return new LoadNoteCommand(inputs[1]); + case "delete_note": + return new DeleteNoteCommand(inputs[1]); + default: + throw new DukeException("Sorry, I don't know what do you mean by that."); + } + } catch (IndexOutOfBoundsException e) { + throw new DukeException("Invalid input format!"); + } catch (NumberFormatException e) { + throw new DukeException("Invalid input format!"); + } catch (DateTimeParseException e) { + throw new DukeException("Invalid date format!"); + } + } +} diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 0000000000..64988c7d0e --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,165 @@ +package duke.storage; + +import duke.common.DukeException; +import duke.task.Task; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Handle load & save user's task list + * + * @author Pontakorn Prasertsuk + */ +public class Storage { + private final String filePath; + private final String notePath; + + /** + * Constructs a new Storage instance + * + * @param filePath the file path to be used + */ + public Storage(String filePath, String notePath) { + this.filePath = filePath; + this.notePath = notePath; + } + + /** + * Creates a new file if it doesn't exist + * + * @throws DukeException if error occurs + */ + private void createFileIfNotExist(String path) throws DukeException { + try { + File file = new File(path); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + if (!file.exists()) { + file.createNewFile(); + } + + assert file.exists(); + } catch (IOException e) { + throw new DukeException("Unable to create save file!"); + } + } + + /** + * Loads the task list from the file + * + * @return the task list + * @throws DukeException if error occurs + */ + public List load() throws DukeException { + createFileIfNotExist(filePath); + + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + List list = new ArrayList(); + + String line = br.readLine(); + while (line != null) { + list.add(Task.decode(line)); + line = br.readLine(); + } + br.close(); + + return list; + } catch (FileNotFoundException e) { + throw new DukeException("Save file not found!"); + } catch (IOException e) { + throw new DukeException("Unable to read save file!"); + } + } + + /** + * Saves the task list to the file + * + * @param list the task list to be saved + * @throws DukeException if error occurs + */ + public void save(List list) throws DukeException { + createFileIfNotExist(filePath); + + try (PrintWriter pw = new PrintWriter(filePath, "UTF-8")) { + list.forEach(task -> pw.println(task.encode())); + pw.close(); + } catch (FileNotFoundException e) { + throw new DukeException("Save file not found!"); + } catch (UnsupportedEncodingException e) { + throw new DukeException("Unable to write save file!"); + } + } + + /** + * Loads note from the file + * + * @param key note name + * @return note content + * @throws DukeException if error occurs + */ + public String loadNote(String key) throws DukeException { + String currentNotePath = notePath + "/" + key + ".txt"; + + try (BufferedReader br = new BufferedReader(new FileReader(currentNotePath))) { + String note = ""; + + String line = br.readLine(); + while (line != null) { + note += line + "\n"; + line = br.readLine(); + } + br.close(); + + return note; + } catch (FileNotFoundException e) { + throw new DukeException("Note file not found!"); + } catch (IOException e) { + throw new DukeException("Unable to read note file!"); + } + } + + /** + * Saves note to the file + * + * @param key note name + * @param note note content + * @throws DukeException if error occurs + */ + public void saveNote(String key, String note) throws DukeException { + String currentNotePath = notePath + "/" + key + ".txt"; + createFileIfNotExist(currentNotePath); + + try (PrintWriter pw = new PrintWriter(currentNotePath, "UTF-8")) { + pw.println(note); + pw.close(); + } catch (FileNotFoundException e) { + throw new DukeException("Note file not found!"); + } catch (UnsupportedEncodingException e) { + throw new DukeException("Unable to write note file!"); + } + } + + /** + * Deletes note from the file + * + * @param key note name + * @throws DukeException if error occurs + */ + public void deleteNote(String key) throws DukeException { + String currentNotePath = notePath + "/" + key + ".txt"; + + try { + File file = new File(currentNotePath); + if (file.exists()) { + file.delete(); + } else { + throw new DukeException("Note file not found!"); + } + } catch (Exception e) { + throw new DukeException("Unable to delete note 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..35e7931202 --- /dev/null +++ b/src/main/java/duke/task/Deadline.java @@ -0,0 +1,44 @@ +package duke.task; + +import java.time.LocalDate; +import java.time.format.DateTimeParseException; + +/** + * A deadline task + * + * @author Pontakorn Prasertsuk + */ +public class Deadline extends Task { + + public static final String SYMBOL = "D"; + + protected final LocalDate date; + + /** + * Constructs a new Deadline instance + * + * @param title the name of the task + * @param isDone whether the task is completed or not + * @param date the deadline of the task + */ + public Deadline(String title, boolean isDone, String date) throws DateTimeParseException { + super(title, isDone); + this.date = LocalDate.parse(date); + } + + /** + * Encodes the task for saving into the file + * + * @return the string to be saved into the file + */ + @Override + public String encode() { + return (SYMBOL + " | " + (this.isDone ? "1" : "0") + " | " + this.title + " | " + + this.date); + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + this.date + ")"; + } +} diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java new file mode 100644 index 0000000000..c2129bca6d --- /dev/null +++ b/src/main/java/duke/task/Event.java @@ -0,0 +1,44 @@ +package duke.task; + +import java.time.LocalDate; +import java.time.format.DateTimeParseException; + +/** + * An event task + * + * @author Pontakorn Prasertsuk + */ +public class Event extends Task { + + public static final String SYMBOL = "E"; + + protected final LocalDate date; + + /** + * Constructs a new Event instance + * + * @param title the name of the task + * @param isDone whether the task is completed or not + * @param date the date where the event occurs + */ + public Event(String title, boolean isDone, String date) throws DateTimeParseException { + super(title, isDone); + this.date = LocalDate.parse(date); + } + + /** + * Encodes the task for saving into the file + * + * @return the string to be saved into the file + */ + @Override + public String encode() { + return (SYMBOL + " | " + (this.isDone ? "1" : "0") + " | " + this.title + " | " + + this.date); + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + this.date + ")"; + } +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java new file mode 100644 index 0000000000..496bee7167 --- /dev/null +++ b/src/main/java/duke/task/Task.java @@ -0,0 +1,86 @@ +package duke.task; + +import duke.common.DukeException; + +import java.time.format.DateTimeParseException; + +/** + * A task for the application + * + * @author Pontakorn Prasertsuk + */ +public abstract class Task implements Comparable { + protected final String title; + protected boolean isDone; + + /** + * Constructs a new Task instance + * + * @param title the name of the task + * @param isDone whether the task is completed or not + */ + public Task(String title, boolean isDone) { + this.title = title; + this.isDone = isDone; + } + + /** + * Decodes the string from the file + * + * @param line the string to be decoded + * @return the decoded task + */ + public static Task decode(String input) throws DukeException { + String[] inputs = input.trim().split("\\s+\\|\\s+"); + + try { + switch (inputs[0]) { + case Todo.SYMBOL: + return new Todo(inputs[2], inputs[1].equals("1")); + case Deadline.SYMBOL: + return new Deadline(inputs[2], inputs[1].equals("1"), inputs[3]); + case Event.SYMBOL: + return new Event(inputs[2], inputs[1].equals("1"), inputs[3]); + default: + throw new DukeException("Invalid task format!"); + } + } catch (IndexOutOfBoundsException e) { + throw new DukeException("Invalid task format!"); + } catch (DateTimeParseException e) { + throw new DukeException("Invalid date format!"); + } + } + + /** + * Sets task status + * + * @param isDone the status to be set + * @return the task after changing status + */ + public Task setStatus(boolean isDone) { + this.isDone = isDone; + + return this; + } + + /** + * Encodes the task for saving into the file + * + * @return the string to be saved into the file + */ + public abstract String encode(); + + @Override + public int compareTo(Task rhs) { + if (this.isDone == rhs.isDone) { + return this.title.compareTo(rhs.title); + } + + return this.isDone ? 1 : -1; + } + + @Override + public String toString() { + return "[" + (this.isDone ? "X" : " ") + "] " + this.title; + } +} diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java new file mode 100644 index 0000000000..6b79b571a6 --- /dev/null +++ b/src/main/java/duke/task/TaskList.java @@ -0,0 +1,135 @@ +package duke.task; + +import duke.common.DukeException; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Task list + * + * @author Pontakorn Prasertsuk + */ +public class TaskList { + + private List list; + + /** + * Constructs a new TaskList instance from the list of tasks + * + * @param list the list of tasks + */ + public TaskList(List list) { + this.list = list; + } + + /** + * Constructs a new empty TaskList instance + */ + public TaskList() { + this.list = new ArrayList(); + } + + /** + * Returns the list of tasks + * + * @return the list of tasks + */ + public List getTaskList() { + return list; + } + + /** + * Adds a task to the list of tasks + * + * @param task the task to be added + * @return task list after adding the task + */ + public TaskList add(Task task) { + list.add(task); + + return this; + } + + /** + * Deletes a task from the list of tasks + * + * @param index the index of the task to be deleted + * @return task list after deleting the task + * @throws DukeException if an error occurs + */ + public TaskList delete(int index) throws DukeException { + if (index < 0 || index >= list.size()) { + throw new DukeException("Task " + (index + 1) + " does not exist!"); + } + list.remove(index); + + return this; + } + + /** + * Marks a task with status + * + * @param index the index of the task to be marked + * @param status the status of the task to be marked + * @return the task list after marking the task + * @throws DukeException if an error occurs + */ + public TaskList mark(int index, boolean status) throws DukeException { + if (index < 0 || index >= list.size()) { + throw new DukeException("Task " + (index + 1) + " does not exist!"); + } + list.get(index).setStatus(status); + + return this; + } + + /** + * Returns the list of tasks filtered by keyword + * + * @param keyword the keyword to be searched + * @return the list of tasks filtered by keyword + */ + public TaskList filter(String keyword) { + List filtered = list.stream().filter(task -> task.title.contains(keyword)) + .collect(Collectors.toList()); + + return new TaskList(filtered); + } + + /** + * Returns the list of unfinished deadline tasks sorted by date + * + * @return the list of unfinished deadline tasks sorted by date + */ + public TaskList getDeadlineList() { + List filtered = list.stream().filter(task -> task instanceof Deadline && !task.isDone) + .collect(Collectors.toList()); + filtered.sort((x, y) -> ((Deadline) x).date.compareTo(((Deadline) y).date)); + + return new TaskList(filtered); + } + + /** + * Sorts the list of tasks by status then by title + * + * @return Sorted task list + */ + public TaskList sort() { + Collections.sort(this.list); + + return this; + } + + @Override + public String toString() { + String ret = ""; + for (int i = 0; i < list.size(); i++) { + ret += (i + 1) + ") " + list.get(i).toString() + "\n"; + } + + return ret; + } +} diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java new file mode 100644 index 0000000000..0b2059b45b --- /dev/null +++ b/src/main/java/duke/task/Todo.java @@ -0,0 +1,36 @@ +package duke.task; + +/** + * A todo task + * + * @author Pontakorn Prasertsuk + */ +public class Todo extends Task { + + public static final String SYMBOL = "T"; + + /** + * Constructs a new Todo instance + * + * @param title the name of the task + * @param isDone whether the task is completed or not + */ + public Todo(String title, boolean isDone) { + super(title, isDone); + } + + /** + * Encodes the task for saving into the file + * + * @return the string to be saved into the file + */ + @Override + public String encode() { + return SYMBOL + " | " + (this.isDone ? "1" : "0") + " | " + this.title; + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java new file mode 100644 index 0000000000..61559141f1 --- /dev/null +++ b/src/main/java/duke/ui/Ui.java @@ -0,0 +1,61 @@ +package duke.ui; + +/** + * User interface for the application + * + * @author Pontakorn Prasertsuk + */ +public class Ui { + + private static final String LOGO = "______ _ \n" + "| ___ \\ | | \n" + + "| |_/ / ___ | |__ \n" + "| ___ \\/ _ \\| '_ \\ \n" + "| |_/ / (_) | |_) |\n" + + "\\____/ \\___/|_.__/ \n"; + + /** + * Shows the logo of the application + */ + public void showGreeting() { + System.out.println("Hello from"); + System.out.println(LOGO); + System.out.println("What can I do for you?"); + } + + /** + * Shows goodbye message + */ + public void showGoodbye() { + System.out.println("Goodbye!"); + } + + /** + * Shows the divider + */ + public void showLine() { + System.out.println("----------------------------------------------------"); + } + + /** + * Shows the input cursor + */ + public void showInput() { + System.out.print("> "); + } + + /** + * Shows the output message + * + * @param output the output to be shown + */ + public void showOutput(String message) { + System.out.println(message); + } + + /** + * Shows the error message + * + * @param error the error to be shown + */ + public void showError(String error) { + System.out.println("Error: " + error); + } +} diff --git a/src/main/resources/images/robot.png b/src/main/resources/images/robot.png new file mode 100644 index 0000000000..113d270398 Binary files /dev/null and b/src/main/resources/images/robot.png differ diff --git a/src/main/resources/images/user.png b/src/main/resources/images/user.png new file mode 100644 index 0000000000..cd901a5630 Binary files /dev/null and b/src/main/resources/images/user.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..eb098fbb89 --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..0777c1671d --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + +