Skip to content

Commit

Permalink
Merge pull request #2 from randallnhr/branch-A-Assertions
Browse files Browse the repository at this point in the history
Add assertions
  • Loading branch information
randallnhr authored Feb 12, 2023
2 parents 1318085 + caacaec commit 0af8473
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ shadowJar {

run{
standardInput = System.in
enableAssertions = true
}
13 changes: 8 additions & 5 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public String mark(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size()) {
if (index > taskList.size() || index <= 0) {
throw new DukeException("Invalid task.");
}
return taskList.markTask(Integer.parseInt(input[1]) - 1);
assert index > 0 : "Number less than equal 0.";
return taskList.markTask(index - 1);
}

/**
Expand All @@ -84,10 +85,11 @@ public String unmark(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size()) {
if (index > taskList.size() || index <= 0) {
throw new DukeException("Invalid task.");
}
return taskList.unmarkTask(Integer.parseInt(input[1]) - 1);
assert index > 0 : "Number less than equal 0.";
return taskList.unmarkTask(index - 1);
}

/**
Expand Down Expand Up @@ -169,9 +171,10 @@ public String delete(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size()) {
if (index > taskList.size() || index <= 0) {
throw new DukeException("Invalid task.");
}
assert index > 0 : "Number less than equal 0.";
return taskList.deleteTask(index - 1);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public TaskList loadData() throws DukeException {
if (file.createNewFile()){
return taskList;
}
assert file.exists(): "File was not successfully created.";
} catch (IOException e) {
throw new DukeException("Something went wrong when accessing file");
}
Expand Down Expand Up @@ -83,7 +84,8 @@ public void storeData(ArrayList<Task> t) throws DukeException {
try {
if (file.createNewFile()) {
System.out.println("Created new file taskList.txt");
}
}
assert file.exists(): "File was not successfully created.";
FileWriter fw = new FileWriter(file);
String taskString = "";
for (int i = 0; i < t.size(); i++){
Expand Down
6 changes: 5 additions & 1 deletion taskList.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
D|0|2|2023-05-05
D|1|2|2023-05-05
T|0|3
T|0|4
T|0|5
T|0|6
D|0||2023-05-25
E|0|3|2023-12-23|2023-12-24
E|0|4|2023-12-23|2023-12-24
E|0|5|2023-03-23|2023-05-06

0 comments on commit 0af8473

Please # to comment.