diff --git a/src/main/java/duke/ActionHandler.java b/src/main/java/duke/ActionHandler.java index e64a1e2e8..d13cb00e7 100644 --- a/src/main/java/duke/ActionHandler.java +++ b/src/main/java/duke/ActionHandler.java @@ -24,6 +24,10 @@ protected static void byeHandler() { // Print out everything in the list, index starts from 1 protected static void listHandler(Vector tasks) { + if (tasks.size() == 0) { + Helper.printlnWithIndent("You don't have a task in your list!"); + return; + } Helper.printlnWithIndent("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i += 1) { Helper.printlnWithIndent(String.format("%d.\t%s", i + 1, tasks.get(i))); @@ -55,6 +59,28 @@ protected static void doneHandler(Vector tasks, String[] arguments) throws } } + // Delete a task from the list + protected static void deleteHandler(Vector tasks, String[] arguments) throws InvalidInputException { + if (arguments.length < 2) { + // An index must be provided for the task to be marked "done" + Helper.printlnWithIndent("You will need to give me an index, like this: `delete 2`."); + } else { + try { + int index = Integer.parseInt(arguments[1]); + if (index > tasks.size() || index < 1) { + // This index is out of the boundary of our database + throw new InvalidInputException(InputExceptionType.INDEX_OUT_OF_BOUND); + } + + Task task = tasks.remove(index - 1); + Helper.printlnWithIndent("Sure! I've removed this task:"); + Helper.printlnWithIndent("\t" + task); + } catch (NumberFormatException e) { + throw new InvalidInputException(InputExceptionType.NOT_INTEGER, e); + } + } + } + // Create a deadline task protected static void deadlineHandler(Vector tasks, String[] arguments) throws InvalidInputException { int i = Helper.findIndex(arguments, "/by"); diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1b762a8fb..d674c0aa6 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -44,6 +44,9 @@ public static void main(String[] args) { case "done": ActionHandler.doneHandler(tasks, arguments); break; + case "delete": + ActionHandler.deleteHandler(tasks, arguments); + break; case "deadline": ActionHandler.deadlineHandler(tasks, arguments); break; diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index db9cba53c..c0898d0ee 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -99,5 +99,35 @@ 3. [D][✓] Deadline A (by: 1 Aug 2021) ------------------------------------------------------------ ------------------------------------------------------------ + Oops! We don't have an existing entry with this index. + ------------------------------------------------------------ + ------------------------------------------------------------ + Oops! Only integer is accepted as the argument. + ------------------------------------------------------------ + ------------------------------------------------------------ + Oops! We don't have an existing entry with this index. + ------------------------------------------------------------ + ------------------------------------------------------------ + Here are the tasks in your list: + 1. [T][✓] Task A + 2. [E][✓] Event A (at: 1 Apr 2021) + 3. [D][✓] Deadline A (by: 1 Aug 2021) + ------------------------------------------------------------ + ------------------------------------------------------------ + Sure! I've removed this task: + [T][✓] Task A + ------------------------------------------------------------ + ------------------------------------------------------------ + Sure! I've removed this task: + [D][✓] Deadline A (by: 1 Aug 2021) + ------------------------------------------------------------ + ------------------------------------------------------------ + Sure! I've removed this task: + [E][✓] Event A (at: 1 Apr 2021) + ------------------------------------------------------------ + ------------------------------------------------------------ + You don't have a task in your list! + ------------------------------------------------------------ + ------------------------------------------------------------ Bye. Hope to see you again soon! ------------------------------------------------------------ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 4af119470..d8f8337c1 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -21,4 +21,12 @@ done -1 done Not_A_Number done 1000 list +delete -1 +delete Not_A_Number +delete 1000 +list +delete 1 +delete 2 +delete 1 +list bye \ No newline at end of file diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index a414006a1..e6c6d907a 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -1,4 +1,6 @@ #!/usr/bin/env bash +cwd=$(pwd) +cd $(dirname "$0") # create bin directory if it doesn't exist if [ ! -d "../bin" ] @@ -35,4 +37,6 @@ then else echo "Test result: FAILED" exit 1 -fi \ No newline at end of file +fi + +cd $cwd \ No newline at end of file