diff --git a/README.md b/README.md index 8715d4d915..3241de1a7f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Veneto project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,12 +13,14 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/veneto.Veneto.java` file, right-click it, and choose `Run veneto.Veneto.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| + ___ ___ __________ ___ ___ __________ __________ __________ + \ \ / /| _______|| \ | || _______||___ ___|| ____ | + \ \ / / | |_______ | \| || |_______ | | | | | | + \ \/ / | _______|| || _______| | | | | | | + \ / | |_______ | |\ || |_______ | | | |____| | + \__/ |__________||___| \___||__________| |__| |__________| + ``` diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..b817c8536c --- /dev/null +++ b/build.gradle @@ -0,0 +1,61 @@ +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("veneto.Launcher") + mainClassName = 'veneto.Main' +} + +shadowJar { + archiveBaseName = "veneto" + archiveClassifier = null + dependsOn("distZip", "distTar") +// archiveFileName = 'veneto-v1.0.2.jar' +} + + +run{ + standardInput = System.in + enableAssertions = true +} diff --git a/docs/README.md b/docs/README.md index 8077118ebe..101181f311 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,69 @@ # User Guide +Veneto is an app that can remember things you need to do + + ## Features -### Feature-ABC +### Add task + +Adds a task to Veneto's tasklist. +Format: +``` +toDo [TASK] +deadline [TASK] /by [DEADLINE(YYYY-MM-DD)] +event [TASK] /from [START_TIME(YYYY-MM-DD)] /to [END_TIME(YYYY-MM-DD)] +``` +Examples: +`event meeting A /from 2001-01-01 /to 2003-01-01` + -Description of the feature. +### List tasks -### Feature-XYZ +Lists all tasks from Veneto's tasklist. +Format: +`list` -Description of the feature. -## Usage +### Mark task -### `Keyword` - Describe action +Marks the ith task as done. +Format: +``` +mark [TASK_ID] +``` +Examples: +`mark 1` -Describe the action and its outcome. -Example of usage: +### Unmark task -`keyword (optional arguments)` +Unmarks the ith task as undone. +Format: +``` +unmark [TASK_ID] +``` +Examples: +`unmark 1` + + +### Find task + +Finds the task(s) that contains specific keyword(s). +Format: +``` +find [KEYWORD(S)] +``` +Examples: +`find borrow book` -Expected outcome: -Description of the outcome. +### Delete task +Deletes the ith task. +Format: ``` -expected output +delete [TASK_ID] ``` +Examples: +`delete 1` diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..e7942f879f 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..033e24c4cd 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..66c01cfeba --- /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-7.6.2-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..fcb6fca147 --- /dev/null +++ b/gradlew @@ -0,0 +1,248 @@ +#!/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. +# + +############################################################################## +# +# 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/subprojects/plugins/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##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || 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=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=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, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +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..6689b85bee --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@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=. +@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. +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 execute + +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 + +: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/data/veneto.txt b/src/main/data/veneto.txt new file mode 100644 index 0000000000..ba5609eada --- /dev/null +++ b/src/main/data/veneto.txt @@ -0,0 +1,3 @@ +toDo,borrow book,0 +deadline,return book,1,2019-08-10 +event, project meeting,0,2020-05-09,2020-05-10 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/veneto/Launcher.java b/src/main/java/veneto/Launcher.java new file mode 100644 index 0000000000..0079f18e0e --- /dev/null +++ b/src/main/java/veneto/Launcher.java @@ -0,0 +1,9 @@ +package veneto; + +import javafx.application.Application; + +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/veneto/Main.java b/src/main/java/veneto/Main.java new file mode 100644 index 0000000000..221def21ee --- /dev/null +++ b/src/main/java/veneto/Main.java @@ -0,0 +1,34 @@ +package veneto; + +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; +import veneto.ui.MainWindow; + +/** + * A GUI for Duke using FXML. + */ +public class Main extends Application { + + private Veneto veneto = new Veneto(); + + @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().setUi(veneto); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} + diff --git a/src/main/java/veneto/Veneto.java b/src/main/java/veneto/Veneto.java new file mode 100644 index 0000000000..9686c496d1 --- /dev/null +++ b/src/main/java/veneto/Veneto.java @@ -0,0 +1,46 @@ +package veneto; + +import veneto.command.Command; +import veneto.exceptions.VenetoException; +import veneto.storage.Storage; +import veneto.task.TaskList; +import veneto.ui.Ui; + +public class Veneto { + /* Fields */ + private TaskList tasks; + private Storage storage; + private Ui ui; + + /* Constructor */ + + public Veneto() { + try { + ui = new Ui(); + storage = new Storage(); + tasks = storage.load(); + } catch (VenetoException e) { + ui.showError(e); + tasks = new TaskList(); + storage.init(tasks); + } + } + + /* Methods */ + /** + * operate the user input and return responses + * @return the responses + */ + public String getResponse(String input) { + String response; + try { + Command command = ui.getCommand(input); + command.op(tasks); + storage.checkChange(tasks); + response = ui.afterCommand(command, tasks); + } catch (VenetoException e) { + response = ui.showError(e); + } + return response; + } +} diff --git a/src/main/java/veneto/command/AddCommand.java b/src/main/java/veneto/command/AddCommand.java new file mode 100644 index 0000000000..c7b4d4d14b --- /dev/null +++ b/src/main/java/veneto/command/AddCommand.java @@ -0,0 +1,66 @@ +package veneto.command; + +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoOperateException; +import veneto.task.*; + +public class AddCommand extends Command { + /* Fields */ + public static final String type = "add"; + private Task newTask; + + /* Constructor */ + + public AddCommand(Task t) { + super(); + this.newTask = t; + } + + /* Methods */ + /** + * the AddCommand operates + * @param tasks the TaskList where the AddCommand put new task + * @throws VenetoOperateException when newTask is duplicate + * or when + */ + @Override + public void op(TaskList tasks) throws VenetoException { + try { + if (!isDuplicated(tasks)) { + tasks.add(newTask); + tasks.storageChanged = 1; + } else { + throw new VenetoOperateException("Duplicate"); + } + } catch (IndexOutOfBoundsException e) { + throw new VenetoOperateException("Add"); + } + } + + + @Override + /** + * @return the String representation of the task added to the TaskList + */ + public String toString() { + return newTask.toString(); + } + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } + + private boolean isDuplicated(TaskList tasks) { + boolean hasSameTask = false; + for (Task currTask : tasks) { + if (currTask.equals(newTask)) { + hasSameTask = true; + break; + } + } + return hasSameTask; + } +} diff --git a/src/main/java/veneto/command/Command.java b/src/main/java/veneto/command/Command.java new file mode 100644 index 0000000000..c38e2cb50b --- /dev/null +++ b/src/main/java/veneto/command/Command.java @@ -0,0 +1,16 @@ +package veneto.command; + +import veneto.task.TaskList; + +public abstract class Command { + /** + * the Command operates + * @param tasks the TaskList that the Command operates on + */ + public abstract void op(TaskList tasks); + + /** + * @return the type of the Command + */ + public String getType() {return null;} +} diff --git a/src/main/java/veneto/command/DeleteCommand.java b/src/main/java/veneto/command/DeleteCommand.java new file mode 100644 index 0000000000..abd977c21a --- /dev/null +++ b/src/main/java/veneto/command/DeleteCommand.java @@ -0,0 +1,44 @@ +package veneto.command; + +import veneto.task.Task; +import veneto.task.TaskList; + +public class DeleteCommand extends Command { + /* Fields */ + public static final String type = "delete"; + + private int taskId; + private Task removedTask; + + /* Constructor */ + public DeleteCommand(int taskId) { + super(); + this.taskId = taskId; + } + + /* Methods */ + /** + * the DeleteCommand operates + * @param tasks the TaskList that the DeleteCommand operates on + */ + @Override + public void op(TaskList tasks) { + removedTask = tasks.remove(taskId-1); + tasks.storageChanged = 1; + } + + @Override + /** + * @return the String representation of the task removed to the TaskList + */ + public String toString() { + return removedTask.toString(); + } + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/veneto/command/ExitCommand.java b/src/main/java/veneto/command/ExitCommand.java new file mode 100644 index 0000000000..5da5330168 --- /dev/null +++ b/src/main/java/veneto/command/ExitCommand.java @@ -0,0 +1,28 @@ +package veneto.command; + +import veneto.task.TaskList; + +public class ExitCommand extends Command { + /* Fields */ + public static final String type = "exit"; + + /* Constructor */ + public ExitCommand() { + super(); + } + + /* Methods */ + /** + * the ExitCommand operates + * @param tasks the TaskList of Veneto + */ + @Override + public void op(TaskList tasks) {} + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/veneto/command/FindCommand.java b/src/main/java/veneto/command/FindCommand.java new file mode 100644 index 0000000000..13df48361b --- /dev/null +++ b/src/main/java/veneto/command/FindCommand.java @@ -0,0 +1,59 @@ +package veneto.command; + +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoOperateException; +import veneto.task.Task; +import veneto.task.TaskList; + +public class FindCommand extends Command{ + /* Fields */ + public static final String type = "find"; + private String keyword; + private TaskList foundTasks; + + /* Constructor */ + /** + * creates a find command + * @param keyword contained in the task which should be found + */ + public FindCommand(String keyword) { + super(); + this.keyword = keyword; + this.foundTasks = new TaskList(); + } + + /* Methods */ + /** + * the FindCommand operates + * @param tasks the TaskList where the Command search from + * @throws VenetoException + */ + @Override + public void op(TaskList tasks) throws VenetoException { + Task currTask; + for (int i = 0; i < tasks.size(); i++) { + currTask = tasks.get(i); + if (currTask.contains(keyword)) { + foundTasks.add(currTask); + } + } + if (foundTasks.isEmpty()) { + throw new VenetoOperateException("Not Found"); + } + } + + /** + * @return the tasks found + */ + @Override + public String toString() { + return foundTasks.toString(); + } + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/veneto/command/ListCommand.java b/src/main/java/veneto/command/ListCommand.java new file mode 100644 index 0000000000..b2e8d91585 --- /dev/null +++ b/src/main/java/veneto/command/ListCommand.java @@ -0,0 +1,28 @@ +package veneto.command; + +import veneto.task.TaskList; + +public class ListCommand extends Command { + /* Fields */ + public static final String type = "list"; + + /* Constructor */ + public ListCommand() { + super(); + } + + /* Methods */ + /** + * the ListCommand operates + * @param tasks the TaskList that the ListCommand shows + */ + @Override + public void op(TaskList tasks) {} + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/veneto/command/MarkCommand.java b/src/main/java/veneto/command/MarkCommand.java new file mode 100644 index 0000000000..08949d19f9 --- /dev/null +++ b/src/main/java/veneto/command/MarkCommand.java @@ -0,0 +1,52 @@ +package veneto.command; + +import veneto.exceptions.VenetoException; +import veneto.task.Task; +import veneto.task.TaskList; + +public class MarkCommand extends Command { + /* Fields */ + public static final String type = "add"; + private int taskId; + + /** + * This variable indicates if the function is "mark()" or "unmark()" + * funcId == 1 -> mark() ; funcId = 0 -> unmark() + */ + private int funcId; + private Task currTask; + + /* Constructor */ + public MarkCommand(int taskId, int funcId) { + super(); + this.taskId = taskId; + this.funcId = funcId; + } + + /* Methods */ + /** + * the MarkCommand operates + * @param tasks the TaskList that the MarkCommand operates on + */ + @Override + public void op(TaskList tasks) throws VenetoException { + currTask = tasks.get(taskId - 1); + currTask.mark(funcId); + tasks.storageChanged = 1; + } + + @Override + /** + * @return the String representation of the task marked or unmarked + */ + public String toString() { + return currTask.toString(); + } + + /** + * @return the type of the Command + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/veneto/exceptions/VenetoException.java b/src/main/java/veneto/exceptions/VenetoException.java new file mode 100644 index 0000000000..00d1566452 --- /dev/null +++ b/src/main/java/veneto/exceptions/VenetoException.java @@ -0,0 +1,11 @@ +package veneto.exceptions; + +public class VenetoException extends RuntimeException { + /** + * Exception that may occur when Veneto runs + * @param s the message of the exception + */ + public VenetoException(String s) { + super(s); + } +} diff --git a/src/main/java/veneto/exceptions/VenetoOperateException.java b/src/main/java/veneto/exceptions/VenetoOperateException.java new file mode 100644 index 0000000000..8fca633a9d --- /dev/null +++ b/src/main/java/veneto/exceptions/VenetoOperateException.java @@ -0,0 +1,11 @@ +package veneto.exceptions; + +public class VenetoOperateException extends VenetoException { + /** + * exception that may occur when a Command operates + * @param s the message of the exception + */ + public VenetoOperateException(String s) { + super(s); + } +} diff --git a/src/main/java/veneto/exceptions/VenetoStorageException.java b/src/main/java/veneto/exceptions/VenetoStorageException.java new file mode 100644 index 0000000000..0b4bccf943 --- /dev/null +++ b/src/main/java/veneto/exceptions/VenetoStorageException.java @@ -0,0 +1,11 @@ +package veneto.exceptions; + +public class VenetoStorageException extends VenetoException { + /** + * exception that may occur when operate on the Storage + * @param s the message of the exception + */ + public VenetoStorageException(String s) { + super(s); + } +} diff --git a/src/main/java/veneto/parser/Parser.java b/src/main/java/veneto/parser/Parser.java new file mode 100644 index 0000000000..f4f4554271 --- /dev/null +++ b/src/main/java/veneto/parser/Parser.java @@ -0,0 +1,113 @@ +package veneto.parser; + +import veneto.command.*; +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoOperateException; +import veneto.task.Deadline; +import veneto.task.Event; +import veneto.task.Task; +import veneto.task.ToDo; + +import java.time.format.DateTimeParseException; + +public class Parser { + /* Fields */ + public static final int FIND_COMMAND_INDEX = 5; + public static final int TODO_DESCRIPTION_INDEX = 5; + public static final int DEADLINE_DESCRIPTION_INDEX = 9; + public static final int DEADLINE_START_INDEX = 3; + public static final int EVENT_DESCRIPTION_INDEX = 5; + public static final int EVENT_START_INDEX = 5; + public static final int EVENT_END_INDEX = 3; + + /* Methods */ + /** + * translate user put to commands that need to operate + * @param text the input from user + * @return the Command that the user wants to operate + */ + public static Command translateCommand(String text) { + String[] texts = text.split(" "); + String command = texts[0].toLowerCase(); + switch (command) { + case "bye": + return new ExitCommand(); + case "list": + return new ListCommand(); + case "mark": + return new MarkCommand(Integer.parseInt(texts[1]), 1); + case "unmark": + return new MarkCommand(Integer.parseInt(texts[1]), 0); + case "todo": + return prepareAdd(text, 1); + case "deadline": + return prepareAdd(text, 2); + case "event": + return prepareAdd(text, 3); + case "delete": + return new DeleteCommand(Integer.parseInt(texts[1])); + case "find": + return prepareFind(text); + default: + throw new VenetoException("Invalid Command"); + } + } + + /** + * generate FindCommand according to the user input + * @param text the details about the task + * @return the command that the user want to operate + */ + private static Command prepareFind(String text) { + String keyword = text.substring(FIND_COMMAND_INDEX); + return new FindCommand(keyword); + } + + /** + * generate AddCommand according to the user input + * @param text the details about the task + * @param funcId indicates the type of task + * @return the command that the user want to operate + */ + private static Command prepareAdd(String text, int funcId) { + try { + String[] texts = text.split("/"); + for (int i = 0; i < texts.length; i++) { + texts[i] = texts[i].trim(); + } + Task newTask = null; + String description; + switch (funcId) { + case 1: + if (texts.length != 1) { + throw new VenetoOperateException("add"); + } + description = texts[0].substring(TODO_DESCRIPTION_INDEX); + newTask = new ToDo(description); + break; + case 2: + if (texts.length != 2) { + throw new VenetoOperateException("add"); + } + description = texts[0].substring(DEADLINE_DESCRIPTION_INDEX); + String deadline = texts[1].substring(DEADLINE_START_INDEX); + newTask = new Deadline(description, deadline); + break; + case 3: + if (texts.length != 3) { + throw new VenetoOperateException("add"); + } + description = texts[0].substring(EVENT_DESCRIPTION_INDEX); + String start = texts[1].substring(EVENT_START_INDEX); + String end = texts[2].substring(EVENT_END_INDEX); + newTask = new Event(description, start, end); + break; + default: + assert false : "invalid funcId"; + } + return new AddCommand(newTask); + } catch (DateTimeParseException e) { + throw new VenetoOperateException("add"); + } + } +} diff --git a/src/main/java/veneto/storage/Storage.java b/src/main/java/veneto/storage/Storage.java new file mode 100644 index 0000000000..d72e1dd0c4 --- /dev/null +++ b/src/main/java/veneto/storage/Storage.java @@ -0,0 +1,127 @@ +package veneto.storage; + +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoStorageException; +import veneto.task.*; + +import java.io.*; +import java.util.Scanner; + +public class Storage { + /* Fields */ + private Writer writer; + private TaskList tasks; + + private static final String STORAGE_DIR = "./src/main/data/veneto.txt"; + private static final String STORAGE_PATH = "./src/main/data/veneto.txt"; + + /* Constructors */ + + /* Methods */ + /** + * initialize the storage environment + * @param tasks + * @throws VenetoStorageException when it fails to create the directories + */ + public void init(TaskList tasks) { + this.tasks = tasks; + new File(STORAGE_DIR).mkdir(); + try { + new File(STORAGE_PATH).createNewFile(); + } catch (IOException e) { + throw new VenetoStorageException("make path fail"); + } + } + + /** + * load the data in storage file if exist + * @return TaskList containing the data stored + * @throws VenetoException if the path is invalid + */ + public TaskList load() throws VenetoException { + tasks = new TaskList(100); + File f = new File(STORAGE_PATH); + if (!f.exists()) { + throw new VenetoStorageException("Storage File Destroyed"); + } + Scanner sc = null; + try { + sc = new Scanner(f); + String text; + while (sc.hasNext()) { + text = sc.nextLine(); + addTask(text); + } + return tasks; + } catch (IOException e) { + throw new VenetoStorageException("May not happen"); + } finally { + if (sc != null) { + sc.close(); + } + } + } + + /** + * translate the text into a Task and add it to the TaskList + * @param text a task stored in the file + * @throws VenetoStorageException if the storage file is destroyed + */ + public void addTask(String text) throws VenetoException { + try { + String[] task = text.split(","); + Task t = null; + int isDone = Integer.parseInt(task[2]); + switch (task[0]) { + case "toDo": + t = new ToDo(task[1], isDone); + break; + case "deadline": + t = new Deadline(task[1], isDone, task[3]); + break; + case "event": + t = new Event(task[1], isDone, task[3], task[4]); + break; + default: + assert false : "invalid task type"; + } + tasks.add(t); + } catch (IndexOutOfBoundsException | NumberFormatException | NullPointerException e) { + throw new VenetoStorageException("Storage File Destroyed"); + } + } + + /** + * save the data if there's a change + * @param tasks the TaskList that may be modified + */ + public void checkChange(TaskList tasks) { + if (tasks.storageChanged == 1) { + save(); + tasks.storageChanged = 0; + } + } + + /** + * save the data + * @throws VenetoException when write fails or close fails + */ + private void save() { + try { + new File(STORAGE_PATH).delete(); + new File(STORAGE_PATH).createNewFile(); + writer = new BufferedWriter(new FileWriter(STORAGE_PATH)); + for (int i = 0; i < tasks.size(); i++) { + writer.write(tasks.get(i).saveToString() + "\n"); + } + } catch (IOException e) { + throw new VenetoException("write fails"); + } finally { + try { + writer.close(); + } catch (IOException e) { + throw new VenetoException("close fails"); + } + } + } +} diff --git a/src/main/java/veneto/task/Deadline.java b/src/main/java/veneto/task/Deadline.java new file mode 100644 index 0000000000..ba32c325e0 --- /dev/null +++ b/src/main/java/veneto/task/Deadline.java @@ -0,0 +1,65 @@ +package veneto.task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Objects; + +public class Deadline extends Task { + /* fields */ + protected LocalDate deadline; + + /* Constructors */ + /** + * create a new Deadline Task + * @param description the description of the task + * @param deadline the deadline of the task + */ + public Deadline(String description, String deadline) throws DateTimeParseException { + super(description); + this.deadline = LocalDate.parse(deadline); + } + + /** + * create a new Deadline task from storage file + * @param description the description of the task + * @param mark if the task is marked + * @param deadline the deadline of the task + */ + public Deadline(String description, int mark, String deadline) { + super(description, mark != 0); + this.deadline = LocalDate.parse(deadline); + } + + /* Methods */ + /** + * explanation of the task + * @return return task details + */ + @Override + public String toString() { + return "[D]" + super.toString() + + " (by: " + deadline.format(DateTimeFormatter.ofPattern("MMM d yyyy"))+ ")"; + } + + /** + * generate texts for storage + * @return String of the task data + */ + @Override + public String saveToString() { + return "deadline," + super.saveToString() + "," + deadline; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Deadline deadline1 = (Deadline) o; + boolean sameDescription = super.equals(o); + boolean sameDeadline = deadline.equals(deadline1.deadline); + + return sameDescription && sameDeadline; + } +} diff --git a/src/main/java/veneto/task/Event.java b/src/main/java/veneto/task/Event.java new file mode 100644 index 0000000000..335e25f91b --- /dev/null +++ b/src/main/java/veneto/task/Event.java @@ -0,0 +1,71 @@ +package veneto.task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class Event extends Task { + /* fields */ + protected LocalDate start; + protected LocalDate end; + + /* Constructors */ + /** + * create a new Event Task + * @param description the description of the task + * @param start the start time of the event + * @param end the end time of the event + */ + public Event(String description, String start, String end) throws DateTimeParseException { + super(description); + this.start = LocalDate.parse(start); + this.end = LocalDate.parse(end); + } + + /** + * create a new Event task from storage file + * @param description the description of the task + * @param mark if the task is marked + * @param start the start time of the event + * @param end the end time of the event + */ + public Event(String description, int mark, String start, String end) { + super(description, mark != 0); + this.start = LocalDate.parse(start); + this.end = LocalDate.parse(end); + } + + /* Methods */ + /** + * explanation of the task + * @return return task details + */ + @Override + public String toString() { + return "[E]" + super.toString() + + " (from: " + start.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + + " to: " + end.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; + } + + /** + * generate texts for storage + * @return String of the task data + */ + @Override + public String saveToString() { + return "event," + super.saveToString() + "," + start + "," + end; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Event event1 = (Event) o; + boolean sameDescription = super.equals(o); + boolean sameStart = start.equals(event1.start); + boolean sameEnd = end.equals(event1.end); + + return sameDescription && sameStart && sameEnd; + } +} diff --git a/src/main/java/veneto/task/Task.java b/src/main/java/veneto/task/Task.java new file mode 100644 index 0000000000..5576a452eb --- /dev/null +++ b/src/main/java/veneto/task/Task.java @@ -0,0 +1,107 @@ +package veneto.task; + +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoOperateException; + +import java.util.Objects; + +public class Task { + /* fields */ + protected String description; + protected boolean isDone; + + /* Constructors */ + /** + * create a new Task + * @param description the description of the task + */ + public Task(String description) { + this.description = description; + this.isDone = false; + } + + /** + * create a new task from storage file + * @param description the description of the task + * @param isDone the status of the task + */ + public Task(String description, boolean isDone) { + this.description = description; + this.isDone = isDone; + } + + /* Methods */ + /** + * mark or unmark the task + * @param funcId the function id + * @throws VenetoException if the mark operation is done before + */ + public void mark(int funcId) throws VenetoException { + if (funcId == 1) { /* to mark task */ + markTask(); + } else { /* to unmark task */ + unmarkTask(); + } + } + + private void unmarkTask() { + if (isDone == true) { + isDone = false; + } else { /* if the task is already unmarked */ + throw new VenetoOperateException("Unmarked"); + } + } + + private void markTask() { + if (isDone == false) { + isDone = true; + } else { /* if the task is already marked */ + throw new VenetoOperateException("Marked"); + } + } + + /** + * @param keyword the target keyword + * @return if the description of the task contains the keyword + */ + public boolean contains(String keyword) { + return description.contains(keyword); + } + + // toString + /** + * explanation of the task + * @return return task details + */ + @Override + public String toString() { + return getStatusIcon() + this.description; + } + + /** + * generate texts for storage + * @return String of the task data + */ + public String saveToString() { + return this.description + "," + (isDone ? 1 : 0); + } + + /** + * return the status icon of the task + * @return the status icon of the task + */ + public String getStatusIcon() { + return (isDone ? "[V] " : "[ ] "); // mark done task with V + } + + /** + * + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Task task1 = (Task) o; + return description.equals(task1.description); + } +} diff --git a/src/main/java/veneto/task/TaskList.java b/src/main/java/veneto/task/TaskList.java new file mode 100644 index 0000000000..2e0b498cfb --- /dev/null +++ b/src/main/java/veneto/task/TaskList.java @@ -0,0 +1,40 @@ +package veneto.task; + +import java.util.ArrayList; + +public class TaskList extends ArrayList { + /* Fields */ + /** + * indicates if the storage is changed after a command operates + * will be modified when command operates. + */ + public int storageChanged = 0; + + /* Constructors */ + /** + * creates a new TaskList + */ + public TaskList() {} + + /** + * creates a new TaskList + * @param initialCapacity the initial capacity of the list + */ + public TaskList(int initialCapacity) { + super(initialCapacity); + } + + /* Methods */ + /** + * the string representation of the list + * @return all tasks in the list + */ + @Override + public String toString() { + StringBuffer rt = new StringBuffer(); + for (int i = 0; i < this.size(); i++) { + rt.append(" " + (i+1) + ". " + this.get(i).toString() + "\n"); + } + return rt.toString(); + } +} diff --git a/src/main/java/veneto/task/ToDo.java b/src/main/java/veneto/task/ToDo.java new file mode 100644 index 0000000000..0c1f3d2262 --- /dev/null +++ b/src/main/java/veneto/task/ToDo.java @@ -0,0 +1,45 @@ +package veneto.task; + +public class ToDo extends Task { + /* Constructors */ + /** + * create a new ToDo task + * @param description the description of the task + */ + public ToDo(String description) { + super(description); + } + + /** + * create a new ToDo task from storage file + * @param des the description of the task + * @param mark if the task is marked + */ + public ToDo(String des, int mark) { + super(des, mark != 0); + } + + /* Methods */ + /** + * explanation of the task + * @return return task details + */ + @Override + public String toString() { + return "[T]" + super.toString(); + } + + /** + * generate texts for storage + * @return String of the task data + */ + @Override + public String saveToString() { + return "toDo," + super.saveToString(); + } + + @Override + public boolean equals(Object o) { + return super.equals(o); + } +} diff --git a/src/main/java/veneto/ui/DialogBox.java b/src/main/java/veneto/ui/DialogBox.java new file mode 100644 index 0000000000..69d04f992a --- /dev/null +++ b/src/main/java/veneto/ui/DialogBox.java @@ -0,0 +1,63 @@ +package veneto.ui; + +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; + } +} + + diff --git a/src/main/java/veneto/ui/MainWindow.java b/src/main/java/veneto/ui/MainWindow.java new file mode 100644 index 0000000000..35a3600b24 --- /dev/null +++ b/src/main/java/veneto/ui/MainWindow.java @@ -0,0 +1,62 @@ +package veneto.ui; + +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; +import veneto.Veneto; + +/** + * 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 Veneto veneto; + + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/User.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/Veneto.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void setUi(Veneto v) { + this.veneto = v; + } + + /** + * 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 = veneto.getResponse(input); + boolean shouldExit = false; + if (response.endsWith(Ui.EXIT_SUFFIX)) { + response = response.substring(0, response.length()-1); + shouldExit = true; + } + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + if (shouldExit) { + System.exit(0); + } + userInput.clear(); + } +} + diff --git a/src/main/java/veneto/ui/Ui.java b/src/main/java/veneto/ui/Ui.java new file mode 100644 index 0000000000..fb79de3922 --- /dev/null +++ b/src/main/java/veneto/ui/Ui.java @@ -0,0 +1,134 @@ +package veneto.ui; + +import veneto.command.*; +import veneto.exceptions.VenetoException; +import veneto.exceptions.VenetoOperateException; +import veneto.exceptions.VenetoStorageException; +import veneto.parser.Parser; +import veneto.task.TaskList; + +import java.util.Arrays; +import java.util.Scanner; + +public class Ui { + /* Fields */ + public final static String EXIT_SUFFIX = "0"; + private final static String GREETS = "\nVeneto: \n"; + private final static String COMMANDS = + " toDo [TASK],\n" + + " deadline [TASK] /by [DEADLINE(YYYY-MM-DD)],\n" + + " event [TASK] /from [START_TIME(YYYY-MM-DD)] /to [END_TIME(YYYY-MM-DD)],\n" + + " mark [TASK_ID], " + "unmark [TASK_ID],\n" + + " find [KEYWORDS],\n" + + " list, " + "bye"; + private final static String LOGO = + " ___ ___ __________ ___ ___ __________ __________ __________\n" + + " \\ \\ / /| _______|| \\ | || _______||___ ___|| ____ |\n" + + " \\ \\ / / | |_______ | \\| || |_______ | | | | | |\n" + + " \\ \\/ / | _______|| || _______| | | | | | |\n" + + " \\ / | |_______ | |\\ || |_______ | | | |____| |\n" + + " \\__/ |__________||___| \\___||__________| |__| |__________| ...starts\n\n"; + private final static String INCORRECT_ERROR = GREETS + " ???\n"; + + /* Methods */ + /** + * shows which exception occurs and give hint to the user + * @param e the DanException + */ + public String showError(VenetoException e) { + if (e instanceof VenetoOperateException) { + return showOperationError(e); + } else if (e instanceof VenetoStorageException) { + return showStorageError(e); + } else if (e.getMessage().equals("Invalid Command")) { + return showInvalidCommandError(); + } else { + assert false : "Invalid Error"; + } + return INCORRECT_ERROR; + } + + private String showStorageError(VenetoException e) { + if (e.getMessage().equals("Storage File Destroyed")) { + return GREETS + " 没找到内存哦 现在重新创建一个!\n"; + } else { + assert false : "Invalid type of exception"; + } + return INCORRECT_ERROR; + } + + private String showOperationError(VenetoException e) { + switch (e.getMessage()) { + case "Unmarked": + return GREETS + " 这个没标记过哦!\n"; + case "Marked": + return GREETS + " 这个已经做完了哦!\n"; + case "Add": + return showInvalidCommandError(); + case "Not Found": + return GREETS + " 好像没有这样的任务要做哦\n"; + case "Duplicate": + return GREETS + " 已经记录过这个任务啦\n"; + default: + assert false : "Invalid type of exception"; + } + return INCORRECT_ERROR; + } + + private String showInvalidCommandError() { + return GREETS + " 输入格式不对!" + " 你可以跟我说:\n" + COMMANDS + "\n"; + } + + /** + * give responses to user after they call commands + * @param command the Command operated just now + * @param tasks the TaskList of Veneto + */ + public String afterCommand(Command command, TaskList tasks) { + switch (command.getType()) { + case "exit": + return goodbye(); + case "add": + return GREETS + " 记下来了:\n " + command + + "!\n 现在有" + tasks.size() + "项任务要做\n"; + case "list": + return GREETS + " 还有这些要做哦!\n" + tasks.toString(); + case "mark": + return GREETS + " 我这就帮您标记好!\n " + command + "\n"; + case "unmark": + return GREETS + " 啊 没关系 帮您擦掉标记了哦" + "\n " + command + "\n"; + case "delete": + return GREETS + " 好啦,帮你擦掉了一条任务哦:\n " + command + + "\n 现在还剩下" + tasks.size() + "项任务\n"; + case "find": + return GREETS + " 相关的任务都在这里了哦:\n" + command; + default: + assert false : "Invalid type of command"; + } + return "Parser error"; + } + + /** + * get input from user + * @return the command translated from user input + */ + public Command getCommand(String input) throws VenetoException { + return Parser.translateCommand(input); + } + + /** + * greets when Veneto runs + */ + public String hello() { + return LOGO + GREETS + + " Veneto为您服务哦\n" + + " 有什么可以要帮忙可以跟我说!\n"; + } + + /** + * says goodbye when session ends + */ + public String goodbye() { + return GREETS + " 拜拜啦 下次见\n" + EXIT_SUFFIX; + } +} diff --git a/src/main/resources/images/Rain.png b/src/main/resources/images/Rain.png new file mode 100755 index 0000000000..0e010f9304 Binary files /dev/null and b/src/main/resources/images/Rain.png differ diff --git a/src/main/resources/images/User.png b/src/main/resources/images/User.png new file mode 100644 index 0000000000..1f6d2221df Binary files /dev/null and b/src/main/resources/images/User.png differ diff --git a/src/main/resources/images/Veneto.png b/src/main/resources/images/Veneto.png new file mode 100644 index 0000000000..f3666999d6 Binary files /dev/null and b/src/main/resources/images/Veneto.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..59d16ac91a --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..842dbed3e9 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/veneto/VenetoTest.java b/src/test/java/veneto/VenetoTest.java new file mode 100644 index 0000000000..cb850a0688 --- /dev/null +++ b/src/test/java/veneto/VenetoTest.java @@ -0,0 +1,21 @@ +package veneto; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class VenetoTest { + + @Test + public void dummyTest(){ + assertEquals(2, 2); + } + + @Test + public void anotherDummyTest(){ + assertEquals(4, 4); + } + + + +} diff --git a/src/test/java/veneto/command/AddCommandTest.java b/src/test/java/veneto/command/AddCommandTest.java new file mode 100644 index 0000000000..b386a7d9d3 --- /dev/null +++ b/src/test/java/veneto/command/AddCommandTest.java @@ -0,0 +1,20 @@ +package veneto.command; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class AddCommandTest { + + @Test + void op() { + } + + @Test + void testToString() { + } + + @Test + void getType() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/command/DeleteCommandTest.java b/src/test/java/veneto/command/DeleteCommandTest.java new file mode 100644 index 0000000000..70d51fe23c --- /dev/null +++ b/src/test/java/veneto/command/DeleteCommandTest.java @@ -0,0 +1,20 @@ +package veneto.command; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DeleteCommandTest { + + @Test + void op() { + } + + @Test + void testToString() { + } + + @Test + void getType() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/command/FindCommandTest.java b/src/test/java/veneto/command/FindCommandTest.java new file mode 100644 index 0000000000..5cdb65a4bc --- /dev/null +++ b/src/test/java/veneto/command/FindCommandTest.java @@ -0,0 +1,20 @@ +package veneto.command; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FindCommandTest { + + @Test + void op() { + } + + @Test + void testToString() { + } + + @Test + void getType() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/command/MarkCommandTest.java b/src/test/java/veneto/command/MarkCommandTest.java new file mode 100644 index 0000000000..8f43cc7b2b --- /dev/null +++ b/src/test/java/veneto/command/MarkCommandTest.java @@ -0,0 +1,20 @@ +package veneto.command; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MarkCommandTest { + + @Test + void op() { + } + + @Test + void testToString() { + } + + @Test + void getType() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/parser/ParserTest.java b/src/test/java/veneto/parser/ParserTest.java new file mode 100644 index 0000000000..e2a4373d84 --- /dev/null +++ b/src/test/java/veneto/parser/ParserTest.java @@ -0,0 +1,12 @@ +package veneto.parser; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ParserTest { + + @Test + void translateCommand() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/storage/StorageTest.java b/src/test/java/veneto/storage/StorageTest.java new file mode 100644 index 0000000000..3e16b4cbbd --- /dev/null +++ b/src/test/java/veneto/storage/StorageTest.java @@ -0,0 +1,24 @@ +package veneto.storage; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class StorageTest { + + @Test + void init() { + } + + @Test + void load() { + } + + @Test + void addTask() { + } + + @Test + void checkChange() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/task/DeadlineTest.java b/src/test/java/veneto/task/DeadlineTest.java new file mode 100644 index 0000000000..dea00b4d0c --- /dev/null +++ b/src/test/java/veneto/task/DeadlineTest.java @@ -0,0 +1,16 @@ +package veneto.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DeadlineTest { + + @Test + void testToString() { + } + + @Test + void saveToString() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/task/EventTest.java b/src/test/java/veneto/task/EventTest.java new file mode 100644 index 0000000000..5e640c83b8 --- /dev/null +++ b/src/test/java/veneto/task/EventTest.java @@ -0,0 +1,40 @@ +package veneto.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class EventTest { + + @Test + void toString_toDoWithDescriptionOnly_unmarkedToDo() { + assertEquals( + "[E][ ] a (from: Sep 10 2019 to: Sep 11 2019)", + new Event("a", "2019-09-10", "2019-09-11").toString() + ); + } + + @Test + void toString_toDoWithDescriptionAndStatus_markedToDo() { + assertEquals( + "[E][V] a (from: Sep 10 2019 to: Sep 11 2019)", + new Event("a", 1, "2019-09-10", "2019-09-11").toString() + ); + } + + @Test + void saveToString_toDoWithDescriptionOnly_unmarkedToDo() { + assertEquals( + "event,a,0,2019-09-10,2019-09-11", + new Event("a", "2019-09-10", "2019-09-11").saveToString() + ); + } + + @Test + void saveToString_toDoWithDescriptionAndStatus_markedToDo() { + assertEquals( + "event,a,1,2019-09-10,2019-09-11", + new Event("a", 1, "2019-09-10", "2019-09-11").saveToString() + ); + } +} \ No newline at end of file diff --git a/src/test/java/veneto/task/TaskListTest.java b/src/test/java/veneto/task/TaskListTest.java new file mode 100644 index 0000000000..591084fc72 --- /dev/null +++ b/src/test/java/veneto/task/TaskListTest.java @@ -0,0 +1,12 @@ +package veneto.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TaskListTest { + + @Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/task/TaskTest.java b/src/test/java/veneto/task/TaskTest.java new file mode 100644 index 0000000000..e860554507 --- /dev/null +++ b/src/test/java/veneto/task/TaskTest.java @@ -0,0 +1,28 @@ +package veneto.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TaskTest { + + @Test + void mark() { + } + + @Test + void contains() { + } + + @Test + void testToString() { + } + + @Test + void saveToString() { + } + + @Test + void getStatusIcon() { + } +} \ No newline at end of file diff --git a/src/test/java/veneto/task/ToDoTest.java b/src/test/java/veneto/task/ToDoTest.java new file mode 100644 index 0000000000..8abb3c7a76 --- /dev/null +++ b/src/test/java/veneto/task/ToDoTest.java @@ -0,0 +1,28 @@ +package veneto.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ToDoTest { + + @Test + void toString_toDoWithDescriptionOnly_unmarkedToDo() { + assertEquals("[T][ ] a", new ToDo("a").toString()); + } + + @Test + void toString_toDoWithDescriptionAndStatus_markedToDo() { + assertEquals("[T][V] a", new ToDo("a", 1).toString()); + } + + @Test + void saveToString_toDoWithDescriptionOnly_unmarkedToDo() { + assertEquals("toDo,a,0", new ToDo("a").saveToString()); + } + + @Test + void saveToString_toDoWithDescriptionAndStatus_markedToDo() { + assertEquals("toDo,a,1", new ToDo("a", 1).saveToString()); + } +} \ No newline at end of file diff --git a/src/test/java/veneto/ui/UiTest.java b/src/test/java/veneto/ui/UiTest.java new file mode 100644 index 0000000000..4bf1094bcf --- /dev/null +++ b/src/test/java/veneto/ui/UiTest.java @@ -0,0 +1,23 @@ +package veneto.ui; + +import org.junit.jupiter.api.Test; + +class UiTest { + +// @Test +// void showError_unmarkedOperateException_writtenCorrectly() { +// assertEquals("\nVeneto: \n 这个没标记过哦!\n", Ui.showError(new DanOperateException("Unmarked"))); +// } + + @Test + void afterCommand() { + } + + @Test + void getCommand() { + } + + @Test + void goodbye() { + } +} \ No newline at end of file diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e7..2f08cad872 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,53 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| +Dan: + 我是小丹! + 有什么可以要帮忙可以跟我说! +Dan: + 新任务: + [T][ ] borrow book! + 现在有1项任务哦! + +Dan: + 新任务: + [D][ ] return book (by: sunday)! + 现在有2项任务哦! + +Dan: + 新任务: + [E][ ] project meeting (from: mon 2pm to: 4pm)! + 现在有3项任务哦! + +Dan: + 你还有些要做的事情呢 我看看有什么吧! + 1. [T][ ] borrow book + 2. [D][ ] return book (by: sunday) + 3. [E][ ] project meeting (from: mon 2pm to: 4pm) + +Dan: + 哟 做完啦?帮你标记好了! + [T][V] borrow book + +Dan: + 哟 做完啦?帮你标记好了! + [E][V] project meeting (from: mon 2pm to: 4pm) + +Dan: + 啊?没做完啊 是不小心手滑了么? + [E][ ] project meeting (from: mon 2pm to: 4pm) + +Dan: + 这个没标记过哦! + +Dan: + 你还有些要做的事情呢 我看看有什么吧! + 1. [T][V] borrow book + 2. [D][ ] return book (by: sunday) + 3. [E][ ] project meeting (from: mon 2pm to: 4pm) + +Dan: + 新任务: + [D][ ] do homework (by: no idea :-p)! + 现在有4项任务哦! + +Dan: + 拜拜啦!下次见! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb2..df1cee7300 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,11 @@ +todo borrow book +deadline return book /by 2019-08-10 +event project meeting /from 2020-05-09 /to 2020-05-10 +list +mark 1 +mark 3 +unmark 3 +unmark 2 +list +delete 3 +Bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 0873744649..ec29ac0e55 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,7 @@ IF ERRORLEVEL 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 +java -classpath ..\bin veneto.Veneto < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh old mode 100644 new mode 100755 index c9ec870033..5631e28fb2 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -20,7 +20,7 @@ then fi # 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 +java -classpath ../bin Dan < input.txt > ACTUAL.TXT # convert to UNIX format cp EXPECTED.TXT EXPECTED-UNIX.TXT