diff --git a/build.gradle b/build.gradle index 2b30c45c6f..fda04b6e7c 100644 --- a/build.gradle +++ b/build.gradle @@ -49,11 +49,11 @@ test { } application { - mainClassName = "seedu.duke.Duke" + mainClassName = "jarvis.Jarvis" } shadowJar { - archiveBaseName = "duke" + archiveBaseName = "jarvis" archiveClassifier = null } diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index 6e91cfdedc..30e0cc17c0 100644 --- a/src/main/java/META-INF/MANIFEST.MF +++ b/src/main/java/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -jarvis.Main-Class: jarvis.Jarvis +jarvis.GUI.Main-Class: jarvis.Jarvis diff --git a/src/main/java/jarvis/DialogBox.java b/src/main/java/jarvis/GUI/DialogBox.java similarity index 98% rename from src/main/java/jarvis/DialogBox.java rename to src/main/java/jarvis/GUI/DialogBox.java index f9d47b3420..1df8a5971d 100644 --- a/src/main/java/jarvis/DialogBox.java +++ b/src/main/java/jarvis/GUI/DialogBox.java @@ -1,4 +1,4 @@ -package jarvis; +package jarvis.GUI; import java.io.IOException; import java.util.Collections; @@ -58,4 +58,4 @@ public static DialogBox getDukeDialog(String text, Image img) { db.flip(); return db; } -} \ No newline at end of file +} diff --git a/src/main/java/jarvis/Main.java b/src/main/java/jarvis/GUI/Main.java similarity index 94% rename from src/main/java/jarvis/Main.java rename to src/main/java/jarvis/GUI/Main.java index a6c14c461e..de5d4a22d6 100644 --- a/src/main/java/jarvis/Main.java +++ b/src/main/java/jarvis/GUI/Main.java @@ -1,7 +1,8 @@ -package jarvis; +package jarvis.GUI; import java.io.IOException; +import jarvis.Jarvis; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -28,4 +29,4 @@ public void start(Stage stage) { e.printStackTrace(); } } -} \ No newline at end of file +} diff --git a/src/main/java/jarvis/MainWindow.java b/src/main/java/jarvis/GUI/MainWindow.java similarity index 92% rename from src/main/java/jarvis/MainWindow.java rename to src/main/java/jarvis/GUI/MainWindow.java index dde9a9cb4d..bf3a879d8d 100644 --- a/src/main/java/jarvis/MainWindow.java +++ b/src/main/java/jarvis/GUI/MainWindow.java @@ -1,5 +1,7 @@ -package jarvis; +package jarvis.GUI; +import jarvis.Jarvis; +import jarvis.Parser; import jarvis.exception.JarvisException; import javafx.application.Platform; import javafx.fxml.FXML; @@ -9,6 +11,7 @@ import javafx.scene.image.Image; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; + /** * Controller for MainWindow. Provides the layout for the other controls. */ @@ -27,6 +30,9 @@ public class MainWindow extends AnchorPane { private Image userImage = new Image(this.getClass().getResourceAsStream("/images/user.png")); private Image jarvisImage = new Image(this.getClass().getResourceAsStream("/images/jarvis.png")); + /** + * Initialise the scroll window in the UI with introduction message. + */ @FXML public void initialize() { scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); @@ -54,4 +60,4 @@ private void handleUserInput() throws JarvisException { } userInput.clear(); } -} \ No newline at end of file +} diff --git a/src/main/java/jarvis/Jarvis.java b/src/main/java/jarvis/Jarvis.java index a8ea736fa8..5efb854c1c 100644 --- a/src/main/java/jarvis/Jarvis.java +++ b/src/main/java/jarvis/Jarvis.java @@ -1,15 +1,23 @@ package jarvis; -import jarvis.task.TaskList; +import java.io.IOException; + +import jarvis.GUI.Main; import jarvis.exception.JarvisException; +import jarvis.task.TaskList; import javafx.application.Application; -import java.io.IOException; - +/** + * Jarvis is the main class where the program is initialised. + */ +public class Jarvis { + private static Parser parser; -public class Jarvis{ - static Parser parser; - public static void main(String[] args){ + /** + * Main method for program + * @param args + */ + public static void main(String[] args) { String filePath = "data/taskList.txt"; @@ -34,10 +42,9 @@ public static void main(String[] args){ } /** - * You should have your own function to generate a response to user input. - * Replace this stub with your completed method. + * Passes the input to the parser and returns the response */ - String getResponse(String input) throws JarvisException { + public String getResponse(String input) throws JarvisException { return parser.readCommand(input); } diff --git a/src/main/java/jarvis/Parser.java b/src/main/java/jarvis/Parser.java index 72e28d0927..82508c117c 100644 --- a/src/main/java/jarvis/Parser.java +++ b/src/main/java/jarvis/Parser.java @@ -1,13 +1,19 @@ package jarvis; -import jarvis.task.*; import jarvis.exception.JarvisException; +import jarvis.task.Deadline; +import jarvis.task.Event; +import jarvis.task.Task; +import jarvis.task.TaskList; +import jarvis.task.ToDo; +/** + * Parser processes commands given by user and returns a response by the Jarvis. + */ public class Parser { - - private static String introduction = "Hello. I am Jarvis \n" + private static final String INTRODUCTION = "Hello. I am Jarvis \n" + "What can I do for you today?"; - private static String farewell = "Goodbye, have a good day."; + private static final String FAREWELL = "Goodbye, have a good day."; private TaskList tasks; /** @@ -23,102 +29,168 @@ public Parser(TaskList tasks) { * @throws JarvisException if command entered is not recognised. */ public String readCommand(String input) throws JarvisException { - String output = ""; + String output = ""; - //check if userinput is bye, break if true - if (input.equals("bye")) { - return farewell; - } - //if userinput equals list, return task list - if (input.equals("list")) { - output = output + ("Here are the tasks in your list:\n"); - for (int i = 0; i < tasks.getList().size(); i++) { - if (tasks.getList().get(i) == null) { - break; - } - output = output + (i + 1) + ". " + tasks.getList().get(i).toString() + "\n"; - } - return output; - } - // if userInput equals find, find tasks which match given string - if (input.length() > 4 && input.substring(0,4).equals("find")) { - String keyword = input.substring((5)); - output = output + ("Here are the matching tasks in your list:\n"); - for (int i = 0; i < tasks.getList().size(); i++) { - Task currTask = tasks.getList().get(i); - if (currTask == null) { - return output; - } - if (currTask.toString().contains(keyword)) { - output = output + ((i + 1) + ". " + currTask) + "\n"; - } - } - return output; - } + //check if userinput is bye, break if true + if (equalsBye(input)) { + return FAREWELL; + } + //if userinput equals list, return task list + if (equalsList(input)) { + return handleList(tasks); + } + // if userInput equals find, find tasks which match given string + if (equalsFind(input)) { + return handleFind(tasks, input); + } - //if userinput equals mark, check which task and mark it - if (input.length() > 4 && input.substring(0, 4).equals("mark")) { - int toMark = Integer.parseInt(input.substring(5)) - 1; - tasks.getList().get(toMark).mark(); - output = "Great. I have marked this task as done:\n " + tasks.getList().get(toMark).toString(); - return output; - } - //if userinput equals unmark, check which task and unmark - if (input.length() > 6 && input.substring(0, 6).equals("unmark")) { - int toMark = Integer.parseInt(input.substring(7)) - 1; - tasks.getList().get(toMark).unmark(); - String markResponse = "Ok, I have marked this task as not done yet:\n "; - output = markResponse + tasks.getList().get(toMark).toString(); - return output; - } - //if userinput equals delete, check which task and delete - if (input.length() > 6 && input.substring(0, 6).equals("delete")) { - int toDelete = Integer.parseInt(input.substring(7)) - 1; - Task deleteTask = tasks.getList().get(toDelete); - tasks.getList().remove(toDelete); - String deleteResponse = "Noted. I have removed this task:\n "; - output = deleteResponse + deleteTask.toString(); - return output; - } + //if userinput equals mark, check which task and mark it + if (equalsMark(input)) { + return handleMark(tasks, input); + } + //if userinput equals unmark, check which task and unmark + if (equalsUnmark(input)) { + return handleUnmark(tasks, input); + } + //if userinput equals delete, check which task and delete + if (equalsDelete(input)) { + return handleDelete(tasks, input); + } - //if userinput equals to do add new to do task to list - if (input.length() > 3 && input.substring(0, 4).equals("todo")) { - String description = input.substring(4); - if (description.equals("")) { - throw new JarvisException("The description of a todo cannot be empty"); - } - tasks.getList().add(new ToDo(description)); - output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) - + "\nNow you have " + (Task.count) + " tasks in the list."); - return output; + //if userinput equals to do add new to do task to list + if (equalsToDo(input)) { + return handleToDo(tasks, input); + } + + //if userinput equals deadline add new deadline task to list + if (equalsDeadline(input)) { + return handleDeadline(tasks, input); + } + //if userinput equals event add new event task to list + if (equalsEvent(input)) { + return handleEvent(tasks, input); + } else { + return "I'm sorry, but I don't know what that means"; + } + } + + public static String introduction() { + return INTRODUCTION; + } + + private static String handleList(TaskList tasks) { + String output = ("Here are the tasks in your list:\n"); + for (int i = 0; i < tasks.getList().size(); i++) { + if (tasks.getList().get(i) == null) { + break; } + output = output + (i + 1) + ". " + tasks.getList().get(i).toString() + "\n"; + } + return output; + } - //if userinput equals deadline add new deadline task to list - if (input.length() > 8 && input.substring(0, 8).equals("deadline")) { - int divisor = input.indexOf("/by"); - String description = input.substring(9, divisor - 1); - String date = input.substring(divisor + 4); - tasks.getList().add(new Deadline(description, date)); - output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) - + "\nNow you have " + (Task.count) + " tasks in the list."); + private static String handleFind(TaskList tasks, String input) { + String keyword = input.substring((5)); + String output = ("Here are the matching tasks in your list:\n"); + for (int i = 0; i < tasks.getList().size(); i++) { + Task currTask = tasks.getList().get(i); + if (currTask == null) { return output; } - //if userinput equals event add new event task to list - if (input.length() > 7 && input.substring(0, 5).equals("event")) { - int divisor = input.indexOf("/at"); - String description = input.substring(6, divisor - 1); - String date = input.substring(divisor + 4); - tasks.getList().add(new Event(description, date)); - output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) - + "\nNow you have " + (Task.count) + " tasks in the list."); - return output; - } else { - return "I'm sorry, but I don't know what that means"; + if (currTask.toString().contains(keyword)) { + output = output + ((i + 1) + ". " + currTask) + "\n"; } + } + return output; } - public static String introduction() { - return introduction; + private static String handleMark(TaskList tasks, String input) { + int toMark = Integer.parseInt(input.substring(5)) - 1; + tasks.getList().get(toMark).mark(); + String output = "Great. I have marked this task as done:\n " + tasks.getList().get(toMark).toString(); + return output; } -} + private static String handleUnmark(TaskList tasks, String input) { + int toMark = Integer.parseInt(input.substring(7)) - 1; + tasks.getList().get(toMark).unmark(); + String markResponse = "Ok, I have marked this task as not done yet:\n "; + String output = markResponse + tasks.getList().get(toMark).toString(); + return output; + } + + private static String handleDelete(TaskList tasks, String input) { + int toDelete = Integer.parseInt(input.substring(7)) - 1; + Task deleteTask = tasks.getList().get(toDelete); + tasks.getList().remove(toDelete); + String deleteResponse = "Noted. I have removed this task:\n "; + String output = deleteResponse + deleteTask.toString(); + return output; + } + + private static String handleToDo(TaskList tasks, String input) throws JarvisException { + String description = input.substring(4); + if (description.equals("")) { + throw new JarvisException("The description of a todo cannot be empty"); + } + tasks.getList().add(new ToDo(description)); + String output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.getCount() - 1) + + "\nNow you have " + (Task.getCount()) + " tasks in the list."); + return output; + } + + private static String handleDeadline(TaskList tasks, String input) { + int divisor = input.indexOf("/by"); + String description = input.substring(9, divisor - 1); + String date = input.substring(divisor + 4); + tasks.getList().add(new Deadline(description, date)); + String output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.getCount() - 1) + + "\nNow you have " + (Task.getCount()) + " tasks in the list."); + return output; + } + + private static String handleEvent(TaskList tasks, String input) { + int divisor = input.indexOf("/at"); + String description = input.substring(6, divisor - 1); + String date = input.substring(divisor + 4); + tasks.getList().add(new Event(description, date)); + String output = ("Got it. I've added this task:\n " + tasks.getList().get(Task.getCount() - 1) + + "\nNow you have " + (Task.getCount()) + " tasks in the list."); + return output; + } + + private static boolean equalsBye(String input) { + return input.equals("bye"); + } + + private static boolean equalsList(String input) { + return input.equals("list"); + } + private static boolean equalsFind(String input) { + return input.length() > 4 && input.substring(0,4).equals("find"); + } + + private static boolean equalsMark(String input) { + return input.length() > 4 && input.substring(0,4).equals("mark"); + } + + private static boolean equalsUnmark(String input) { + return input.length() > 6 && input.substring(0,6).equals("unmark"); + } + + private static boolean equalsDelete(String input) { + return input.length() > 6 && input.substring(0,6).equals("delete"); + } + + private static boolean equalsToDo(String input) { + return input.length() > 3 && input.substring(0,4).equals("todo"); + } + + private static boolean equalsDeadline(String input) { + return input.length() > 8 && input.substring(0,8).equals("deadline"); + } + + private static boolean equalsEvent(String input) { + return input.length() > 7 && input.substring(0,5).equals("event"); + } +} diff --git a/src/main/java/jarvis/Storage.java b/src/main/java/jarvis/Storage.java index d87204ce6a..f4afe3b719 100644 --- a/src/main/java/jarvis/Storage.java +++ b/src/main/java/jarvis/Storage.java @@ -1,13 +1,21 @@ package jarvis; -import jarvis.task.*; - +import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.io.File; import java.util.Scanner; + +import jarvis.task.Deadline; +import jarvis.task.Event; +import jarvis.task.Task; +import jarvis.task.TaskList; +import jarvis.task.ToDo; + +/** + * Storage stores the File Path and handles loading and writing to the file. + */ public class Storage { private String filePath; @@ -34,30 +42,13 @@ public List load() throws IOException { int divisor = next.lastIndexOf("|"); if (next.charAt(0) == 'D') { - String description = next.substring(8, divisor - 1); - String date = next.substring((divisor + 2)); - taskList.add(new Deadline(description, date)); - - if (next.charAt(4) == '1') { - taskList.get(taskList.size() - 1).mark(); - } + readDeadline(next, divisor, taskList); } if (next.charAt(0) == 'E') { - String description = next.substring(8, divisor - 1); - String date = next.substring((divisor + 2)); - taskList.add(new Event(description, date)); - - if (next.charAt(4) == '1') { - taskList.get(taskList.size() - 1).mark(); - } + readEvent(next, divisor, taskList); } if (next.charAt(0) == 'T') { - String description = next.substring(8); - taskList.add(new ToDo(description)); - - if (next.charAt(4) == '1') { - taskList.get(taskList.size() - 1).mark(); - } + readToDo(next, taskList); } } } @@ -73,28 +64,69 @@ public void write(TaskList tasks) throws IOException { File myFile = new File(filePath); myFile.createNewFile(); FileWriter myWriter = new FileWriter(myFile); - for (int i = 0; i < tasks.getList().size(); i++ ) { + for (int i = 0; i < tasks.getList().size(); i++) { Task curr = tasks.getList().get(i); if (curr instanceof Deadline) { - if (curr.isDone) { - myWriter.write("D" + " | 1 | " + curr.description + " | " + ((Deadline) curr).by + "\n"); - } else { - myWriter.write("D" + " | 0 | " + curr.description + " | " + ((Deadline) curr).by + "\n"); - } + writeDeadline(curr, myWriter); } else if (curr instanceof Event) { - if (curr.isDone) { - myWriter.write("E" + " | 1 | " + curr.description + " | " + ((Event) curr).at + "\n"); - } else { - myWriter.write("E" + " | 0 | " + curr.description + " | " + ((Event) curr).at + "\n"); - } + writeEvent(curr, myWriter); } else { - if (curr.isDone) { - myWriter.write("T" + " | 1 | " + curr.description + "\n"); - } else { - myWriter.write("T" + " | 0 | " + curr.description + "\n"); - } + writeToDo(curr, myWriter); } } myWriter.close(); } + + private static void writeDeadline(Task curr, FileWriter myWriter) throws IOException { + if (curr.getDone()) { + myWriter.write("D" + " | 1 | " + curr.getDescription() + " | " + ((Deadline) curr).getBy() + "\n"); + } else { + myWriter.write("D" + " | 0 | " + curr.getDescription() + " | " + ((Deadline) curr).getBy() + "\n"); + } + } + + private static void writeEvent(Task curr, FileWriter myWriter) throws IOException { + if (curr.getDone()) { + myWriter.write("E" + " | 1 | " + curr.getDescription() + " | " + ((Event) curr).getAt() + "\n"); + } else { + myWriter.write("E" + " | 0 | " + curr.getDescription() + " | " + ((Event) curr).getAt() + "\n"); + } + } + + private static void writeToDo(Task curr, FileWriter myWriter) throws IOException { + if (curr.getDone()) { + myWriter.write("T" + " | 1 | " + curr.getDescription() + "\n"); + } else { + myWriter.write("T" + " | 0 | " + curr.getDescription() + "\n"); + } + } + + private static void readDeadline(String next, int divisor, List taskList) { + String description = next.substring(8, divisor - 1); + String date = next.substring((divisor + 2)); + taskList.add(new Deadline(description, date)); + + if (next.charAt(4) == '1') { + taskList.get(taskList.size() - 1).mark(); + } + } + + private static void readEvent(String next, int divisor, List taskList) { + String description = next.substring(8, divisor - 1); + String date = next.substring((divisor + 2)); + taskList.add(new Event(description, date)); + + if (next.charAt(4) == '1') { + taskList.get(taskList.size() - 1).mark(); + } + } + + private static void readToDo(String next, List taskList) { + String description = next.substring(8); + taskList.add(new ToDo(description)); + + if (next.charAt(4) == '1') { + taskList.get(taskList.size() - 1).mark(); + } + } } diff --git a/src/main/java/jarvis/Ui.java b/src/main/java/jarvis/Ui.java deleted file mode 100644 index 8985fbe5ef..0000000000 --- a/src/main/java/jarvis/Ui.java +++ /dev/null @@ -1,5 +0,0 @@ -package jarvis; - -public class Ui { - -} diff --git a/src/main/java/jarvis/exception/JarvisException.java b/src/main/java/jarvis/exception/JarvisException.java index ca4f0cc77b..5b2a302f04 100644 --- a/src/main/java/jarvis/exception/JarvisException.java +++ b/src/main/java/jarvis/exception/JarvisException.java @@ -1,6 +1,9 @@ package jarvis.exception; -public class JarvisException extends Exception{ +/** + * Exception thrown by Jarvis when error is encountered. + */ +public class JarvisException extends Exception { public JarvisException(String errorMessage) { super(errorMessage); } diff --git a/src/main/java/jarvis/task/Deadline.java b/src/main/java/jarvis/task/Deadline.java index 9f6b1b3241..561ca31e24 100644 --- a/src/main/java/jarvis/task/Deadline.java +++ b/src/main/java/jarvis/task/Deadline.java @@ -7,8 +7,8 @@ * Represents a Deadline which is a subclass of Task. * */ -public class Deadline extends Task{ - public LocalDate by; +public class Deadline extends Task { + private LocalDate by; /** * Returns a new Deadline Object with the given description and date. @@ -30,4 +30,8 @@ public Deadline(String description, String date) { public String toString() { return "[D]" + super.toString() + " (by: " + by.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; } + + public LocalDate getBy() { + return by; + } } diff --git a/src/main/java/jarvis/task/Event.java b/src/main/java/jarvis/task/Event.java index c7a95b01eb..92399c9b92 100644 --- a/src/main/java/jarvis/task/Event.java +++ b/src/main/java/jarvis/task/Event.java @@ -7,9 +7,9 @@ * Represents an Event which is a subclass of Task. * */ -public class Event extends Task{ +public class Event extends Task { - public LocalDate at; + private LocalDate at; /** * Returns a new Event Object with the given description and date. @@ -31,4 +31,8 @@ public Event(String description, String date) { public String toString() { return "[E]" + super.toString() + " (at: " + at.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; } + + public LocalDate getAt() { + return at; + } } diff --git a/src/main/java/jarvis/task/Task.java b/src/main/java/jarvis/task/Task.java index 04532e4d8d..a1403598bf 100644 --- a/src/main/java/jarvis/task/Task.java +++ b/src/main/java/jarvis/task/Task.java @@ -4,9 +4,9 @@ * Represents a Task. */ public abstract class Task { - public String description; - public boolean isDone; - public static int count = 0; + private static int count = 0; + private String description; + private boolean isDone; /** * Returns a Task object with the given Description. @@ -23,7 +23,7 @@ public Task(String description) { /** * Marks the task as done. */ - public void mark(){ + public void mark() { isDone = true; } @@ -50,4 +50,15 @@ public String toString() { return "[" + getStatusIcon() + "] " + this.description; } -} \ No newline at end of file + public String getDescription() { + return this.description; + } + + public boolean getDone() { + return this.isDone; + } + + public static int getCount() { + return count; + } +} diff --git a/src/main/java/jarvis/task/ToDo.java b/src/main/java/jarvis/task/ToDo.java index 421e7e6ee3..c53343b2a7 100644 --- a/src/main/java/jarvis/task/ToDo.java +++ b/src/main/java/jarvis/task/ToDo.java @@ -4,7 +4,7 @@ * Represents a ToDo which is a subclass of Task. * */ -public class ToDo extends Task{ +public class ToDo extends Task { /** * Returns a new Todo Object with the given description. diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml index ec69a4eba6..0545d24066 100644 --- a/src/main/resources/view/MainWindow.fxml +++ b/src/main/resources/view/MainWindow.fxml @@ -6,7 +6,7 @@ - +