diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..20c0521cc7 --- /dev/null +++ b/build.gradle @@ -0,0 +1,46 @@ +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' +} + +test { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + showStandardStreams = false + } +} + +application { + mainClassName = "seedu.duke.Duke" +} + +shadowJar { + archiveBaseName = "duke" + archiveClassifier = null +} + +checkstyle { + toolVersion = '8.29' +} + +run{ + standardInput = System.in +} diff --git a/data/memory.txt b/data/memory.txt new file mode 100644 index 0000000000..90458a6aa7 --- /dev/null +++ b/data/memory.txt @@ -0,0 +1,2 @@ +[T] [✓] homework +[T] [✕] hell diff --git a/data/updated.txt b/data/updated.txt new file mode 100644 index 0000000000..e69de29bb2 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/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..e63382cd01 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.DukeMan + diff --git a/src/main/java/duke/Deadlines.java b/src/main/java/duke/Deadlines.java new file mode 100644 index 0000000000..695cf4fab1 --- /dev/null +++ b/src/main/java/duke/Deadlines.java @@ -0,0 +1,44 @@ +package duke; +/** + * Deadlines is a task that have a deadline which is a string. + * @author Dominic Siew Zhen Yu + * + */ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + +public class Deadlines extends Task{ + private String deadline; + String TASKINDICATOR = "[D]"; + + /** + * The constructor for the Deadlines class with the userInput (which refers to the name of the task) + * and the deadline parameter which is the deadline of the deadlines task. + * + * @param userInput the name of the deadlines + * @param deadline the time you have to complete the Deadlines + */ + + public Deadlines(String userInput, String deadline, boolean newInput) { + super(userInput); + + if (newInput) { + LocalDate date = LocalDate.parse(deadline); + this.deadline = date.format(DateTimeFormatter.ofPattern("MMM d yyyy")); + } else { + this.deadline = deadline; + } + + } + + /** + * the printName() method prints the name of the deadlines task. + * + * @return the string representation of the deadlines + */ + + public String printName() { + return TASKINDICATOR + " " + super.printName() + " (by: " + this.deadline + " )"; + } +} \ No newline at end of file diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..cd633e3383 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,137 @@ +//package duke; +// +///** +// * Duke is a chatbot that takes in tasks, which includes todos, deadlines and events, and compiles them +// * in a list. Tasks can be removed from the list and be marked as completed. +// * +// * @author Dominic Siew Zhen Yu +// * +// */ +// +//import java.io.*; +//import java.util.Scanner; +// +//public class Duke { +// +// public enum Tasks { +// DEADLINE, +// EVENT, +// TODO +// } +// +// public static void main(String[] args) throws IOException { +// String logo = " ____ _\n" +// + "| _ \\ _ _| | _____\n" +// + "| | | | | | | |/ / _ \\\n" +// + "| |_| | |_| | < __/\n" +// + "|____/ \\__,_|_|\\_\\___|\n"; +// System.out.println("Hello from\n" + logo); +// System.out.println("____________________________________________________________\nHello! I'm Duke\nWhat can I do for you?\n____________________________________________________________"); +// +// boolean isEnded = false; +// taskList taskList = null; +// taskList = new taskList(); +// +// File file = new File("src/main/memory.txt"); +// +// if (!(file.exists())){ +// System.out.println("processing!"); +// try { +// PrintWriter writer = new PrintWriter("src/main/memory.txt"); +// writer.print(""); +// writer.close(); +// +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } else { +// +// File updated = new File("src/main/updated.txt"); +// BufferedReader reader = new BufferedReader(new FileReader("src/main/memory.txt")); +// BufferedWriter writer = new BufferedWriter(new FileWriter(updated)); +// String currentLine = ""; +// int numberAdded = 1; +// +// +// +// while ((currentLine = reader.readLine()) != null) { +// String[] seperate1 = currentLine.toString().split(" ", 2); +// String taskTypeText = seperate1[0]; +// String taskType; +// +// String[] seperate2 = seperate1[1].toString().split(" ", 2); +// boolean isCompleted = (seperate2[0].equals("[✓]"))? true: false; +// String eventInfo = seperate2[1]; +// +// switch(taskTypeText) { +// case "[T]": +// String eventname = eventInfo; +// taskList.addTodo(eventname, false); +// break; +// case "[D]": +// String[] seperateAgain = seperate2[1].split(" \\(by: ", 2); +// String eventName = seperateAgain[0]; +// String deadline = seperateAgain[1].split(" ", 2)[0]; +// taskList.addDeadline(eventName, deadline, false); +// break; +// case "[E]": +// String[] seperateAgaining = seperate2[1].split(" \\(at: ", 2); +// String event = seperateAgaining[0]; +// String timeline = seperateAgaining[1].split(" ", 2)[0]; +// taskList.addEvent(event, timeline, false); +// break; +// } +// +// if (isCompleted){ +// taskList.updateTaskStatus(numberAdded, false); +// } +// numberAdded++; +// } +// +// } +// +// +// +// +// Scanner sc= new Scanner(System.in); +// +// while(!isEnded) { +// String userInput = sc.nextLine(); +// System.out.println("____________________________________________________________"); +// String[] parsedInput = userInput.split(" ", 2); +// +// +// if (parsedInput[0].equals("todo")) { +// if (parsedInput.length == 1 ) { +// throw new DukeException("empty"); +// } +// taskList.addTodo(parsedInput[1], true); +// +// } else if (parsedInput[0].equals("deadline")) { +// String[] furtherParsed = parsedInput[1].split(" /by ", 2); +// taskList.addDeadline(furtherParsed[0], furtherParsed[1], true); +// } else if (parsedInput[0].equals("event")){ +// String[] furtherParsed = parsedInput[1].split(" /at ",2); +// taskList.addEvent(furtherParsed[0],furtherParsed[1], true); +// } else if(userInput.equals("bye")){ +// System.out.println("Bye. Hope to see you again soon!"); +// isEnded = true; +// } else if (userInput.equals("list")){ +// taskList.printList(); +// } else if (parsedInput[0].equals("done")) { +// int itemRank = Integer.parseInt(parsedInput[1]); +// taskList.updateTaskStatus(itemRank, true); +// +// } else if (parsedInput[0].equals("remove")){ +// int itemRank = Integer.parseInt(parsedInput[1]); +// taskList.removeTask(itemRank - 1 ); +// } else { +// System.out.println("Please give an appropriate response."); +// throw new DukeException("generic"); +// } +// System.out.println("____________________________________________________________"); +// } +// +// +// } +//} diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..0f6c1419bd --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,35 @@ +package duke; +/** + * DukeException is an ArrayIndexOutOfBoundsException that are caused by errors in the Duke program. + */ +public class DukeException extends ArrayIndexOutOfBoundsException { + private String cause; + + /** + * this is a constructor for the DukeException class; it contains a message parameter + * that specifies the error message to be displayed. + * + * @param message a String that is used to specify the error message to be produced + */ + + DukeException(String message) { + this.cause = message; + } + + /** + * the toString() method of the class that returns a string representation of the + * error produced in the program Duke. + * + * @return a string representation of the error produced + */ + @Override + public String toString() { + if (this.cause.equals("empty")) { + return "OOPS!!! The description of a todo cannot be empty."; + } else if (this.cause.equals("please parse first")){ + return "Please parse!!"; + } else { + return "OOPS!!! I'm sorry, but I don't know what that means :-("; + } + } +} diff --git a/src/main/java/duke/DukeMan.java b/src/main/java/duke/DukeMan.java new file mode 100644 index 0000000000..c30a726ab6 --- /dev/null +++ b/src/main/java/duke/DukeMan.java @@ -0,0 +1,93 @@ +package duke; +import java.io.File; +import java.io.IOException; +import java.util.Scanner; + +public class DukeMan { + + private Storage storage; + private taskList tasks; + private Ui ui; + + public DukeMan(String filePath) { + ui = new Ui(); + File file = new File(filePath); + storage = new Storage(filePath); + + try { + tasks = new taskList(storage.load()); + } catch (DukeException e) { + ui.showLoadingError(); + tasks = new taskList(); + } + } + + public void run() { + String logo = " ____ _\n" + + "| _ \\ _ _| | _____\n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + System.out.println("____________________________________________________________\nHello! I'm Duke\nWhat can I do for you?\n____________________________________________________________"); + + boolean isEnded = false; + Scanner sc= new Scanner(System.in); + + while(!isEnded) { + String userInput = sc.nextLine(); + System.out.println("____________________________________________________________"); + + Parser parser = new Parser(); + parser.parsing(userInput); + String command = parser.getCommand(); + + switch(command) { + case "todo": + String todoName = parser.getTaskName(); + tasks.addTodo(todoName, true); + break; + case "deadline": + String deadlineName = parser.getTaskName(); + String deadline = parser.getTimeline(); + tasks.addDeadline(deadlineName, deadline, true); + break; + case "event": + String eventName = parser.getTaskName(); + String timeline = parser.getTimeline(); + tasks.addEvent(eventName, timeline, true); + break; + case "bye": + System.out.println("Bye. Hope to see you again soon!"); + isEnded = true; + break; + case "list": + tasks.printList(); + break; + case "done": + int doneRank = Integer.parseInt(parser.getTaskName()); + tasks.updateTaskStatus(doneRank, true); + break; + case "remove": + int removeRank = Integer.parseInt(parser.getTaskName()); + tasks.removeTask(removeRank - 1 ); + break; + case "find": + String keyWord = parser.getTaskName(); + tasks.findWord(keyWord); + break; + default: + System.out.println("Please give an appropriate response."); + throw new DukeException("generic"); + + } + + System.out.println("____________________________________________________________"); + } + + } + + public static void main(String[] args) { + new DukeMan("data/memory.txt").run(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java new file mode 100644 index 0000000000..83fa9352af --- /dev/null +++ b/src/main/java/duke/Event.java @@ -0,0 +1,30 @@ +package duke; +/** + * Events are tasks that have a timeline. + * + * @author Dominic Siew Zhen Yu + */ +public class Event extends Task{ + private String timeline; + String TASKINDICATOR = "[E]"; + + /** + * The constructor of the events class. + * + * @param userInput the name of the task + * @param timeline the period of which the event is taking place + */ + public Event(String userInput, String timeline) { + super(userInput); + this.timeline = timeline; + } + + /** + * printName() method returns the name and the task indicator of the Event object. + * + * @return a string representation of a Event object + */ + public String printName() { + return TASKINDICATOR + " " + super.printName() + " (at: " + this.timeline + " )"; + } +} \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 0000000000..83609cb08d --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,75 @@ +package duke; + +/** + * Parser class parses the commands given by the users. + */ + +public class Parser { + + private String taskName; + private String command; + private String deadlineOrTimeline; + + public Parser() { + } + + /** + * parsing(String input) takes in a string from the given input and parses the string + * into the taskName, command and deadlineOrTimeline variables. + * @param input the given input by the user + */ + + public void parsing(String input) { + String[] parsedInput = input.split(" ", 2); + String eventType = parsedInput[0]; + this.command = eventType; + + if (this.command.equals("deadline")){ + String[] parsedAgain = parsedInput[1].split(" /by ", 2); + this.taskName = parsedAgain[0]; + this.deadlineOrTimeline = parsedAgain[1]; + } else if (this.command.equals("event")) { + String[] parsedAgain = parsedInput[1].split(" /at ", 2); + this.taskName = parsedAgain[0]; + this.deadlineOrTimeline = parsedAgain[1]; + } else if (this.command.equals("todo")) { + this.taskName = parsedInput[1]; + } else if (this.command.equals("bye")){ + + } else if (this.command.equals("done")) { + this.taskName = parsedInput[1]; + } else if (this.command.equals("remove")) { + this.taskName = parsedInput[1]; + } else if (this.command.equals("find")) { + this.taskName = parsedInput[1]; + } + } + + /** + * getCommand() returns the command of the user's input. + * @return the command of the user's input + */ + + public String getCommand() { + return this.command; + } + + /** + * getTaskName() returns the task's name from the user's input. + * @return the name of the task + */ + + public String getTaskName() { + return this.taskName; + } + + /** + * getTimeline() returns the timeline or the deadline of + * the event/deadline respectively. + * @return the timeline/deadline of the task + */ + + public String getTimeline() { + return this.deadlineOrTimeline; + } +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 0000000000..8583dcb2c4 --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,42 @@ +package duke; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +public class Storage { + + private File memory; + + public Storage(String filePath) { + File file = new File(filePath); + + + try { + if (!file.exists()){ + file.getParentFile().mkdir(); + file.createNewFile();System.out.println("hello"); + } + this.memory = file; + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public File load() { + return this.memory; + } + + public void addTaskToMemory(String event) { + try { + FileWriter fw = null; + fw = new FileWriter(this.memory, true); + fw.write(event + "\n"); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java new file mode 100644 index 0000000000..ef268ba3d8 --- /dev/null +++ b/src/main/java/duke/Task.java @@ -0,0 +1,58 @@ +package duke; +/** + * Task are work that has a name (that's a string) and a boolean which is true when a task is completed. + * + * @author Dominic Siew Zhen Yu + */ +public class Task { + private String name; + private boolean completed; + + private String COMPLETED = "[✓]"; + private String UNCOMPLETED = "[✕]"; + + /** + * the constructor of the Task class. + * + * @param name the name of the task + */ + + public Task(String name) { + this.name = name; + completed = false; + } + + /** + * printName() method returns the name of the Task object, and whether the task is completed or not. + * + * @return a string representation of a Task object + */ + + public String printName() { + String completionStatus = completed? COMPLETED: UNCOMPLETED; + return completionStatus + " " + this.name; + } + + /** + * getName() just returns the name of the tasks + * + * @return the name of the task + */ + + public String getName() { + return this.name; + } + + + + /** + * the toggleComplete() method toggles the boolean "completed" when the task is completed, + * or if the user wishes to indicate that a task is incompleted if it is indicated as + * otherwise. + * + */ + + public void toggleComplete() { + completed = !completed; + } +} diff --git a/src/main/java/duke/Todos.java b/src/main/java/duke/Todos.java new file mode 100644 index 0000000000..ac3fb68d40 --- /dev/null +++ b/src/main/java/duke/Todos.java @@ -0,0 +1,30 @@ +package duke; +/** + * Todos are generic tasks, with a "Todo" indicator. + * + * @author Dominic Siew Zhen Yu + */ +public class Todos extends Task{ + String TASKINDICATOR = "[T]"; + + /** + * the constructor of the Todos object + * + * @param userInput the name of the object + */ + + public Todos (String userInput) { + super(userInput); + } + + /** + * printName() method returns the name of the Todos object, and an indication that it + * is a todos task. + * + * @return a string representation of a Task object + */ + + public String printName() { + return TASKINDICATOR + " " + super.printName(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 0000000000..004b77efa6 --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,20 @@ +package duke; + +/** + * Ui is a class that contains the user interface of the DukeMan program. + */ + +public class Ui { + /** + * constructor for a Ui object + */ + public Ui() {} + + /** + * showLoadingError() outputs a string if there are errors processing the user's input. + */ + + public void showLoadingError() { + System.out.println("Apologies there was some errors processing your tasks."); + } +} diff --git a/src/main/java/duke/taskList.java b/src/main/java/duke/taskList.java new file mode 100644 index 0000000000..4eb851802c --- /dev/null +++ b/src/main/java/duke/taskList.java @@ -0,0 +1,270 @@ +package duke; +/** + * List refers to a task list that is used in the Duke program. + * An List object contains the tasks and several methods that work on the task. + * + * @author Dominic Siew Zhen Yu + */ + +import java.io.*; +import java.util.ArrayList; + +public class taskList { + private ArrayList taskList; + private FileWriter taskFile; + private boolean isFirstTimeOpening; + private Storage memory; + + /** + * The constructor for the List class that instantiates a List object. + */ + + public taskList() { + this.taskList = new ArrayList(); + File file = new File("data/memory.txt"); + this.memory = new Storage("data/memory.txt"); + } + + + public taskList(File memory) { + this.taskList = new ArrayList(); + this.memory = new Storage(memory.getPath()); + + try { + memory.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + File updated = new File("data/updated.txt"); + BufferedReader reader = null; + reader = new BufferedReader(new FileReader(memory)); + BufferedWriter writer = new BufferedWriter(new FileWriter(updated)); + String currentLine = ""; + int numberAdded = 1; + + while ((currentLine = reader.readLine()) != null) { + String[] seperate1 = currentLine.toString().split(" ", 2); + String taskTypeText = seperate1[0]; + String taskType; + + String[] seperate2 = seperate1[1].toString().split(" ", 2); + boolean isCompleted = (seperate2[0].equals("[✓]"))? true: false; + String eventInfo = seperate2[1]; + + switch(taskTypeText) { + case "[T]": + String eventname = eventInfo; + this.addTodo(eventname, false); + break; + case "[D]": + String[] seperateAgain = seperate2[1].split(" \\(by: ", 2); + String eventName = seperateAgain[0]; + String deadline = seperateAgain[1].split("\\)", 2)[0]; + this.addDeadline(eventName, deadline, false); + break; + case "[E]": + String[] seperateAgaining = seperate2[1].split(" \\(at: ", 2); + String event = seperateAgaining[0]; + String timeline = seperateAgaining[1].split("\\)", 2)[0]; + this.addEvent(event, timeline, false); + break; + } + + if (isCompleted){ + this.updateTaskStatus(numberAdded, false); + } + numberAdded++; + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * the addTask() places generic tasks into the list (which was used in level 1-3). + * + * @param userInput + */ + + public void addTask(String userInput) { + taskList.add(new Task(userInput)); + } + + /** + * the printList() method prints out the String representation of the tasks + * in the List object. + */ + + public void printList() { + int numb = 1; + System.out.println("Here are the tasks in your list:"); + for(Task task: taskList) { + System.out.println(numb + ". " + task.printName()); + numb++; + } + } + + /** + * the updateTaskStatus() method marks a task to be as completed. + * + * @param userInput the rank of the task in a list + */ + + public void updateTaskStatus(int userInput, boolean isInput) { + if (userInput > taskList.size()) { + System.out.println("please enter a number that's between 1 and " + taskList.size()); + return ; + } + Task task = taskList.get(userInput - 1); + + String lineToRemove = task.printName(); + task.toggleComplete(); + + if (isInput) { + System.out.println("Nice! I've marked this task as done:"); + System.out.println(task.printName()); + + try { +// File input = new File("data/memory.txt"); + File updated = new File("data/updated.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(this.memory.load())); + BufferedWriter writer = new BufferedWriter(new FileWriter(updated)); + + String currentLine = ""; + + while ((currentLine = reader.readLine()) != null) { + + if (currentLine.equals(lineToRemove)) { + writer.write(task.printName() + "\n"); + continue; + } + writer.write(currentLine + "\n"); + } + writer.close(); + reader.close(); + + boolean successful = updated.renameTo(this.memory.load()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + + } + + /** + * the addTodo() method adds todos task into the list object + * + * @param userInput the name of the todo task + */ + + public void addTodo(String userInput, boolean isInput) { + Task task = new Todos(userInput); + taskList.add(task); + + if (isInput) { + System.out.println("Got it. I've added this task:"); + System.out.println(task.printName()); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + this.memory.addTaskToMemory(task.printName()); + } else { + + } + + } + /** + * the addEvent() method adds Events task into the list object. + * + * @param name the name of the event + * @param timeline the period that the event is taking place + */ + public void addEvent(String name, String timeline, boolean isInput) { + Task task = new Event(name, timeline); + taskList.add(task); + if (isInput) { + System.out.println("Got it. I've added this task:"); + System.out.println(task.printName()); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + this.memory.addTaskToMemory(task.printName()); + } + + } + /** + * the addDeadline() method adds Events task into the list object. + * + * @param name the name of the deadline + * @param deadline the deadline of the deadline task + */ + public void addDeadline(String name, String deadline, boolean isInput) { + Task task = new Deadlines(name, deadline, isInput); + taskList.add(task); + + if (isInput) { + System.out.println("Got it. I've added this task:"); + System.out.println(task.printName()); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + this.memory.addTaskToMemory(task.printName()); + } + } + /** + * the removeTask() method removes the task from the list object. + * + * @param userInput the rank of the task in a list + */ + public void removeTask(int userInput) { + Task task = taskList.get(userInput); + System.out.println("Noted. I've removed the task:"); + System.out.println(task.printName()); + taskList.remove(userInput); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + + String lineToRemove = task.printName(); + + try { +// File input = new File("data/memory.txt"); + File updated = new File("data/updated.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(this.memory.load())); + BufferedWriter writer = new BufferedWriter(new FileWriter(updated)); + + String currentLine = ""; + + while ((currentLine = reader.readLine()) != null) { + if (currentLine.equals(lineToRemove)) { + + continue; + } + writer.write(currentLine + "\n"); + } + writer.close(); + reader.close(); + + boolean successful = updated.renameTo(memory.load()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * findWord() method finds methods with the keyword inputted by users. + * + */ + + public void findWord(String keyWord) { + System.out.println("Here are the matching tasks in your list:"); + int numb = 1; + for(Task task: taskList) { + if (task.getName().contains(keyWord)) { + System.out.println(numb + ". " + task.printName()); + } + numb++; + } + } +} diff --git a/src/main/memory.txt b/src/main/memory.txt new file mode 100644 index 0000000000..8c7f860466 --- /dev/null +++ b/src/main/memory.txt @@ -0,0 +1,2 @@ +[T] [✕] homework +[D] [✕] ass (by: Jan 1 2021 ) diff --git a/src/main/test/duke/ParserTest.java b/src/main/test/duke/ParserTest.java new file mode 100644 index 0000000000..1d0d483e04 --- /dev/null +++ b/src/main/test/duke/ParserTest.java @@ -0,0 +1,42 @@ +package duke; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ParserTest { + + @Test + void parsingEvents() { + Parser parser = new Parser(); + parser.parsing("todo homework"); + assertEquals(parser.getCommand(), "todo"); + assertEquals(parser.getTaskName(), "homework"); + + parser.parsing("deadline homework /by 2021-08-26"); + assertEquals(parser.getCommand(), "deadline"); + assertEquals(parser.getTaskName(), "homework"); + assertEquals(parser.getTimeline(), "2021-08-26"); + + parser.parsing("event homework /at monday"); + assertEquals(parser.getCommand(), "event"); + assertEquals(parser.getTaskName(), "homework"); + assertEquals(parser.getTimeline(), "monday"); + } + + @Test + void parsingCommands() { + Parser parser = new Parser(); + parser.parsing("done 2"); + assertEquals(parser.getCommand(), "done"); + assertEquals(parser.getTaskName(), "2"); + + parser.parsing("remove 3"); + assertEquals(parser.getCommand(), "remove"); + assertEquals(parser.getTaskName(), "3"); + + Parser parser2 = new Parser(); + parser2.parsing("bye"); + assertEquals(parser2.getCommand(), "bye"); + } +} \ No newline at end of file diff --git a/src/main/test/duke/taskListTest.java b/src/main/test/duke/taskListTest.java new file mode 100644 index 0000000000..3252a17569 --- /dev/null +++ b/src/main/test/duke/taskListTest.java @@ -0,0 +1,34 @@ +package duke; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.*; + +class taskListTest { + + @Test + void addingTask() { + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + duke.taskList tasks = new taskList(); + tasks.addTodo("homework", true); + assertEquals("Got it. I've added this task:\n[T] [✕] homework\n" + + "Now you have 1 tasks in the list.\n" , outContent.toString()); + + outContent.reset(); + tasks.addDeadline("assignment", "2021-01-01", true); + assertEquals("Got it. I've added this task:\n" + + "[D] [✕] assignment (by: Jan 1 2021 )\n" + + "Now you have 2 tasks in the list.\n", outContent.toString()); + + outContent.reset(); + tasks.addEvent("carnival", "Monday 4-5PM", true); + assertEquals("Got it. I've added this task:\n" + + "[E] [✕] carnival (at: Monday 4-5PM )\n" + + "Now you have 3 tasks in the list.\n", outContent.toString()); + } +} \ No newline at end of file diff --git a/src/main/updated.txt b/src/main/updated.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e7..99d638f1a9 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,49 @@ Hello from - ____ _ -| _ \ _ _| | _____ + ____ _lis +| _ \ _ _| | _____ | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| +____________________________________________________________ +Hello! I'm Duke +What can I do for you? +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[T][UNCOMPLETED]meeting +Now you have 1 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[D][UNCOMPLETED]application(by: 4th August) +Now you have 2 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[E][UNCOMPLETED]carnival(at: January 4-5pm) +Now you have 3 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Here are the tasks in your list: +1. [T][UNCOMPLETED]meeting +2. [D][UNCOMPLETED]application(by: 4th August) +3. [E][UNCOMPLETED]carnival(at: January 4-5pm) +____________________________________________________________ +____________________________________________________________ +Noted. I've removed the task: +[E][UNCOMPLETED]carnival(at: January 4-5pm) +Now you have 2 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Here are the tasks in your list: +1. [T][UNCOMPLETED]meeting +2. [D][UNCOMPLETED]application(by: 4th August) +____________________________________________________________ +____________________________________________________________ +Nice! I've marked this task as done: +[D][COMPLETED]application(by: 4th August) +____________________________________________________________ +____________________________________________________________ +Bye. Hope to see you again soon! +____________________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb2..2c21dbaa64 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,8 @@ +todo meeting +deadline application /by 4th August +event carnival /at January 4-5pm +list +remove 3 +list +done 2 +bye \ No newline at end of file diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh old mode 100644 new mode 100755 index e169618a34..c9ec870033 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,7 +13,7 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/Duke.java +if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java then echo "********** BUILD FAILURE **********" exit 1