diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..46b9671e45 --- /dev/null +++ b/build.gradle @@ -0,0 +1,60 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + id 'com.github.johnrengelman.shadow' version '5.1.0' +} + +repositories { + mavenCentral() +} + +dependencies { + String javaFxVersion = '11' + + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0' + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "duke.Launcher" +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +checkstyle { + toolVersion = '8.29' +} + +run{ + standardInput = System.in +} diff --git a/docs/README.md b/docs/README.md index fd44069597..3563c55d71 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,17 +1,80 @@ # User Guide -## Features +This is a User Guide for the chatbox 'Duke!'. -### Feature 1 -Description of feature. +## Features + +### Load task list from the hard drive. + +###### Task list is initialised from loading the data from the hard drive (if available) upon starting the programme, using pre-set file path. + +### Add a task given by the user to a list and display upon requested. + +###### Task type can be todos with no date/time associated, event within a specific timing and tasks with a deadline + +### Mark a task as done + +###### Task can be marked as done using the position of the task in the list displayed. Task is displayed after marked done for user reference + +### Delete a task from the list + +###### Task can be deleted using the position of the task in the list displayed. The deleted task is displayed after deletion for user reference + +### Find tasks which match the keyword given + +###### Given keyword from user, return all tasks with matching task description + +### Store list on hard disk + +###### Upon user input "bye", the updated list is stored on the hard disk using the pre-set file path. ## Usage -### `Keyword` - Describe action +### todo + +Example: +`todo return book` +Expected outcome: +A todo task with the description of 'return book' is added to the task list + +### deadline + +Example: +`deadline return book /by 2020-02-02` +Expected outcome: +A deadline task with the description of 'return book' and deadline of 2020-02-02 is added to the task list + +### event + +Example: +`event birthday party /at Sunday 4pm to 6pm` +Expected outcome: +An event task with the description of 'birthday party' and time of Sunday 4pm to 6pm is added to the task list + +### delete + +Example: +`delete 2` +Expected outcome: +The second task in the task list is deleted and the information of the task is displayed afterwards + +### done + +Example: +`done 2` +Expected outcome: +The second task in the task list is marked as done and the updated information of the task is displayed afterwards + +### find + +Example: +`find book` +Expected outcome: +All tasks containing the word book in their description are displayed on the screen Describe action and its outcome. -Example of usage: +Example of usage: `keyword (optional arguments)` diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..efea918fc3 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/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..6e864153e8 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Deadlines.java b/src/main/java/duke/Deadlines.java new file mode 100644 index 0000000000..9b5c845fb5 --- /dev/null +++ b/src/main/java/duke/Deadlines.java @@ -0,0 +1,27 @@ +package duke; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Deadlines extends Task { + protected LocalDate deadLine; + + /** + * This constructor create a Task which is of type Deadlines + * @param isDone whether the task is already completed + * @param eventName description of the event + * @param deadLine the deadline for the task + * @return return the task object + */ + public Deadlines(boolean isDone, String eventName, String deadLine) { + super(isDone, eventName, "D"); + this.deadLine = LocalDate.parse(deadLine); + } + + @Override + public String toString() { + return "[D] " + super.toString() + "(by: " + + deadLine.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + + ")\n"; + } +} diff --git a/src/main/java/duke/DialogBox.java b/src/main/java/duke/DialogBox.java new file mode 100644 index 0000000000..20d74ec38a --- /dev/null +++ b/src/main/java/duke/DialogBox.java @@ -0,0 +1,61 @@ +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..13fbf6a561 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,75 @@ +package duke; + +public class Duke { + private Storage storage; + private TaskList taskList; + + private enum Commands { + LIST, + TODO, + DEADLINE, + EVENT, + BYE, + FIND, + DONE, + DELETE + } + + + /** + * This constructor create a duke class after loading the tasks from a given file path + * @param filePath path of the file which stores the list of tasks from last time + * @return a Duke object with all of its variables initialised + */ + public Duke(String filePath) { + storage = new Storage(filePath); + try { + taskList = new TaskList(storage.load()); + } catch (DukeException e) { + this.showError(e); + taskList = new TaskList(); + } + } + public static String getHelp() { + return "You have entered invalid commands! " + + "Type 'help' if you are unsure of which commands I can understand:>"; + } + + + + public String getResponse(String string) { + switch (string.toLowerCase()) { + case "help": + String commandsList = "The commands that I can understand are: \n"; + for (Commands command : Commands.values()) { + commandsList += command.toString() + "\n"; + } + return commandsList; + case "bye": + try { + storage.writeToFile(taskList, "./data/duke.txt"); + } catch (DukeException e) { + showError(e); + } + return "Bye. See you again!"; + case "list": + String output = ""; + if (taskList.getSize() == 0) { + return "You have no tasks in the list!"; + } + for (int i = 0; i < taskList.getSize(); i++) { + output += i+1 + ". " + taskList.getTask(i).toString() + "\n"; + } + assert output.length() != 0 : "No tasks in the list"; + return output; + default: + Pair result = Parser.parseInput(string, taskList); + taskList = result.getTaskList(); + return result.getMessage(); + } + } + + public String showError(Exception e) { + return e.getMessage(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..620605f96b --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,17 @@ +package duke; + +public class DukeException extends Exception { + /** + * This constructor returns a Duke Exception + * @param message Error Message + * @return a Duke Exception + */ + public DukeException(String message) { + + super(message); + } + public DukeException() { + + super(); + } +} diff --git a/src/main/java/duke/Events.java b/src/main/java/duke/Events.java new file mode 100644 index 0000000000..afd1270fd1 --- /dev/null +++ b/src/main/java/duke/Events.java @@ -0,0 +1,24 @@ +package duke; + +public class Events extends Task { + String time; + + /** + * This constructor create a Task which is of type Events + * @param isDone whether the task is already completed + * @param eventName description of the event + * @param time the time of which the Event will take place + * @return return the task object + */ + public Events(boolean isDone, String eventName, String time) { + + super(isDone, eventName, "E"); + this.time = time; + } + + @Override + public String toString() { + + return "[E] " + super.toString() + " (at: " + time + ")" + "\n"; + } +} diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..756e70af9c --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,8 @@ +package duke; +import javafx.application.Application; + +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..0992388f5c --- /dev/null +++ b/src/main/java/duke/Main.java @@ -0,0 +1,34 @@ +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("data/duke.txt"); + + @Override + public void start(Stage stage) { + try { + FXMLLoader fxmlLoader = new FXMLLoader( + Main.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = fxmlLoader.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + stage.setTitle("Duke!"); + fxmlLoader.getController().setDuke(duke); + fxmlLoader.getController().greet(); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java new file mode 100644 index 0000000000..51c92e5162 --- /dev/null +++ b/src/main/java/duke/MainWindow.java @@ -0,0 +1,62 @@ +package duke; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; + +/** + * Controller for MainWindow. Provides the layout for the other controls. + */ +public class MainWindow extends AnchorPane { + private static String USER_IMAGE_PATH = "/images/User.png"; + private static String DUKE_IMAGE_PATH = "/images/Duke.png"; + @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(USER_IMAGE_PATH)); + private Image dukeImage = new Image( + this.getClass().getResourceAsStream(DUKE_IMAGE_PATH)); + + @FXML + public void initialize() { + + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void setDuke(Duke d) { + duke = d; + } + + public void greet() { + dialogContainer.getChildren().add( + DialogBox.getDukeDialog("Hello! What can I do for you:>", dukeImage)); + } + + /** + * 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/Pair.java b/src/main/java/duke/Pair.java new file mode 100644 index 0000000000..1740a030c9 --- /dev/null +++ b/src/main/java/duke/Pair.java @@ -0,0 +1,19 @@ +package duke; + +public class Pair { + private final TaskList taskList; + private final String message; + + public Pair(TaskList taskList, String message) { + this.taskList = taskList; + this.message = message; + } + + public TaskList getTaskList() { + return this.taskList; + } + + public String getMessage() { + return this.message; + } +} diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 0000000000..2e91886e14 --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,84 @@ +package duke; + +import java.util.Arrays; + +public class Parser { + + /** + * This function returns a Task object after parsing the data from the file + * @param data line data from the file + * @return Task object of various types, e.g. Deadlines, ToDos, Events + * @throws DukeException + */ + public static Task parseFile(String data) throws DukeException { + String[] dataSplit = data.split(" \\| "); + switch (dataSplit[0]) { + case "T": + return new ToDos(Boolean.parseBoolean(dataSplit[1]), dataSplit[2]); + case "D": + return new Deadlines(Boolean.parseBoolean(dataSplit[1]), dataSplit[2], dataSplit[3]); + case "E": + return new Events(Boolean.parseBoolean(dataSplit[1]), dataSplit[2], dataSplit[3]); + default: + throw new DukeException("Error while reading file"); + } + } + + /** + * This function returns the TaskList object after passing the commands from user + * @param input User input + * @param tasks TaskList object + * @return TaskList after adding the new task from the user input + */ + public static Pair parseInput(String input, TaskList tasks) { + String[] inputSplit = input.split(" "); + if (inputSplit.length < 2) { + return new Pair(tasks, Duke.getHelp()); + } + Task task; + String message; + + switch (inputSplit[0]) { + case "find": + String keyword = inputSplit[1]; + TaskList result = tasks.findTask(keyword); + message = "Here are the matching tasks in your list: \n"; + for (int i = 0; i < result.getSize(); i++) { + message += i+1 + ". " + result.getTask(i).toString(); + } + break; + case "done": + message = "Nice, I have set this task as done!" + + tasks.setDone(Integer.valueOf(inputSplit[1]) - 1).toString(); + break; + case "delete": + task = tasks.deleteTask(Integer.valueOf(inputSplit[1]) - 1); + message = "Noted. I have deleted this task: \n" + task.toString(); + break; + case "todo": + String name = String.join(" ", Arrays.copyOfRange(inputSplit, 1, inputSplit.length)); + task = new ToDos(false, name); + tasks.addTask(task); + message = "Got it. I have added this task: \n" + task.toString(); + break; + case "deadline": + String deadlineName = String.join(" ", Arrays.copyOfRange(inputSplit, 1, inputSplit.length)); + String[] deadlineSplit = deadlineName.split(" /by "); + task = new Deadlines(false, deadlineSplit[0], deadlineSplit[1]); + tasks.addTask(task); + message = "Got it. I have added this task: \n" + task.toString(); + break; + case "event": + String event = String.join(" ", Arrays.copyOfRange(inputSplit, 1, inputSplit.length)); + String[] eventSplit = event.split(" /at "); + task = new Events(false, eventSplit[0], eventSplit[1]); + tasks.addTask(task); + message = "Got it. I have added this task: \n" + task.toString(); + break; + default: + message = Duke.getHelp(); + } + + return new Pair(tasks, message); + } +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 0000000000..44576ca5b8 --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,84 @@ +package duke; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private String filePath; + + public Storage(String filePath) { + + this.filePath = filePath; + } + + /** + * This function loads the tasks from the file which is specified by the file path + * @return an arraylist of Tasks + * @throws DukeException + */ + public ArrayList load() throws DukeException { + ArrayList list = new ArrayList<>(); + File tasks = new File(filePath); + + try { + if (tasks.exists()) { + Scanner sc1 = new Scanner(tasks); + String data; + while (sc1.hasNextLine()) { + data = sc1.nextLine(); + list.add(Parser.parseFile(data)); + } + sc1.close(); + } else { + File directory = new File("./data"); + if (!directory.exists()) { + directory.mkdir(); + } + tasks.createNewFile(); + } + + assert tasks.exists() : "File creation error"; + } catch (FileNotFoundException e){ + throw new DukeException("File not found."); + } catch (IOException e) { + throw new DukeException("Error occurred when creating file."); + } catch (DukeException e) { + throw e; + } + + return list; + } + + /** + * This function writes and save the taskList to a file + * @param tasks TaskList object containing a list of tasks + * @param filePath The path to save the file + * @throws DukeException + */ + public void writeToFile(TaskList tasks, String filePath) throws DukeException { + try { + FileWriter writer = new FileWriter(filePath, false); + int size = tasks.getSize(); + Task task; + + for (int i = 0; i < size; i++) { + task = tasks.getTask(i); + writer.write(task.eventType + " | " + task.isDone + " | " + task.eventName); + if (task instanceof Deadlines) { + writer.write(" | " + ((Deadlines) task).deadLine); + } else if (task instanceof Events) { + writer.write(" | " + ((Events) task).time); + } + writer.write(System.lineSeparator()); + } + + writer.close(); + } catch (IOException e) { + throw new DukeException(); + } + } +} diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java new file mode 100644 index 0000000000..b8276c4a61 --- /dev/null +++ b/src/main/java/duke/Task.java @@ -0,0 +1,30 @@ +package duke; + +public class Task { + public boolean isDone; + public String eventName; + public String eventType; + + public Task(boolean done, String eventName, String eventType) { + this.isDone = done; + this.eventName = eventName; + this.eventType = eventType; + } + + /** + * This function sets a task to be done + */ + public void setDone() { + + this.isDone = true; + } + + @Override + public String toString() { + if (this.isDone == true) { + return "[X] " + eventName; + } else { + return "[ ] " + eventName; + } + } +} diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java new file mode 100644 index 0000000000..8eb545d4a5 --- /dev/null +++ b/src/main/java/duke/TaskList.java @@ -0,0 +1,75 @@ +package duke; + +import java.util.ArrayList; +public class TaskList { + private ArrayList tasks; + + public TaskList(ArrayList tasks) { + + this.tasks = tasks; + } + public TaskList() { + + this.tasks = new ArrayList<>(); + } + + /** + * This function adds a task in the task list + * @param task task to be added + */ + public void addTask(Task task) { + + tasks.add(task); + } + + /** + * This function returns the task at a specified index + * @param index the index of the task to be returned + * @return Task object in the given position + */ + public Task getTask(int index) { + + return tasks.get(index); + } + + /** + * This function deletes the task at a specified index + * @param index the index of the task to be deleted + * @return return the deleted task + */ + public Task deleteTask(int index) { + Task task = tasks.get(index); + tasks.remove(index); + return task; + } + + /** + * This function sets a task in the TaskList to be done + * @param index index of the task to be set done + * @return return the task after setting it to be done + */ + public Task setDone(int index) { + tasks.get(index).setDone(); + return tasks.get(index); + } + + public TaskList findTask(String keyword) { + ArrayList result = new ArrayList<>(); + for (Task task : tasks) { + if (task.eventName.contains(keyword)) { + result.add(task); + } + } + + return new TaskList(result); + } + + /** + * This function returns the number of tasks in the list + * @return the size of the task list + */ + public int getSize() { + + return tasks.size(); + } +} diff --git a/src/main/java/duke/ToDos.java b/src/main/java/duke/ToDos.java new file mode 100644 index 0000000000..6df4656a01 --- /dev/null +++ b/src/main/java/duke/ToDos.java @@ -0,0 +1,14 @@ +package duke; + +public class ToDos extends Task { + + public ToDos(boolean isDone, String eventName) { + + super(isDone, eventName, "T"); + } + + @Override + public String toString() { + return "[T] " + super.toString(); + } +} diff --git a/src/main/resources/images/Duke.png b/src/main/resources/images/Duke.png new file mode 100644 index 0000000000..9af26bae00 Binary files /dev/null and b/src/main/resources/images/Duke.png differ diff --git a/src/main/resources/images/User.png b/src/main/resources/images/User.png new file mode 100644 index 0000000000..7d1c7fc8f1 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..2782a2943b --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ 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..2357f74eb2 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,21 @@ + + + + + + + + + + + +