From db0170492c6c43527e04871a9906f93b9feb49f7 Mon Sep 17 00:00:00 2001 From: LimZiJia Date: Tue, 13 Feb 2024 22:06:59 +0800 Subject: [PATCH] Add mass ops mark, unmark and delete now accepts a list of indexes separated by spaces. --- src/main/duke/duke/Parser.java | 16 ++++--- src/main/duke/tasks/TaskList.java | 76 ++++++++++++++++++------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/main/duke/duke/Parser.java b/src/main/duke/duke/Parser.java index c23196b206..ae8afcb2dd 100644 --- a/src/main/duke/duke/Parser.java +++ b/src/main/duke/duke/Parser.java @@ -78,13 +78,13 @@ public String parse(String input) { reply = Duke.storage.save(Duke.tasks); break; case MARK: - reply = Duke.tasks.mark(inputParsed[1]); // mark(task number) + reply = Duke.tasks.mark(inputParsed); // mark(task number) break; case UNMARK: - reply = Duke.tasks.unmark(inputParsed[1]); // unmark(task number) + reply = Duke.tasks.unmark(inputParsed); // unmark(task number) break; case DELETE: - reply = Duke.tasks.delete(inputParsed[1]); // delete(task number) + reply = Duke.tasks.delete(inputParsed); // delete(task number) break; case FIND: reply = Duke.tasks.find(inputParsed[1]); // find(task name) @@ -153,9 +153,13 @@ public String addTask(String[] inputs, Command command) { private void checkValidFormat(String[] inputs, Command command) { switch (command) { - case MARK: // Command: mark taskNumber - case UNMARK: // Command: unmark taskNumber - case DELETE:// Command: delete taskNumber + case MARK: // Command: mark listOfTaskNumbers + case UNMARK: // Command: unmark listOfTaskNumbers + case DELETE:// Command: delete listOfTaskNumbers + if (inputs.length < 2) { + throw new DukeEmptyArgumentException(); + } + break; case FIND: // Command: find taskName case TODO: // Command: todo taskName if (inputs.length < 2) { diff --git a/src/main/duke/tasks/TaskList.java b/src/main/duke/tasks/TaskList.java index ecbe0e31ea..a955c2d38b 100644 --- a/src/main/duke/tasks/TaskList.java +++ b/src/main/duke/tasks/TaskList.java @@ -66,22 +66,26 @@ public List getList() { * * @param input The index of the task that should be marked as done. */ - public String mark(String input) { - String s; + public String mark(String[] input) { + StringBuilder s = new StringBuilder(); + String forPrintingError = ""; try { - int i = Integer.parseInt(input); - Task t = Duke.tasks.get(i - 1); - t.markAsDone(); - Duke.tasks.set(i - 1, t); - s = "Nice! I've marked this task as done:\n " + t; - + // Start at 1 beacuse indexes start at 1. Index 0 is "mark" + for (int i = 1; i < input.length; i++) { + forPrintingError = input[i]; + int idx = Integer.parseInt(input[i]); + Task t = Duke.tasks.get(idx - 1); + t.markAsDone(); + Duke.tasks.set(idx - 1, t); + s.append("Nice! I've marked this task as done:\n ").append(t).append('\n'); + } } catch (NumberFormatException e) { - s = "Not a valid number!\n"; + s = new StringBuilder(forPrintingError + " is not a valid number!\n"); } catch (IndexOutOfBoundsException e) { - s = "Sorry, index out of range!\n"; + s = new StringBuilder(forPrintingError + "--> index out of range!\n"); } - return s; + return s.toString(); } /** @@ -89,22 +93,26 @@ public String mark(String input) { * * @param input The index of the task that should be marked as not done. */ - public String unmark(String input) { - String s; + public String unmark(String[] input) { + StringBuilder s = new StringBuilder(); + String forPrintingError = ""; try { - int i = Integer.parseInt(input); - Task t = Duke.tasks.get(i - 1); - t.markAsNotDone(); - Duke.tasks.set(i - 1, t); - s = "OK, I've marked this task as not done yet:\n " + t; - + // Start at 1 beacuse indexes start at 1. Index 0 is "unmark" + for (int i = 1; i < input.length; i++) { + forPrintingError = input[i]; + int idx = Integer.parseInt(input[i]); + Task t = Duke.tasks.get(idx - 1); + t.markAsNotDone(); + Duke.tasks.set(idx - 1, t); + s.append("OK, I've marked this task as not done yet:\n ").append(t).append('\n'); + } } catch (NumberFormatException e) { - s = "Not a valid number!\n"; + s = new StringBuilder(forPrintingError + " is not a valid number!\n"); } catch (IndexOutOfBoundsException e) { - s = "Sorry, index out of range!\n"; + s = new StringBuilder(forPrintingError + "--> index out of range!\n"); } - return s; + return s.toString(); } /** @@ -112,21 +120,25 @@ public String unmark(String input) { * * @param input The index of the task that should be deleted. */ - public String delete(String input) { - String s; + public String delete(String[] input) { + StringBuilder s = new StringBuilder(); + String forPrintingError = ""; try { - int i = Integer.parseInt(input); - Task t = Duke.tasks.remove(i - 1); - s = "Noted. I've removed this task:\n " + t; - s += String.format("\nNow you have %d tasks in the list.\n", Duke.tasks.size()); - + // Start at 1 beacuse indexes start at 1. Index 0 is "delete" + for (int i = 1; i < input.length; i++) { + forPrintingError = input[i]; + int idx = Integer.parseInt(input[i]); + Task t = Duke.tasks.remove(idx - 1); + s.append("Noted. I've removed this task:\n ").append(t); + s.append(String.format("Now you have %d tasks in the list.\n\n", Duke.tasks.size())); + } } catch (NumberFormatException e) { - s = "Not a valid number! Or perhaps add a ' '\n"; + s = new StringBuilder(forPrintingError + " is not a valid number! Or perhaps add a ' '\n"); } catch (IndexOutOfBoundsException e) { - s = "Sorry, index out of range!\n"; + s = new StringBuilder(forPrintingError + "--> index out of range!\n"); } - return s; + return s.toString(); } /**