diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..e3f9ce9b1e --- /dev/null +++ b/build.gradle @@ -0,0 +1,39 @@ +plugins { + id 'java' + id 'application' + id 'com.github.johnrengelman.shadow' version '7.1.2' +} + +repositories { + mavenCentral() +} + +application { + mainClass.set("JavaX.Launcher") +} + +shadowJar { + archiveBaseName = "ip-Duke" + archiveClassifier = null +} + +repositories { + mavenCentral() +} + +dependencies { + 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' +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..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/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/JavaX/DialogBox.java b/src/main/java/JavaX/DialogBox.java new file mode 100644 index 0000000000..06f82ed9cf --- /dev/null +++ b/src/main/java/JavaX/DialogBox.java @@ -0,0 +1,57 @@ +package JavaX; + +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; + +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 s, Image i) { + return new DialogBox(s, i); + } + + public static DialogBox getLukaDialog(String s, Image i) { + var db = new DialogBox(s, i); + db.flip(); + return db; + } +} \ No newline at end of file diff --git a/src/main/java/JavaX/Launcher.java b/src/main/java/JavaX/Launcher.java new file mode 100644 index 0000000000..e607e6aa9a --- /dev/null +++ b/src/main/java/JavaX/Launcher.java @@ -0,0 +1,12 @@ +package JavaX; + +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + */ +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/JavaX/Luka.java b/src/main/java/JavaX/Luka.java new file mode 100644 index 0000000000..3a7dba5973 --- /dev/null +++ b/src/main/java/JavaX/Luka.java @@ -0,0 +1,42 @@ +package JavaX; + +import ui.CommandParser; +import command.Command; +import ui.TaskService; +import ui.ui; + +public class Luka { + private TaskService service; + private ui ui; + + public Luka() { + this.service = new TaskService(); + this.ui = new ui(); + } + + public static void main(String[] args) { + System.out.println("Hello!"); + } + + /** + * Generates a response for the user's chat message. + */ + public String getResponse(String input) { + CommandParser parser = new CommandParser(service, ui); + boolean isRunning = true; + while(isRunning){ + Command command = parser.parseCommand(input); + + if (command != null) { + command.execute(); + return command.toString(); + } else if (input.equals("help")) { + return "valid command: \n" + "adding task : todo, event, deadline\n" + + "listing task : list, find\n" + "else : mark, unmark, delete"; + } else { + return "I'm sorry, but I don't know what that means :-("; + } + } + return null; + } +} diff --git a/src/main/java/JavaX/Main.java b/src/main/java/JavaX/Main.java new file mode 100644 index 0000000000..c6df5497e4 --- /dev/null +++ b/src/main/java/JavaX/Main.java @@ -0,0 +1,31 @@ +package JavaX; + +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 Luka luka = new Luka(); + + @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().setLuka(luka); // inject the Duke instance + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/JavaX/MainWindow.java b/src/main/java/JavaX/MainWindow.java new file mode 100644 index 0000000000..33ec902789 --- /dev/null +++ b/src/main/java/JavaX/MainWindow.java @@ -0,0 +1,62 @@ +package JavaX; + +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 Luka luka; + + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/im.png")); + private Image lukaImage = new Image(this.getClass().getResourceAsStream("/images/im1.png")); + + @FXML + public void initialize() { + + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + + showWelcomMessage(); + } + + /** Injects the Duke instance */ + public void setLuka(Luka d) { + luka = d; + } + + private void showWelcomMessage() { + String welcomeMessage = "Hello! I'm Luka\n What can I do for you?"; + dialogContainer.getChildren().addAll( + DialogBox.getLukaDialog(welcomeMessage, lukaImage) + ); + } + + /** + * 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 = luka.getResponse(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getLukaDialog(response, lukaImage) + ); + userInput.clear(); + } +} \ No newline at end of file diff --git a/src/main/java/Luka.java b/src/main/java/Luka.java new file mode 100644 index 0000000000..ab199e0e3e --- /dev/null +++ b/src/main/java/Luka.java @@ -0,0 +1,20 @@ +import ui.CommandParser; +import ui.TaskService; +import ui.ui; + + +/** + * The main entry point of the robot + * With greeting and exiting functions + */ +public class Luka { + public static void main(String[] args) { + TaskService taskService = new TaskService(); + ui ui = new ui(); + CommandParser parser = new CommandParser(taskService, ui); + + ui.greet(); + parser.run(); + ui.exit(); + } +} \ No newline at end of file diff --git a/src/main/java/command/AddDeadlineCommand.java b/src/main/java/command/AddDeadlineCommand.java new file mode 100644 index 0000000000..3e7b54abbd --- /dev/null +++ b/src/main/java/command/AddDeadlineCommand.java @@ -0,0 +1,71 @@ +package command; + +import java.time.LocalDate; + +import model.Deadline; +import ui.DateTimeService; +import ui.TaskService; +import ui.ui; + +/** + * Add command to support user add a task with specific deadline + */ +public class AddDeadlineCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private DateTimeService dateTimeService; + private Deadline deadline; + + public AddDeadlineCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.dateTimeService = new DateTimeService(); + this.deadline = null; + } + + @Override + public void execute() { + if (!arguments.contains("/by")) { + ui.showError("Deadline requires '/by' parameter"); + return; + } + + String[] parts = arguments.split("/by", 2); + String description = parts[0].trim(); + String date = parts[1].trim(); + + if (description.isEmpty()) { + ui.showError("Deadline description is empty"); + return; + } + + String datetime = dateTimeService.outputDateTime(date); + deadline = new Deadline(description, datetime); + taskService.addTask(deadline); + ui.showTaskAdded(deadline, taskService.getTaskCount()); + } + + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + if (!arguments.contains("/by")) { + return "Deadline requires '/by' parameter"; + } + String[] parts = arguments.split("/by", 2); + String description = parts[0].trim(); + String date = parts[1].trim(); + + if (description.isEmpty()) { + return "Deadline description is empty"; + } + int taskCount = taskService.getTaskCount(); + return "Got it. I've added this task: \n" + deadline.toString() + "\n" + + "Now you have " + taskCount + " tasks in the list"; + } +} diff --git a/src/main/java/command/AddEventCommand.java b/src/main/java/command/AddEventCommand.java new file mode 100644 index 0000000000..f619e57491 --- /dev/null +++ b/src/main/java/command/AddEventCommand.java @@ -0,0 +1,77 @@ +package command; + +import java.time.LocalDateTime; + +import model.Event; +import ui.DateTimeService; +import ui.TaskService; +import ui.ui; + +/** + * Add a command to support user to add a Event Task + */ +public class AddEventCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private DateTimeService dateTimeService; + private Event event; + + public AddEventCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.dateTimeService = new DateTimeService(); + this.event = null; + } + + @Override + public void execute() { + if (!arguments.contains("/from") || !arguments.contains("/to")) { + ui.showError("Event requires '/from' and '/to' parameter"); + return; + } + + String[] parts = arguments.split(" /from | /to"); + + if (parts.length != 3) { + ui.showError("Invalid event format!"); + return; + } + + String description = parts[0]; + String from = parts[1]; + String to = parts[2]; + + if (description.isEmpty()) { + ui.showError("Description cannot be empty!"); + } + + event = new Event(description, from, to); + taskService.addTask(event); + ui.showTaskAdded(event, taskService.getTaskCount()); + } + + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + if (!arguments.contains("/from") || !arguments.contains("/to")) { + return "Event requires '/from' and '/to' parameter"; + } + + String[] parts = arguments.split("/from|/to"); + + if (parts.length != 3) { + return "Invalid event format!\n" + "requires description, starting time and ending time"; + } + + int taskCount = taskService.getTaskCount(); + + return "Got it. I've added this task: \n" + event.toString() + "\n" + + "Now you have " + taskCount + " tasks in the list"; + } +} diff --git a/src/main/java/command/AddTodoCommand.java b/src/main/java/command/AddTodoCommand.java new file mode 100644 index 0000000000..1601ae9d6b --- /dev/null +++ b/src/main/java/command/AddTodoCommand.java @@ -0,0 +1,48 @@ +package command; + +import model.Todo; +import ui.TaskService; +import ui.ui; + +/** + * Add command that support user to add a Todo Task + */ +public class AddTodoCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private Todo todo; + + public AddTodoCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.todo = null; + } + + @Override + public void execute() { + if (arguments.isEmpty()) { + ui.showError("The description of a todo cannot be empty"); + return; + } + todo = new Todo(arguments); + taskService.addTask(todo); + ui.showTaskAdded(todo, taskService.getTaskCount()); + } + + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + if (arguments.isEmpty()) { + return "The description of a todo cannot be empty"; + } + int taskCount = taskService.getTaskCount(); + return "Got it. I've added this task: \n" + todo.toString() + "\n" + + "Now you have " + taskCount + " tasks in the list"; + } +} diff --git a/src/main/java/command/Command.java b/src/main/java/command/Command.java new file mode 100644 index 0000000000..54b5bff5b0 --- /dev/null +++ b/src/main/java/command/Command.java @@ -0,0 +1,6 @@ +package command; + +public interface Command { + void execute(); + boolean isExit(); +} diff --git a/src/main/java/command/DeleteCommand.java b/src/main/java/command/DeleteCommand.java new file mode 100644 index 0000000000..c4a00c0890 --- /dev/null +++ b/src/main/java/command/DeleteCommand.java @@ -0,0 +1,52 @@ +package command; + +import model.Task; +import ui.TaskService; +import ui.ui; + +/** + * Add command that support user to delete Task added form tasklist + */ +public class DeleteCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private Task task; + private int taskId; + + public DeleteCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.task = null; + this.taskId = 0; + } + + @Override + public void execute() { + try{ + taskId = Integer.parseInt(arguments) - 1; + task = taskService.getTask(taskId); + taskService.removeTask(taskId); + ui.showTaskDeleted(task, taskService.getTaskCount()); + } catch (Exception e) { + ui.showError("Invalid task number!"); + } + } + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + try{ + int taskCount = taskService.getTaskCount(); + return "Noted. I've removed this task: \n" + task.toString() + "\n" + + "Now you have " + taskCount + " tasks in the list"; + } catch (Exception e) { + return "Invalid task number!"; + } + } + +} diff --git a/src/main/java/command/ExitCommand.java b/src/main/java/command/ExitCommand.java new file mode 100644 index 0000000000..b5039a4cfc --- /dev/null +++ b/src/main/java/command/ExitCommand.java @@ -0,0 +1,29 @@ +package command; + +import ui.ui; + +/** + * Command to terminate running process + */ +public class ExitCommand implements Command { + private ui ui; + + public ExitCommand(ui ui) { + this.ui = ui; + } + + @Override + public void execute() { + + } + + @Override + public boolean isExit(){ + return true; + } + + @Override + public String toString() { + return "Bye.Hope to see you again soon!"; + } +} diff --git a/src/main/java/command/FindCommand.java b/src/main/java/command/FindCommand.java new file mode 100644 index 0000000000..7812481ed6 --- /dev/null +++ b/src/main/java/command/FindCommand.java @@ -0,0 +1,40 @@ +package command; + +import ui.TaskService; +import ui.ui; + +/** + * Add command support finding task with a description + */ +public class FindCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + + public FindCommand(TaskService taskService, ui ui, String arguments){ + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + } + + @Override + public void execute() { + if (arguments.isEmpty()){ + ui.showError("Please enter a task"); + } + ui.showMatchingTask(taskService.searchTask(arguments)); + } + + @Override + public boolean isExit() { + return false; + } + + @Override + public String toString() { + if (arguments.isEmpty()){ + return "Please enter a task"; + } + return ui.showMatchingTaskInString(taskService.searchTask(arguments)); + } +} diff --git a/src/main/java/command/ListCommand.java b/src/main/java/command/ListCommand.java new file mode 100644 index 0000000000..e2768306e3 --- /dev/null +++ b/src/main/java/command/ListCommand.java @@ -0,0 +1,32 @@ +package command; + +import ui.TaskService; +import ui.ui; + +/** + * Add command to support user checking TaskList + */ +public class ListCommand implements Command { + private TaskService taskService; + private ui ui; + + public ListCommand(TaskService taskService, ui ui) { + this.taskService = taskService; + this.ui = ui; + } + + @Override + public void execute() { + ui.showListTask(taskService.getTasks()); + } + + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + return ui.showListTaskInString(taskService.getTasks()); + } +} diff --git a/src/main/java/command/MarkCommand.java b/src/main/java/command/MarkCommand.java new file mode 100644 index 0000000000..e24150db50 --- /dev/null +++ b/src/main/java/command/MarkCommand.java @@ -0,0 +1,47 @@ +package command; + +import ui.TaskService; +import ui.ui; + +/** + * Add command for user to mark Task in a specific index as done + */ +public class MarkCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private int taskId; + + public MarkCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.taskId = 0; + } + + @Override + public void execute() { + try{ + taskId = Integer.parseInt(arguments) - 1; + taskService.markTask(taskId); + ui.showMassage("Nice ! I've marked this task as done: \n" + + taskService.getTask(taskId)); + } catch (Exception e) { + ui.showError("Invalid task number!"); + } + } + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + try{ + return "Nice ! I've marked this task as done: \n" + + taskService.getTask(taskId).toString(); + } catch (Exception e) { + return "Invalid task number!"; + } + } +} diff --git a/src/main/java/command/UnmarkCommand.java b/src/main/java/command/UnmarkCommand.java new file mode 100644 index 0000000000..ada97b5e44 --- /dev/null +++ b/src/main/java/command/UnmarkCommand.java @@ -0,0 +1,47 @@ +package command; + +import ui.TaskService; +import ui.ui; + +/** + * Add command for user to mark Task in a specific index as not done + */ +public class UnmarkCommand implements Command { + private TaskService taskService; + private ui ui; + private String arguments; + private int taskId; + + public UnmarkCommand(TaskService taskService, ui ui, String arguments) { + this.taskService = taskService; + this.ui = ui; + this.arguments = arguments; + this.taskId = 0; + } + + @Override + public void execute() { + try{ + taskId = Integer.parseInt(arguments) - 1; + taskService.unmarkTasks(taskId); + ui.showMassage("OK, I've marked this task as not done yet: \n" + + taskService.getTask(taskId)); + } catch (Exception e) { + ui.showError("Invalid task number!"); + } + } + @Override + public boolean isExit(){ + return false; + } + + @Override + public String toString() { + try{ + return "OK, I've marked this task as not done yet: \n" + + taskService.getTask(taskId).toString(); + } catch (Exception e) { + return "Invalid task number!"; + } + } +} diff --git a/src/main/java/model/Deadline.java b/src/main/java/model/Deadline.java new file mode 100644 index 0000000000..5b700b53db --- /dev/null +++ b/src/main/java/model/Deadline.java @@ -0,0 +1,39 @@ +package model; + +/** + * Represents a Deadline Task + * Contains a date as deadline + */ +public class Deadline extends Task{ + private String deadline; + + /** + * Constructs a Deadline Task + * + * @param description A short description of the task + * @param deadline The date of task deadline + */ + public Deadline(String description, String deadline) { + super(description); + this.deadline = deadline; + } + + /** + * Returns the type of task + * + * @return type represent as string "D" + */ + @Override + public String getType() { + return "D"; + } + + public String getDeadline() { + return deadline; + } + + @Override + public String toString() { + return "[" + getType() + "]" + getStatus() + " " + description + " (by:" + deadline + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/model/Event.java b/src/main/java/model/Event.java new file mode 100644 index 0000000000..a4e0232dbc --- /dev/null +++ b/src/main/java/model/Event.java @@ -0,0 +1,61 @@ +package model; + +import java.time.format.DateTimeFormatter; +import java.time.LocalDate; +import java.time.LocalDateTime; + +/** + * Represents a Event Task + * Each task contains both starting time and ending time + */ +public class Event extends Task{ + private String start; + private String end; + + /** + * Constructs a Event task + * + * @param description A short description of the task + * @param start The time task start + * @param end The time task end + */ + public Event(String description, String start, String end) { + super(description); + this.start = start; + this.end = end; + } + + /** + * Returns the type of event task + * + * @return type represent as string "E" + */ + @Override + public String getType() { + return "E"; + } + + /** + * Returns starting time of task + * + * @return the starting time + */ + public String getStart() { + return start; + } + + + /** + * Returns ending time of task + * + * @return the ending time + */ + public String getEnd() { + return end; + } + + @Override + public String toString() { + return "[" + getType() + "]" + getStatus() + " " + description + " (from: " + start + " to:" + end + ")"; + } +} diff --git a/src/main/java/model/Task.java b/src/main/java/model/Task.java new file mode 100644 index 0000000000..792186a1b6 --- /dev/null +++ b/src/main/java/model/Task.java @@ -0,0 +1,50 @@ +package model; + +/** + * Represents a task made by user + * Each task contains a description of the task + * The status of task + */ +public abstract class Task { + public String description; + private boolean isDone; + + /** + * Constructs a Task + * + * @param description A short description of the task + */ + public Task(String description) { + this.description = description; + this.isDone = false; + } + + /** + * Mark the status of task as done + */ + public void markAsDone(){ + this.isDone = true; + } + + /** + * Mark the status of task as not done + */ + public void unmark(){ + this.isDone = false; + } + + /** + * Returns the status of the task, representing as string + * + * @return the status + */ + public String getStatus(){ + return (isDone ? "[x]" : "[ ]"); + } + public abstract String getType(); + + @Override + public String toString() { + return "[" + getType() + "]" + getStatus() + " " + description; + } +} diff --git a/src/main/java/model/Todo.java b/src/main/java/model/Todo.java new file mode 100644 index 0000000000..2355c03fbb --- /dev/null +++ b/src/main/java/model/Todo.java @@ -0,0 +1,27 @@ +package model; + + +/** + * Represents a Todo Task + */ +public class Todo extends Task { + + /** + * Constructs a Todo task + * + * @param description A short description of the task + */ + public Todo(String description){ + super(description); + } + + /** + * Returns the type of todo task + * + * @return type represent as string "T" + */ + @Override + public String getType() { + return "T"; + } +} diff --git a/src/main/java/ui/CommandParser.java b/src/main/java/ui/CommandParser.java new file mode 100644 index 0000000000..eea39cec1a --- /dev/null +++ b/src/main/java/ui/CommandParser.java @@ -0,0 +1,71 @@ +package ui; + +import command.AddDeadlineCommand; +import command.AddEventCommand; +import command.AddTodoCommand; +import command.Command; +import command.DeleteCommand; +import command.ExitCommand; +import command.FindCommand; +import command.ListCommand; +import command.MarkCommand; +import command.UnmarkCommand; + + +public class CommandParser { + private TaskService service; + private ui ui; + + public CommandParser(TaskService service, ui ui) { + this.service = service; + this.ui = ui; + } + + public void run() { + boolean isRunning = true; + while(isRunning){ + String input = ui.readcommand(); + Command command = parseCommand(input); + + if (command != null) { + command.execute(); + if (command.isExit()) { + isRunning = false; + } + } else { + ui.showError("I'm sorry, but I don't know what that means :-("); + } + } + } + + public Command parseCommand(String input){ + String[] parts = input.split(" ", 2); + String commandWord = parts[0]; + String arguments = ""; + if (parts.length > 1){ + arguments = parts[1]; + } + switch (commandWord) { + case "bye": + return new ExitCommand(ui); + case "list": + return new ListCommand(service, ui); + case "mark": + return new MarkCommand(service, ui, arguments); + case "unmark": + return new UnmarkCommand(service, ui, arguments); + case "todo": + return new AddTodoCommand(service, ui, arguments); + case "deadline": + return new AddDeadlineCommand(service, ui, arguments); + case "event": + return new AddEventCommand(service, ui, arguments); + case "delete": + return new DeleteCommand(service, ui, arguments); + case "find": + return new FindCommand(service, ui, arguments); + default: + return null; + } + } +} diff --git a/src/main/java/ui/DateTimeService.java b/src/main/java/ui/DateTimeService.java new file mode 100644 index 0000000000..0cc2aea216 --- /dev/null +++ b/src/main/java/ui/DateTimeService.java @@ -0,0 +1,56 @@ +package ui; + +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.List; + +/** + * Support reading some specific string as date and time + */ +public class DateTimeService { + private LocalDate currentdate = LocalDate.now(); + private static final List SUPPORTED_FORMATTERS = Arrays.asList( + DateTimeFormatter.ofPattern("yyyy-MM-dd"), + DateTimeFormatter.ofPattern("dd/MM/yyyy"), + DateTimeFormatter.ofPattern("d/MM/yyyy"), + + DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"), + DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"), + DateTimeFormatter.ofPattern("d/MM/yyyy HHmm") + ); + + private static final DateTimeFormatter OUTPUT_DATE = DateTimeFormatter.ofPattern("MMM dd, yyyy"); + private static final DateTimeFormatter OUTPUT_DATETIME = DateTimeFormatter.ofPattern("hh:mm, MMM dd, yyyy"); + + /** + * Read input String as date and time and print in different format + * + * @param input The string of date and time to be read + * @return date and time red + */ + public static String outputDateTime(String input) { + String trimmed = input.trim(); + + for (DateTimeFormatter formatter : SUPPORTED_FORMATTERS) { + + try { + boolean hasTime = formatter.toString().contains("HHmm"); + + if (hasTime) { + LocalDateTime dateTime = LocalDateTime.parse(trimmed, formatter); + return dateTime.format(OUTPUT_DATETIME); + } else { + LocalDate date = LocalDate.parse(trimmed, formatter); + return date.format(OUTPUT_DATE); + } + } catch (DateTimeParseException e) { + continue; + } + } + return input; + } + +} diff --git a/src/main/java/ui/TaskService.java b/src/main/java/ui/TaskService.java new file mode 100644 index 0000000000..2432c2a18b --- /dev/null +++ b/src/main/java/ui/TaskService.java @@ -0,0 +1,96 @@ +package ui; + +import java.util.ArrayList; +import java.util.List; + +import model.Task; + +/** + * Some operations dealing with task + */ +public class TaskService { + private List tasks = new ArrayList<>(); + private int counter = 0; + + /** + * Get all the existing tasks + * + * @return the tasks + */ + public List getTasks() { + return tasks; + } + + /** + * Returns task in a index + * + * @param index index of task in tasklist + * @return the task + */ + public Task getTask(int index) { + return tasks.get(index); + } + + /** + * Get all tasks with its description contains given taskName + * + * @param taskName The name target task contains + * @return all matched tasks + */ + public List searchTask(String taskName) { + List selectedTasks = new ArrayList<>(); + for (Task task : tasks) { + if (task.description.contains(taskName)) { + selectedTasks.add(task); + } + } + return selectedTasks; + } + + /** + * Add a task to tasklist + * + * @param task The task added + */ + public void addTask(Task task) { + tasks.add(task); + counter++; + } + + /** + * Remove a task in tasklist + * + * @param index The index of task in tasklist to remove + */ + public void removeTask(int index) { + tasks.remove(index); + counter--; + } + + /** + * Get total number of task existed + * + * @return the current number of tasks + */ + public int getTaskCount() { + return counter; + } + + /** + * Mark specific task as done + * + * @param index The index of task to be marked + */ + public void markTask(int index) { + tasks.get(index).markAsDone(); + } + + /** + * Unmark specific task that been marked + * + * @param index The index of task to be unmark + */ + public void unmarkTasks(int index) { + tasks.get(index).unmark(); + } +} diff --git a/src/main/java/ui/ui.java b/src/main/java/ui/ui.java new file mode 100644 index 0000000000..7f7f9fcc20 --- /dev/null +++ b/src/main/java/ui/ui.java @@ -0,0 +1,130 @@ +package ui; + +import java.util.Scanner; + +import model.Task; + +/** + * Some messages to print as output + */ +public class ui { + private static Scanner sc = new Scanner(System.in); + + /** + * Show greeting message + */ + public void greet() { + System.out.println("Hello! I'm Luka\n" + + "What can I do for you?"); + } + + /** + * Show goodbye message + */ + public static void exit() { + System.out.println("Bye.Hope to see you again soon!"); + sc.close(); + } + + /** + * Show command message + * + * @return the command + */ + public String readcommand() { + return sc.nextLine(); + } + + /** + * Show error message + * + * @param massage The error exist + */ + public void showError(String massage) { + System.out.println("OPPS!! " + massage); + } + + /** + * Show output message + * + * @param massage The massage to print + */ + public void showMassage(String massage) { + System.out.println(massage); + } + + /** + * Show task added message + * + * @param task The task added + * @param totalTasks The current number of task exist + */ + public void showTaskAdded(Task task, int totalTasks) { + System.out.println("Got it. I've added this task: \n" + task); + System.out.println("Now you have " + totalTasks + " tasks in the list"); + } + + /** + * Show task delete message + * + * @param task The task deleted + * @param totalTasks The current number of task exist + */ + public void showTaskDeleted(Task task, int totalTasks) { + System.out.println("Noted. I've removed this task: \n" + task); + System.out.println("Now you have " + totalTasks + " tasks in the list"); + } + + /** + * Show list of task message + * + * @param tasks All current tasks in list + */ + public void showListTask(java.util.List tasks) { + if (tasks.isEmpty()) { + System.out.println("No tasks found"); + } + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); + } + } + + public void showMatchingTask(java.util.List tasks){ + if(tasks.isEmpty()){ + System.out.println("No tasks found"); + } + System.out.println("Here are the matching tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); + } + } + + public String showListTaskInString(java.util.List tasks) { + if (tasks.isEmpty()) { + return "No tasks found"; + } + + StringBuilder result = new StringBuilder(); + result.append("Here are the matching tasks in your list:\n"); + + for (int i = 0; i < tasks.size(); i++) { + result.append((i + 1) + "." + tasks.get(i) + "\n"); + } + return result.toString(); + } + + public String showMatchingTaskInString(java.util.List tasks){ + if(tasks.isEmpty()){ + return "No tasks found"; + } + + StringBuilder result = new StringBuilder(); + result.append("Here are the matching tasks in your list:\n"); + + for (int i = 0; i < tasks.size(); i++) { + result.append((i + 1) + "." + tasks.get(i) + "\n"); + } + return result.toString(); + } +} diff --git a/src/main/resources/images/im.png b/src/main/resources/images/im.png new file mode 100644 index 0000000000..559ff3a4ba Binary files /dev/null and b/src/main/resources/images/im.png differ diff --git a/src/main/resources/images/im1.png b/src/main/resources/images/im1.png new file mode 100644 index 0000000000..ca97e8235d Binary files /dev/null and b/src/main/resources/images/im1.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..a709c891a0 --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 0000000000..e9b7f24277 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh old mode 100644 new mode 100755