diff --git a/.gitignore b/.gitignore index 2873e189e1..e6e947ebb1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +Stickiem.txt diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..73f411efb3 --- /dev/null +++ b/build.gradle @@ -0,0 +1,58 @@ +plugins { + id 'java' + id 'application' + id 'com.github.johnrengelman.shadow' version '7.1.2' +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0' + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0' + + String javaFxVersion = '17.0.7' + + 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 { + mainClass.set("Stickiem.Launcher") +} + +shadowJar { + archiveFileName = 'Stickiem.jar' + archiveBaseName = "Stickiem" + archiveClassifier = null +} + +run{ + standardInput = System.in + enableAssertions = true +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..a4b76b9530 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..9355b41557 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000000..f5feea6d6b --- /dev/null +++ b/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original 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. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# 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 ;; #( + MSYS* | 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 + if ! command -v java >/dev/null 2>&1 + then + 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 +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# 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"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..9b42019c79 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@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 +@rem SPDX-License-Identifier: Apache-2.0 +@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=. +@rem This is normally unused +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% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +: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 %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 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! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +: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/Stickiem/Deadline.java b/src/main/java/Stickiem/Deadline.java new file mode 100644 index 0000000000..2e565c4ba9 --- /dev/null +++ b/src/main/java/Stickiem/Deadline.java @@ -0,0 +1,34 @@ +package Stickiem; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Deadline extends Task { + private LocalDate endDate; + + public Deadline(String name, String endDate, boolean status) { + super(name, status); + endDate = endDate.trim(); + this.endDate = LocalDate.parse(endDate); + } + + @Override + public String getDetails() { + String marking = this.isDone ? "X" : " "; + return "[D][" + marking + "] " + this.name + "(by: "+ this.endDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy")) + ")"; + } + + @Override + public String getType() { + + return "deadline"; + } + + @Override + public String getCommand() { + String marking = isDone ? "X" : ""; + return marking + " " + this.getType() + " " + this.name + "/by " + this.endDate.toString(); + + } + +} diff --git a/src/main/java/Stickiem/DialogBox.java b/src/main/java/Stickiem/DialogBox.java new file mode 100644 index 0000000000..feb2a0f03e --- /dev/null +++ b/src/main/java/Stickiem/DialogBox.java @@ -0,0 +1,60 @@ +package Stickiem; +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; + +/** + * 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; + } +} + diff --git a/src/main/java/Stickiem/Event.java b/src/main/java/Stickiem/Event.java new file mode 100644 index 0000000000..73fb673f73 --- /dev/null +++ b/src/main/java/Stickiem/Event.java @@ -0,0 +1,37 @@ +package Stickiem; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Event extends Task { + private LocalDate startDate; + private LocalDate endDate; + + private String deadline; + public Event(String name, String startDate, String endDate, boolean status) { + super(name, status); + startDate = startDate.trim(); + endDate = endDate.trim(); + this.startDate = LocalDate.parse(startDate); + this.endDate = LocalDate.parse(endDate); + } + + @Override + public String getDetails() { + String marking = this.isDone ? "X" : " "; + return "[E][" + marking + "] " + this.name + "(from: " + + this.startDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy")) + + " to: " + this.endDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy")) + ")"; + } + + @Override + public String getType() { + return "event"; + } + + @Override + public String getCommand() { + String marking = isDone ? "X" : ""; + return marking + " " + this.getType() + " " + name + "/from " + startDate + " /to " + endDate; + } +} diff --git a/src/main/java/Stickiem/Launcher.java b/src/main/java/Stickiem/Launcher.java new file mode 100644 index 0000000000..b130423af5 --- /dev/null +++ b/src/main/java/Stickiem/Launcher.java @@ -0,0 +1,8 @@ +package Stickiem; + +import javafx.application.Application; +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/Stickiem/Main.java b/src/main/java/Stickiem/Main.java new file mode 100644 index 0000000000..bdaa66b993 --- /dev/null +++ b/src/main/java/Stickiem/Main.java @@ -0,0 +1,31 @@ +package Stickiem; + +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 Stickiem stickiem = new Stickiem("stickiem.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); + fxmlLoader.getController().setDuke(stickiem); // inject the Duke instance + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/Stickiem/MainWindow.java b/src/main/java/Stickiem/MainWindow.java new file mode 100644 index 0000000000..9da43aa2c5 --- /dev/null +++ b/src/main/java/Stickiem/MainWindow.java @@ -0,0 +1,53 @@ +package Stickiem; + +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 the main GUI. + */ +public class MainWindow extends AnchorPane { + @FXML + private ScrollPane scrollPane; + @FXML + private VBox dialogContainer; + @FXML + private TextField userInput; + @FXML + private Button sendButton; + + private Stickiem duke; + + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + /** Injects the Duke instance */ + public void setDuke(Stickiem d) { + duke = d; + } + + /** + * 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.run(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + } +} + diff --git a/src/main/java/Stickiem/Parser.java b/src/main/java/Stickiem/Parser.java new file mode 100644 index 0000000000..b4f83c412a --- /dev/null +++ b/src/main/java/Stickiem/Parser.java @@ -0,0 +1,35 @@ +package Stickiem; + +/** + * Deals with making sense of the user command + */ +public class Parser { + + /** + * Categorizes user command to different types, "bye", + * "list", "mark", "delete", "add" or "find" + * + * @param command user command + * @return type command type + */ + public static String parse(String command) { + String type = ""; + if (command.equals("bye")) { + type = "bye"; + } else if (command.equals("list")) { + type = "list"; + } else if (command.contains("mark")) { + type = "mark"; + } else if (command.contains("delete")) { + type = "delete"; + } else if (command.contains("todo") || command.contains("deadline") || command.contains("event")) { + type = "add"; + } else if(command.contains("find")) { + type = "find"; + } + + return type; + } + + +} diff --git a/src/main/java/Stickiem/Save.java b/src/main/java/Stickiem/Save.java new file mode 100644 index 0000000000..f9268f3ba1 --- /dev/null +++ b/src/main/java/Stickiem/Save.java @@ -0,0 +1,63 @@ +package Stickiem; + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.Scanner; + +public class Save { + private File file; + //private String filePath; + + public Save(String filePath) { + //this.filePath = filePath; + + this.file = new File(filePath); + try { + if (!file.exists()) { + file.createNewFile(); + } + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + + assert file.exists(); + + + + } + + public void save(ArrayList details) { + assert file.exists(); + try { + + FileWriter fw = new FileWriter(file); + String input = ""; + for(String detail : details) { + + input += detail + System.lineSeparator(); + } + fw.write(input); + fw.close(); + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + + + } + + public ArrayList load() { + ArrayList details = new ArrayList<>(); + try { + Scanner input = new Scanner(file); + while (input.hasNextLine()) { + details.add(input.nextLine()); + } + } catch (FileNotFoundException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + return details; + } +} diff --git a/src/main/java/Stickiem/Stickiem.java b/src/main/java/Stickiem/Stickiem.java new file mode 100644 index 0000000000..7fed2e8ba0 --- /dev/null +++ b/src/main/java/Stickiem/Stickiem.java @@ -0,0 +1,272 @@ +package Stickiem; +/** + * The main program for the chatbot to run. + */ + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Stickiem { + private Ui ui; + private Save save; + private TaskList taskList; + + /** + * Sets up Stickiem chatbot. + * Loads saved data if any. + * + * @param filePath location of file storage + */ + public Stickiem(String filePath) { + this.ui = new Ui(); + this.save = new Save(filePath); + this.taskList = new TaskList(); + + try { + ArrayList initialValues = save.load(); + for (int i = 0; i < initialValues.size(); i++) { + taskList.addTask(createTask(initialValues.get(i))); + } + } catch (StickiemCommandException e) { + System.out.println(e.getMessage()); + } + + } + + /** + *Runs the Stickiem chatbot + */ + public String run(String userInput) { + while(this.ui.getActivity()) { + //String userInput = this.ui.getUserInput(); + String type = Parser.parse(userInput); + try { + checkCommand(userInput); + } catch (StickiemCommandException e) { + System.out.println(e.getMessage()); + continue; + } + if(type.equals("bye")) { + ui.exit(); + return "bye"; + } else if (type.equals("list")) { + return this.taskList.getTaskDetails(); + } else if (type.equals("mark")) { + String output = ""; + String[] details = userInput.split(" "); + int index = Integer.parseInt(details[1]) - 1; + Task currentTask = this.taskList.getTask(index); + if (userInput.contains("unmark")) { + currentTask.unmarkStatus(); + output += "OK, I've marked this task as not done yet:"; + + } else { + currentTask.markStatus(); + output += "Nice! I've marked this task as done:"; + } + this.save.save(this.taskList.convertCommand()); + return output + "\n" + currentTask.getDetails(); + } else if (type.equals("delete")) { + String[] details = userInput.split(" "); + int index = Integer.parseInt(details[1]) - 1; + String output = this.taskList.removeTask(index); + this.save.save(this.taskList.convertCommand()); + + return output; + } else if (type.equals("add")) { + try { + Task currentTask = createTask(userInput); + String output = taskList.addTask(currentTask); + this.save.save(this.taskList.convertCommand()); + + return output; + + } catch (StickiemCommandException e) { + System.out.println(e.getMessage()); + } + + } else if (type.equals("find")) { + int index = userInput.indexOf("find"); + String keyword = userInput.substring(index + 5); + + + return "Here are the matching tasks in your list:" + new TaskList(taskList.getTasks(keyword)).getTaskDetails(); + + + } else { + return "Invalid command"; + } + + } + return "hmm"; + } + + + + + +public static void main(String[] args) { + //new Stickiem("stickiem.txt").run(); +} +/* + public static void main(String[] args) { + String output = "____________________________________________________________"; + output += "\nHello! I'm Stickiem!\nWhat can I do for you?"; + output += "\n____________________________________________________________"; + System.out.println(output); + Scanner input = new Scanner(System.in); + List storage = new ArrayList(); + + Save file = new Save("Stickiem.txt"); + ArrayList initialValue = file.load(); + try { + for (int i = 0; i < initialValue.size(); i++) { + storage.add(createTask(initialValue.get(i))); + } + } catch (StickiemCommandException e) { + System.out.println(e.getMessage()); + } + + while (true) { + output = ""; + String userInput = input.nextLine(); + if (userInput.equals("bye")) { + break; + + } else if (userInput.contains("mark")) { + String[] details = userInput.split(" "); + int index = Integer.parseInt(details[1]) - 1; + Task currentTask = storage.get(index); + if (userInput.contains("unmark")) { + currentTask.unmarkStatus(); + output += "OK, I've marked this task as not done yet:"; + + } else { + currentTask.markStatus(); + output += "Nice! I've marked this task as done:"; + } + initialValue.set(index, currentTask.getCommand()); + file.save(initialValue); + output += "\n" + currentTask.getDetails(); + + + } else if(userInput.contains("delete")) { + String[] details = userInput.split(" "); + int index = Integer.parseInt(details[1]) - 1; + Task currentTask = storage.get(index); + + output = "Noted. I've removed this task:" + currentTask.getDetails(); + storage.remove(currentTask); + initialValue.remove(index); + file.save(initialValue); + output += "\nNow you have " + storage.size() + " tasks in the list."; + + } else if (userInput.equals("list")) { + int len = storage.size(); + + for (int i = 0; i < len; i++) { + int index = i + 1; + Task currentTask = storage.get(i); + output += "\n" + index + "." + currentTask.getDetails(); + + } + } else { + try { + checkCommand(userInput); + Task currentTask = createTask(userInput); + storage.add(currentTask); + int len = storage.size(); + initialValue.add(currentTask.getCommand()); + file.save(initialValue); + output = "Got it. I've added this task: \n" + storage.get(len - 1).getDetails(); + output += "\nNow you have " + len + " tasks in the list."; + } catch (StickiemCommandException e) { + System.out.println(e.getMessage()); + } + + } + output += "\n____________________________________________________________"; + System.out.println(output); + } + + output = "Bye. Hope to see you again soon!"; + output += "\n____________________________________________________________"; + System.out.println(output); + + } +*/ + /** + * Returns the task created based on the user input. + * + * @param userInput command given by the user. + * @return newItem task generated. + * @throws StickiemCommandException if command does not exists. + */ + +public static Task createTask(String userInput) throws StickiemCommandException { + boolean isMarked = false; + if(userInput.charAt(0) == 'X') { + isMarked = true; + } + + if (userInput.contains("todo")) { + //String name = userInput.replace("todo", ""); + String name = userInput.substring(userInput.indexOf("todo") + 5); + + ToDo newItem = new ToDo(name, isMarked); + return newItem; + } else if (userInput.contains("deadline")) { + //String name = userInput.replace("deadline", ""); + String name = userInput.substring(userInput.indexOf("deadline") + 9); + + int index = name.indexOf("/by"); + String date = name.substring(index + 3); + name = name.substring(0, index); + + Deadline newItem = new Deadline(name, date, isMarked); + return newItem; + } else if (userInput.contains("event")) { + //String name = userInput.replace("event", ""); + String name = userInput.substring(userInput.indexOf("event") + 6); + int indexFrom = name.indexOf("/from"); + int indexTo = name.indexOf("/to"); + String startDate = name.substring(indexFrom + 5, indexTo); + String endDate = name.substring(indexTo + 3); + name = name.substring(0, indexFrom); + + Event newItem = new Event(name, startDate, endDate, isMarked); + return newItem; + } else { + throw new StickiemCommandException("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + + + + +} + +public static void checkCommand(String userInput) throws StickiemCommandException { + if(userInput.isEmpty()) { + throw new StickiemCommandException("Ehhh, no input detected"); + } else{ + List types = Arrays.asList("todo", "deadline", "event"); + for(int i = 0; i tasks; + public TaskList() { + + this.tasks = new ArrayList(); + } + + public TaskList(ArrayList tasks) { + + this.tasks = tasks; + } + + public String addTask(Task task) { + this.tasks.add(task); + + String output = "Got it. I've added this task: \n" + task.getDetails(); + output += "\nNow you have " + this.tasks.size() + " tasks in the list."; + + return output; + } + + public String removeTask(int index) { + Task task = this.tasks.get(index); + String output = "Noted. I've removed this task:" + task.getDetails(); + this.tasks.remove(index); + return output; + } + + public int getTaskIndex(Task task) { + return this.tasks.indexOf(task); + } + + public String getTaskDetails() { + int len = tasks.size(); + + String output = ""; + for (int i = 0; i < len; i++) { + int index = i + 1; + Task currentTask = tasks.get(i); + output += "\n" + index + "." + currentTask.getDetails(); + + } + + return output; + } + + public Task getTask(int index) { + + return this.tasks.get(index); + } + + public ArrayList getTasks(String keyword) { + ArrayList tasks = new ArrayList(); + for(Task task : this.tasks) { + if(task.getName().contains(keyword)) { + tasks.add(task); + } + } + return tasks; + } + + public ArrayList convertCommand() { + ArrayList commands = new ArrayList(); + for(int i = 0; i < this.tasks.size(); i++) { + commands.add(this.tasks.get(i).getCommand()); + } + return commands; + } +} diff --git a/src/main/java/Stickiem/ToDo.java b/src/main/java/Stickiem/ToDo.java new file mode 100644 index 0000000000..bbc5140164 --- /dev/null +++ b/src/main/java/Stickiem/ToDo.java @@ -0,0 +1,25 @@ +package Stickiem; + +public class ToDo extends Task { + + public ToDo(String name, boolean status) { + super(name, status); + } + + @Override + public String getDetails() { + String marking = isDone ? "X" : " "; + return "[T][" + marking + "] " + name; + } + + @Override + public String getType() { + return "todo"; + } + + @Override + public String getCommand() { + String marking = isDone ? "X" : ""; + return marking + " " + this.getType() + " " + name; + } +} diff --git a/src/main/java/Stickiem/Ui.java b/src/main/java/Stickiem/Ui.java new file mode 100644 index 0000000000..7dac6963c0 --- /dev/null +++ b/src/main/java/Stickiem/Ui.java @@ -0,0 +1,40 @@ +package Stickiem; +/** + * Deals with the interaction between user. + */ + +import java.util.Scanner; + +public class Ui { + private Scanner scanner; + private String output; + private boolean isActive; + + /** + * Sets up the Ui and welcome message. + */ + public Ui() { + this.scanner = new Scanner(System.in); + this.isActive = true; + this.output = "____________________________________________________________"; + this.output += "\nHello! I'm Stickiem!\nWhat can I do for you?"; + this.output += "\n____________________________________________________________"; + System.out.println(output); + } + + /** + * Close the active status of the Ui. + * Prints "bye". + */ + public void exit() { + this.output = "bye"; + this.isActive = false; + } + + + public boolean getActivity() { + return this.isActive; + } + + +} diff --git a/src/main/resources/images/DaDuke.png b/src/main/resources/images/DaDuke.png new file mode 100644 index 0000000000..d893658717 Binary files /dev/null and b/src/main/resources/images/DaDuke.png differ diff --git a/src/main/resources/images/DaUser.png b/src/main/resources/images/DaUser.png new file mode 100644 index 0000000000..3c82f45461 Binary files /dev/null and b/src/main/resources/images/DaUser.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..fe672c90ea --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..cb90bc0a4a --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,44 @@ + + + + + + + + + + + +