Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add assertions #2

Merged
merged 3 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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