From ac7c4b38192265bd9e7d6296e7c469bc8385f873 Mon Sep 17 00:00:00 2001 From: ngxzs Date: Thu, 7 Mar 2024 23:45:57 +0800 Subject: [PATCH] Add JavaDoc --- saveFile.txt | 7 +- src/main/java/RecrBad.java | 25 ++-- src/main/java/helperFunctions/Parser.java | 55 ++++--- src/main/java/helperFunctions/Storage.java | 158 ++++++++++++++------ src/main/java/helperFunctions/TaskList.java | 121 ++++++++++----- src/main/java/helperFunctions/Ui.java | 29 +++- src/main/java/tasks/Deadline.java | 4 +- src/main/java/tasks/Event.java | 4 +- src/main/java/tasks/Task.java | 12 +- src/main/java/tasks/Todo.java | 2 +- 10 files changed, 287 insertions(+), 130 deletions(-) diff --git a/saveFile.txt b/saveFile.txt index 810a57d4b..f8424e1bc 100644 --- a/saveFile.txt +++ b/saveFile.txt @@ -6,8 +6,5 @@ 6. [D][0] kjdfl (by: sdf) 7. [D][0] sdf(by: sdf sd /fds) 8. [D][0] something (by: by 2/12/2019) -9. [D][0] something (by: 2/12/2019) -10. [D][1] haha (by: 2/12/2019) -11. [D][1] halo (by: Dec 11 2019) -12. [T][0] testing -13. [D][0] someting (by: Aug 22 2014) +9. [T][0] fdsjfk +10. [E][0] sjk (from: sd fsto ) diff --git a/src/main/java/RecrBad.java b/src/main/java/RecrBad.java index 48ac7f94a..11154fbc2 100644 --- a/src/main/java/RecrBad.java +++ b/src/main/java/RecrBad.java @@ -5,29 +5,34 @@ public class RecrBad { private TaskList tasks; - public RecrBad(String FILE_PATH) { + + /** + * Constructor to start main program: sayHi() on UI, open and process FILE_NAME, + * create FILE_NAME if does not exist, and asks User for input + * + * @param FILE_NAME to store tasks in + */ + public RecrBad(String FILE_NAME) { tasks = new TaskList(); Ui.sayHi(); try { - Storage.readFile(FILE_PATH, tasks); + Storage.readFile(FILE_NAME, tasks); } catch (InvalidParamsException e) { Ui.showLoadingError(e.getMessage()); return; } - - Ui.readInput(tasks, FILE_PATH); + Ui.readInput(tasks, FILE_NAME); } /** - * Input - * [any text] to addToList, - * list to displayList, - * mark/unmark [index] to mark item + * Main programme + * + * @param args default arguments */ public static void main(String[] args) { - String FILE_PATH = "saveFile.txt"; - new RecrBad(FILE_PATH); + String FILE_NAME = "saveFile.txt"; + new RecrBad(FILE_NAME); } } diff --git a/src/main/java/helperFunctions/Parser.java b/src/main/java/helperFunctions/Parser.java index 370cb8bee..7add1ae9c 100644 --- a/src/main/java/helperFunctions/Parser.java +++ b/src/main/java/helperFunctions/Parser.java @@ -4,40 +4,61 @@ import java.time.format.DateTimeFormatter; public class Parser { - public static boolean processUserInput(TaskList tasks, String userInput, String FILE_PATH, boolean isReadMode) throws InvalidParamsException { - String[] req = userInput.split(" "); + /** + * Process User Input and calls appropriate TaskList function + * + * @param tasks TaskList object to hold many tasks + * @param userInput from CLI or from FILE_NAME + * @param FILE_NAME to store tasks in + * @param isReadMode specifies if reading from FILE_NAME or writing to it + * @return true to continue asking for user input; false to stop + * @throws InvalidParamsException if invalid command + */ + public static boolean processUserInput(TaskList tasks, String userInput, String FILE_NAME, boolean isReadMode) throws InvalidParamsException { + String[] userInputInParts = userInput.split(" "); - if (req[0].equalsIgnoreCase("BYE")) { + if (userInputInParts[0].equalsIgnoreCase("BYE")) { return false; // EXITS loop } - if (req[0].equalsIgnoreCase("LIST")) { + if (userInputInParts[0].equalsIgnoreCase("LIST")) { System.out.print(tasks.displayList()); - } else if (req[0].toUpperCase().contains("MARK")) { // both unmark & mark contains 'mark' + } else if (userInputInParts[0].toUpperCase().contains("MARK")) { // both unmark & mark contains 'mark' boolean isMark = !userInput.toUpperCase().contains("UNMARK"); - tasks.markOperation(req, isMark, FILE_PATH, isReadMode); - } else if (req[0].equalsIgnoreCase("DELETE")) { - tasks.deleteOperation(req, FILE_PATH); - } else if (req[0].equalsIgnoreCase("FIND")) { - tasks.findOperation(req, FILE_PATH); - } else if (req[0].equalsIgnoreCase("TODO")) { - tasks.addTodoTask(req, userInput, FILE_PATH, isReadMode); // change tasks to ArrayList, f void now - } else if (req[0].equalsIgnoreCase("DEADLINE")) { - tasks.addDeadlineTask(req, userInput, FILE_PATH, isReadMode); - } else if (req[0].equalsIgnoreCase("EVENT")) { - tasks.addEventTask(req, userInput, FILE_PATH, isReadMode); + tasks.markOperation(userInputInParts, isMark, FILE_NAME, isReadMode); + } else if (userInputInParts[0].equalsIgnoreCase("DELETE")) { + tasks.deleteOperation(userInputInParts, FILE_NAME); + } else if (userInputInParts[0].equalsIgnoreCase("FIND")) { + tasks.findOperation(userInputInParts); + } else if (userInputInParts[0].equalsIgnoreCase("TODO")) { + tasks.addTodoTask(userInputInParts, userInput, FILE_NAME, isReadMode); // change tasks to ArrayList, f void now + } else if (userInputInParts[0].equalsIgnoreCase("DEADLINE")) { + tasks.addDeadlineTask(userInputInParts, userInput, FILE_NAME, isReadMode); + } else if (userInputInParts[0].equalsIgnoreCase("EVENT")) { + tasks.addEventTask(userInputInParts, userInput, FILE_NAME, isReadMode); } else { throw new InvalidParamsException("No such command." + System.lineSeparator() + Ui.printCommandsList()); } return true; } + /** + * Takes in deadline of "yyyy/mm/dd" and formats to "MMM d yyyy" + * + * @param deadline is unformatted + * @return deadline of format "MMM d yyyy" + */ public static String formatDeadline(String deadline) { String formattedDeadline = deadline.replaceAll("/", "-").trim(); LocalDate dateObject = LocalDate.parse(formattedDeadline); return dateObject.format(DateTimeFormatter.ofPattern("MMM d yyyy")); } - // Checks for format "yyyy-mm-dd" + /** + * Checks for valid deadline format "yyyy/mm/dd" + * + * @param deadline of unknown format + * @return true if valid, else false + */ public static boolean isValidDeadline(String deadline) { int LENGTH_OF_DEADLINE_COMPONENTS = 3; int LENGTH_OF_YEAR = 4; diff --git a/src/main/java/helperFunctions/Storage.java b/src/main/java/helperFunctions/Storage.java index cff330fa8..53182e84b 100644 --- a/src/main/java/helperFunctions/Storage.java +++ b/src/main/java/helperFunctions/Storage.java @@ -7,10 +7,20 @@ import java.io.IOException; import java.util.Scanner; +/** + * Functions relating to FILE_NAME storing all the tasks + */ public class Storage { - public static void appendToFile(String filePath, String textToAppend) throws InvalidParamsException { + /** + * Appends a task to Tasks in FILE_NAME + * + * @param FILE_NAME to store tasks in + * @param textToAppend the new Task to add + * @throws InvalidParamsException if error appending to file + */ + public static void appendToFile(String FILE_NAME, String textToAppend) throws InvalidParamsException { try { - FileWriter fw = new FileWriter(filePath, true); // in append mode + FileWriter fw = new FileWriter(FILE_NAME, true); // in append mode fw.write(textToAppend); fw.close(); } catch (IOException e) { @@ -18,9 +28,16 @@ public static void appendToFile(String filePath, String textToAppend) throws Inv } } - public static void writeToFile(String filePath, String textToWrite) throws InvalidParamsException { + /** + * Overwrites all tasks in FILE_NAME + * + * @param FILE_NAME to store tasks in + * @param textToWrite the new tasks to write + * @throws InvalidParamsException if error writting to file + */ + public static void writeToFile(String FILE_NAME, String textToWrite) throws InvalidParamsException { try { - FileWriter fw = new FileWriter(filePath); // overwrites file + FileWriter fw = new FileWriter(FILE_NAME); // overwrites file fw.write(textToWrite); fw.close(); } catch (IOException e) { @@ -28,16 +45,24 @@ public static void writeToFile(String filePath, String textToWrite) throws Inval } } - public static String getAbsoluteFilePath(String FILE_PATH) { + /** + * Gets absolute file path to FILE_NAME + * + * @param FILE_NAME to store tasks in + * @return absolute file path + */ + public static String getAbsoluteFilePath(String FILE_NAME) { String currentDirectory = System.getProperty("user.dir"); java.nio.file.Path directoryPath = java.nio.file.Paths.get(currentDirectory); - return directoryPath + "/" + FILE_PATH; + return directoryPath + "/" + FILE_NAME; } /** - * Checks if saveFile.txt exists + * Checks if FILE_NAME exists. If not, create it. * + * @param f File object pointing to absoluteFilePath * @param absoluteFilePath of saveFile.txt + * @throws InvalidParamsException if error creating file */ public static void isFileExist(File f, String absoluteFilePath) throws InvalidParamsException { try { @@ -52,22 +77,34 @@ public static void isFileExist(File f, String absoluteFilePath) throws InvalidPa } /** - * convert each line in textFile to a Task + * Checks if FILE_NAME exists and process it + * + * @param FILE_NAME to store tasks in + * @param tasks TaskList object to store all tasks + * @throws InvalidParamsException if error creating or processing file **/ - public static void readFile(String FILE_PATH, TaskList tasks) throws InvalidParamsException { - String absoluteFilePath = getAbsoluteFilePath(FILE_PATH); + public static void readFile(String FILE_NAME, TaskList tasks) throws InvalidParamsException { + String absoluteFilePath = getAbsoluteFilePath(FILE_NAME); File f = new File(absoluteFilePath); try { isFileExist(f, absoluteFilePath); - processFile(f, FILE_PATH, tasks); + processFile(f, FILE_NAME, tasks); } catch (InvalidParamsException e) { throw new InvalidParamsException(e.getMessage()); } } - private static void processFile(File f, String FILE_PATH, TaskList tasks) throws InvalidParamsException { + /** + * Process FILE_NAME ie adds tasks to Tasks + * + * @param f File Object pointing to absolute file path of FILE_NAME + * @param FILE_NAME to store tasks in + * @param tasks TaskList object to hold all tasks + * @throws InvalidParamsException if file not found or invalid mark command + */ + private static void processFile(File f, String FILE_NAME, TaskList tasks) throws InvalidParamsException { Scanner s; try { s = new Scanner(f); // create a Scanner using file as source @@ -76,55 +113,74 @@ private static void processFile(File f, String FILE_PATH, TaskList tasks) throws } int lineNum = 1; // keeps track of current lineNum while (s.hasNext()) { - String lineInFile = s.nextLine(); // read line in file + String currentLineInFile = s.nextLine(); // process lines in saveFile.txt - int START_INDEX_OF_TYPE = lineInFile.indexOf('[') + 1; // format of text: "1. [T][1] startOfTaskDescription" + int START_INDEX_OF_TYPE = currentLineInFile.indexOf('[') + 1; // format of text: "1. [T][1] startOfTaskDescription" int START_INDEX_OF_MARK = START_INDEX_OF_TYPE + 3; // mark index is 3 positions to the right int START_INDEX_OF_DESCRIPTION = START_INDEX_OF_MARK + 3; // description index is 3 more positions to the right // checks validity of index - if (!isIndexesValid(lineInFile, START_INDEX_OF_TYPE, START_INDEX_OF_MARK, START_INDEX_OF_DESCRIPTION)) { - throw new InvalidParamsException(lineInFile + " is not valid"); + if (!isIndexesValid(currentLineInFile, START_INDEX_OF_TYPE, START_INDEX_OF_MARK, START_INDEX_OF_DESCRIPTION)) { + throw new InvalidParamsException(currentLineInFile + " is not valid"); } String command = ""; char mark; try { - command += generateCommandFromFileLine(START_INDEX_OF_TYPE, START_INDEX_OF_DESCRIPTION, lineInFile); - mark = getMarkStatus(START_INDEX_OF_MARK, lineInFile); + command += generateCommandFromFileLine(START_INDEX_OF_TYPE, START_INDEX_OF_DESCRIPTION, currentLineInFile); + mark = getMarkStatus(START_INDEX_OF_MARK, currentLineInFile); } catch (InvalidParamsException e) { throw new InvalidParamsException(e.getMessage()); } // process command in file boolean isReadMode = true; - Parser.processUserInput(tasks, command, FILE_PATH, isReadMode); + Parser.processUserInput(tasks, command, FILE_NAME, isReadMode); // create mark command (for mark tasks only) if (mark == '1') { // create mark command String markCommand; markCommand = "mark " + lineNum; - Parser.processUserInput(tasks, markCommand, FILE_PATH, isReadMode); + Parser.processUserInput(tasks, markCommand, FILE_NAME, isReadMode); } lineNum += 1; } } - private static boolean isIndexesValid(String lineInFile, int START_INDEX_OF_TYPE, + + /** + * Checks if indexes are valid to add tasks when processing FILE_NAME + * + * @param currentLineInFile which is being processed + * @param START_INDEX_OF_TYPE in currentLineInFile for task.getType() + * @param START_INDEX_OF_MARK in currentLineInFile for task.getStatus() + * @param START_INDEX_OF_DESCRIPTION in currentLineInFile for task.getDescription() + * @return true if valid, else false + */ + private static boolean isIndexesValid(String currentLineInFile, int START_INDEX_OF_TYPE, int START_INDEX_OF_MARK, int START_INDEX_OF_DESCRIPTION) { - return !(lineInFile.indexOf('[') == -1) || - (START_INDEX_OF_TYPE + 1 > lineInFile.length()) || - (START_INDEX_OF_MARK + 1 > lineInFile.length()) || - (START_INDEX_OF_DESCRIPTION + 1 > lineInFile.length()); + return !(currentLineInFile.indexOf('[') == -1) || + (START_INDEX_OF_TYPE + 1 > currentLineInFile.length()) || + (START_INDEX_OF_MARK + 1 > currentLineInFile.length()) || + (START_INDEX_OF_DESCRIPTION + 1 > currentLineInFile.length()); } + /** + * Generate command to add tasks when processing FILE_NAME + * + * @param START_INDEX_OF_TYPE in currentLineInFile for task.getType() + * @param START_INDEX_OF_DESCRIPTION in currentLineInFile for task.getDescription() + * @param currentLineInFile which is being processed + * @return command to add tasks + * @throws InvalidParamsException if invalid or missing task.type + */ private static String generateCommandFromFileLine(int START_INDEX_OF_TYPE, - int START_INDEX_OF_DESCRIPTION, String lineInFile) throws InvalidParamsException { + int START_INDEX_OF_DESCRIPTION, String currentLineInFile) throws InvalidParamsException { try { String command = ""; - char type = lineInFile.charAt(START_INDEX_OF_TYPE); + char type = currentLineInFile.charAt(START_INDEX_OF_TYPE); if (type == 'T') { command += "todo "; @@ -133,9 +189,9 @@ private static String generateCommandFromFileLine(int START_INDEX_OF_TYPE, } else if (type == 'E') { command += "event "; } else { - throw new InvalidParamsException(lineInFile + " type is not valid"); + throw new InvalidParamsException(currentLineInFile + " type is not valid"); } - command += getDescriptionCommand(type, START_INDEX_OF_DESCRIPTION, lineInFile); + command += getDescriptionCommand(type, START_INDEX_OF_DESCRIPTION, currentLineInFile); return command; @@ -144,33 +200,49 @@ private static String generateCommandFromFileLine(int START_INDEX_OF_TYPE, } } - private static String getDescriptionCommand(char type, int START_INDEX_OF_DESCRIPTION, String lineInFile) { + /** + * Get description from currentLineInFile when processing FILE_NAME + * + * @param type of task + * @param START_INDEX_OF_DESCRIPTION in currentLineInFile for task.getDescription() + * @param currentLineInFile which is being processed + * @return taskDescription + */ + private static String getDescriptionCommand(char type, int START_INDEX_OF_DESCRIPTION, String currentLineInFile) { String taskDescription = ""; if (type == 'D') { - int startIndexToChange = lineInFile.lastIndexOf("(by: "); // change "(by: " to "/" - int endIndexToChange = lineInFile.lastIndexOf(")"); // change ")" to "" - taskDescription += lineInFile.substring(START_INDEX_OF_DESCRIPTION, startIndexToChange); + int startIndexToChange = currentLineInFile.lastIndexOf("(by: "); // change "(by: " to "/" + int endIndexToChange = currentLineInFile.lastIndexOf(")"); // change ")" to "" + taskDescription += currentLineInFile.substring(START_INDEX_OF_DESCRIPTION, startIndexToChange); taskDescription += "/"; - taskDescription += lineInFile.substring(startIndexToChange + 5, endIndexToChange); + taskDescription += currentLineInFile.substring(startIndexToChange + 5, endIndexToChange); } else if (type == 'E') { // process input for event params - int midIndexToChange = lineInFile.lastIndexOf("to "); // change "to " to "/" - int startIndexToChange = lineInFile.lastIndexOf("(from: ", midIndexToChange); // change "(from: " to "/" - int endIndexToChange = lineInFile.lastIndexOf(')'); // change ")" to "" ie last elem - taskDescription += lineInFile.substring(START_INDEX_OF_DESCRIPTION, startIndexToChange); + int midIndexToChange = currentLineInFile.lastIndexOf("to "); // change "to " to "/" + int startIndexToChange = currentLineInFile.lastIndexOf("(from: ", midIndexToChange); // change "(from: " to "/" + int endIndexToChange = currentLineInFile.lastIndexOf(')'); // change ")" to "" ie last elem + taskDescription += currentLineInFile.substring(START_INDEX_OF_DESCRIPTION, startIndexToChange); taskDescription += "/"; - taskDescription += lineInFile.substring(startIndexToChange + 7, midIndexToChange); + taskDescription += currentLineInFile.substring(startIndexToChange + 7, midIndexToChange); taskDescription += "/"; - taskDescription += lineInFile.substring(midIndexToChange + 3, endIndexToChange); + taskDescription += currentLineInFile.substring(midIndexToChange + 3, endIndexToChange); } else { // type == 'T' - taskDescription = lineInFile.substring(START_INDEX_OF_DESCRIPTION); + taskDescription = currentLineInFile.substring(START_INDEX_OF_DESCRIPTION); } return taskDescription; } - private static char getMarkStatus(int START_INDEX_OF_MARK, String lineInFile) + /** + * Gets mark status of file when processing FILE_NAME + * + * @param START_INDEX_OF_MARK in currentLineInFile for task.getStatus() + * @param currentLineInFile which is being processed + * @return mark: '0' = unmarked, '1' = marked + * @throws InvalidParamsException if invalid mark + */ + private static char getMarkStatus(int START_INDEX_OF_MARK, String currentLineInFile) throws InvalidParamsException { - char mark = lineInFile.charAt(START_INDEX_OF_MARK); + char mark = currentLineInFile.charAt(START_INDEX_OF_MARK); if (mark != '0' && mark != '1') { throw new InvalidParamsException("invalid mark"); diff --git a/src/main/java/helperFunctions/TaskList.java b/src/main/java/helperFunctions/TaskList.java index 4fd147ef5..6ceda6dff 100644 --- a/src/main/java/helperFunctions/TaskList.java +++ b/src/main/java/helperFunctions/TaskList.java @@ -10,12 +10,17 @@ public class TaskList { private ArrayList tasks; + /** + * Initializes ArrayList of Task objects + */ public TaskList() { this.tasks = new ArrayList<>(); } /** - * Prints each Task in Task array + * Prints all task in Tasks + * + * @return string of all tasks */ protected String displayList() { if (tasks.isEmpty()) { @@ -34,7 +39,7 @@ protected String displayList() { /** * Returns a task in String format * - * @param index index of specific task + * @param index index of specific task (zero-indexed) */ private String displayListItem(int index) { return ((index + 1) + ". [" + tasks.get(index).getType() + "][" @@ -43,11 +48,20 @@ private String displayListItem(int index) { + System.lineSeparator()); } - protected void addEventTask(String[] req, String line, String FILE_PATH, boolean isReadMode) throws InvalidParamsException { - final int startIndexOfDescription = req[0].length() + 1; // req[0].equals(event) + /** + * Adds Event task to Tasks + * + * @param userInputInParts separates each word in userInput + * @param userInput is the original CLI user input + * @param FILE_NAME to store tasks in + * @param isReadMode specifies if reading from FILE_NAME or writing to it + * @throws InvalidParamsException if invalid/ missing event arguments + */ + protected void addEventTask(String[] userInputInParts, String userInput, String FILE_NAME, boolean isReadMode) throws InvalidParamsException { + final int startIndexOfDescription = userInputInParts[0].length() + 1; // userInputInParts[0].equals(event) - int indexTo = line.lastIndexOf('/'); - int indexFrom = line.lastIndexOf('/', indexTo - 1); // counts from back + int indexTo = userInput.lastIndexOf('/'); + int indexFrom = userInput.lastIndexOf('/', indexTo - 1); // counts from back // checks for invalid input if (indexFrom == -1 || indexTo == -1) { throw new InvalidParamsException("No event arguments: Add a '/', then the startTime, followed by a '/' and the endTime"); @@ -56,25 +70,34 @@ protected void addEventTask(String[] req, String line, String FILE_PATH, boolean throw new InvalidParamsException(("no event description")); } // process input as Event object - String description = line.substring(startIndexOfDescription, indexFrom - 1); - String timeRange = " (from: " + line.substring(indexFrom + 1, indexTo) + - "to " + line.substring(indexTo + 1) + ")"; + String description = userInput.substring(startIndexOfDescription, indexFrom - 1); + String timeRange = " (from: " + userInput.substring(indexFrom + 1, indexTo) + + "to " + userInput.substring(indexTo + 1) + ")"; // add to tasks Task newTask = new Event(description, timeRange, tasks.size() + 1); tasks.add(newTask); if (isReadMode) { return; // no need execute code below (for writing only) } - Storage.appendToFile(FILE_PATH, displayListItem(tasks.indexOf(newTask))); + Storage.appendToFile(FILE_NAME, displayListItem(tasks.indexOf(newTask))); System.out.println("Event added!"); System.out.println(displayListItem(tasks.size() - 1)); System.out.println("Congrats, now have " + tasks.size() + " tasks"); } - protected void addDeadlineTask(String[] req, String line, String FILE_PATH, boolean isReadMode) throws InvalidParamsException { - final int startIndexOfDescription = req[0].length() + 1; // req[0].equals(deadline); + /** + * Adds Deadline to Tasks + * + * @param userInputInParts separates each word in userInput + * @param userInput is the original CLI user input + * @param FILE_NAME to store tasks in + * @param isReadMode specifies if reading from FILE_NAME or writing to it + * @throws InvalidParamsException if invalid/ missing deadline arguments + */ + protected void addDeadlineTask(String[] userInputInParts, String userInput, String FILE_NAME, boolean isReadMode) throws InvalidParamsException { + final int startIndexOfDescription = userInputInParts[0].length() + 1; // userInputInParts[0].equals(deadline); - int indexDeadline = line.indexOf('/'); + int indexDeadline = userInput.indexOf('/'); // checks for invalid input if (indexDeadline == -1) { throw new InvalidParamsException("No deadline argument: Add a parameter '/' followed by the deadline"); @@ -84,13 +107,13 @@ protected void addDeadlineTask(String[] req, String line, String FILE_PATH, bool } // process input as Deadline object String deadline = "(by: "; - if (Parser.isValidDeadline(line.substring(indexDeadline + 1))) { - deadline += Parser.formatDeadline(line.substring(indexDeadline + 1)); + if (Parser.isValidDeadline(userInput.substring(indexDeadline + 1))) { + deadline += Parser.formatDeadline(userInput.substring(indexDeadline + 1)); } else { - deadline += line.substring(indexDeadline + 1); + deadline += userInput.substring(indexDeadline + 1); } deadline += ")"; - String description = line.substring(startIndexOfDescription, indexDeadline); + String description = userInput.substring(startIndexOfDescription, indexDeadline); // add to tasks Task newTask = new Deadline(description, deadline, tasks.size() + 1); tasks.add(newTask); @@ -98,21 +121,29 @@ protected void addDeadlineTask(String[] req, String line, String FILE_PATH, bool if (isReadMode) { return; // no need execute code below (for writing only) } - Storage.appendToFile(FILE_PATH, displayListItem(tasks.indexOf(newTask))); + Storage.appendToFile(FILE_NAME, displayListItem(tasks.indexOf(newTask))); System.out.println("Deadline added!"); System.out.println(displayListItem(tasks.size() - 1)); System.out.println("Congrats, now have " + tasks.size() + " tasks"); } - protected void addTodoTask(String[] req, String line, String FILE_PATH, boolean isReadMode) throws InvalidParamsException { - final int startIndexOfDescription = req[0].length() + 1; // req[0].equals(TODO); + /** + * + * @param userInputInParts separates each word in userInput + * @param userInput is the original CLI user input + * @param FILE_NAME to store tasks in + * @param isReadMode specifies if reading from FILE_NAME or writing to it + * @throws InvalidParamsException if invalid/ missing t0do arguments + */ + protected void addTodoTask(String[] userInputInParts, String userInput, String FILE_NAME, boolean isReadMode) throws InvalidParamsException { + final int startIndexOfDescription = userInputInParts[0].length() + 1; // userInputInParts[0].equals(TODO); // checks for invalid input - if (startIndexOfDescription > line.length() - 1) { + if (startIndexOfDescription > userInput.length() - 1) { throw new InvalidParamsException("No task description"); } // process input - String description = line.substring(startIndexOfDescription); + String description = userInput.substring(startIndexOfDescription); // add to tasks Task newTask = new Todo(description, tasks.size() + 1); tasks.add(newTask); @@ -120,22 +151,29 @@ protected void addTodoTask(String[] req, String line, String FILE_PATH, boolean // no need execute code below (for writing only) return; } - Storage.appendToFile(FILE_PATH, displayListItem(tasks.indexOf(newTask))); + Storage.appendToFile(FILE_NAME, displayListItem(tasks.indexOf(newTask))); System.out.println("Todo added!"); System.out.println(displayListItem(tasks.size() - 1)); System.out.println("Congrats, now have " + tasks.size() + " tasks"); } - protected void deleteOperation(String[] req, String FILE_PATH) throws InvalidParamsException { + /** + * Deletes a specific task by index + * + * @param userInputInParts separates each word in userInput + * @param FILE_NAME to store tasks in + * @throws InvalidParamsException if invalid/ missing delete arguments + */ + protected void deleteOperation(String[] userInputInParts, String FILE_NAME) throws InvalidParamsException { // check for input validity - if (req.length < 2) { + if (userInputInParts.length < 2) { throw new InvalidParamsException("Invalid delete operation"); } // process input int taskIndex; try { - taskIndex = Integer.parseInt(req[1]); + taskIndex = Integer.parseInt(userInputInParts[1]); } catch (NumberFormatException e) { throw new InvalidParamsException("Invalid non-integer delete index"); } @@ -146,25 +184,28 @@ protected void deleteOperation(String[] req, String FILE_PATH) throws InvalidPar System.out.println("Good riddance, task deleted!"); System.out.println(displayListItem(taskIndex - 1)); tasks.remove(taskIndex - 1); - Storage.writeToFile(FILE_PATH, displayList()); + Storage.writeToFile(FILE_NAME, displayList()); System.out.println("Congrats, now have " + tasks.size() + " tasks"); } /** - * Marks Task as done or not done + * Marks a Task or unmarks a task * - * @param req String[] input from user - * @param isMark type of operation: mark or unmark + * @param userInputInParts separates each word in userInput + * @param isMark if true, marks task, else unmarks it + * @param FILE_NAME to store tasks in + * @param isReadMode specifies if reading from FILE_NAME or writing to it + * @throws InvalidParamsException if invalid/ missing mark arguments */ - protected void markOperation(String[] req, boolean isMark, String FILE_PATH, boolean isReadMode) throws InvalidParamsException { + protected void markOperation(String[] userInputInParts, boolean isMark, String FILE_NAME, boolean isReadMode) throws InvalidParamsException { // check for input validity - if (req.length < 2) { + if (userInputInParts.length < 2) { throw new InvalidParamsException("invalid mark/ unmark operation"); } int taskNum; try { - taskNum = Integer.parseInt(req[1]); + taskNum = Integer.parseInt(userInputInParts[1]); if (tasks.size() < taskNum || taskNum < 1) { throw new InvalidParamsException("No such taskNum"); } @@ -183,18 +224,24 @@ protected void markOperation(String[] req, boolean isMark, String FILE_PATH, boo return; } // mark tasks - Storage.writeToFile(FILE_PATH, displayList()); + Storage.writeToFile(FILE_NAME, displayList()); String mark = isMark ? "marked" : "unmarked"; System.out.println("Has " + mark + " task" + taskNum + ":"); System.out.print(displayListItem(taskNum - 1)); } - public void findOperation(String[] req, String filePath) throws InvalidParamsException { + /** + * Prints tasks containing a keyword + * + * @param userInputInParts separates each word in userInput + * @throws InvalidParamsException if invalid/ missing find arguments + */ + public void findOperation(String[] userInputInParts) throws InvalidParamsException { // check for input validity - if (req.length < 2) { + if (userInputInParts.length < 2) { throw new InvalidParamsException("invalid find operation"); } - String findKeyword = req[1]; + String findKeyword = userInputInParts[1]; String tasksToPrint = ""; for (Task task : tasks) { if (task.getDescription().contains(findKeyword)) { diff --git a/src/main/java/helperFunctions/Ui.java b/src/main/java/helperFunctions/Ui.java index 15a4f1b1b..458e6e4bb 100644 --- a/src/main/java/helperFunctions/Ui.java +++ b/src/main/java/helperFunctions/Ui.java @@ -6,29 +6,43 @@ public class Ui { /** - * Reads user input + * Reads user input and process accordingly, infinitely till sayBye() + * + * @param tasks TaskList object of tasks + * @param FILE_NAME to store tasks in */ - public static void readInput(TaskList tasks, String FILE_PATH) { + public static void readInput(TaskList tasks, String FILE_NAME) { Scanner in = new Scanner(System.in); - boolean isRun = true; + boolean isRunning = true; - while (isRun) { + while (isRunning) { printLine(); String userInput = in.nextLine(); // reads input // process input try { boolean isReadMode = false; - isRun = Parser.processUserInput(tasks, userInput, FILE_PATH, isReadMode); + isRunning = Parser.processUserInput(tasks, userInput, FILE_NAME, isReadMode); } catch (InvalidParamsException e) { showLoadingError(e.getMessage()); // prints out error message } } sayBye(); } + + /** + * Prints errorMessage + * + * @param errorMessage if got error + */ public static void showLoadingError(String errorMessage) { System.out.println(errorMessage); } + /** + * Returns list of possible commands + * + * @return list of possible commands + */ public static String printCommandsList() { return "## Possible user commands: \n" + "1. todo : Add todo task \n" + @@ -36,7 +50,8 @@ public static String printCommandsList() { "3. event / / : Add event \n" + "4. list : List all tasks\n" + "5. mark/unmark : Mark task as done/ undone\n" + - "6. bye : Exit Program"; + "6. find : Finds keyword in list of tasks\n" + + "7. bye : Exit Program"; } /** @@ -66,7 +81,7 @@ public static void sayHi() { } /** - * Prints ending message + * Prints ending message and a random quote */ public static void sayBye() { // generating random quote diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index 7882cbc6b..9b752d055 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -1,9 +1,9 @@ package tasks; -public class Deadline extends Task{ +public class Deadline extends Task { protected String by; - public Deadline(String description, String by, int taskNum){ + public Deadline(String description, String by, int taskNum) { super(description + by, taskNum); this.type = 'D'; } diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 08195740d..bc239fe05 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -1,9 +1,9 @@ package tasks; -public class Event extends Task{ +public class Event extends Task { protected String timeRange; - public Event(String description, String timeRange, int taskNum){ + public Event(String description, String timeRange, int taskNum) { super(description + timeRange, taskNum); this.type = 'E'; } diff --git a/src/main/java/tasks/Task.java b/src/main/java/tasks/Task.java index 8651a22ef..c07bcd8ea 100644 --- a/src/main/java/tasks/Task.java +++ b/src/main/java/tasks/Task.java @@ -7,26 +7,26 @@ public class Task { protected int taskNum; - public Task(String description, int taskNum){ + public Task(String description, int taskNum) { this.description = description; this.isDone = false; this.type = ' '; this.taskNum = taskNum; } - public String getDescription(){ + public String getDescription() { return description; } - public String getStatus(){ + public String getStatus() { return (isDone ? "1" : "0"); // mark done task w X } - public void markAsDone(){ + public void markAsDone() { isDone = true; } - public void markAsNotDone(){ + public void markAsNotDone() { isDone = false; } - public char getType(){ + public char getType() { return type; } public int getTaskNum() { diff --git a/src/main/java/tasks/Todo.java b/src/main/java/tasks/Todo.java index 4a6892544..6ca909f2c 100644 --- a/src/main/java/tasks/Todo.java +++ b/src/main/java/tasks/Todo.java @@ -7,7 +7,7 @@ public Todo(String description, int taskNum) { this.type = 'T'; } @Override - public char getType(){ + public char getType() { return type; } }