Skip to content

Commit

Permalink
Add Dude Level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronxujiachen committed Sep 20, 2023
1 parent 4c76995 commit 317108a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/dude/Dude.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 :-(");
}
Expand Down Expand Up @@ -164,7 +166,28 @@ private static void printAddedTask(ArrayList<Task> tasks) {
System.out.println("Now you have " + tasks.size() + (tasks.size() == 1 ? " task" : " tasks") + " in the list.");
}

private static void deleteTask(ArrayList<Task> 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() {
Expand Down

0 comments on commit 317108a

Please sign in to comment.