From f447992257f9c7c9691e1bb0a2b77c9f04945c10 Mon Sep 17 00:00:00 2001 From: cheahTJ Date: Fri, 23 Feb 2024 14:07:03 +0800 Subject: [PATCH] Abstract out lengthier method Some methods are still quite lengthy They make it harder to follow the logic Use SLAP to abstract some longer method This is to make the method readable --- build.gradle | 2 +- data/PingMeData.txt | 6 ++ docs/README.md | 12 +++ src/main/java/pingmebot/Parser.java | 100 +++++++++++------- src/main/java/pingmebot/PingMe.java | 2 +- src/main/java/pingmebot/TaskList.java | 10 ++ .../java/pingmebot/command/AddCommand.java | 1 - .../java/pingmebot/command/DeleteCommand.java | 1 - .../java/pingmebot/command/ExitCommand.java | 1 + .../java/pingmebot/command/MarkCommand.java | 1 - .../pingmebot/command/PostponeCommand.java | 3 +- .../java/pingmebot/command/UnmarkCommand.java | 1 - src/test/java/pingmebot/ParserTest.java | 7 +- 13 files changed, 102 insertions(+), 45 deletions(-) create mode 100644 data/PingMeData.txt diff --git a/build.gradle b/build.gradle index fc3d369945..8bdd133e00 100644 --- a/build.gradle +++ b/build.gradle @@ -50,7 +50,7 @@ test { } application { - mainClass.set("pingmebot.PingMe") + mainClass.set("pingmebot.Launcher") } shadowJar { diff --git a/data/PingMeData.txt b/data/PingMeData.txt new file mode 100644 index 0000000000..962ae18dba --- /dev/null +++ b/data/PingMeData.txt @@ -0,0 +1,6 @@ +todo | 0 | proj +deadline | 0 | jkjk | May 05 2024 1800 +todo | 0 | dasds +todo | 1 | dsadsa +event | 0 | dsads | 1800 | 1900 +deadline | 0 | a | Jan 01 2000 1800 diff --git a/docs/README.md b/docs/README.md index 8b51630dff..04005b5018 100644 --- a/docs/README.md +++ b/docs/README.md @@ -58,6 +58,18 @@ Now you have X tasks in the list. where X is the current total number of tasks in your task list +## Saving your task: `bye` + +This command will save all your tasks internally in a file. Bot will say bye to you. + +> Format: `bye` + +Example: `bye` + +``` +Bye. Hope to see you again soon! +``` + ## Viewing all tasks: `list` diff --git a/src/main/java/pingmebot/Parser.java b/src/main/java/pingmebot/Parser.java index 5d3c4505b8..2ab971266b 100644 --- a/src/main/java/pingmebot/Parser.java +++ b/src/main/java/pingmebot/Parser.java @@ -129,10 +129,6 @@ public AddCommand parseDeadlineCommand() throws PingMeException { } } - System.out.println("By:" + by.toString()); - System.out.println("Description:" + description.toString()); - - if (by.toString().isEmpty() || description.toString().isEmpty()) { throw new PingMeException("You have missing fields! " + "You need a task description & a deadline to finish your task, try again!"); @@ -186,7 +182,6 @@ public AddCommand parseEventsCommand() throws PingMeException { throw new PingMeException("You having missing fields! " + "You need a task description, start and end date/time for your task, try again!"); } - return new AddCommand(new Events(description.toString().strip(), start.toString().strip(), end.toString().strip())); } @@ -209,13 +204,17 @@ public MarkCommand parseMarkCommand(int currentNumOfTask) throws PingMeException + "Please specify the task you wish to mark and try again!"); } - if (Integer.parseInt(words.get(1)) > currentNumOfTask - || Integer.parseInt(words.get(1)) <= 0) { - throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " - + "You cannot mark task larger or smaller than this!"); - - } else { - return new MarkCommand(Integer.parseInt(words.get(1)) - 1); + String taskNumberToMark = words.get(1); + try { + int taskNumber = Integer.parseInt(taskNumberToMark); + if (taskNumber > currentNumOfTask || taskNumber <= 0) { + throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " + + "You cannot mark a task larger than this or smaller than 1!"); + } else { + return new MarkCommand(taskNumber - 1); + } + } catch (NumberFormatException e) { + throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer."); } } @@ -237,15 +236,18 @@ public UnmarkCommand parseUnmarkCommand(int currentNumOfTask) throws PingMeExcep + "Please specify the task you wish to un-mark and try again!"); } - if (Integer.parseInt(words.get(1)) > currentNumOfTask - || Integer.parseInt(words.get(1)) <= 0) { - throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " - + "You cannot un-mark task larger or smaller than this!"); - - } else { - return new UnmarkCommand(Integer.parseInt(words.get(1)) - 1); + String taskNumberToMark = words.get(1); + try { + int taskNumber = Integer.parseInt(taskNumberToMark); + if (taskNumber > currentNumOfTask || taskNumber <= 0) { + throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " + + "You cannot un-mark a task larger than this or smaller than 1!"); + } else { + return new UnmarkCommand(taskNumber - 1); + } + } catch (NumberFormatException e) { + throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer."); } - } /** @@ -266,13 +268,19 @@ public DeleteCommand parseDeleteCommand(int currentNumOfTask) throws PingMeExcep + "you want to delete and try again!"); } - if (Integer.parseInt(words.get(1)) > currentNumOfTask - || Integer.parseInt(words.get(1)) <= 0) { - throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " - + "You cannot delete task larger or smaller than this!"); - } else { - return new DeleteCommand(Integer.parseInt(words.get(1)) - 1); + String taskNumberToMark = words.get(1); + try { + int taskNumber = Integer.parseInt(taskNumberToMark); + if (taskNumber > currentNumOfTask || taskNumber <= 0) { + throw new PingMeException("You have currently " + currentNumOfTask + " tasks. " + + "You cannot delete task larger than this or smaller than 1!"); + } else { + return new DeleteCommand(taskNumber - 1); + } + } catch (NumberFormatException e) { + throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer."); } + } /** @@ -319,18 +327,33 @@ public PostponeCommand parsePostponeCommand() throws PingMeException { if (indexOfFrom != -1) { if (indexOfTo == -1) { - throw new PingMeException("Make sure to include " - + "/from ___ /to ___ for events object"); + throw new PingMeException("Make sure to include /by ___ for deadline task" + + " and /from ___ /to ___ for events task"); } } else if (indexOfBy == -1) { - throw new PingMeException("Make sure to include /by ___ for deadline object" - + "and /from ___ /to ___ for events object"); + throw new PingMeException("Make sure to include /by ___ for deadline task" + + " and /from ___ /to ___ for events task"); } + PostponeCommand pc = userCommandToPostponeCommand(taskToPostpone, indexOfBy, indexOfFrom, indexOfTo); + return pc; + } + + /** + * Returns a postpone command object after retrieving all the necessary timings from user input. + * + * @param taskToPostpone The task number the user wants to postpone. + * @param indexOfBy The index of the word 'by' from user input. + * @param indexOfFrom The index of the word 'from' from user input. + * @param indexOfTo The index of the word 'to' from user input. + * @return A postpone command. + * @throws PingMeException If the user inputs the deadline time format wrongly or has an invalid command. + */ + public PostponeCommand userCommandToPostponeCommand(int taskToPostpone, int indexOfBy, + int indexOfFrom, int indexOfTo) throws PingMeException { StringBuilder from = new StringBuilder(); StringBuilder to = new StringBuilder(); StringBuilder by = new StringBuilder(); - if (words.size() < 6) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy HHmm"); for (int i = 2; i < words.size(); i++) { @@ -338,10 +361,14 @@ public PostponeCommand parsePostponeCommand() throws PingMeException { by.append(" ").append(words.get(i)); } } - return new PostponeCommand(taskToPostpone - 1, " ", " ", - LocalDateTime.parse(by.toString().trim(), formatter)); + try { + return new PostponeCommand(taskToPostpone - 1, "", "", + LocalDateTime.parse(by.toString().trim(), formatter)); + } catch (DateTimeParseException e) { + throw new PingMeException("Input your deadline by time in the" + + " d/M/yyyy HHmm format"); + } } - if (words.size() > 5) { for (int i = 2; i < words.size(); i++) { if (i > indexOfFrom && i < indexOfTo) { @@ -350,9 +377,10 @@ public PostponeCommand parsePostponeCommand() throws PingMeException { to.append(words.get(i)); } } - return new PostponeCommand(taskToPostpone - 1, from.toString().trim() - , to.toString().trim(), null); + return new PostponeCommand(taskToPostpone - 1, from.toString().trim(), + to.toString().trim(), null); } throw new PingMeException("Try again with your command!"); } + } diff --git a/src/main/java/pingmebot/PingMe.java b/src/main/java/pingmebot/PingMe.java index c9d1c384d0..3ca2e8191c 100644 --- a/src/main/java/pingmebot/PingMe.java +++ b/src/main/java/pingmebot/PingMe.java @@ -7,7 +7,7 @@ * It allows user to interact with it via command line interface. */ public class PingMe { - protected static String filePath = "./data/dukeData.txt"; + protected static String filePath = "./data/PingMeData.txt"; private Storage storage; private TaskList tasks; private UI ui; diff --git a/src/main/java/pingmebot/TaskList.java b/src/main/java/pingmebot/TaskList.java index fc36f033b6..39b307a38b 100644 --- a/src/main/java/pingmebot/TaskList.java +++ b/src/main/java/pingmebot/TaskList.java @@ -135,6 +135,16 @@ public void postponeTask(int taskNum, String timingStart, String timingEnd, LocalDateTime by) throws PingMeException { Task t = tasks.get(taskNum); + if (t instanceof Deadline && by == null) { + throw new PingMeException("You might have keyed in the details meant for" + + " events task as the deadline task"); + } + + if (t instanceof Events && timingStart.isEmpty() && timingEnd.isEmpty()) { + throw new PingMeException("You might have keyed in the details meant for" + + " deadline task as the events task"); + } + if (t instanceof Deadline) { Deadline d = (Deadline) t; d.setDeadlineByTiming(by); diff --git a/src/main/java/pingmebot/command/AddCommand.java b/src/main/java/pingmebot/command/AddCommand.java index 1df64670b6..f77dd3cf8c 100644 --- a/src/main/java/pingmebot/command/AddCommand.java +++ b/src/main/java/pingmebot/command/AddCommand.java @@ -35,7 +35,6 @@ public AddCommand(Task task) { @Override public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { tasks.addTask(task); - tasks.updateTaskToStorage(storage); ui.additionToTasksText(task, tasks); } diff --git a/src/main/java/pingmebot/command/DeleteCommand.java b/src/main/java/pingmebot/command/DeleteCommand.java index b7ff5bdaad..79b1e4b58e 100644 --- a/src/main/java/pingmebot/command/DeleteCommand.java +++ b/src/main/java/pingmebot/command/DeleteCommand.java @@ -34,7 +34,6 @@ public DeleteCommand(int taskNumber) { @Override public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { ui.deletionToTasksText(taskNumber, tasks); - tasks.updateTaskToStorage(storage); } /** diff --git a/src/main/java/pingmebot/command/ExitCommand.java b/src/main/java/pingmebot/command/ExitCommand.java index 40444d45c7..2df565ac1a 100644 --- a/src/main/java/pingmebot/command/ExitCommand.java +++ b/src/main/java/pingmebot/command/ExitCommand.java @@ -20,6 +20,7 @@ public class ExitCommand extends Command { */ @Override public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { + tasks.updateTaskToStorage(storage); ui.sayGoodbye(); } } diff --git a/src/main/java/pingmebot/command/MarkCommand.java b/src/main/java/pingmebot/command/MarkCommand.java index 165be4fb6b..dc7a31e2bd 100644 --- a/src/main/java/pingmebot/command/MarkCommand.java +++ b/src/main/java/pingmebot/command/MarkCommand.java @@ -30,7 +30,6 @@ public MarkCommand(int taskNumber) { * @throws PingMeException If an error occurs during the execution process. */ public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { - tasks.updateTaskToStorage(storage); ui.markTaskText(taskNumber, tasks); } diff --git a/src/main/java/pingmebot/command/PostponeCommand.java b/src/main/java/pingmebot/command/PostponeCommand.java index ae3e735015..2936e938cf 100644 --- a/src/main/java/pingmebot/command/PostponeCommand.java +++ b/src/main/java/pingmebot/command/PostponeCommand.java @@ -5,7 +5,7 @@ import pingmebot.Storage; import pingmebot.TaskList; import pingmebot.UI; -import pingmebot.task.Deadline; + /** @@ -47,7 +47,6 @@ public PostponeCommand(int taskNumber, String timingStart, String timingEnd, Loc @Override public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { tasks.postponeTask(taskNumber, timingStart, timingEnd, by); - tasks.updateTaskToStorage(storage); ui.postponeTaskText(taskNumber, tasks); } diff --git a/src/main/java/pingmebot/command/UnmarkCommand.java b/src/main/java/pingmebot/command/UnmarkCommand.java index 46dd1b8b13..afd26173f6 100644 --- a/src/main/java/pingmebot/command/UnmarkCommand.java +++ b/src/main/java/pingmebot/command/UnmarkCommand.java @@ -30,7 +30,6 @@ public UnmarkCommand(int taskNumber) { * @throws PingMeException If an error occurs during the execution process. */ public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException { - tasks.updateTaskToStorage(storage); ui.unmarkTaskText(taskNumber, tasks); } diff --git a/src/test/java/pingmebot/ParserTest.java b/src/test/java/pingmebot/ParserTest.java index 093a64bb96..e68cea0a2c 100644 --- a/src/test/java/pingmebot/ParserTest.java +++ b/src/test/java/pingmebot/ParserTest.java @@ -7,7 +7,12 @@ import org.junit.jupiter.api.Test; -import pingmebot.command.*; +import pingmebot.command.AddCommand; +import pingmebot.command.DeleteCommand; +import pingmebot.command.FindCommand; +import pingmebot.command.MarkCommand; +import pingmebot.command.PostponeCommand; +import pingmebot.command.UnmarkCommand; import pingmebot.task.Deadline; import pingmebot.task.Events; import pingmebot.task.ToDos;