From 73407527b42fa7b42f5dd213b9c6a4f8c2fea74e Mon Sep 17 00:00:00 2001 From: raydenlim <109991591+raydenlim@users.noreply.github.com> Date: Fri, 15 Sep 2023 17:36:58 +0800 Subject: [PATCH] A-CodeQuality, refactored code to follow good coding practices. --- data/tasks.txt | 7 +- src/main/java/Command/DeleteCommand.java | 2 +- src/main/java/Parser/Parser.java | 238 +++++++++++++++++++---- src/main/java/Ui/Ui.java | 25 +-- 4 files changed, 220 insertions(+), 52 deletions(-) diff --git a/data/tasks.txt b/data/tasks.txt index 3ae9af6f4a..9967022d22 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1,5 +1,2 @@ -[A][ ] hehe -[D][ ] (by: now) -[A][ ] airubg -[D][ ] ugbia (by: efuba woubjs) -[A][ ] ff +[D][ ] uabrgiub (by: 12 Dec 1212 00:00) +[A][ ] iguiugbf diff --git a/src/main/java/Command/DeleteCommand.java b/src/main/java/Command/DeleteCommand.java index cf1be98760..aac1e29a0d 100644 --- a/src/main/java/Command/DeleteCommand.java +++ b/src/main/java/Command/DeleteCommand.java @@ -42,7 +42,7 @@ public DeleteCommand(int taskIndex) { @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { try { - assert taskIndex > 0 && taskIndex < taskList.getTaskCount() : "Invalid task index/ out of range"; + assert taskIndex >= 0 && taskIndex <= taskList.getTaskCount() : "Invalid task index/ out of range"; Task removedTask = taskList.deleteTask(taskIndex); storage.saveTask(taskList.getTasks()); return ui.showDeletedTask(removedTask, taskList.getTaskCount()); diff --git a/src/main/java/Parser/Parser.java b/src/main/java/Parser/Parser.java index 92b0ba9284..afce11dabe 100644 --- a/src/main/java/Parser/Parser.java +++ b/src/main/java/Parser/Parser.java @@ -54,20 +54,9 @@ public static Task parseTask(String taskData) throws FileNotFoundException, Duke taskToAdd = new ToDo(taskInfo); break; case "D": - taskInfo = taskInfo.substring(0, taskInfo.indexOf("(") - 1); - String xtraInfo = taskData.substring(taskData.indexOf("(") + 1, taskData.indexOf(")")); - String[] deadLineInfo = xtraInfo.split(": "); - String by = deadLineInfo[1]; - taskToAdd = new DeadLine(taskInfo, by); - break; + return createDeadLineTask(taskInfo); case "E": - taskInfo = taskInfo.substring(0, taskInfo.indexOf('(') - 1); - String addInfo = taskData.substring(taskData.indexOf('(') + 1, taskData.indexOf(')')); - String[] eventInfo = addInfo.split(": "); - String from = eventInfo[1].substring(0, eventInfo[1].length() - 2).trim(); - String to = eventInfo[2].trim(); - taskToAdd = new Event(taskInfo, from, to); - break; + return createEventTask(taskInfo); default: throw new DukeException("File is corrupted!"); } @@ -83,6 +72,44 @@ public static Task parseTask(String taskData) throws FileNotFoundException, Duke return null; } + /** + * Creates a DeadLine task based on task information. + * + * @param taskInfo The task information string containing the task description and deadline. + * @return A DeadLine task instance based on the task information. + * @throws DukeException If there is an issue parsing the task information. + */ + private static Task createDeadLineTask(String taskInfo) throws DukeException { + try { + String[] parts = taskInfo.split(" \\(by: "); + String description = parts[0]; + String by = parts[1].substring(0, parts[1].length() - 1); + return new DeadLine(description, by); + } catch (Exception e) { + throw new DukeException("Error parsing Deadline task: " + taskInfo); + } + } + + /** + * Creates an Event task based on task information. + * + * @param taskInfo The task information string containing the task description and event details. + * @return An Event task instance based on the task information. + * @throws DukeException If there is an issue parsing the task information. + */ + private static Task createEventTask(String taskInfo) throws DukeException { + try { + String[] parts = taskInfo.split(" \\(from: "); + String description = parts[0]; + String[] dateParts = parts[1].split(" to "); + String from = dateParts[0]; + String to = dateParts[1].substring(0, dateParts[1].length() - 1); + return new Event(description, from, to); + } catch (Exception e) { + throw new DukeException("Error parsing Event task: " + taskInfo); + } + } + /** * Parses a user input string and creates the corresponding `Command` object. * @@ -100,18 +127,78 @@ public static Command parse(String input) throws DukeException { case "list": return new ListCommand(); case "todo": - String todoDesc = input.substring(5).trim(); // Extract the description + return createToDoCommand(input); + case "add": + return createAddCommand(input); + case "deadline": + return createDeadLineCommand(input); + case "event": + return createEventCommand(input); + case "unmark": + return createUnmarkCommand(input); + case "mark": + return createMarkCommand(input); + case "echo": + return createEchoCommand(input); + case "delete": + return createDeleteCommand(input); + case "help": + return new HelpCommand(); + case "find": + return createFindCommand(input); + default: + throw new DukeException("I'm sorry, but I don't understand that command."); + } + } + + /** + * Creates a ToDoCommand based on user input. + * + * @param input The user input string containing the command and description. + * @return A ToDoCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static ToDoCommand createToDoCommand(String input) throws DukeException { + try { + String todoDesc = input.substring(5).trim(); Task todoTask = new ToDo(todoDesc); return new ToDoCommand(todoTask); - case "add": + } catch (Exception e) { + throw new DukeException("Error parsing ToDo command: " + input); + } + } + + /** + * Creates an AddCommand based on user input. + * + * @param input The user input string containing the command and description. + * @return An AddCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static AddCommand createAddCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); if (words.length < 2) { throw new DukeException("Please specify a task to add."); } String addDesc = input.substring(4).trim(); Task addTask = new Add(addDesc); return new AddCommand(addTask); - case "deadline": - String deadlineDesc = input.substring(8).trim(); // Extract the description + } catch (Exception e) { + throw new DukeException("Error parsing Add command: " + input); + } + } + + /** + * Creates a DeadLineCommand based on user input. + * + * @param input The user input string containing the command and description. + * @return A DeadLineCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static DeadLineCommand createDeadLineCommand(String input) throws DukeException { + try { + String deadlineDesc = input.substring(8).trim(); if (!deadlineDesc.contains("/by")) { throw new DukeException("The deadline command should include '/by'."); } @@ -119,8 +206,21 @@ public static Command parse(String input) throws DukeException { String description = deadlineParts[0].trim(); String by = deadlineParts[1].trim(); return new DeadLineCommand(description, by); - case "event": - String eventDesc = input.substring(5).trim(); // Extract the description + } catch (Exception e) { + throw new DukeException("Error parsing Deadline command: " + input); + } + } + + /** + * Creates an EventCommand based on user input. + * + * @param input The user input string containing the command and event description. + * @return An EventCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static EventCommand createEventCommand(String input) throws DukeException { + try { + String eventDesc = input.substring(5).trim(); if (!eventDesc.contains("/from") || !eventDesc.contains("/to")) { throw new DukeException("The event command should include '/from' and '/to'."); } @@ -130,40 +230,108 @@ public static Command parse(String input) throws DukeException { String from = dateParts[0].trim(); String to = dateParts[1].trim(); return new EventCommand(eventDescription, from, to); - case "unmark": - if (words.length < 2) { - throw new DukeException("Please specify a task number to unmark."); - } - int taskNumberToUnmark = Integer.parseInt(words[1]) - 1; // Assuming tasks are 1-based - return new UnmarkCommand(taskNumberToUnmark); - case "mark": + } catch (Exception e) { + throw new DukeException("Error parsing Event command: " + input); + } + } + + /** + * Creates a MarkCommand based on user input. + * + * @param input The user input string containing the command and task number to mark as done. + * @return A MarkCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static MarkCommand createMarkCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); if (words.length < 2) { throw new DukeException("Please specify a task number to mark as done."); } - int taskNumberToMark = Integer.parseInt(words[1]) - 1; // Assuming tasks are 1-based + int taskNumberToMark = Integer.parseInt(words[1]) - 1; return new MarkCommand(taskNumberToMark); - case "echo": + } catch (Exception e) { + throw new DukeException("Error parsing Mark command: " + input); + } + } + + /** + * Creates an UnmarkCommand based on user input. + * + * @param input The user input string containing the command and task number to unmark. + * @return An UnmarkCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static UnmarkCommand createUnmarkCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); + if (words.length < 2) { + throw new DukeException("Please specify a task number to unmark."); + } + int taskNumberToUnmark = Integer.parseInt(words[1]) - 1; + return new UnmarkCommand(taskNumberToUnmark); + } catch (Exception e) { + throw new DukeException("Error parsing Unmark command: " + input); + } + } + + /** + * Creates an EchoCommand based on user input. + * + * @param input The user input string containing the command and text for the echo. + * @return An EchoCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static EchoCommand createEchoCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); if (words.length < 2) { throw new DukeException("Please provide text for the echo command."); } - String echoText = input.substring(5).trim(); // Extract the text + String echoText = input.substring(5).trim(); return new EchoCommand(echoText); - case "delete": + } catch (Exception e) { + throw new DukeException("Error parsing Echo command: " + input); + } + } + + /** + * Creates a DeleteCommand based on user input. + * + * @param input The user input string containing the command and task index to delete. + * @return A DeleteCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static DeleteCommand createDeleteCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); if (words.length < 2) { throw new DukeException("Please provide me a task index to delete"); } int taskNumToDel = Integer.parseInt(words[1]) - 1; return new DeleteCommand(taskNumToDel); - case "help": - return new HelpCommand(); - case "find": + } catch (Exception e) { + throw new DukeException("Error parsing Delete command: " + input); + } + } + + /** + * Creates a FindCommand based on user input. + * + * @param input The user input string containing the command and keyword for searching. + * @return A FindCommand instance based on the user input. + * @throws DukeException If there is an issue parsing the input. + */ + private static FindCommand createFindCommand(String input) throws DukeException { + try { + String[] words = input.split(" "); if (words.length < 2) { throw new DukeException("Please provide a keyword for the find command."); } String keyword = input.substring(5).trim(); return new FindCommand(keyword); - default: - throw new DukeException("I'm sorry, but I don't understand that command."); + } catch (Exception e) { + throw new DukeException("Error parsing Find command: " + input); } } } diff --git a/src/main/java/Ui/Ui.java b/src/main/java/Ui/Ui.java index b0d62e0996..4ba5d2bff3 100644 --- a/src/main/java/Ui/Ui.java +++ b/src/main/java/Ui/Ui.java @@ -14,6 +14,19 @@ */ public class Ui { + private static final String HELP_DESK = "List of Commands: Add, Deadline, Event, Todo, Echo," + + "Mark, Unmark, Delete, Find, Bye\n" + + "1. Add - Add a task to the list\n" + + "2. Deadline - Add a task with a deadline\n" + + "3. Event - Add an event task\n" + + "4. Todo - Add a todo task\n" + + "5. Echo - Echo a message\n" + + "6. Mark - Mark a task as done\n" + + "7. Unmark - Unmark a task as done\n" + + "8. Delete - Delete a task\n" + + "9. Find - Find tasks by keyword\n" + + "10. Bye - Exit the program"; + /** * Displays a welcome message to the user when the application starts. * @@ -49,17 +62,7 @@ public String showError(DukeException e) { * @return A list of available commands. */ public String showCommands() { - return "List of Commands: Add, Deadline, Event, Todo, Echo, Mark, Unmark, Delete, Find, Bye\n" - + "1. Add - Add a task to the list\n" - + "2. Deadline - Add a task with a deadline\n" - + "3. Event - Add an event task\n" - + "4. Todo - Add a todo task\n" - + "5. Echo - Echo a message\n" - + "6. Mark - Mark a task as done\n" - + "7. Unmark - Unmark a task as done\n" - + "8. Delete - Delete a task\n" - + "9. Find - Find tasks by keyword\n" - + "10. Bye - Exit the program"; + return HELP_DESK; } /**