From eb30631b9aaee96580d97631d9753a43ee5ba572 Mon Sep 17 00:00:00 2001 From: Moon Ji Hoon Date: Wed, 13 Sep 2023 20:58:29 +0800 Subject: [PATCH] A-Assertions * Enable Assertions as it is disabled by default. * Add assert in TaskList to check index range. --- build.gradle | 1 + data/duke.txt | 2 +- src/main/java/duke/Duke.java | 1 - src/main/java/duke/TaskList.java | 3 +++ src/main/java/duke/command/MarkCommand.java | 4 +++- src/main/java/duke/command/UnmarkCommand.java | 4 +++- 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index d724cb0f85..e1ac861ebb 100644 --- a/build.gradle +++ b/build.gradle @@ -66,4 +66,5 @@ shadowJar { run{ standardInput = System.in + enableAssertions = true } diff --git a/data/duke.txt b/data/duke.txt index e68e4b4602..20d304df4e 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1 +1 @@ -T|false|1 +T|true|1 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ecd4dade5a..e44dc0bf3e 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -45,7 +45,6 @@ public static void main(String[] args) { public void start(Stage stage) { // Read file Storage.readTask(tasks, ui); - //Step 1. Setting up required components //The container for the content of the chat to scroll. diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 1d9c13475e..deab195484 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -36,6 +36,7 @@ public void add(Task task) { * @param index The index of the task to be removed. */ public void remove(int index) { + assert index >= 0 : "index should be >= 0"; tasks.remove(index); } @@ -45,6 +46,7 @@ public void remove(int index) { * @param index The index of the task to be marked as done. */ public void mark(int index) { + assert index >= 0 : "index should be >= 0"; tasks.get(index).mark(); } @@ -54,6 +56,7 @@ public void mark(int index) { * @param index The index of the task to be unmarked. */ public void unmark(int index) { + assert index >= 0 : "index should be >= 0"; tasks.get(index).unmark(); } diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index 235b8e50c5..3578601f13 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -3,6 +3,7 @@ import duke.TaskList; import duke.Ui; import duke.exception.DukeException; +import duke.task.Task; /** * Command that marks a specific task. @@ -22,8 +23,9 @@ public boolean execute(TaskList tasks, Ui ui) throws DukeException { String msg = ui.getLastMsg(); String[] words = msg.toLowerCase().split("\\s+"); int index = Integer.parseInt(words[1]) - 1; + Task task = tasks.get(index); tasks.mark(index); - ui.respond("Nice! I've marked this task as done: " + "\n" + tasks.get(index)); + ui.respond("Nice! I've marked this task as done: " + "\n" + task); } catch (Exception e) { throw new DukeException("Wrong index. Try checking your list first."); } diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index 4670b9da1b..ab32152135 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -3,6 +3,7 @@ import duke.TaskList; import duke.Ui; import duke.exception.DukeException; +import duke.task.Task; /** * Command that un-marks a specific task. @@ -23,8 +24,9 @@ public boolean execute(TaskList tasks, Ui ui) throws DukeException { String msg = ui.getLastMsg(); String[] words = msg.toLowerCase().split("\\s+"); int index = Integer.parseInt(words[1]) - 1; + Task task = tasks.get(index); tasks.unmark(index); - ui.respond("OK, I've marked this task as not done yet:" + "\n" + tasks.get(index)); + ui.respond("OK, I've marked this task as not done yet:" + "\n" + task); } catch (Exception e) { throw new DukeException("Wrong index. Try checking your list first."); }