diff --git a/.gitignore b/.gitignore index f69985ef1f..c254956646 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,8 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT + +gradle/ +gradlew* + +tasks.ser diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..c947f6d63a --- /dev/null +++ b/build.gradle @@ -0,0 +1,63 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + 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 = "Launcher" +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +checkstyle { + toolVersion = '8.29' +} + +run{ + standardInput = System.in +} + +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index fd44069597..9371ebeb8f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,19 +2,138 @@ ## Features -### Feature 1 -Description of feature. +### Feature 1: View tasks +Shows all tasks with status and date. + +### Feature 2: Add a task +Tasks can be in the form of a todo [T], deadline [D], or event [E]. + +### Feature 3: Mark as done +Marks specified task as done. + +### Feature 4: Delete a task +Deletes a specific task. + +### Feature 5: Sort tasks +Sorts tasks by dates. + +### Feature 6: Exit the program +Saves all data and closes the window for you. ## Usage -### `Keyword` - Describe action +### `todo` - Adds a todo item to the list + +Format: `todo DESCRIPTION` + +Example of usage: + +`todo fail CS2103T` + +Expected outcome: + + Got it. I've added this task: + [T][✘] fail CS2103T + You now have ... tasks in the list. + +### `deadline` - Adds a deadline item to the list, with a specified date + +Format: `deadline DESCRIPTION /by YYYY-MM-DD` + +Example of usage: + +`deadline fail CS2103T /by 2020-12-01` + +Expected outcome: + + Got it. I've added this task: + [D][✘] fail CS2103T (by: 2020-12-01) + You now have ... tasks in the list. + +### `event` - Adds an event item to the list, with a specified date + +Format: `event DESCRIPTION /at YYYY-MM-DD` + +Example of usage: + +`event fail CS2103T /at 2020-12-02` + +Expected outcome: + + Got it. I've added this task: + [E][✘] fail CS2103T (at: 2020-12-02) + You now have ... tasks in the list. + +### `list` - Shows all current tasks + +Format: `list` + +Example of usage: + +`list` + +Expected outcome: + +If there are no items in the list: + + Theres currently nothing in your list. + +If there are existing items in the list, it may look something like: + + 1. [T][✓] fail CS2103T + 2. [D][✓] fail CS2103T (by: 2020-12-01) + 3. [E][✓] fail CS2103T (at: 2020-12-02) + +### `done` - Marks a current task as done + +Format: `done ITEM NUMBER` + +Example of usage: + +`done 1` + +Expected outcome: + + Nice, I've marked this item as done: + [T][✓] fail CS2103T + +### `delete` - Deletes an item from the list. + +Format: `delete ITEM NUMBER` + +Example of usage: + +`delete 1` + +Expected outcome: + + Noted, I've removed this task: + [T][✓] fail CS2103T + You now have ... items in the list. + +### `sort` - Shows all current tasks + +Format: `sort` + +Example of usage: + +`sort` + +Expected outcome: + + Tasks sorted by date + 1. [D][✓] fail CS2103T (by: 2020-12-01) + 2. [E][✓] fail CS2103T (at: 2020-12-02) + 3. [T][✓] fail CS2103T + +### `bye` - Exits the program. -Describe action and its outcome. +Format: `bye` Example of usage: -`keyword (optional arguments)` +`bye` Expected outcome: -`outcome` +The window closes. \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..a25ffaaec3 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/DialogBox.java b/src/main/java/DialogBox.java new file mode 100644 index 0000000000..60636ac988 --- /dev/null +++ b/src/main/java/DialogBox.java @@ -0,0 +1,59 @@ +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.java b/src/main/java/Duke.java index 5d313334cc..17a7eb706b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,73 @@ +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.layout.VBox; +import javafx.scene.image.Image; + +/** + * Main class for Duke program. + */ public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + + private Image user = new Image(this.getClass().getResourceAsStream("/images/DaMith.png")); + private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.jpg")); + private ScrollPane scrollPane; + private VBox dialogContainer; + private TextField userInput; + private Button sendButton; + private Scene scene; + + /** + * Entry point of the duke program. Read from storage and creates a UI class instance. + * Serves as intermediate between the UI and parser. + * + * @param args + * @throws DukeException + */ +// public static void main(String[] args) throws DukeException { +// TaskList tasks = Storage.read(); +// assert tasks != null : "error initializing tasks"; +// UI ui = new UI(); +// assert ui != null : "error initializing UI"; +// ui.welcome(tasks); +// String input = ui.getInput(); +// Parser parser = new Parser(tasks); +// assert ui != null : "error initializing parser"; +// while (!input.equals("bye")) { +// if (input.isEmpty()) { +// input = ui.getInput(); +// continue; +// } +// parser.parse(input); +// +// input = ui.getInput(); +// } +// ui.bye(); +// } + + TaskList tasks; + Parser parser; + + public Duke(){ + + tasks = Storage.read(); + assert tasks != null : "error initializing tasks"; + UI ui = new UI(); + assert ui != null : "error initializing UI"; + ui.init(); + parser = new Parser(tasks); } + + public String welcome() { + return parser.welcome(); + } + /** + * You should have your own function to generate a response to user input. + * Replace this stub with your completed method. + */ + public String getResponse(String input) { + return parser.parse(input); + } + } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 0000000000..5c714f7078 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,12 @@ +/** + * An exception class used for errors caused by inappropriate user input. Displays a message. + */ +public class DukeException extends Exception { + /** + * Constructor for DukeException. + * @param msg the message to be displayed. + */ + DukeException(String msg) { + super("☹ OOPS!!! " + msg); + } +} \ No newline at end of file diff --git a/src/main/java/Launcher.java b/src/main/java/Launcher.java new file mode 100644 index 0000000000..11dbf00c62 --- /dev/null +++ b/src/main/java/Launcher.java @@ -0,0 +1,10 @@ +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + */ +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..9f37e4e0aa --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000000..b3cb403ea4 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,30 @@ +import java.io.IOException; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +/** + * A GUI for Duke using FXML. + */ +public class Main extends Application { + + private Duke duke = new Duke(); + + @Override + public void start(Stage stage) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = fxmlLoader.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + fxmlLoader.getController().setDuke(duke); + stage.show(); + fxmlLoader.getController().welcome(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/MainWindow.java b/src/main/java/MainWindow.java new file mode 100644 index 0000000000..43ee34b481 --- /dev/null +++ b/src/main/java/MainWindow.java @@ -0,0 +1,55 @@ +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 { + @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/DaMith.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.jpg")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void setDuke(Duke d) { + duke = d; + } + + public void welcome() { + dialogContainer.getChildren().addAll( + DialogBox.getDukeDialog(duke.welcome(), 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(); + } +} \ No newline at end of file diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 0000000000..008a5b0086 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,117 @@ +import Task.Task; +import Task.Todo; +import Task.Deadline; +import Task.Event; + +import java.time.LocalDate; +import java.time.format.DateTimeParseException; + +/** + * Class that handles user input + */ +public class Parser { + TaskList tasks; + + /** + * @param tasks TaskList to use. + */ + public Parser(TaskList tasks) { + this.tasks = tasks; + } + + /** + * Parses the user command. + * + * @param input input from the user. + * @return + */ + String parse(String input) { + try { + assert !input.isEmpty() : "error: input is null"; + String[] words = input.split(" "); + String command = words[0]; + Task newTask; + String[] subst; + LocalDate date; + switch (command) { + case "list": + tasks.print_tasks(); + break; + case "done": + case "delete": + int i = Integer.parseInt(words[1]) - 1; + Task task = tasks.get(i); + if (command.equals("done")) { + tasks.setDone(i, true); + UI.print("Nice! I've marked this task as done: \n" + task); + } else if (command.equals("delete")) { + tasks.remove(i); + UI.print("Noted. I've removed this task: \n" + task + tasks.numTasks()); + } + break; + case "todo": + if (input.length() < 6) { + throw new DukeException("The description of a todo cannot be empty."); + } + String text = input.substring(5); + newTask = new Todo(text); + tasks.addTask(newTask); + break; + case "deadline": + if (input.length() < 10) { + throw new DukeException("The description of a deadline cannot be empty."); + } + subst = input.substring(9).split(" /by "); + if (subst.length < 2) { + throw new DukeException("The due date must be specified."); + } + try { + date = LocalDate.parse(subst[1]); + newTask = new Deadline(subst[0], date); + tasks.addTask(newTask); + } catch (DateTimeParseException e) { + throw new DukeException(e.getMessage()); + } + break; + case "event": + if (input.length() < 7) { + throw new DukeException("The description of an event cannot be empty."); + } + subst = input.substring(6).split(" /at "); + if (subst.length < 2) { + throw new DukeException("The event date must be specified."); + } + try { + date = LocalDate.parse(subst[1]); + newTask = new Event(subst[0], date); + tasks.addTask(newTask); + } catch (DateTimeParseException e) { + throw new DukeException(e.getMessage()); + } + break; + case "find": + if (input.length() < 6) { + throw new DukeException("Text to search for cannot be empty."); + } + tasks.find(input.substring(5)); + break; + case "sort": + tasks.sort(); + break; + case "bye": + System.exit(0); + break; + default: + throw new DukeException("I'm sorry, but I don't know what that means :-("); + } + } catch (DukeException e) { + UI.print(e.getMessage() + "\n"); + } + return UI.getResponse(); + } + + public String welcome() { + UI.welcome(tasks); + return UI.getResponse(); + } +} \ No newline at end of file diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 0000000000..bca58a2e2c --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,61 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.nio.file.Paths; + +/** + * Class that provides file storage utilities. + */ +public class Storage { + public static final File storage_file = Paths.get("tasks.ser").toFile(); + + public Storage() { + } + + /** + * Serializes a TaskList and writes it to file. + * @param t tasklist to be stored. + */ + public static void store(TaskList t) { + try { + //noinspection ResultOfMethodCallIgnored + storage_file.createNewFile(); //creates file if it does not exist + FileOutputStream fileOut = new FileOutputStream(storage_file); + ObjectOutputStream out = new ObjectOutputStream(fileOut); + out.writeObject(t); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Reads a serialized TaskList. + * @return tasklisk read from file. + */ + public static TaskList read() { + try { + TaskList t; + FileInputStream fileIn = new FileInputStream(storage_file); + ObjectInputStream in = new ObjectInputStream(fileIn); + t = (TaskList) in.readObject(); + in.close(); + fileIn.close(); + t.loadMessage = "Successfully loaded from storage. " + t.numTasks() + t.toString(); + return t; + } catch (FileNotFoundException i) { + return new TaskList(); + } catch (IOException i) { + i.printStackTrace(); + return new TaskList(); + } catch (ClassNotFoundException c) { + System.out.println("Employee class not found"); + c.printStackTrace(); + return new TaskList(); + } + } +} \ No newline at end of file diff --git a/src/main/java/Task/Deadline.java b/src/main/java/Task/Deadline.java new file mode 100644 index 0000000000..c41283910f --- /dev/null +++ b/src/main/java/Task/Deadline.java @@ -0,0 +1,25 @@ +package Task; + +import java.time.LocalDate; + +/** + * Represents a task to be completed by a certain time. + */ +public class Deadline extends Task { + + /** + * @param description Task.Task description. + * @param date Task.Deadline for the task. + */ + public Deadline(String description, LocalDate date) { + super(description, date); + } + + /** + * @return String representation of the task. + */ + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + date.toString() + ")\n"; + } +} \ No newline at end of file diff --git a/src/main/java/Task/Event.java b/src/main/java/Task/Event.java new file mode 100644 index 0000000000..30744b34a7 --- /dev/null +++ b/src/main/java/Task/Event.java @@ -0,0 +1,25 @@ +package Task; + +import java.time.LocalDate; + +/** + * Represents a task occurring at a certain time + */ +public class Event extends Task { + + /** + * @param description Task.Task description + * @param date Date of the event + */ + public Event(String description, LocalDate date) { + super(description, date); + } + + /** + * @return String representation of the task + */ + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + date.toString() + ")\n"; + } +} \ No newline at end of file diff --git a/src/main/java/Task/Task.java b/src/main/java/Task/Task.java new file mode 100644 index 0000000000..801341179a --- /dev/null +++ b/src/main/java/Task/Task.java @@ -0,0 +1,50 @@ +package Task; + +import java.time.LocalDate; + +/** + * Represents a task. + */ +public class Task implements java.io.Serializable, Comparable { + public String text; + public boolean isDone; + LocalDate date; + + /** + * @param text Task.Task description. + * @param date Date of the task. + */ + public Task(String text, LocalDate date) { + this.text = text; + this.date = date; + } + + /** + * @return String representation of the task. + */ + public String toString() { + return "[" + (isDone ? "✓" : "✗") + "] " + text; + } + + /** + * Compares this task with the specified task for order based on date. The task with no date is considered greater. + * Otherwise, the task with a later date is considered greater. + * + * @param t the object to be compared. + * @return a negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. + * @throws NullPointerException if the specified object is null + * @throws ClassCastException if the specified object's type prevents it + * from being compared to this object. + */ + @Override + public int compareTo(Task t) { + if(this.date == null){ + return 1; + } else if (t.date == null){ + return -1; + } else { + return this.date.compareTo(t.date); + } + } +} diff --git a/src/main/java/Task/Todo.java b/src/main/java/Task/Todo.java new file mode 100644 index 0000000000..7a5d310e26 --- /dev/null +++ b/src/main/java/Task/Todo.java @@ -0,0 +1,22 @@ +package Task; + +/** + * Represents a to do task without a date. + */ +public class Todo extends Task { + + /** + * @param description Task.Task description. + */ + public Todo(String description) { + super(description, null); + } + + /** + * @return String representation of the task. + */ + @Override + public String toString() { + return "[T]" + super.toString() + "\n"; + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 0000000000..198c060de6 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,127 @@ +import Task.Task; + +import java.util.ArrayList; +import java.util.Collections; + +/** + * Stores tasks and supports various operations on them. + */ +public class TaskList implements java.io.Serializable { + + private ArrayList tasks; + String loadMessage = ""; + + /** + * Initializes a new task list + */ + public TaskList() { + tasks = new ArrayList<>(); + } + + private TaskList(ArrayList tasks) { + this.tasks = tasks; + } + + /** + * Adds a task to the task list. + * @param task the task to be added. + */ + public void addTask(Task task) { + tasks.add(task); + UI.print("added: " + task.toString() + numTasks()); + Storage.store(this); + } + + /** + * @return a string describing the number of tasks + */ + public String numTasks() { + int size = tasks.size(); + return "You now have " + size + " task" + (size > 1 ? "s" : "") + " in the list.\n"; + } + + /** + * Prints the list of tasks through the UI class + */ + public void print_tasks() { + UI.print(this.toString()); + } + + @Override + public String toString(){ + if(tasks.size() == 0){ + return "There are no tasks!\n"; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < tasks.size(); i++) { + sb.append(i + 1).append(".").append(tasks.get(i).toString()); + } + return sb.toString(); + } + + /** + * @param i Index of the task to be returned. + * @return The task at index I. + * @throws DukeException + */ + public Task get(int i) throws DukeException { + if (i < 0 || i >= tasks.size()) { + throw new DukeException("invalid task number"); + } + return tasks.get(i); + } + + /** + * @param i Index of the task to be removed. + * @throws DukeException + */ + public void remove(int i) throws DukeException { + if (i < 0 || i >= tasks.size()) { + throw new DukeException("invalid task number"); + } + tasks.remove(i); + Storage.store(this); + } + + /** + * @param i the index of the task to be set. + * @param value Doneness of the task to be set. + * @throws DukeException + */ + public void setDone(int i, boolean value) throws DukeException { + if (i < 0 || i >= tasks.size()) { + throw new DukeException("invalid task number"); + } + tasks.get(i).isDone = value; + Storage.store(this); + } + + /** + * Finds tasks in this tasklist that matches the provided substring, and prints them. + * @param substring string to match + */ + public void find(String substring) { + StringBuilder sb = new StringBuilder(); + int counter = 0; + sb.append("Here are the matching tasks in your list:\n"); + for (Task t : tasks) { + if (t.text.contains(substring)) { + counter++; + sb.append(counter + "." + t.toString()); + } + } + if (counter > 0) { + UI.print(sb.toString()); + } else { + UI.print("No match found!\n"); + } + } + + /** + * Sorts tasks by date then prints the new list. + */ + public void sort(){ + Collections.sort(tasks); + UI.print("Tasks sorted by date.\n" + this.toString()); + } +} \ No newline at end of file diff --git a/src/main/java/UI.java b/src/main/java/UI.java new file mode 100644 index 0000000000..44eadd0fc6 --- /dev/null +++ b/src/main/java/UI.java @@ -0,0 +1,72 @@ +import java.util.Scanner; + +/** + * Class for reading user input and user interface. + */ +public class UI { + + final static String BYE = "aight imma head out\n"; + final static String WELCOME = "Hello! I'm Duke\nWhat can I do for you?\n"; + final static String LINE = "____________________________________________________________\n"; + Scanner scanner = new Scanner(System.in); + + private static StringBuilder response; + + /** + * print greeting content + */ + + public static void init() { + response = new StringBuilder(); + } + + private static void println(String message) { + response.append(message).append("\n"); + } + + /** + * Prints a string of text surrounded by a line of underscores above and below. + * + * @param str text to be printed. + */ + public static void print(String str) { + println(LINE + str + LINE); + } + +// /** +// * Reads user input. +// * +// * @return input from user. +// */ +// String getInput() { +// return scanner.nextLine(); +// } + + /** + * Prints welcome message and current number of tasks + * + * @param tasks TaskList loaded from storage. + */ + public static void welcome(TaskList tasks) { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + println("Hello from\n" + logo); + print(WELCOME + tasks.loadMessage); + } + + /** + * Prints goodbye message. + */ + public static void bye() { + print(BYE); + } + + public static String getResponse() { + String responseToReturn = response.toString(); + response.setLength(0); + return responseToReturn; + } +} diff --git a/src/main/resources/images/DaDuke.jpg b/src/main/resources/images/DaDuke.jpg new file mode 100644 index 0000000000..05aa2d0517 Binary files /dev/null and b/src/main/resources/images/DaDuke.jpg differ diff --git a/src/main/resources/images/DaMith.png b/src/main/resources/images/DaMith.png new file mode 100644 index 0000000000..e87db85810 Binary files /dev/null and b/src/main/resources/images/DaMith.png differ diff --git a/src/main/resources/images/FTEA0910.png b/src/main/resources/images/FTEA0910.png new file mode 100644 index 0000000000..aaecd599c1 Binary files /dev/null and b/src/main/resources/images/FTEA0910.png differ diff --git a/src/main/resources/images/FTEA0910_cr.png b/src/main/resources/images/FTEA0910_cr.png new file mode 100644 index 0000000000..68f8c81907 Binary files /dev/null and b/src/main/resources/images/FTEA0910_cr.png differ diff --git a/src/main/resources/images/download.jpg b/src/main/resources/images/download.jpg new file mode 100644 index 0000000000..ea52a86ffa Binary files /dev/null and b/src/main/resources/images/download.jpg differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..f369df2776 --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..b4688e474b --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +