diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..491135b6ec --- /dev/null +++ b/build.gradle @@ -0,0 +1,56 @@ +plugins { + id 'java' + id 'application' + id 'com.github.johnrengelman.shadow' version '5.1.0' +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0' + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0' + + String javaFxVersion = '11' + + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "pony.javafx.Launcher" +} + +shadowJar { + archiveBaseName = "pony" + archiveClassifier = "" +} + +run{ + standardInput = System.in +} diff --git a/data/pony.txt b/data/pony.txt new file mode 100644 index 0000000000..754cd90ed6 --- /dev/null +++ b/data/pony.txt @@ -0,0 +1,2 @@ +T | 0 | watch movie +D | 1 | return book | 2022-06-20 16:00 diff --git a/docs/README.md b/docs/README.md index 8077118ebe..5f654fd1b4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,230 @@ + +@@author ciaoosuuu-reused + +Reused from [kshan29](https://kshan29.github.io/ip/#quick-start) + +With minor modifications # User Guide -## Features +Pony is a desktop app for managing tasks, optimized for use via a Command Line Interface (CLI) +while still having the benefits of a Graphical User Interface (GUI). Pony provides flexibility by +allowing the tracking of various types of tasks including events and deadlines. + +- [Quick start](https://ciaoosuuu.github.io/ip/#quick-start) +- [Features](https://ciaoosuuu.github.io/ip/#features) +- [Usage](https://ciaoosuuu.github.io/ip/#usage) + - [Adding a todo: `todo`](https://ciaoosuuu.github.io/ip/#adding-a-todo-todo) + - [Adding a deadline: `deadline`](https://ciaoosuuu.github.io/ip/#adding-a-deadline-deadline) + - [Adding a event: `event`](https://ciaoosuuu.github.io/ip/#adding-a-event-event) + - [Listing all tasks: `list`](https://ciaoosuuu.github.io/ip/#listing-all-tasks-list) + - [Deleting a task: `delete`](https://ciaoosuuu.github.io/ip/#deleting-a-task-delete) + - [Finding specific tasks: `find`](https://ciaoosuuu.github.io/ip/#finding-specific-tasks-find) + - [Marking a task: `mark`](https://ciaoosuuu.github.io/ip/#marking-a-task-mark) + - [Unmarking a task: `unmark`](https://ciaoosuuu.github.io/ip/#unmarking-a-task-unmark) + - [Exiting the program: `bye`](https://ciaoosuuu.github.io/ip/#exiting-the-program-bye) +- [Command summary](https://ciaoosuuu.github.io/ip/#command-summary) + +## Quick Start + +1. Ensure you have Java `11` or above installed in your Computer. +2. Download the latest `Pony.jar` file from [here](https://github.com/ciaoosuuu/ip/releases/tag/v1.0). +3. Locate the file and double-click the file to start the app. + +![Duke UI display](Ui.png "Pony UI") + +5. Type the command in the command box and press Send to execute it. + +6. Refer to the [Features](https://ciaoosuuu.github.io/ip/#features) below for details of each command. + + -### Feature-ABC +## Features -Description of the feature. +### Task management +Add tasks by indicating type of task to add or delete tasks by indicating index of task to delete. -### Feature-XYZ +### Track task status +Mark or unmark task to track completed and incomplete tasks. -Description of the feature. +### View tasks +Display all tasks in a list or filter tasks using a keyword. ## Usage +### Adding a task +You can add either a todo, event (occurs on a specified date) or deadline (due by a specified date) as tasks. + +#### Adding a todo: `todo` +Adds a todo. + +Format: `todo TASK_DESCRIPTION` + +Examples: +- `todo watch movie` + +Expected outcome: + +Todo added to task list when valid description given. + +``` +Got it my friend. I've added this task: +[T][ ] TASK_DESCRIPTION +Now you have X tasks in the list. +``` + +#### Adding a deadline: `deadline` +Adds a deadline. + +Format: `deadline TASK_DESCRIPTION /by YYYY-MM-DD HH:mm` + +Examples: + +- `deadline return book /by 2022-09-20 16:00` + +Expected outcome: + +Deadline added to task list when valid description and date given. + +``` +Got it my friend. I've added this task: +[D][ ] TASK_DESCRIPTION (by: MMM DD YYYY HH:mm) +Now you have X tasks in the list. +``` +#### Adding an event: `event` +Adds an event. + +Format: `event TASK_DESCRIPTION /at YYYY-MM-DD HH:mm` + +Examples: + +- `event NUS Supernova /at 2022-09-22 16:00` + +Expected outcome: + +Event added to task list when valid description and date given. + +``` +Got it. I've added this task: +[E][ ] TASK_DESCRIPTION (at: MMM DD YYYY HH:mm) +Now you have X tasks in the list. +``` + +### Listing all tasks: `list` +Displays a list of tasks added. + +Format: `list` + +Expected outcome: + +``` +Let's look at the tasks in your list: +1. ... +2. ... +``` + +### Deleting a task: `delete` +Deletes the task from task list. + +Format: `delete INDEX` +- Deletes the task at the specified `INDEX`. +- `INDEX` refers to the index number shown after using `list`. + +Examples: + +- `delete 1` deletes the 1st task in `list` if it exists. + +Expected outcome: + +Deletes a task when valid index is given. + +``` +Alright my friend, I've removed this task: +[X][ ] TASK_DESCRIPTION +Now you have X tasks in the list. +``` + +### Finding specific tasks: `find` +Display tasks where the description matches given keywords. + +Format: `find KEYWORD` +- Search is not case-sensitive. e.g. `homework` will still match with `Homework` +- Partial keywords cannot be used. + +Examples: +- `find Homework` returns a list of all tasks with description containing the keyword `homework` + +Expected outcome: + +``` +Alright, Here's what pony found! +1. ... +2. ... +``` + +### Marking a task: `mark` +Marks a task as completed. + +Format: `mark INDEX` +- Marks the task at the specified `INDEX` as completed. +- `INDEX` refers to the index number shown after using `list`. -### `Keyword` - Describe action +Examples: +- `mark 1` marks the first task in `list` if it exists. -Describe the action and its outcome. +Expected outcome: -Example of usage: +Marks a task as completed when valid index is given. -`keyword (optional arguments)` +``` +Well done, my friend! I've marked this task as done: +[X][X] TASK_DESCRIPTION +``` + +### Unmarking a task: `unmark` +Marks a task as incomplete. + +Format: `unmark INDEX` +- Marks the task at the specified `INDEX` as incomplete. +- `INDEX` refers to the index number shown after using `list`. + +Examples: +- `unmark 1` marks the first task in `list` as incomplete if it exists. Expected outcome: -Description of the outcome. +Marks a task as incomplete when valid index is given. ``` -expected output +My friend, I've marked this task as not done yet: +[X][ ] TASK_DESCRIPTION ``` + +### Exiting the program: `bye` +Exits the program. + +Format: `bye` + +Expected outcome: + +Prints goodbye message and exits the program. + +``` +Bye friend. Hope to see you again soon! +... +``` + + +## Command summary + +| **Action** | **Format, Examples** | +|------------|-------------------------------------------------------------------------------------------------------| +| todo | `todo TASK_DESCRIPTION`
e.g. `todo watch movie` | +| deadline | `deadline TASK_DESCRIPTION /by YYYY-MM-DD HH:mm`
e.g. `deadline Assignment /by 2022-09-15 16:00` | +| event | `event TASK_DESCRIPTION /at YYYY-MM-DD`
e.g. `event NUS Supernova /at 2022-09-01 18:00` | +| list | `list` | +| delete | `delete INDEX`
e.g. `delete 1` | +| find | `find KEYWORD`
e.g. `find test` | +| mark | `mark INDEX`
e.g. `mark 3` | +| unmark | `unmark INDEX`
e.g. `unmark 2` | +| bye | `bye` | + +@@author \ No newline at end of file diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..e9437b963f Binary files /dev/null and b/docs/Ui.png differ diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..f3d88b1c2f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..b7c8c5dbf5 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000000..2fe81a7d95 --- /dev/null +++ b/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..62bd9b9cce --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,103 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/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/pony/Parser.java b/src/main/java/pony/Parser.java new file mode 100644 index 0000000000..8660920081 --- /dev/null +++ b/src/main/java/pony/Parser.java @@ -0,0 +1,149 @@ +package pony; + +import pony.command.AddDeadlineCommand; +import pony.command.AddEventCommand; +import pony.command.AddToDoCommand; +import pony.command.Command; +import pony.command.DeleteCommand; +import pony.command.ExitCommand; +import pony.command.FindCommand; +import pony.command.InvalidCommand; +import pony.command.ListCommand; +import pony.command.MarkCommand; +import pony.command.UnmarkCommand; + + +/** + * A parser class to parse command inputs. + */ +public class Parser { + + /** + * Parse the full command given by user. + * + * @param fullCommand Command given by user. + * @return A command object depends on the user input. + */ + public static Command parseCommand(String fullCommand) { + String[] strArr = fullCommand.split(" ",2); + String commandType = strArr[0]; + String commandDetails; + + if (strArr.length != 2) { + commandDetails = null; + } else { + commandDetails = strArr[1]; + } + + switch (commandType) { + case "bye": + return new ExitCommand(); + case "list": + return new ListCommand(); + case "mark": + return new MarkCommand(commandDetails); + case "unmark": + return new UnmarkCommand(commandDetails); + case "delete": + return new DeleteCommand(commandDetails); + case "todo": + return new AddToDoCommand(commandDetails); + case "deadline": + return new AddDeadlineCommand(commandDetails); + case "event": + return new AddEventCommand(commandDetails); + case "find": + return new FindCommand(commandDetails); + default: + return new InvalidCommand(); + } + } + + /** + * Parse a command expecting a task index input. + * + * @param commandDetails Command given by user. + * @return Index of the task specified by the user command. + * @throws PonyException User did not provide any details. + * @throws NumberFormatException User did not provide an integer for task index. + */ + public static int parseTaskIndex(String commandDetails) throws PonyException, NumberFormatException{ + if (commandDetails == null) { + throw new PonyException(":( OOPS!!! Please provide the details!!"); + } else { + int taskIndex = Integer.parseInt(commandDetails); + return taskIndex; + } + } + + /** + * Parse a Todo command. + * + * @param commandDetails Command given by user. + * @return Details of the task. + * @throws PonyException User did not provide any details. + */ + public static String parseTodoDetails(String commandDetails) throws PonyException { + if (commandDetails == null || commandDetails.isBlank()) { + throw new PonyException(":( OOPS!!! Please provide the details!!"); + } else { + return commandDetails; + } + } + + /** + * Parse a Deadline command. + * + * @param commandDetails Command given by user. + * @param format Format the command should follow. + * @return Details of the task. + * @throws PonyException User did not provide any details or not following the format. + */ + public static String[] parseDeadlineDetails(String commandDetails, String format) throws PonyException { + if (commandDetails == null) { + throw new PonyException(":( OOPS!!! Please provide the details!!"); + } else { + String[] taskInfoArr = commandDetails.split(" /by ", 2); + if (taskInfoArr.length != 2) { + throw new PonyException(":( OOPS!!! Please provide the details in the following format: " + format); + } + return taskInfoArr; + } + } + + /** + * Parse an Event command. + * + * @param commandDetails Command given by user. + * @param format Format the command should follow. + * @return Details of the task. + * @throws PonyException User did not provide any detail or not following the format. + */ + public static String[] parseEventDetails(String commandDetails, String format) throws PonyException { + if (commandDetails == null) { + throw new PonyException(":( OOPS!!! Please provide the details!!"); + } else { + String[] taskInfoArr = commandDetails.split(" /at ", 2); + if (taskInfoArr.length != 2) { + throw new PonyException(":( OOPS!!! Please provide the details in the following format: " + format); + } + return taskInfoArr; + } + } + + /** + * Parse a Find command. + * + * @param commandDetails Command given by user. + * @return Tasks found according to keyword. + * @throws PonyException User did not provide any detail. + */ + public static String parseFindDetails(String commandDetails) throws PonyException { + if (commandDetails == null) { + throw new PonyException(":( OOPS!!! Please provide the details!!"); + } else { + return commandDetails; + } + } + +} diff --git a/src/main/java/pony/Pony.java b/src/main/java/pony/Pony.java new file mode 100644 index 0000000000..2b9a8d5966 --- /dev/null +++ b/src/main/java/pony/Pony.java @@ -0,0 +1,37 @@ +package pony; + +import pony.command.Command; + +/** + * Pony contains the main logic for the task management program. + */ +public class Pony { + + private Storage storage; + private TaskList tasks; + private Ui ui; + + /** + * Constructor for Pony. + * @param filePath File path of the memory file. + */ + public Pony(String filePath) { + assert filePath != null : "File path should not be empty"; + ui = new Ui(); + storage = new Storage(filePath); + tasks = new TaskList(storage.loadTaskList()); + } + + /** + * Executes a command from a string and return the response. + * + * @param command Command detail input by user. + * @return Response from the program. + */ + public String runCommand(String command) { + Command c = Parser.parseCommand(command); + String message = c.execute(tasks, storage, ui); + return message; + } + +} \ No newline at end of file diff --git a/src/main/java/pony/PonyException.java b/src/main/java/pony/PonyException.java new file mode 100644 index 0000000000..a82e9aac02 --- /dev/null +++ b/src/main/java/pony/PonyException.java @@ -0,0 +1,15 @@ +package pony; + +import java.lang.RuntimeException; +public class PonyException extends RuntimeException { + + /** + * Constructor for PonyException. + * + * @param message Message to be displayed by the exception is thrown. + */ + public PonyException(String message) { + super(message); + } + +} diff --git a/src/main/java/pony/Storage.java b/src/main/java/pony/Storage.java new file mode 100644 index 0000000000..df05d27782 --- /dev/null +++ b/src/main/java/pony/Storage.java @@ -0,0 +1,115 @@ +package pony; + +import pony.task.Deadline; +import pony.task.Event; +import pony.task.Task; +import pony.task.ToDo; + +import java.io.File; +import java.io.IOException; +import java.io.FileWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/** + * Storage handles all the interaction with the memory. + */ +public class Storage { + private Path taskListPath = null; + private String fileName; + + /** + * Constructor for Storage. + * @param fileName Description of the path to the memory file. + */ + public Storage(String fileName) { + this.fileName = fileName; + this.taskListPath = Paths.get(fileName); + } + + /** + * Loads the list of tasks loaded from memory into the program. + * + * @return An ArrayList of Tasks. + */ + public ArrayList loadTaskList() { + // Check if file exist + ArrayList tasks = new ArrayList<>(); + System.out.println("does the file exist?: " + Files.exists(taskListPath)); + if (Files.exists(taskListPath)) { + try { + List taskList = Files.readAllLines(taskListPath); + for (int i = 0; i < taskList.size(); i++) { + String taskDetail = taskList.get(i); + processTaskDetail(taskDetail, tasks); + } + } catch (IOException | SecurityException e) { + System.out.println(e.getMessage()); + } + } else { + try { + File directory = new File("./data"); + if (!directory.exists()) { + directory.mkdir(); + } + Files.createFile(taskListPath); + System.out.println("I created a file!"); + } catch (IOException | SecurityException e) { + System.out.println("File creating error"); + System.out.println(e.getMessage()); + } + } + return tasks; + } + + /** + * Process task details and turn it into a task. + * + * @param taskDetail Task details. + * @param tasks An ArrayList to store all the tasks. + */ + private void processTaskDetail(String taskDetail, ArrayList tasks) { + String[] strArr = taskDetail.split(" \\Q|\\E ", 4); + Task newTask = null; + switch (strArr[0]) { + case "T": + newTask = new ToDo(strArr[2]); + tasks.add(newTask); + break; + case "D": + newTask = new Deadline(strArr[2], strArr[3]); + tasks.add(newTask); + break; + case "E": + newTask = new Event(strArr[2], strArr[3]); + tasks.add(newTask); + break; + } + if (strArr[1].equals("1")) { + newTask.markAsDone(); + } + } + + /** + * Updates the memory with an updated task list. + * @param tasks Task list with updated details. + */ + public void updateDisk(TaskList tasks) { + String data = ""; + ArrayList taskArr = tasks.getAllTasks(); + for (Task task : taskArr) { + data += task.saveToDisk(); + } + try { + FileWriter fw = new FileWriter(this.fileName, false); + fw.write(data); + fw.close(); + } catch (IOException e) { + System.out.println("Fail to write to file."); + } + } +} + diff --git a/src/main/java/pony/TaskList.java b/src/main/java/pony/TaskList.java new file mode 100644 index 0000000000..bd80bc34fd --- /dev/null +++ b/src/main/java/pony/TaskList.java @@ -0,0 +1,76 @@ +package pony; + +import java.util.ArrayList; +import pony.task.Task; + +/** + * List of all the tasks. + */ +public class TaskList { + private ArrayList tasks; + + /** + * Constructor for TaskList. + * + * @param tasks An ArrayList of tasks. + */ + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + + /** + * Add task to the list if the list does not already contain the task. + * + * @param task Task to be added in. + * @throws PonyException If the list contains that task already. + */ + public void addTask(Task task) throws PonyException { + if (tasks.contains(task)) { + throw new PonyException("My friend, you have already recorded this task!! So forgetful!"); + } + this.tasks.add(task); + } + + /** + * Remove a task from the list. + * + * @param index Index of the task to be removed. + */ + public void deleteTask(int index) { + this.tasks.remove(index); + } + + /** + * Get all the tasks. + * + * @return An ArrayList of tasks. + */ + public ArrayList getAllTasks() { + return this.tasks; + } + + /** + * Get how many tasks in the list. + * + * @return The number of tasks in the list. + */ + public int sizeOf() { + return this.tasks.size(); + } + + /** + * Get a task from the list using an index. + * + * @param index The task index in the list. + * @return A task. + * @throws PonyException if the task index is invalid. + */ + public Task getTask(int index) throws PonyException { + // Check if index is valid + if (index < 0 || index >= sizeOf()) { + throw new PonyException("My friend, I can't find this task number!!"); + } + assert index < sizeOf() && index >= 0 : "Task index should be valid"; + return tasks.get(index); + } +} diff --git a/src/main/java/pony/Ui.java b/src/main/java/pony/Ui.java new file mode 100644 index 0000000000..185edabbf0 --- /dev/null +++ b/src/main/java/pony/Ui.java @@ -0,0 +1,133 @@ +package pony; + +import java.util.ArrayList; +import java.util.Scanner; + +import pony.task.Task; + + + +/** + * A UI class handling all user interaction. + */ +public class Ui { + Scanner sc; + + /** + * Constructor for Ui. + */ + public Ui() { + this.sc = new Scanner(System.in); + }; + + /** + * Generate the welcome message. + * + * @return A welcome message. + */ + public static String printWelcome() { + String message = "Hello! I'm Pony!!!!\nNever forget - Friendship is Magic!!!\n" + + "My dear friend, what can I do for you?"; + return message; + } + + /** + * Generate an exit message. + * + * @return An exit message. + */ + public String printExit() { + String message = "Bye, friend. Hope to see you again soon!\nNever forget - Friendship is Magic!!!"; + return message; + } + + /** + * Generate a message when a task is marked. + * + * @param task Task to be mark. + * @return A command message. + */ + public String printMarkedTask(Task task) { + String message = "Well done, my friend! I've marked this task as done:\n"; + message += task.toString(); + return message; + } + + /** + * Generate a message when a task is unmarked. + * + * @param task Task to be unmark. + * @return A command message. + */ + public String printUnmarkedTask(Task task) { + String message = "My friend, I've marked this task as not done yet:\n"; + message += task.toString(); + return message; + } + + /** + * Generate a massage when a task is deleted. + * + * @param task Task to be deleted. + * @param tasks A list of tasks. + * @return A command message. + */ + public String printDeletedTask(Task task, TaskList tasks) { + String message = "Alright my friend, I've removed this task:\n"; + message += task.toString() + "\n" + "Now you have " + tasks.sizeOf() + " tasks in the list."; + return message; + } + + /** + * Generate a message when a task is added. + * + * @param task Task to be added. + * @param tasks A list of tasks. + * @return A command message. + */ + public String printAddedTask(Task task, TaskList tasks) { + String message = "Got it my friend. I've added this task:\n"; + message += task.toString() + "\n" + "Now you have " + tasks.sizeOf() + " tasks in the list."; + return message; + } + + /** + * Generate the task list. + * + * @param tasks A list of tasks. + * @return A command message. + */ + public String printTaskList(TaskList tasks) { + String message = ""; + if (tasks.sizeOf() == 0) { + message = "There is nothing on the list!"; + } else { + message = "Let's look at the tasks in your list:\n"; + for (int i = 0; i < tasks.sizeOf(); i++) { + int serialNumber = i + 1; + message += serialNumber + ". " + tasks.getTask(i).toString() + "\n"; + } + } + return message; + } + + /** + * Generate a message of a find command. + * + * @param tasks A list of tasks. + * @return A list of matching tasks. + */ + public String printFindResult(ArrayList tasks) { + String message = ""; + if (tasks.size() == 0) { + message = "There is no matching task!"; + } else { + message = "Alright, Here's what pony found!\n"; + for (int i = 0; i < tasks.size(); i++) { + int serialNumber = i + 1; + message += serialNumber + ". " + tasks.get(i).toString() + "\n"; + } + } + return message; + } +} diff --git a/src/main/java/pony/command/AddDeadlineCommand.java b/src/main/java/pony/command/AddDeadlineCommand.java new file mode 100644 index 0000000000..7ec2ce03e4 --- /dev/null +++ b/src/main/java/pony/command/AddDeadlineCommand.java @@ -0,0 +1,58 @@ +package pony.command; + +import pony.Parser; +import pony.PonyException; +import pony.Storage; +import pony.TaskList; +import pony.Ui; + +import pony.task.Deadline; +import pony.task.Task; + + +import java.time.format.DateTimeParseException; + +/** + * Command for adding Deadline Task. + */ +public class AddDeadlineCommand extends Command { + + private String commandDetails; + private String format = " /by