diff --git a/.gitignore b/.gitignore index 99712178bf..57d1be034a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ /build/ src/main/resources/docs/ +# data file +/data.duke.txt + # MacOS custom attributes files created by Finder .DS_Store *.iml diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..a82bd2a08b --- /dev/null +++ b/build.gradle @@ -0,0 +1,52 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + id 'com.github.johnrengelman.shadow' version '5.1.0' + id 'org.openjfx.javafxplugin' version '0.0.7' +} + +checkstyle { + toolVersion = '8.23' +} + +shadowJar { + archiveBaseName = "duke" + archiveVersion = "0.1.3" + archiveClassifier = null + archiveAppendix = null +} + +repositories { + mavenCentral() +} + +javafx { + version = "11.0.2" + modules = [ 'javafx.controls', 'javafx.fxml' ] +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0' +} + +test { + useJUnitPlatform() +} + + +group 'seedu.duke' +version '0.1.0' + +repositories { + mavenCentral() +} + +application { + // Change this to your main class. + mainClassName = "Launcher" +} + +run { + standardInput = System.in +} \ No newline at end of file diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..b1a57ba6c0 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,257 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/JUtest1.in b/data/JUtest1.in new file mode 100644 index 0000000000..dd7dbd308d --- /dev/null +++ b/data/JUtest1.in @@ -0,0 +1,6 @@ +deadline return book /by 2/12/2019 1800 +deadline return book /by 2/12/2019 1800 +deadline return book /by 2/12/2019 1800 +deadline return book /by 2/12/2019 1800 +deadline return book /by 2/12/2019 1800 + diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000..e63c633d64 --- /dev/null +++ b/data/duke.txt @@ -0,0 +1,3 @@ +false@[E]@asdasda1@at 02/12/19 0600 +false@[E]@asdasda2@at 01/12/19 0600 +true@[E]@asdasda3@at 02/12/19 0600 diff --git a/data/tasks.txt b/data/tasks.txt new file mode 100644 index 0000000000..a9dff21b64 --- /dev/null +++ b/data/tasks.txt @@ -0,0 +1,3 @@ +false@[E]@club carnival@at 01/12/19 0600 +false@[E]@animal book carnival@at 02/12/19 0600 +true@[E]@book club bees@at 02/12/19 0600 diff --git a/docs/README.md b/docs/README.md index fd44069597..6543477d8f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,19 +2,131 @@ ## Features -### Feature 1 -Description of feature. +### Add Todo +Adds a todo item to your list ## Usage -### `Keyword` - Describe action +### `todo` - adds an item -Describe action and its outcome. +Adds a todo item at the end of your todo list Example of usage: -`keyword (optional arguments)` +`todo (item)` Expected outcome: -`outcome` +`Got it. I've added this task:` +`[T][X](item)` +`Now you have 1 tasks in the list` + +### Add Event +Adds an event item to your list + +## Usage + +### `event` - adds an item + +Adds a event item at the end of your todo list + +Example of usage: + +`event (item) /at dd/MM/yy HHss` + +Expected outcome: + +`Got it. I've added this task:` +`[E][X](item) at (dd/MM/yy HHss)` +`Now you have 1 tasks in the list` + +### Add Deadline +Adds an deadline item to your list + +## Usage + +### `deadline` - adds an item + +Adds a deadline item at the end of your todo list + +Example of usage: + +`deadline (item) /by dd/MM/yy HHss` + +Expected outcome: + +`Got it. I've added this task:` +`[D][X](item) by (dd/MM/yy HHss)` +`Now you have 1 tasks in the list` + +### Mark as done +Mark item on list as done + +## Usage + +### `done` - marks item as done + +Mark numbered item on list as done + +Example of usage: + +`done 5` + +Expected outcome: + +`Nice! I've marked this task as done:` +`[ITEM][DONE] Buy juice` + + +### Delete +Deletes item from list + +## Usage + +### `delete` - deletes an item + +Deletes numbered item on list + +Example of usage: + +`delete 5` + +Expected outcome: + +`Noted. I've removed this task: ` +`[E][✗] project meeting (at: Aug 6th 2-4pm)` +`Now you have 4 tasks in the list.` + +### Sort +Sorts list by date + +## Usage + +### `sort` - sorts list + +Sorts list by date + +Example of usage: + +`sort` + +Expected outcome: +`Your list is now sorted` + +### Find +Finds substring in all item description + +## Usage + +### `find (subString)` - finds an items containing subString + +Finds and lists your item + +Example of usage: + +`find (subString)` + +Expected outcome: +`Here are the matching tasks in your list:` +`2.[T][X] Do fives times (substring) homework` +`2.[T][X] Do (substring)ly` \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..e7a655d42f Binary files /dev/null and b/docs/Ui.png differ diff --git a/docs/_config.yml b/docs/_config.yml new file mode 100644 index 0000000000..c50ff38dab --- /dev/null +++ b/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-merlot \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..87b738cbd0 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..8cffc066e7 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Sep 03 17:48:48 SGT 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100755 index 0000000000..af6708ff22 --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## 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"' + +# 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, switch paths to Windows format before running java +if $cygwin ; 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=$((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" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..6d57edc706 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@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 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" + +@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/java b/java new file mode 100644 index 0000000000..e69de29bb2 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000000..d1e92fe5db --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'duke' diff --git a/src/main/java/Date.java b/src/main/java/Date.java new file mode 100644 index 0000000000..e7184aef12 --- /dev/null +++ b/src/main/java/Date.java @@ -0,0 +1,53 @@ +import java.text.SimpleDateFormat; + +/** + * Date representation of date string that's given + */ +public class Date{ + boolean exists = false; + String precursor = ""; + SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy HHss"); + java.util.Date data; + public Date(String dateString) { + precursor = dateString.split(" ", 2)[0]; + System.out.println(precursor); + System.out.println(dateString.split(" ", 2)[1]); + if (!dateString.equals(" ")) { + try { + this.data = formatter.parse(dateString.split(" ", 2)[1]); + this.exists = true; + } + catch (Exception e) { + System.out.println("WrongDateFormat"); + } + } + } + + public java.util.Date getData() { + if (this.exists) { + return this.data; + } + else { + return new java.util.Date(); + } + } + + public String format() { + if (exists) { + return precursor + " " + formatter.format(data); + } + else { + return "NIL"; + } + } + + @Override + public String toString() { + if (exists) { + return "(" + precursor + " " + formatter.format(data) + ")"; + } + else { + return " "; + } + } +} diff --git a/src/main/java/DialogBox.java b/src/main/java/DialogBox.java new file mode 100644 index 0000000000..8c558ccd55 --- /dev/null +++ b/src/main/java/DialogBox.java @@ -0,0 +1,48 @@ +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +public class DialogBox extends HBox { + + private Label text; + private ImageView displayPicture; + + public DialogBox(Label l, ImageView iv) { + text = l; + displayPicture = iv; + + text.setWrapText(true); + displayPicture.setFitWidth(100.0); + displayPicture.setFitHeight(100.0); + + this.setAlignment(Pos.TOP_RIGHT); + this.getChildren().addAll(text, displayPicture); + } + /** + * Flips the dialog box such that the ImageView is on the left and text on the right. + */ + private void flip() { + this.setAlignment(Pos.TOP_LEFT); + ObservableList tmp = FXCollections.observableArrayList(this.getChildren()); + FXCollections.reverse(tmp); + this.getChildren().setAll(tmp); + } + + public static DialogBox getUserDialog(Label l, ImageView iv) { + return new DialogBox(l, iv); + } + + public static DialogBox getDukeDialog(Label l, ImageView iv) { + var db = new DialogBox(l, iv); + 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..a51578ec3b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,221 @@ -public class Duke { +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.stage.Stage; + +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; + +import javafx.scene.layout.Region; + +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +import java.util.ArrayList; + + +public class Duke extends Application{ + + private Storage storage; + private TaskList tasks; + private UserInterface ui; + private ArrayList userIn = new ArrayList<>(); + private ArrayList reply = new ArrayList<>(); + private ScrollPane scrollPane; + private VBox dialogContainer; + private TextField userInput; + private Button sendButton; + private Scene scene; + + private Image user = new Image(this.getClass().getResourceAsStream("/images/User.jpg")); + private Image duke = new Image(this.getClass().getResourceAsStream("/images/Duke.jpg")); + + public Duke() { + String path = System.getProperty("user.dir"); + path += "\\tasks.txt"; + System.out.println(path); + storage = new Storage(path); + try { + tasks = new TaskList(storage.load()); + } catch (Exception e) { + tasks = new TaskList(path); + } + ui = new UserInterface(tasks, storage); + + } + + public Duke(String filePath) { + storage = new Storage(filePath); + try { + tasks = new TaskList(storage.load()); + } catch (Exception e) { + tasks = new TaskList(filePath); + } + ui = new UserInterface(tasks, storage); + } + + void run() { + dukePrint("Hello! I'm Duke", "What can I do for you?"); + this.ui.listen(); + dukePrint("Bye. Hope to see you again soon!"); + } + /** + * Main driver class for Duke. + * + */ public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + new Duke("data/tasks.txt").run(); + } + + /** + * Echos string. + * @param echoedString targeted String to be echoed + */ + private static void echo(String echoedString) { + dukePrint(echoedString); + } + + /** + * Prints what Duke says in correct format. + * @param texts any number of String arguments + * each prints on a new line. + */ + + /** + * Prints texts in the correct format on console + * @param texts varargs to print on different lines + */ + private static void dukePrint(Object... texts) { + System.out.println(" _____________________________"); + for (Object text : texts) { + System.out.println(" " + text); + } + System.out.println(" _____________________________"); + + } + + + @Override + public void start(Stage stage) { + //Step 1. Setting up required components + //The container for the content of the chat to scroll. + scrollPane = new ScrollPane(); + dialogContainer = new VBox(); + scrollPane.setContent(dialogContainer); + + userInput = new TextField(); + sendButton = new Button("Send"); + + AnchorPane mainLayout = new AnchorPane(); + mainLayout.getChildren().addAll(scrollPane, userInput, sendButton); + + scene = new Scene(mainLayout); + + stage.setScene(scene); + stage.show(); + + //Step 2. Formatting the window to look as expected + stage.setTitle("Duke"); + stage.setResizable(false); + stage.setMinHeight(600.0); + stage.setMinWidth(400.0); + + mainLayout.setPrefSize(400.0, 600.0); + + scrollPane.setPrefSize(385, 535); + scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); + scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); + + scrollPane.setVvalue(1.0); + scrollPane.setFitToWidth(true); + + // You will need to import `javafx.scene.layout.Region` for this. + dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE); + + userInput.setPrefWidth(325.0); + + sendButton.setPrefWidth(55.0); + + AnchorPane.setTopAnchor(scrollPane, 1.0); + + AnchorPane.setBottomAnchor(sendButton, 1.0); + AnchorPane.setRightAnchor(sendButton, 1.0); + + AnchorPane.setLeftAnchor(userInput , 1.0); + AnchorPane.setBottomAnchor(userInput, 1.0); + + // more code to be added here later + + //Step 3. Add functionality to handle user input. + sendButton.setOnMouseClicked((event) -> { + dialogContainer.getChildren().add(getDialogLabel(userInput.getText())); + userInput.clear(); + }); + + userInput.setOnAction((event) -> { + dialogContainer.getChildren().add(getDialogLabel(userInput.getText())); + userInput.clear(); + }); + + //Scroll down to the end every time dialogContainer's height changes. + dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0)); + + //Part 3. Add functionality to handle user input. + sendButton.setOnMouseClicked((event) -> { + handleUserInput(); + }); + + userInput.setOnAction((event) -> { + handleUserInput(); + }); + } + + + + + /** + * Iteration 1: + * Creates a label with the specified text and adds it to the dialog container. + * @param text String containing text to add + * @return a label with the specified text that has word wrap enabled. + */ + private Label getDialogLabel(String text) { + // You will need to import `javafx.scene.control.Label`. + Label textToAdd = new Label(text); + textToAdd.setWrapText(true); + + return textToAdd; + + } + + private void handleUserInput() { + Label userText = new Label(userInput.getText()); + Label dukeText = new Label(getResponse(userInput.getText())); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(userText, new ImageView(user)), + DialogBox.getDukeDialog(dukeText, new ImageView(duke)) + ); + userInput.clear(); + } + + /** + * You should have your own function to generate a response to user input. + * Replace this stub with your completed method. + */ + private String getResponse(String input) { + return ui.getReply(input); } + + //Scroll down to the end every time dialogContainer's height changes. } diff --git a/src/main/java/Launcher.java b/src/main/java/Launcher.java new file mode 100644 index 0000000000..6d7bc95383 --- /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(Duke.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/ListItem.java b/src/main/java/ListItem.java new file mode 100644 index 0000000000..894c0489db --- /dev/null +++ b/src/main/java/ListItem.java @@ -0,0 +1,73 @@ +public class ListItem implements Comparable{ + + private String description; + private String status; + private boolean isDone = false; + private Date date; + + + ListItem(String description, String command, String date) { + try { + assert(!command.equals("")); + assert (!description.equals("")); + switch (command) { + case "todo": + case "[T]": + this.status = "[T]"; + this.description = description; + this.date = new Date(" "); + break; + case "event": + case "[E]": + this.status = "[E]"; + this.description = description.split("/")[0]; + this.date = new Date(date); + break; + case "deadline": + case "[D]": + this.status = "[D]"; + this.description = description.split("/")[0]; + this.date = new Date(date); + break; + } + } + catch (Error e) { + System.out.println("Item Creation Error"); + throw e; + } + } + + void done() { + this.isDone = true; + } + + Date getDate() { + return this.date; + } + + String format() { + return isDone + "@" + this.status + "@" + this.description + "@" + this.date.format() + "\n"; + } + + String getDescription() { + return description; + } + + @Override + public String toString() { + String toReturn; + if (isDone) { + toReturn = "[DONE] " + this.description; + } + else { + toReturn = "[X] " + this.description; + } + + return this.status + toReturn + this.date; + } + + @Override + public int compareTo(ListItem other) { + return getDate().getData().compareTo(other.getDate().getData()); + } +} diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 0000000000..381c2d173b --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,59 @@ +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + String filepath; + + Storage(String filepath) { + this.filepath = filepath; + } + + /** + * loads a list from the initiated filepath + * @return formatted and read list + */ + ArrayList load() { + try { + Scanner reader = new Scanner(new FileReader(filepath)); + ArrayList tempArray = new ArrayList<>(); + while(reader.hasNext()) { + String commandIn = reader.nextLine(); + String[] commandArray = commandIn.split("@"); + tempArray.add(new ListItem(commandArray[2], commandArray[1], commandArray[3])); + if (commandArray[0].equals("true")) { + tempArray.get((tempArray.size() - 1)).done(); + } + } + ArrayList toReturn = new ArrayList(); + toReturn.addAll(tempArray); + return toReturn; + } + catch (IOException e) { + System.out.println("ERROR cannot read"); + } + return new ArrayList<>(); + } + + /** + * Saves data to associated filepath + * @param toSave List that you want to save + */ + void save(ArrayList toSave) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(filepath)); + for (ListItem item : toSave) { + writer.write(item.format()); + } + writer.flush(); + } + catch (IOException e) { + System.out.println("ERROR Cannot Save"); + } + } + + +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 0000000000..968ca31683 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,65 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +public class TaskList { + + static ArrayList lst = new ArrayList<>(); + private static String filename; + + TaskList(String filepath) { + TaskList.filename = filepath; + + } + + TaskList(ArrayList loadedList) { + lst = loadedList; + } + + /** + * adds an item to the todoList + * @param description of the list item + * @param command type of item it is + * @param date due date if any for the item + * @return Output string for Duke to print + */ + static String addToTodo(String description, String command, String date) { + lst.add(new ListItem(description, command, date)); + + String ret = "Got it. I've added this task:" + "\n" + lst.get(lst.size() - 1).toString() + "\n" + + "Now you have " + lst.size() + " tasks in the list."; + return ret; + } + + /** + * removes an item from list based off of index + * @param index of the item to be removed + * @return Output string for Duke to print + */ + static String removeFromTodo(String index) { + assert(lst.size() != 0); + ListItem target = lst.get(Integer.parseInt(index) - 1); + lst.remove(target); + String ret = "Noted. I've removed this task:" + target + "\n" + + ("Now you have " + lst.size() + " tasks in the list."); + return ret; + } + + static String sortList() { + Collections.sort(lst); + return "Your list is now sorted!"; + } + + @Override + public String toString() { + String toReturn = ""; + for (int i = 0; i < TaskList.lst.size(); i++) { + toReturn = toReturn.concat((i + 1) + "." + TaskList.lst.get(i).toString() + "\n"); + } + if (TaskList.lst.size() < 1) { + return "To do list is Empty :)"; + } + assert(toReturn.length() > 6); + return toReturn; + } +} diff --git a/src/main/java/UserInterface.java b/src/main/java/UserInterface.java new file mode 100644 index 0000000000..9c1cbd4d5d --- /dev/null +++ b/src/main/java/UserInterface.java @@ -0,0 +1,180 @@ +import java.util.Scanner; + +class UserInterface { + static TaskList todoList; + static Storage data; + UserInterface(TaskList todoList, Storage store) { + this.todoList = todoList; + this.data = store; + } + + /** + * starts Duke up until exit command is entered + */ + static void listen () { + String userCommand = ""; + String description = ""; + String date = ""; + Scanner scanner = new Scanner(System.in); + + String userInput; + while (!userCommand.equals("exit")) { + userInput = scanner.nextLine(); + userCommand = userInput.split(" ",2)[0]; + try { + description = userInput.split(" ", 2)[1].split("/")[0]; + } catch (Exception ignored) { + + } + + if (userInput.split("/").length > 1) { + date = userInput.split("/")[1]; + } + else { + date = " "; + } + switch(userCommand) { + case "list": + dukePrint("We are also here"); + break; + case "sort": + dukePrint("We are here"); + //dukePrint(todoList.sortList()); + break; + case "done": + ListItem target = todoList.lst.get(Integer.parseInt(userInput.split(" ", 2)[1]) - 1); + target.done(); + dukePrint("Nice! I've marked this task as done:", " " + target); + break; + case "todo": + case "event": + case "deadline": + try { + dukePrint(todoList.addToTodo(description, userCommand, date)); + } + //try to catch more exceptions + catch (Exception e) { + dukePrint("OOPS!!! The description of a " + userCommand + " cannot be empty."); + } + break; + case "delete": + try { + dukePrint(todoList.removeFromTodo(userInput.split(" ", 2)[1])); + } + catch (Exception e) { + dukePrint("Nothing to delete"); + } + break; + case "find": + String lst = ""; + int counter = 1; + for (ListItem item : TaskList.lst) { + if (item.getDescription().contains(description)) { + lst += counter + "." + item.toString() + "\n "; + counter++; + } + } + dukePrint("Here are the matching tasks in your list:", lst.substring(0, lst.length() - 6)); + break; + case "pause": + try { + Thread.sleep(1000); + } catch (Exception ignored) {} + break; + case "exit": + break; + default: + dukePrint("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + data.save(todoList.lst); + + } + } + + + static String getReply(String userInput) { + String description = " "; + String date; + data.save(todoList.lst); + + String userCommand = userInput.split(" ",2)[0]; + try { + description = userInput.split(" ", 2)[1].split("/")[0]; + } catch (Exception e) { + + } + + if (userInput.split("/").length > 1) { + date = userInput.split("/",2)[1]; + } + else { + date = " "; + } + switch(userCommand) { + case "list": + return (todoList.toString()); + case "sort": + return (todoList.sortList()); + case "done": + ListItem target = todoList.lst.get(Integer.parseInt(userInput.split(" ", 2)[1]) - 1); + target.done(); + return ("Nice! I've marked this task as done:" + " " + target); + case "todo": + case "event": + case "deadline": + try { + return (todoList.addToTodo(description, userCommand, date)); + } + //try to catch more exceptions + catch (Exception e) { + return ("OOPS!!! The description of a " + userCommand + " cannot be empty."); + } + case "delete": + try { + return (todoList.removeFromTodo(userInput.split(" ", 2)[1])); + } + catch (Exception e) { + return ("Nothing to delete"); + } + case "find": + String lst = ""; + int counter = 1; + for (ListItem item : TaskList.lst) { + if (item.getDescription().contains(description)) { + lst += counter + "." + item.toString() + "\n "; + counter++; + } + } + + return ("Here are the matching tasks in your list:" + "\n" + lst.substring(0, lst.length() - 6)); + case "pause": + try { + Thread.sleep(1000); + } catch (Exception ignored) {} + break; + case "exit": + break; + + default: + return ("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + data.save(todoList.lst); + + return ""; + } + + + /** + * Prints what Duke says in correct format. + * @param texts any number of String arguments + * each prints on a new line. + */ + private static void dukePrint(Object... texts) { + System.out.println(" _____________________________"); + for (Object text : texts) { + System.out.println(" " + text); + } + System.out.println(" _____________________________"); + + } +} diff --git a/src/main/resources/images/Duke.jpg b/src/main/resources/images/Duke.jpg new file mode 100644 index 0000000000..b183b27859 Binary files /dev/null and b/src/main/resources/images/Duke.jpg differ diff --git a/src/main/resources/images/User.jpg b/src/main/resources/images/User.jpg new file mode 100644 index 0000000000..a6072905ab Binary files /dev/null and b/src/main/resources/images/User.jpg differ diff --git a/src/test/java/DukeTest.java b/src/test/java/DukeTest.java new file mode 100644 index 0000000000..88be312e0d --- /dev/null +++ b/src/test/java/DukeTest.java @@ -0,0 +1,15 @@ +import org.junit.jupiter.api.Assertions; + +import java.io.File; +import static org.junit.jupiter.api.Assertions.*; + +class DukeTest { + public static void main(String[] args) { + TaskList test = new TaskList("data/TaskListTest1"); + test.addToTodo("Test1 Description", "event", "at 2/12/2019 1800" ); + Assertions.assertEquals("[E][✗] Test1 Description(at 02/12/19 0600)", test.lst.get(0).toString()); + System.out.println("Tested"); + return; + } + +} \ No newline at end of file diff --git a/src/test/java/ListItemTest.java b/src/test/java/ListItemTest.java new file mode 100644 index 0000000000..313db32890 --- /dev/null +++ b/src/test/java/ListItemTest.java @@ -0,0 +1,9 @@ +import org.junit.jupiter.api.Assertions; + +public class ListItemTest { + public static void main(String[] args) { + ListItem testSubject = new ListItem("Description", "deadline", "by 2/12/2019 1800"); + Assertions.assertEquals("[D]" + "[✗] "+ "Description" + "(by 02/12/19 1800)",testSubject.toString()); + return; + } +} diff --git a/tasks.txt b/tasks.txt new file mode 100644 index 0000000000..0d7a9fe40c --- /dev/null +++ b/tasks.txt @@ -0,0 +1,2 @@ +false@[T]@hello@NIL +false@[T]@none@NIL diff --git a/text-ui-test/ACTUAL.TXT b/text-ui-test/ACTUAL.TXT new file mode 100644 index 0000000000..dccc97ee9e --- /dev/null +++ b/text-ui-test/ACTUAL.TXT @@ -0,0 +1,3 @@ +Hello! I'm Duke +What can I do for you? +HELP diff --git a/text-ui-test/EXPECTED.txt b/text-ui-test/EXPECTED.txt new file mode 100644 index 0000000000..8983aba0dd --- /dev/null +++ b/text-ui-test/EXPECTED.txt @@ -0,0 +1 @@ +HELP \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt new file mode 100644 index 0000000000..8983aba0dd --- /dev/null +++ b/text-ui-test/input.txt @@ -0,0 +1 @@ +HELP \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat new file mode 100644 index 0000000000..d0facc6310 --- /dev/null +++ b/text-ui-test/runtest.bat @@ -0,0 +1,21 @@ +@ECHO OFF + +REM create bin directory if it doesn't exist +if not exist ..\bin mkdir ..\bin + +REM delete output from previous run +del ACTUAL.TXT + +REM compile the code into the bin folder +javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\Duke.java +IF ERRORLEVEL 1 ( + echo ********** BUILD FAILURE ********** + exit /b 1 +) +REM no error here, errorlevel == 0 + +REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT +java -classpath ..\bin Duke < input.txt > ACTUAL.TXT + +REM compare the output to the expected output +FC ACTUAL.TXT EXPECTED.TXT