Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add assertion statements #2

Merged
merged 1 commit into from
Sep 18, 2022
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 data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T | 0 | ask
1 change: 1 addition & 0 deletions src/main/java/duke/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AddCommand extends Command {
* @param task task to be added
*/
public AddCommand(Task task) {
assert task != null : "task cannot be null";
this.task = task;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ public boolean isExit() {
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
try {
Task deletedTask = tasks.deleteTaskAtPos(this.index);
assert this.index <= tasks.getCount() + 1 && this.index > 0 :
"Position argument should be more than 0 and less than or equal to the task list size";
storage.save(tasks);
return ui.showDeleted(deletedTask);
} catch (IndexOutOfBoundsException e) {
if (tasks.getCount() == 0){
throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST);
} else {
assert tasks.getCount() > 0 : "task list should have 1 or more tasks";
throw new DukeException(Message.returnTaskNotFound(tasks));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class FindCommand extends Command {
private String keyword;

public FindCommand(String keyword) {
assert keyword != null : "keyword cannot be null";
this.keyword = keyword;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti
try {
tasks.markTaskAtPos(this.index);
Task currentTask = tasks.getTask(this.index);
assert this.index <= tasks.getCount() && this.index > 0 :
"Index should be more than 0 and less than or equal to the task list size";
storage.save(tasks);
return ui.showMarked(currentTask);
} catch (IndexOutOfBoundsException e) {
if (tasks.getCount() == 0) {
throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST);
} else {
assert tasks.getCount() > 0 : "task list should have 1 or more tasks";
throw new DukeException(Message.returnTaskNotFound(tasks));
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti
try {
tasks.unmarkTaskAtPos(this.index);
Task currentTask = tasks.getTask(this.index);
assert this.index <= tasks.getCount() && this.index > 0 :
"Position argument should be more than 0 and less than or equal to the task list size";
storage.save(tasks);
return ui.showUnmarked(currentTask);
} catch (IndexOutOfBoundsException e) {
if (tasks.getCount() == 0) {
throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST);
} else {
assert tasks.getCount() > 0 : "task list should have 1 or more tasks";
throw new DukeException(Message.returnTaskNotFound(tasks));
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import duke.task.ToDo;

import java.time.LocalDate;
import java.util.Arrays;

public class Parser {

Expand Down Expand Up @@ -114,10 +115,16 @@ private static DeleteCommand deleteTask(String command) throws DukeException {
}

private static FindCommand findTask(String command) throws DukeException {
String[] commandList = command.strip().split(" ");
String[] commandList = command.strip().split(" ", 2);
try {
return new FindCommand(commandList[1]);
String keyword = commandList[1].strip();
if (keyword.equals("")) {
throw new DukeException(Message.INVALID_FIND_TASK_FORMAT);
}
assert !keyword.equals("") : "keyword cannot be empty";
return new FindCommand(keyword);
} catch (IndexOutOfBoundsException e) {
assert commandList.length < 2 : "Command list should contain at most 1 element";
throw new DukeException(Message.INVALID_FIND_TASK_FORMAT);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public int getCount(){
*/
public void markTaskAtPos(int position) throws IndexOutOfBoundsException{
Task currTask = getTask(position);
assert position <= this.count && position > 0 :
"Position argument should be more than 0 and less than or equal to the task list size";
currTask.markAsDone();
}

Expand All @@ -77,6 +79,8 @@ public void markTaskAtPos(int position) throws IndexOutOfBoundsException{
*/
public void unmarkTaskAtPos(int position) throws IndexOutOfBoundsException{
Task currTask = getTask(position);
assert position <= this.count && position > 0 :
"Position argument should be more than 0 and less than or equal to the task list size";
currTask.unmark();
}

Expand All @@ -90,6 +94,8 @@ public void unmarkTaskAtPos(int position) throws IndexOutOfBoundsException{
*/
public Task deleteTaskAtPos(int position) throws IndexOutOfBoundsException {
Task deletedTask = getTask(position);
assert position <= this.count && position > 0 :
"Position argument should be more than 0 and less than or equal to the task list size";
this.taskArray.remove(position - 1);
this.count -= 1;
return deletedTask;
Expand Down