diff --git a/.gitignore b/.gitignore index f69985ef1f..d62ce3e918 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT +duke.txt +duke-backup.txt diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..5ae0701bba --- /dev/null +++ b/build.gradle @@ -0,0 +1,58 @@ +plugins { + id 'java' + id 'application' + id 'checkstyle' + 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 = "duke.Launcher" +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +run{ + standardInput = System.in + enableAssertions = true +} diff --git a/docs/README.md b/docs/README.md index 8077118ebe..edf3330504 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,113 @@ + # User Guide +Duke is a desktop app for managing tasks, optimized for use via a Command Line +Interface (CLI). If you can type fast, Duke can get your task management done +faster than traditional GUI apps. + +## Quick start + +1. Ensure you have Java `11` or above installed in your computer. +2. Download the latest `duke.jar` from [here](https://github.com/LikHern/ip/releases/tag/v1.0). +3. Copy the file to the folder you want to use as the _home folder_. +4. Double-click the file to start the app. +5. Refer to the Features below for details of each command. + ## Features -### Feature-ABC +:left_speech_bubble: Notes about the command format: + +- Words in `UPPER_CASE` are the parameters to be supplied by the user. +e.g. in `todo DESCRIPTION`, `DESCRIPTION` is a parameter which can be +used as `todo read book`. +- Extraneous parameters for commands that do not take in parameters + (such as `list` and `bye`) will be ignored. +e.g. if the command specifies `list 123`, it will be interpreted as `list`. + +### Adding a task: `todo` `deadline` `event` + +Duke supports tracking three types of tasks: + +1. To-do: task without any date/time attached to it **_e.g., visit new theme park_** +:memo: +2. Deadline: task that need to be done before a specific date **_e.g., submit +report by 11/10/2019_** :calendar: +3. Event: task that start at a specific time and ends at a specific time **_e.g., +team project meeting on 2/10/2019 2-4pm_** :alarm_clock: + +Format: `todo DESCRIPTION`, `deadline DESCRIPTION /by DATE`, `event DESCRIPTION /at DATETIME` + +:warning: **`DATE` in deadline can only take in date in "yyyy-MM-dd" format, e.g., +2022-12-06** + +Examples: +- todo read book +- deadline return book /by 2022-12-06 +- event project meeting /at Aug 6th 2-4pm + +### Listing all tasks: `list` + +Shows a list of all tasks in Duke. + +Format: `list` + +### Marking/Unmarking a task as completed: `mark` `unmark` + +Marks/Unmarks a task in Duke as done/not done yet. + +Format: `mark INDEX`, `unmark INDEX` + +:left_speech_bubble: `INDEX` follows 1-based indexing and should be an integer. + +### Deleting a task: `delete` + +Deletes a task in Duke. + +Format: `delete INDEX` + +:left_speech_bubble: `INDEX` follows 1-based indexing and should be an integer. + +### Finding task by name: `find` + +Finds any task which the description contains the exact given keyword(s). + +Format: `find KEYWORDS` + +- The search is case-insensitive. e.g., `book` will match `Book` +- The search is partial matching. e.g., `oo` will match `book` + +### Archiving tasks: `archive` + +Archive the tasks saved in Duke to back-up file. + +Format: `archive` -Description of the feature. +:warning: **After this operation, all the tasks in Duke will be cleared!** -### Feature-XYZ +### Exiting the program: `bye` -Description of the feature. +Exits the program. -## Usage +Format: `bye` -### `Keyword` - Describe action +### Saving the data: -Describe the action and its outcome. +Duke data are saved in the hard disk automatically after any command. There is +no need to save manually. -Example of usage: +### Editing the data file -`keyword (optional arguments)` +Duke data and back-up data are saved as a txt file in +`[JAR file location]/duke.txt` and `[JAR file location]/duke-backup.txt` respectively. +Advanced users are welcome to update data directly by editing that data file. -Expected outcome: +:warning: **If your changes to the data file makes its format invalid, Duke will +discard all data and start with an empty data file at the next run.** -Description of the outcome. +### Retrieving data from archive [coming in v2.0] -``` -expected output -``` +Details coming soon... diff --git a/docs/Ui.png b/docs/Ui.png new file mode 100644 index 0000000000..5209dc7889 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/duke/DialogBox.java b/src/main/java/duke/DialogBox.java new file mode 100644 index 0000000000..8968d60eef --- /dev/null +++ b/src/main/java/duke/DialogBox.java @@ -0,0 +1,61 @@ +package duke; + +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; + +import java.io.IOException; +import java.util.Collections; + +/** + * 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/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..c712df127b --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,27 @@ +package duke; + +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.Ui; + +public class Duke { + private static final String DEFAULT_FILEPATH = "duke.txt"; + + private final Storage storage; + private final TaskList taskList; + private final Ui ui; + + public Duke(String filePath) { + storage = new Storage(filePath); + taskList = storage.load(); + ui = new Ui(); + } + + public Duke() { + this(DEFAULT_FILEPATH); + } + + String getResponse(String input) { + return ui.readUserInputThenOutputMessage(taskList, storage, input); + } +} diff --git a/src/main/java/duke/Launcher.java b/src/main/java/duke/Launcher.java new file mode 100644 index 0000000000..b8c0f9c373 --- /dev/null +++ b/src/main/java/duke/Launcher.java @@ -0,0 +1,9 @@ +package duke; + +import javafx.application.Application; + +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} diff --git a/src/main/java/duke/Main.java b/src/main/java/duke/Main.java new file mode 100644 index 0000000000..43b6733f9c --- /dev/null +++ b/src/main/java/duke/Main.java @@ -0,0 +1,31 @@ +package duke; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +import java.io.IOException; + +/** + * A GUI for Duke using FXML. + */ +public class Main extends Application { + private final Duke duke = new Duke(); + + @Override + public void start(Stage stage) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = fxmlLoader.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + fxmlLoader.getController().setDuke(duke); + stage.setTitle("Duke"); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java new file mode 100644 index 0000000000..ed76862dc5 --- /dev/null +++ b/src/main/java/duke/MainWindow.java @@ -0,0 +1,78 @@ +package duke; + +import duke.ui.Ui; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.application.Platform; +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 javafx.util.Duration; + +import static duke.ui.Messages.MESSAGE_WELCOME; + +/** + * 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 Duke duke; + + private final Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); + private final Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + dialogContainer.getChildren().add(DialogBox.getDukeDialog(MESSAGE_WELCOME, dukeImage)); + } + + void setDuke(Duke d) { + duke = d; + } + + /** + * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to + * the dialog container. Clears the user input after processing. + */ + @FXML + private void handleUserInput() { + String input = userInput.getText(); + String response = duke.getResponse(input); + //@@author LikHern-reused + //Reused from https://stackoverflow.com/questions/39235545/add-delay-after-platform-runlater-runnable + // with minor modifications + KeyFrame kf1 = new KeyFrame(Duration.seconds(0), e -> displayUserDialogAndDukeDialog(input, response)); + KeyFrame kf2 = new KeyFrame(Duration.seconds(1), e -> exitApplication()); + Timeline timeline = new Timeline(kf1, kf2); + Platform.runLater(timeline::play); + //@@author + } + + @FXML + private void displayUserDialogAndDukeDialog(String input, String response) { + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage)); + userInput.clear(); + } + + @FXML + private void exitApplication() { + if (Ui.isExit()) { + Platform.exit(); + } + } +} diff --git a/src/main/java/duke/commands/ArchiveCommand.java b/src/main/java/duke/commands/ArchiveCommand.java new file mode 100644 index 0000000000..82b6ca38ac --- /dev/null +++ b/src/main/java/duke/commands/ArchiveCommand.java @@ -0,0 +1,16 @@ +package duke.commands; + +import duke.storage.Storage; +import duke.task.TaskList; + +import static duke.ui.Messages.MESSAGE_ARCHIVE_SUCCESSFULLY; + +public class ArchiveCommand extends Command { + private final Storage backupDataFile = new Storage("duke-backup.txt"); + + public String execute(TaskList taskList) { + backupDataFile.appendSave(taskList); + taskList.clear(); + return MESSAGE_ARCHIVE_SUCCESSFULLY; + } +} diff --git a/src/main/java/duke/commands/ByeCommand.java b/src/main/java/duke/commands/ByeCommand.java new file mode 100644 index 0000000000..167d2d8ee1 --- /dev/null +++ b/src/main/java/duke/commands/ByeCommand.java @@ -0,0 +1,12 @@ +package duke.commands; + +import duke.task.TaskList; + +import static duke.ui.Messages.MESSAGE_FAREWELL; + +public class ByeCommand extends Command { + public String execute(TaskList taskList) { + setIsExitToTrue(); + return MESSAGE_FAREWELL; + } +} diff --git a/src/main/java/duke/commands/Command.java b/src/main/java/duke/commands/Command.java new file mode 100644 index 0000000000..62ab09d033 --- /dev/null +++ b/src/main/java/duke/commands/Command.java @@ -0,0 +1,19 @@ +package duke.commands; + +import duke.task.TaskList; + +public abstract class Command { + static final int DISPLAYED_INDEX_OFFSET = 1; + + private boolean isExit = false; + + public boolean isExit() { + return isExit; + } + + void setIsExitToTrue() { + isExit = true; + } + + public abstract String execute(TaskList taskList); +} diff --git a/src/main/java/duke/commands/DeadlineCommand.java b/src/main/java/duke/commands/DeadlineCommand.java new file mode 100644 index 0000000000..706ae6244d --- /dev/null +++ b/src/main/java/duke/commands/DeadlineCommand.java @@ -0,0 +1,35 @@ +package duke.commands; + +import duke.task.Deadline; +import duke.task.TaskList; + +import java.util.Scanner; + +public class DeadlineCommand extends Command { + private final String deadlineDescription; + private final String deadlineBy; + + /** + * Parse the command from scanner and store the description as well as date for deadline task. + * + * @param scanner User input. + */ + public DeadlineCommand(Scanner scanner) { + scanner.useDelimiter("/by"); + deadlineDescription = scanner.next().strip(); + scanner.reset().skip("/by"); + deadlineBy = scanner.nextLine().strip(); + } + + /** + * Create new deadline task and store it in the task list. Finally, save the task list in storage. + * + * @param taskList Task list that stores new deadline task. + */ + public String execute(TaskList taskList) { + Deadline deadlineTask = new Deadline(deadlineDescription, deadlineBy); + taskList.add(deadlineTask); + return String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in your list.", + deadlineTask, taskList.getSize()); + } +} diff --git a/src/main/java/duke/commands/DeadlineWithStatusCommand.java b/src/main/java/duke/commands/DeadlineWithStatusCommand.java new file mode 100644 index 0000000000..09a0dff55c --- /dev/null +++ b/src/main/java/duke/commands/DeadlineWithStatusCommand.java @@ -0,0 +1,24 @@ +package duke.commands; + +import duke.task.Deadline; +import duke.task.TaskList; + +import java.util.Scanner; + +public class DeadlineWithStatusCommand extends Command { + private final String deadlineDescription; + private final String deadlineBy; + private final boolean isDone; + + public DeadlineWithStatusCommand(Scanner scanner) { + deadlineDescription = scanner.next().strip(); + deadlineBy = scanner.next().strip(); + isDone = Boolean.parseBoolean(scanner.reset().skip("\\|").nextLine().strip()); + } + + public String execute(TaskList taskList) { + Deadline deadlineTask = new Deadline(deadlineDescription, deadlineBy, isDone); + taskList.add(deadlineTask); + return "Success"; + } +} diff --git a/src/main/java/duke/commands/DeleteCommand.java b/src/main/java/duke/commands/DeleteCommand.java new file mode 100644 index 0000000000..1e5356f964 --- /dev/null +++ b/src/main/java/duke/commands/DeleteCommand.java @@ -0,0 +1,31 @@ +package duke.commands; + +import duke.task.Task; +import duke.task.TaskList; + +import java.util.Scanner; + +public class DeleteCommand extends Command { + private final int deleteIndex; + + /** + * Parse the command from scanner and store the index for the task to be deleted. + * + * @param scanner User input. + */ + public DeleteCommand(Scanner scanner) { + deleteIndex = scanner.nextInt(); + } + + /** + * Delete specified task from the task list. Finally, save the task list in storage. + * + * @param taskList Task list that contains the specified task. + */ + public String execute(TaskList taskList) { + Task deletedTask = taskList.get(deleteIndex - DISPLAYED_INDEX_OFFSET); + taskList.delete(deleteIndex - DISPLAYED_INDEX_OFFSET); + return String.format("Noted. I've removed this task:\n%s\nNow you have %d tasks in your list.", + deletedTask, taskList.getSize()); + } +} diff --git a/src/main/java/duke/commands/EventCommand.java b/src/main/java/duke/commands/EventCommand.java new file mode 100644 index 0000000000..cea3533ced --- /dev/null +++ b/src/main/java/duke/commands/EventCommand.java @@ -0,0 +1,35 @@ +package duke.commands; + +import duke.task.Event; +import duke.task.TaskList; + +import java.util.Scanner; + +public class EventCommand extends Command { + private final String eventDescription; + private final String eventAt; + + /** + * Parse the command from scanner and store the description as well as date for event task. + * + * @param scanner User input. + */ + public EventCommand(Scanner scanner) { + scanner.useDelimiter("/at"); + eventDescription = scanner.next().strip(); + scanner.reset().skip("/at"); + eventAt = scanner.nextLine().strip(); + } + + /** + * Create new event task and store it in the task list. Finally, save the task list in storage. + * + * @param taskList Task list that stores new event task. + */ + public String execute(TaskList taskList) { + Event eventTask = new Event(eventDescription, eventAt); + taskList.add(eventTask); + return String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in your list.", + eventTask, taskList.getSize()); + } +} diff --git a/src/main/java/duke/commands/EventWithStatusCommand.java b/src/main/java/duke/commands/EventWithStatusCommand.java new file mode 100644 index 0000000000..18db4f3720 --- /dev/null +++ b/src/main/java/duke/commands/EventWithStatusCommand.java @@ -0,0 +1,24 @@ +package duke.commands; + +import duke.task.Event; +import duke.task.TaskList; + +import java.util.Scanner; + +public class EventWithStatusCommand extends Command { + private final String eventDescription; + private final String eventAt; + private final boolean isDone; + + public EventWithStatusCommand(Scanner scanner) { + eventDescription = scanner.next().strip(); + eventAt = scanner.next().strip(); + isDone = Boolean.parseBoolean(scanner.reset().skip("\\|").nextLine().strip()); + } + + public String execute(TaskList taskList) { + Event eventTask = new Event(eventDescription, eventAt, isDone); + taskList.add(eventTask); + return "Success"; + } +} diff --git a/src/main/java/duke/commands/FindCommand.java b/src/main/java/duke/commands/FindCommand.java new file mode 100644 index 0000000000..e30c5a3a88 --- /dev/null +++ b/src/main/java/duke/commands/FindCommand.java @@ -0,0 +1,35 @@ +package duke.commands; + +import duke.task.Task; +import duke.task.TaskList; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; +import java.util.Scanner; + +public class FindCommand extends Command { + private final String keyword; + + public FindCommand(Scanner scanner) { + keyword = scanner.skip(" ").nextLine(); + } + + public String execute(TaskList tasklist) { + List matchingTasks = new ArrayList<>(); + matchingTasks.add("Here are the matching tasks in your list:"); + ListIterator listIterator = tasklist.getListIterator(); + while (listIterator.hasNext()) { + int taskIndex = listIterator.nextIndex() + DISPLAYED_INDEX_OFFSET; + Task task = listIterator.next(); + addMatchingTask(matchingTasks, taskIndex, task); + } + return String.join("\n", matchingTasks); + } + + private void addMatchingTask(List matchingTasks, int taskIndex, Task task) { + if (task.doesContain(keyword)) { + matchingTasks.add(taskIndex + "." + task); + } + } +} diff --git a/src/main/java/duke/commands/IncorrectCommand.java b/src/main/java/duke/commands/IncorrectCommand.java new file mode 100644 index 0000000000..10ae6a0b62 --- /dev/null +++ b/src/main/java/duke/commands/IncorrectCommand.java @@ -0,0 +1,14 @@ +package duke.commands; + +import duke.task.TaskList; + +import static duke.ui.Messages.MESSAGE_INCORRECT_COMMAND; + +public class IncorrectCommand extends Command { + /** + * Print error message for invalid command. + */ + public String execute(TaskList taskList) { + return MESSAGE_INCORRECT_COMMAND; + } +} diff --git a/src/main/java/duke/commands/ListCommand.java b/src/main/java/duke/commands/ListCommand.java new file mode 100644 index 0000000000..c871f76c81 --- /dev/null +++ b/src/main/java/duke/commands/ListCommand.java @@ -0,0 +1,27 @@ +package duke.commands; + +import duke.task.Task; +import duke.task.TaskList; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; + +public class ListCommand extends Command { + /** + * Returns a list of tasks that is currently stored in the task list. + * + * @param taskList Task list that contains all the tasks. + */ + public String execute(TaskList taskList) { + List tasks = new ArrayList<>(); + tasks.add("Here are the tasks in your list:"); + ListIterator listIterator = taskList.getListIterator(); + while (listIterator.hasNext()) { + int taskIndex = listIterator.nextIndex() + DISPLAYED_INDEX_OFFSET; + Task task = listIterator.next(); + tasks.add(taskIndex + "." + task); + } + return String.join("\n", tasks); + } +} diff --git a/src/main/java/duke/commands/MarkCommand.java b/src/main/java/duke/commands/MarkCommand.java new file mode 100644 index 0000000000..0309c23ee6 --- /dev/null +++ b/src/main/java/duke/commands/MarkCommand.java @@ -0,0 +1,27 @@ +package duke.commands; + +import duke.task.TaskList; + +import java.util.Scanner; + +public class MarkCommand extends Command { + private final int markIndex; + + /** + * Parse the command from scanner and store the index for the task to be marked. + * + * @param scanner User input. + */ + public MarkCommand(Scanner scanner) { + markIndex = scanner.nextInt(); + } + + /** + * Mark the specified task in the task list as done. Finally, save the task list in storage. + * + * @param taskList Task list that contains the specified task. + */ + public String execute(TaskList taskList) { + return taskList.get(markIndex - DISPLAYED_INDEX_OFFSET).markAsDone(); + } +} diff --git a/src/main/java/duke/commands/ToDoCommand.java b/src/main/java/duke/commands/ToDoCommand.java new file mode 100644 index 0000000000..b1f4a2f953 --- /dev/null +++ b/src/main/java/duke/commands/ToDoCommand.java @@ -0,0 +1,35 @@ +package duke.commands; + +import duke.task.TaskList; +import duke.task.ToDo; + +import java.util.Scanner; + +public class ToDoCommand extends Command { + private final String toDoDescription; + + /** + * Parse the command from scanner and store the description for to-do task. + * + * @param scanner User input. + */ + public ToDoCommand(Scanner scanner) { + toDoDescription = scanner.nextLine().strip(); + } + + /** + * Create new to-do task and store it in the task list. Finally, save the task list in storage. + * + * @param taskList Task list that stores new to-do task. + */ + public String execute(TaskList taskList) { + if (toDoDescription.isBlank()) { + return "OOPS!!! The description of a todo cannot be empty."; + } else { + ToDo toDoTask = new ToDo(toDoDescription); + taskList.add(toDoTask); + return String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in your list.", + toDoTask, taskList.getSize()); + } + } +} diff --git a/src/main/java/duke/commands/ToDoWithStatusCommand.java b/src/main/java/duke/commands/ToDoWithStatusCommand.java new file mode 100644 index 0000000000..cc3e1a3c1f --- /dev/null +++ b/src/main/java/duke/commands/ToDoWithStatusCommand.java @@ -0,0 +1,22 @@ +package duke.commands; + +import duke.task.TaskList; +import duke.task.ToDo; + +import java.util.Scanner; + +public class ToDoWithStatusCommand extends Command { + private final String toDoDescription; + private final boolean isDone; + + public ToDoWithStatusCommand(Scanner scanner) { + toDoDescription = scanner.next().strip(); + isDone = Boolean.parseBoolean(scanner.reset().skip("\\|").nextLine().strip()); + } + + public String execute(TaskList taskList) { + ToDo toDoTask = new ToDo(toDoDescription, isDone); + taskList.add(toDoTask); + return "Success"; + } +} diff --git a/src/main/java/duke/commands/UnmarkCommand.java b/src/main/java/duke/commands/UnmarkCommand.java new file mode 100644 index 0000000000..751a551cbe --- /dev/null +++ b/src/main/java/duke/commands/UnmarkCommand.java @@ -0,0 +1,27 @@ +package duke.commands; + +import duke.task.TaskList; + +import java.util.Scanner; + +public class UnmarkCommand extends Command { + private final int unmarkIndex; + + /** + * Parse the command from scanner and store the index for the task to be unmarked. + * + * @param scanner User input. + */ + public UnmarkCommand(Scanner scanner) { + unmarkIndex = scanner.nextInt(); + } + + /** + * Mark the specified task in the task list as not done yet. Finally, save the task list in storage. + * + * @param taskList Task list that contains the specified task. + */ + public String execute(TaskList taskList) { + return taskList.get(unmarkIndex - DISPLAYED_INDEX_OFFSET).markAsUndone(); + } +} diff --git a/src/main/java/duke/commands/UserCommandList.java b/src/main/java/duke/commands/UserCommandList.java new file mode 100644 index 0000000000..793ef01771 --- /dev/null +++ b/src/main/java/duke/commands/UserCommandList.java @@ -0,0 +1,5 @@ +package duke.commands; + +public enum UserCommandList { + TODO, DEADLINE, EVENT, LIST, MARK, UNMARK, DELETE, FIND, ARCHIVE, BYE +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 0000000000..506c19e21f --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,78 @@ +package duke.parser; + +import duke.commands.ArchiveCommand; +import duke.commands.ByeCommand; +import duke.commands.Command; +import duke.commands.DeadlineCommand; +import duke.commands.DeadlineWithStatusCommand; +import duke.commands.DeleteCommand; +import duke.commands.EventCommand; +import duke.commands.EventWithStatusCommand; +import duke.commands.FindCommand; +import duke.commands.IncorrectCommand; +import duke.commands.ListCommand; +import duke.commands.MarkCommand; +import duke.commands.ToDoCommand; +import duke.commands.ToDoWithStatusCommand; +import duke.commands.UnmarkCommand; +import duke.commands.UserCommandList; + +import java.util.Scanner; + +/** + * Parse input and returns the command to be executed. + */ +public class Parser { + public static Command parseUserInput(Scanner userInput) { + UserCommandList command; + try { + command = UserCommandList.valueOf(userInput.next().toUpperCase()); + } catch (IllegalArgumentException iae) { + return new IncorrectCommand(); + } + switch(command) { + case TODO: + return new ToDoCommand(userInput); + case DEADLINE: + return new DeadlineCommand(userInput); + case EVENT: + return new EventCommand(userInput); + case LIST: + return new ListCommand(); + case MARK: + return new MarkCommand(userInput); + case UNMARK: + return new UnmarkCommand(userInput); + case DELETE: + return new DeleteCommand(userInput); + case FIND: + return new FindCommand(userInput); + case ARCHIVE: + return new ArchiveCommand(); + case BYE: + return new ByeCommand(); + default: + throw new AssertionError("Should not reach this stage."); + } + } + + public static Command parseFileInput(Scanner fileReader) { + fileReader.useDelimiter("\\|"); + UserCommandList command; + try { + command = UserCommandList.valueOf(fileReader.next().strip().toUpperCase()); + } catch (IllegalArgumentException iae) { + throw new IllegalStateException(); + } + switch (command) { + case TODO: + return new ToDoWithStatusCommand(fileReader); + case DEADLINE: + return new DeadlineWithStatusCommand(fileReader); + case EVENT: + return new EventWithStatusCommand(fileReader); + default: + throw new AssertionError("Should not reach this stage."); + } + } +} diff --git a/src/main/java/duke/storage/FileDecoder.java b/src/main/java/duke/storage/FileDecoder.java new file mode 100644 index 0000000000..01cf10a454 --- /dev/null +++ b/src/main/java/duke/storage/FileDecoder.java @@ -0,0 +1,40 @@ +package duke.storage; + +import duke.commands.Command; +import duke.parser.Parser; +import duke.task.TaskList; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class FileDecoder { + /** + * Decode the tasks from file and store in the given task list. Finally, return the task list. + * + * @param dataFile File to be read from. + * @return Task list containing tasks. + */ + static TaskList decodeFile(File dataFile, TaskList taskList) { + try { + Scanner fileReader = new Scanner(dataFile); + decodeLine(taskList, fileReader); + } catch (FileNotFoundException fnfe) { + throw new AssertionError("Should not reach this stage."); + } + return taskList; + } + + /** + * Decode the file line by line and store the decoded tasks in task list. + * + * @param taskList Task list to store the decoded tasks. + * @param fileReader Line read by scanner. + */ + private static void decodeLine(TaskList taskList, Scanner fileReader) { + while (fileReader.hasNextLine()) { + Command command = Parser.parseFileInput(fileReader); + command.execute(taskList); + } + } +} diff --git a/src/main/java/duke/storage/FileEncoder.java b/src/main/java/duke/storage/FileEncoder.java new file mode 100644 index 0000000000..2d4f2db324 --- /dev/null +++ b/src/main/java/duke/storage/FileEncoder.java @@ -0,0 +1,32 @@ +package duke.storage; + +import duke.task.Task; +import duke.task.TaskList; + +import java.io.FileWriter; +import java.io.IOException; + +public class FileEncoder { + /** + * Encode the tasks from task list and write it to file. + * + * @param taskList Task list to be saved. + * @throws IOException If error occurs when trying to write to file. + */ + static void encodeFile(FileWriter fileWriter, TaskList taskList) throws IOException { + for (Task task : taskList.view()) { + encodeLine(fileWriter, task); + } + fileWriter.close(); + } + + /** + * Encode task in file format for easier parsing during decoding. + * + * @param task Task to be saved. + * @throws IOException If error occurs when trying to write to file. + */ + private static void encodeLine(FileWriter fileWriter, Task task) throws IOException { + fileWriter.write(task.convertToFileFormat() + System.lineSeparator()); + } +} diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 0000000000..289314fcf5 --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,66 @@ +package duke.storage; + +import duke.task.TaskList; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +public class Storage { + private final File dataFile; + + public Storage(String filePath) { + dataFile = new File(filePath); + } + + /** + * Encode the task list and store in specified file path (overwriting the file). + * + * @param taskList Task list to be saved. + */ + public void overwriteSave(TaskList taskList) { + try { + FileWriter fileWriter = new FileWriter(dataFile); + FileEncoder.encodeFile(fileWriter, taskList); + } catch (IOException ioe) { + System.err.println("Warning! Errors encountered when writing to file. " + + "Your data might not be saved."); + } + } + + /** + * Encode the task list and store in specified file path (append to the back of the file). + * + * @param taskList Task list to be saved. + */ + public void appendSave(TaskList taskList) { + try { + FileWriter fileWriter = new FileWriter(dataFile, true); + FileEncoder.encodeFile(fileWriter, taskList); + } catch (IOException ioe) { + System.err.println("Warning! Errors encountered when writing to file. " + + "Your data might not be saved."); + } + } + + /** + * Decode the file at specified file path and save the decoded tasks in a new task list, if such file exists. + * Else, create a new empty file at specified file path. + * + * @return Task list containing decoded tasks. + */ + public TaskList load() { + TaskList taskList = new TaskList(new ArrayList<>()); + try { + if (dataFile.createNewFile()) { + System.out.println("Creating a new file..."); + } + return FileDecoder.decodeFile(dataFile, taskList); + } catch (IllegalStateException | IOException e) { + System.err.println("Warning! Errors encountered when reading from file. " + + "The rest of the data is discarded."); + } + return taskList; + } +} diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java new file mode 100644 index 0000000000..80336dc24b --- /dev/null +++ b/src/main/java/duke/task/Deadline.java @@ -0,0 +1,32 @@ +package duke.task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Deadline extends Task { + private final LocalDate eventBy; + + public Deadline(String description, String eventBy) { + super(description); + this.eventBy = LocalDate.parse(eventBy); + } + + public Deadline(String description, String eventBy, boolean isDone) { + super(description, isDone); + this.eventBy = LocalDate.parse(eventBy); + } + + /** + * {@inheritDoc} + */ + @Override + public String convertToFileFormat() { + return String.format("deadline | %s | %s | %b", super.description, eventBy, super.isDone); + } + + @Override + public String toString() { + return String.format("[D]%s (by: %s)", super.toString(), + eventBy.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))); + } +} diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java new file mode 100644 index 0000000000..bd8c75903d --- /dev/null +++ b/src/main/java/duke/task/Event.java @@ -0,0 +1,28 @@ +package duke.task; + +public class Event extends Task { + private final String eventAt; + + public Event(String description, String eventAt) { + super(description); + this.eventAt = eventAt; + } + + public Event(String description, String eventAt, boolean isDone) { + super(description, isDone); + this.eventAt = eventAt; + } + + /** + * {@inheritDoc} + */ + @Override + public String convertToFileFormat() { + return String.format("event | %s | %s | %b", super.description, eventAt, super.isDone); + } + + @Override + public String toString() { + return String.format("[E]%s (at: %s)", super.toString(), eventAt); + } +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java new file mode 100644 index 0000000000..36aced9a70 --- /dev/null +++ b/src/main/java/duke/task/Task.java @@ -0,0 +1,63 @@ +package duke.task; + +import static duke.ui.Messages.MESSAGE_TASK_ALREADY_MARKED; +import static duke.ui.Messages.MESSAGE_TASK_ALREADY_UNMARKED; + +public abstract class Task { + String description; + boolean isDone; + + Task(String description) { + this.description = description; + isDone = false; + } + + Task(String description, boolean isDone) { + this.description = description; + this.isDone = isDone; + } + + public boolean doesContain(String keyword) { + return description.toLowerCase().contains(keyword.toLowerCase()); + } + + private String getStatusIcon() { + return isDone ? "X" : " "; + } + + /** + * Mark the task as done, and returns a message indicating task is marked successfully. + * If the task is already marked as done, an error message is returned instead. + */ + public String markAsDone() { + if (isDone) { + return MESSAGE_TASK_ALREADY_MARKED; + } else { + this.isDone = true; + return "Nice! I've marked this task as done:\n" + this; + } + } + + /** + * Mark the task as not done yet, and returns a message indicating task is unmarked successfully. + * If the task is already marked as not done yet, an error message is returned instead. + */ + public String markAsUndone() { + if (!isDone) { + return MESSAGE_TASK_ALREADY_UNMARKED; + } else { + this.isDone = false; + return "OK, I've marked this task as not done yet:\n" + this; + } + } + + /** + * Returns task written in file format for easier parsing during decoding. + */ + public abstract String convertToFileFormat(); + + @Override + public String toString() { + return String.format("[%s] %s", getStatusIcon(), description); + } +} diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java new file mode 100644 index 0000000000..5fe476f757 --- /dev/null +++ b/src/main/java/duke/task/TaskList.java @@ -0,0 +1,43 @@ +package duke.task; + +import java.util.List; +import java.util.ListIterator; + +/** + * Contains task list. Support operations such as add or delete tasks in the list. + */ +public class TaskList { + private final List taskList; + + public TaskList(List taskList) { + this.taskList = taskList; + } + + public List view() { + return taskList; + } + + public Task get(int index) { + return taskList.get(index); + } + + public int getSize() { + return taskList.size(); + } + + public void add(Task task) { + taskList.add(task); + } + + public void delete(int index) { + taskList.remove(index); + } + + public ListIterator getListIterator() { + return taskList.listIterator(); + } + + public void clear() { + taskList.clear(); + } +} diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java new file mode 100644 index 0000000000..4de407a64d --- /dev/null +++ b/src/main/java/duke/task/ToDo.java @@ -0,0 +1,24 @@ +package duke.task; + +public class ToDo extends Task { + public ToDo(String description) { + super(description); + } + + public ToDo(String description, boolean isDone) { + super(description, isDone); + } + + /** + * {@inheritDoc} + */ + @Override + public String convertToFileFormat() { + return String.format("todo | %s | %b", super.description, super.isDone); + } + + @Override + public String toString() { + return String.format("[T]%s", super.toString()); + } +} diff --git a/src/main/java/duke/ui/Messages.java b/src/main/java/duke/ui/Messages.java new file mode 100644 index 0000000000..106a0c0e23 --- /dev/null +++ b/src/main/java/duke/ui/Messages.java @@ -0,0 +1,10 @@ +package duke.ui; + +public class Messages { + public static final String MESSAGE_FAREWELL = "Bye. Hope to see you again soon!"; + public static final String MESSAGE_WELCOME = "Hello! I'm Duke\nWhat can I do for you?"; + public static final String MESSAGE_TASK_ALREADY_MARKED = "This task is already marked as done!"; + public static final String MESSAGE_TASK_ALREADY_UNMARKED = "This task is already marked as not done yet!"; + public static final String MESSAGE_INCORRECT_COMMAND = "OOPS!!! I'm sorry, but I don't know what that means :-("; + public static final String MESSAGE_ARCHIVE_SUCCESSFULLY = "Archive successfully!"; +} diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java new file mode 100644 index 0000000000..44c9b92474 --- /dev/null +++ b/src/main/java/duke/ui/Ui.java @@ -0,0 +1,27 @@ +package duke.ui; + +import duke.commands.Command; +import duke.parser.Parser; +import duke.storage.Storage; +import duke.task.TaskList; + +import java.util.Scanner; + +public class Ui { + private static boolean isExit = false; + + public static boolean isExit() { + return isExit; + } + + public String readUserInputThenOutputMessage(TaskList taskList, Storage storage, String input) { + assert !isExit : "isExit should be false here, and can only be changed after this line of code"; + Scanner scanner = new Scanner(input); + Command command = Parser.parseUserInput(scanner); + scanner.close(); + String message = command.execute(taskList); + isExit = command.isExit(); + storage.overwriteSave(taskList); + return message; + } +} diff --git a/src/main/resources/images/DaDuke.png b/src/main/resources/images/DaDuke.png new file mode 100644 index 0000000000..d893658717 Binary files /dev/null and b/src/main/resources/images/DaDuke.png differ diff --git a/src/main/resources/images/DaUser.png b/src/main/resources/images/DaUser.png new file mode 100644 index 0000000000..3c82f45461 Binary files /dev/null and b/src/main/resources/images/DaUser.png differ diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 0000000000..e0ca36ac32 --- /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..ecde92a22d --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +