Skip to content

Commit

Permalink
A-Assertions
Browse files Browse the repository at this point in the history
* Enable Assertions as it is disabled by default.
* Add assert in TaskList to check index range.
  • Loading branch information
Moon Ji Hoon committed Sep 13, 2023
1 parent 3ca3adf commit eb30631
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ shadowJar {

run{
standardInput = System.in
enableAssertions = true
}
2 changes: 1 addition & 1 deletion data/duke.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
T|false|1
T|true|1
1 change: 0 additions & 1 deletion src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duke.TaskList;
import duke.Ui;
import duke.exception.DukeException;
import duke.task.Task;

/**
* Command that marks a specific task.
Expand All @@ -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.");
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.");
}
Expand Down

0 comments on commit eb30631

Please sign in to comment.