diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..87b738cbd0 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..44e7c4d1d7 --- /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-5.2.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000000..af6708ff22 --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## 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"' + +# 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, switch paths to Windows format before running java +if $cygwin ; 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=$((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" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000000..0f8d5937c4 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@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 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" + +@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/duke/.idea/modules.xml b/src/main/duke/.idea/modules.xml new file mode 100644 index 0000000000..8069133725 --- /dev/null +++ b/src/main/duke/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/duke/.idea/vcs.xml b/src/main/duke/.idea/vcs.xml new file mode 100644 index 0000000000..17fbd1b823 --- /dev/null +++ b/src/main/duke/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/duke/.idea/workspace.xml b/src/main/duke/.idea/workspace.xml new file mode 100644 index 0000000000..3649d07a24 --- /dev/null +++ b/src/main/duke/.idea/workspace.xml @@ -0,0 +1,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1582718953288 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/.idea/.gitignore b/src/main/java/.idea/.gitignore new file mode 100644 index 0000000000..d6fd6f21b4 --- /dev/null +++ b/src/main/java/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/src/main/java/.idea/misc.xml b/src/main/java/.idea/misc.xml new file mode 100644 index 0000000000..909657acfe --- /dev/null +++ b/src/main/java/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/main/java/.idea/modules.xml b/src/main/java/.idea/modules.xml new file mode 100644 index 0000000000..151d513fae --- /dev/null +++ b/src/main/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/.idea/vcs.xml b/src/main/java/.idea/vcs.xml new file mode 100644 index 0000000000..17fbd1b823 --- /dev/null +++ b/src/main/java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 0000000000..99591ecf8b --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,36 @@ +simport java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents a deadline object characterized by an instrution and a date and time. + */ +public class Deadline extends Task{ + private LocalDateTime time; + + /** + * Creates a new Deadline with the given instruction and time. + */ + public Deadline (String instruction, LocalDateTime time) { + super(instruction); + this.time = time; + } + + /** + * Gets the time of a deadline. + * @return the time of this deadline. + */ + public LocalDateTime getTime() { + return this.time; + } + + /** + * Gets a string representation of a deadline. + * @return the string representation of this deadline. + */ + @Override + public String toString() { + return "[D]" + super.toString() + "(by: " + + time.format(DateTimeFormatter.ofPattern("MMM d yyyy HH:mm")) + ")"; + } +} diff --git a/src/main/java/DialogBox.java b/src/main/java/DialogBox.java new file mode 100644 index 0000000000..746f379969 --- /dev/null +++ b/src/main/java/DialogBox.java @@ -0,0 +1,50 @@ +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +public class DialogBox extends HBox { + private Label dialog; + private ImageView iv; + + private DialogBox(Label dialog, ImageView iv) { + this.dialog = dialog; + this.iv = iv; + try { + FXMLLoader l = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml")); + l.setController(this); + l.setRoot(this); + l.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.java b/src/main/java/Duke.java index 5d313334cc..e4e421ad2d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,79 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} +import java.io.FileNotFoundException; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.layout.Region; +import javafx.stage.Stage; +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; + +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; + +/** + * Represents a Duke robot that deal with multiple tasks. + */ +public class Duke extends Application{ + /** + * The Storage used. + */ + private Storage storage; + /** + * The list of task. + */ + private TaskList tasks; + /** + * The user interface. + */ + private Ui ui; + /** + * The Parser used. + */ + private Parser parser; + + /** + * Creates a new Duke with the given filePath. + */ + public Duke (String filePath) { + this.ui = new Ui(); + this.storage = new Storage(filePath); + try { + this.tasks = new TaskList(storage.load()); + } catch (FileNotFoundException e) { + ui.showLoadingError(); + this.tasks = new TaskList(); + } + } + + /** + * Runs Duke. + */ + public void run() { + this.parser = new Parser(ui, tasks, storage); + parser.run(); + } + + /** + * Runs Duke with the given filePath. + */ + public static void main(String[] args) { + new Duke("C:\\Users\\h1430\\Documents\\CS2103T\\duke\\src\\main\\java\\data\\duke.txt").run(); + } + + @Override + public void start(Stage stage) { + try { + FXMLLoader l = new FXMLLoader(Duke.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = (AnchorPane) l.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 0000000000..9b8764e2de --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,21 @@ +/** + * Represents exceptions that may happen while running Duke. + */ +public class DukeException extends Exception { + private String type; + + /** + * Creates a new DukeException with the given type. + */ + public DukeException (String type) { + this.type = type; + } + + /** + * Gets the type of a DukeException. + * @return the type of this exception. + */ + public String getType() { + return this.type; + } +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 0000000000..0f6528bf8f --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,36 @@ +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents an event object characterized by an instrution and a date and time. + */ +public class Event extends Task { + private LocalDateTime time; + + /** + * Creates a new Event with the given instruction and time. + */ + public Event (String instruction, LocalDateTime time) { + super(instruction); + this.time = time; + } + + /** + * Gets the time of an event. + * @return the time of this event. + */ + public LocalDateTime getTime() { + return this.time; + } + + /** + * Gets a string representation of an event. + * @return the string representation of this event. + */ + @Override + public String toString() { + return "[E]" + super.toString() + "(at: " + + time.format(DateTimeFormatter.ofPattern("MMM d yyyy HH:mm")) + ")"; + } +} diff --git a/src/main/java/Launcher.java b/src/main/java/Launcher.java new file mode 100644 index 0000000000..a2ff3cd6ec --- /dev/null +++ b/src/main/java/Launcher.java @@ -0,0 +1,10 @@ +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + */ +public class Launcher { + public static void main(String[] args) { + Application.launch(Duke.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/MainWindow.java b/src/main/java/MainWindow.java new file mode 100644 index 0000000000..8b1ef47912 --- /dev/null +++ b/src/main/java/MainWindow.java @@ -0,0 +1,51 @@ +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.stage.Stage; + +import static duke.common.Messages.WELCOME_MSG; + +/** + * 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 Stage window; + + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); + private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + + private void handleUserInput() { + String input = userInput.getText(); + String response = DukeStub.getResponse(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, user), + DialogBox.getDukeDialog(response, duke) + ); + userInput.clear(); + } + + public static String getResponse(String input) { + return String.format("ok, %s", input); + } + +} \ No newline at end of file diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 0000000000..5cb9730577 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,108 @@ +import java.util.Scanner; +import java.util.ArrayList; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents a Parser. The Parser class deals with making sense of the user command and running the command. + */ +public class Parser { + /** + * The user interface. + */ + private Ui ui; + /** + * The list of task. + */ + private TaskList tasks; + /** + * The storage object used. + */ + private Storage storage; + + /** + * Creates a new Parser with the given ui, tasklist and storage. + */ + public Parser (Ui ui, TaskList tasks, Storage storage) { + this.ui = ui; + this.tasks = tasks; + this.storage = storage; + } + + /** + * Greets and runs the user command line by line. + */ + public void run() { + Scanner scanner = new Scanner(System.in); + ui.greet(); + while (scanner.hasNextLine()) { + String usercommand = scanner.nextLine(); + try { + handleInstruction(usercommand); + } catch (DukeException e) { + ui.showCommandError(e); + } catch (AssertionError e) { + ui.showAssertionError(); + } + } + } + + /** + * Deals with the user command (instruction). + * @throws DukeException when the instruction has an invalid task type, + * or the task type is specified but content is empty. + */ + public void handleInstruction(String instruction) throws DukeException { + if (instruction.equals("bye")) { + ui.exit(); + } else if (instruction.equals("list")) { + tasks.printList(); + } else if (instruction.split(" ")[0].equals("done")) { + int index = Integer.parseInt(instruction.split(" ")[1]); + assert index < 0 : "invalid index"; + tasks.doneTask(index); + storage.record(tasks.getTasks()); + + } else if (instruction.split(" ")[0].equals("delete")) { + int index = Integer.parseInt(instruction.split(" ")[1]); + assert index < 0 : "invalid index"; + tasks.deleteTask(index); + storage.record(tasks.getTasks()); + } else if (instruction.split(" ")[0].equals("find")) { + String keyword = instruction.replace("find ", ""); + tasks.find(keyword); + } else if (instruction.split(" ")[0].equals("todo")) { + String task = instruction.replace("todo", ""); + if (!task.equals("")) { + tasks.addTask(new ToDo(task)); + storage.record(tasks.getTasks()); + } else { + throw new DukeException("EmptyToDo"); + } + } else if (instruction.split(" ")[0].equals("deadline")) { + String task = instruction.split("/")[0].replace("deadline", ""); + if (!task.equals("")) { + String time = instruction.split("/")[1].replace("by ", ""); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + tasks.addTask(new Deadline(task, LocalDateTime.parse(time, formatter))); + storage.record(tasks.getTasks()); + } else { + throw new DukeException("EmptyDeadline"); + } + } else if (instruction.split(" ")[0].equals("event")) { + String task = instruction.split("/")[0].replace("event", ""); + if (!task.equals("")) { + String time = instruction.split("/")[1].replace("at ", ""); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + tasks.addTask(new Event(task, LocalDateTime.parse(time, formatter))); + storage.record(tasks.getTasks()); + } else { + throw new DukeException("EmptyEvent"); + } + } else { + throw new DukeException("invalid"); + } + } + +} diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 0000000000..6e3a3887b8 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,103 @@ +import java.util.Scanner; +import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Represents a Storage. The Storage class deals with loading tasks from the file and saving tasks in the file. + */ +public class Storage { + /** + * The filePath where tasks are loaded and saved. + */ + private String filePath; + /** + * The user interface. + */ + private Ui ui; + + /** + * Creates a new Storage with the given filepath. + * @param filePath The filepath where to load from and write to. + */ + public Storage(String filePath) { + this.filePath = filePath; + this.ui = new Ui(); + } + + /** + * Loads tasks from the file. + * @throws FileNotFoundException when the filePath is invalid. + */ + public ArrayList load() throws FileNotFoundException { + File file = new File(filePath); + Scanner scanner = new Scanner(file); + ArrayList tasks = new ArrayList(100); + while (scanner.hasNextLine()) { + String[] data = scanner.nextLine().split(" \\| "); + if (data[0].equals("T")) { + ToDo todo = new ToDo(data[2]); + if (data[1].equals("1")) { + todo.setDone(); + } + tasks.add(todo); + } else if (data[0].equals("D")) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + Deadline deadline = new Deadline(data[2], LocalDateTime.parse(data[3], formatter)); + if (data[1].equals("1")) { + deadline.setDone(); + } + tasks.add(deadline); + } else if (data[0].equals("E")) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + Event event = new Event(data[2], LocalDateTime.parse(data[3], formatter)); + if (data[1].equals("1")) { + event.setDone(); + } + tasks.add(event); + } + } + return tasks; + } + + /** + * Saves tasks to the file. + * @throws IOException. + */ + public void writeToFile(ArrayList tasks) throws IOException { + FileWriter fileWriter = new FileWriter(filePath); + for (int i = 0; i < tasks.size(); i++) { + Task task = tasks.get(i); + if (task instanceof ToDo) { + String text = "T | " + (task.getStatus() ? "1" : "0") + " | " + task.getInstruction(); + fileWriter.write(text + System.lineSeparator()); + } else if (task instanceof Deadline) { + String text = "D | " + (task.getStatus() ? "1" : "0") + " | " + task.getInstruction() + " | " + + ((Deadline) task).getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm")); + fileWriter.write(text + System.lineSeparator()); + } else if (task instanceof Event) { + String text = "E | " + (task.getStatus() ? "1" : "0") + " | " + task.getInstruction() + " | " + + ((Event) task).getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm")); + fileWriter.write(text + System.lineSeparator()); + } + } + fileWriter.close(); + } + + /** + * Tries to record the changes tasks to the file. + */ + public void record(ArrayList tasks) { + try { + writeToFile(tasks); + } catch (IOException e) { + ui.showIOException(e); + } + } + +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 0000000000..a46af12242 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,58 @@ +/** + * Represents a task object characterized by an instrution. + */ +public class Task { + /** + * The instrution of this task. + */ + protected String instruction; + /** + * Indicates whether this task is done or not. + */ + protected boolean isDone; + + /** + * Creates a new Task with the given instruction. + * Initially the task is not done. + */ + public Task (String instruction) { + this.instruction = instruction; + this.isDone = false; + } + + /** + * Gets the instruction of a task. + * @return the instruction of this task. + */ + public String getInstruction() { + return this.instruction; + } + + /** + * Gets the status of a task. + * @return true if this task is done, false if this task is not done yet. + */ + public boolean getStatus() { + return this.isDone; + } + + /** + * Sets this task as done. + */ + public void setDone() { + this.isDone = true; + } + + /** + * Gets a string representation of a task. + * @return the string representation of this tasks. + */ + @Override + public String toString() { + if (isDone) { + return "[\u2713]" + instruction; + } else { + return "[\u2718]" + instruction; + } + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 0000000000..566598b6e9 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,92 @@ +import java.util.ArrayList; + +/** + * Represents the task list that stores all current tasks. + */ +public class TaskList { + /** + * The list of task. + */ + private ArrayList tasks; + /** + * The user interface. + */ + private Ui ui; + + /** + * Creates a new TaskList with the given arraylist that contains tasks. + */ + public TaskList (ArrayList tasks) { + this.tasks = tasks; + this.ui = new Ui(); + } + + /** + * Creates an empty TaskList (with maximum capacity 100). + */ + public TaskList() { + this.tasks = new ArrayList(100); + this.ui = new Ui(); + } + + /** + * Gets the task list. + * @return list of this TaskList. + */ + public ArrayList getTasks() { + return this.tasks; + } + + /** + * Adds a task to this tasklist. + * @param task The task to be added. + */ + public void addTask (Task task) { + tasks.add(task); + ui.add(task, tasks); + } + + /** + * Marks a task in this list as done. + * @param n Index of the task that is done. + */ + public void doneTask (int n) { + tasks.get(n-1).setDone(); + ui.done(n, tasks); + } + + /** + * Deletes a task in this list. + * @param n Index of the task to be deleted. + */ + public void deleteTask (int n) { + ui.delete(n, tasks); + tasks.remove(n-1); + ui.count(tasks); + } + + /** + * Finds tasks that matches the given keyword. + * @Param keyword The given keyword. + */ + public void find (String keyword) { + ArrayList results = new ArrayList(); + for (Task task: tasks) { + if (task.getInstruction().contains(keyword)) { + results.add(task); + } + } + ui.findKeyword(results); + } + + /** + * Prints the list of tasks. + */ + public void printList() { + for (int i = 0; i < tasks.size(); i++) { + if (tasks.get(i) != null) { + System.out.println((i + 1) + ". " + tasks.get(i).toString()); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 0000000000..309b89438b --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,21 @@ +/** + * Represents a todo object characterized by an instrution. + */ +public class ToDo extends Task { + + /** + * Creates a new ToDo with the given instruction. + */ + public ToDo (String instruction) { + super(instruction); + } + + /** + * Gets a string representation of a todo. + * @return the string representation of this todo. + */ + @Override + public String toString() { + return "[T]" + super.toString(); + } +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 0000000000..84c2be6856 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,109 @@ +import java.io.IOException; +import java.util.ArrayList; + +/** + * Represents the user interface. This Ui class outputs messages and interacts with the user. + */ +public class Ui { + + /** + * Creates a new Ui. + */ + public Ui() { + + } + + /** + * Greets to the user. + */ + public void greet() { + System.out.println("Hello! I'm Duke\n What can I do for you?"); + } + + /** + * Farewell to the user. + */ + public void exit() { + System.out.println("Bye. Hope to see you again soon!"); + } + + /** + * Adds a task for the user. + */ + public void add (Task task, ArrayList tasks) { + System.out.println("Got it. I've added this task:\n" + task.toString() + + "\n Now you have " + tasks.size() + " tasks in the list."); + } + + /** + * Marks a task as done for the user. + */ + public void done (int n, ArrayList tasks) { + System.out.println("Nice! I've marked this task as done: \n" + + tasks.get(n-1).toString()); + } + + /** + * Deletes a task for the user. + */ + public void delete (int n, ArrayList tasks) { + System.out.println("Noted. I've removed this task:\n" + + tasks.get(n-1).toString()); + } + + /** + * Finds tasks that matches the given keyword for the user. + * @Param results The list of matching tasks. + */ + public void findKeyword (ArrayList results) { + System.out.println("Here are the matching tasks in your list:"); + for (int i = 0; i < results.size(); i++) { + if (results.get(i) != null) { + System.out.println((i + 1) + ". " + results.get(i).toString()); + } + } + } + + /** + * Counts the number of task in the current list for the user. + */ + public void count (ArrayList tasks) { + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + + /** + * Shows command error for the user while catching duke exceptions. + */ + public void showCommandError (DukeException e) { + if (e.getType().equals("EmptyToDo")) { + System.out.println("OOPS!!! The description of a todo cannot be empty."); + } + if (e.getType().equals("EmptyDeadline")) { + System.out.println("OOPS!!! The description of a deadline cannot be empty."); + } + if (e.getType().equals("EmptyEvent")) { + System.out.println("OOPS!!! The description of a event cannot be empty."); + } + if (e.getType().equals("invalid")) { + System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } + + /** + * Shows loading error for the user while files are not found. + */ + public void showLoadingError() { + System.out.println("File not found"); + } + + /** + * Shows error for the user while catching IOExceptions. + */ + public void showIOException (IOException e) { + System.out.println("Oops! " + e.getMessage()); + } + + public void showAssertionError() { + System.out.println("Oops! The index is invalid."); + } +} \ No newline at end of file diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt new file mode 100644 index 0000000000..0e40dc8d36 --- /dev/null +++ b/src/main/java/data/duke.txt @@ -0,0 +1,4 @@ +T | 1 | read book +D | 0 | return book | Jun 6 2020 1100 +E | 0 | project meeting | Aug 6 2020 1400 +T | 1 | join sports club \ No newline at end of file diff --git a/src/main/java/text-ui-test/ACTUAL.txt b/src/main/java/text-ui-test/ACTUAL.txt new file mode 100644 index 0000000000..707beb82b4 --- /dev/null +++ b/src/main/java/text-ui-test/ACTUAL.txt @@ -0,0 +1,18 @@ +Hello! I'm Duke + What can I do for you? +Got it. I've added this task: +[T][✘] borrow book + Now you have 1 tasks in the list. +Got it. I've added this task: +[D][✘] return book (by: Sunday) + Now you have 2 tasks in the list. +Got it. I've added this task: +[E][✘] meeting (at: 1200) + Now you have 3 tasks in the list. +OOPS!!! I'm sorry, but I don't know what that means :-( +1. [T][✘] borrow book +2. [D][✘] return book (by: Sunday) +3. [E][✘] meeting (at: 1200) +Nice! I've marked this task as done: +[E][✓] meeting (at: 1200) +Bye. Hope to see you again soon! diff --git a/src/main/java/text-ui-test/EXPECTED.txt b/src/main/java/text-ui-test/EXPECTED.txt new file mode 100644 index 0000000000..09049b28c8 --- /dev/null +++ b/src/main/java/text-ui-test/EXPECTED.txt @@ -0,0 +1,18 @@ +Hello! I'm Duke + What can I do for you? +Got it. I've added this task: +[T][✘] borrow book + Now you have 1 tasks in the list. +Got it. I've added this task: +[D][✘] return book (by: Sunday) + Now you have 2 tasks in the list. +Got it. I've added this task: +[E][✘] meeting (at: 1200) + Now you have 3 tasks in the list. +OOPS!!! I'm sorry, but I don't know what that means :-( +1. [T][✘] borrow book +2. [D][✘] return book (by: Sunday) +3. [E][✘] meeting (at: 1200) +Nice! I've marked this task as done: +[E][✓] meeting (at: 1200) +Bye. Hope to see you again soon! \ No newline at end of file diff --git a/src/main/java/text-ui-test/input.txt b/src/main/java/text-ui-test/input.txt new file mode 100644 index 0000000000..6cddb7b3e7 --- /dev/null +++ b/src/main/java/text-ui-test/input.txt @@ -0,0 +1,7 @@ +todo borrow book +deadline return book /by Sunday +event meeting /at 1200 +blah +list +done 3 +bye \ No newline at end of file diff --git a/src/main/java/text-ui-test/runtest.bat b/src/main/java/text-ui-test/runtest.bat new file mode 100644 index 0000000000..149c050780 --- /dev/null +++ b/src/main/java/text-ui-test/runtest.bat @@ -0,0 +1,21 @@ +@ECHO OFF + +REM create bin directory if it doesn't exist +if not exist ..\bin mkdir ..\bin + +REM delete output from previous run +del ACTUAL.TXT + +REM compile the code into the bin folder +javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\Duke.java +IF ERRORLEVEL 1 ( + echo ********** BUILD FAILURE ********** + exit /b 1 +) +REM no error here, errorlevel == 0 + +REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT +java -classpath ..\bin Duke < input.txt > ACTUAL.TXT + +REM compare the output to the expected output +FC ACTUAL.TXT EXPECTED.TXT \ No newline at end of file diff --git a/src/test/java/.idea/.gitignore b/src/test/java/.idea/.gitignore new file mode 100644 index 0000000000..d6fd6f21b4 --- /dev/null +++ b/src/test/java/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/src/test/java/.idea/modules.xml b/src/test/java/.idea/modules.xml new file mode 100644 index 0000000000..151d513fae --- /dev/null +++ b/src/test/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/test/java/.idea/vcs.xml b/src/test/java/.idea/vcs.xml new file mode 100644 index 0000000000..17fbd1b823 --- /dev/null +++ b/src/test/java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/test/java/EventTest.java b/src/test/java/EventTest.java new file mode 100644 index 0000000000..6a91bba0a1 --- /dev/null +++ b/src/test/java/EventTest.java @@ -0,0 +1,27 @@ +//package duke; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EventTest { + @Test + public void testEventToString(){ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + Event event = new Event("return book", LocalDateTime.parse("2000-01-01 2359", formatter)); + assertEquals("[E][\u2718] return book (at: Jan 1 2000 23:59)", event.toString()); + } + + @Test + public void testSetDone(){ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + Event event = new Event("return book", LocalDateTime.parse("2000-01-01 2359", formatter)); + assertEquals(false, event.getStatus()); + event.setDone(); + assertEquals(true, event.getStatus()); + } +} \ No newline at end of file diff --git a/src/test/java/TaskTest.java b/src/test/java/TaskTest.java new file mode 100644 index 0000000000..d976df15d1 --- /dev/null +++ b/src/test/java/TaskTest.java @@ -0,0 +1,18 @@ +//package duke; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TaskTest { + @Test + public void testToString() { + Task task = new Task("return book"); + assertEquals("[\u2718] return book", task.toString()); + } + @Test + public void testGetInstrution() { + Task task = new Task("return book"); + assertEquals("return book", task.getInstruction()); + } +} \ No newline at end of file