From 317108a29620018715b2622f9abe7a45a96ca429 Mon Sep 17 00:00:00 2001 From: aaronxujiachen <1943999635x@gmail.com> Date: Thu, 21 Sep 2023 00:11:01 +0800 Subject: [PATCH] Add Dude Level 6 --- src/dude/Dude.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/dude/Dude.java b/src/dude/Dude.java index dc29d223c..0a30483c9 100644 --- a/src/dude/Dude.java +++ b/src/dude/Dude.java @@ -52,6 +52,8 @@ public static void storeDude() { addEventTask(tasks, input); } else if (input.startsWith("mark") || input.startsWith("unmark")) { markOrUnmarkTask(tasks, input); + } else if (input.startsWith("delete")) { + deleteTask(tasks, input); } else { System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } @@ -164,7 +166,28 @@ private static void printAddedTask(ArrayList tasks) { System.out.println("Now you have " + tasks.size() + (tasks.size() == 1 ? " task" : " tasks") + " in the list."); } + private static void deleteTask(ArrayList tasks, String input) { + String[] arrOfInput = input.split(" "); + if (arrOfInput.length < 2) { + System.out.println("Please specify the task index to delete."); + return; + } + try { + int index = Integer.parseInt(arrOfInput[1]) - 1; + if (index < 0 || index >= tasks.size()) { + System.out.println("Task index out of range."); + return; + } + + Task removedTask = tasks.remove(index); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + removedTask); + System.out.println("Now you have " + tasks.size() + (tasks.size() == 1 ? " task" : " tasks") + " in the list."); + } catch (NumberFormatException e) { + System.out.println("Invalid task index format."); + } + } // Method to print the goodbye message public static void byeDude() {