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

Assertions for important assumptions. #3

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
11 changes: 11 additions & 0 deletions src/main/java/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Parser {
*/
public static String parseInput(String userInput, TaskList tasks, TextUi textUi, Storage storage) throws ChillChiefException, IOException {
String[] tokens = userInput.split(" ", 2);
assert !tokens[0].isEmpty() : "userInput cannot have empty command keyword.";
String command = tokens[0].toLowerCase();

switch (command) {
Expand Down Expand Up @@ -64,6 +65,7 @@ public static String parseInput(String userInput, TaskList tasks, TextUi textUi,
}

private static String handleFind(String[] tokens, TaskList taskList, TextUi textUi) {
assert tokens != null : "Tokens cannot be null";
int count = 1;
StringBuilder result = new StringBuilder();

Expand All @@ -81,6 +83,8 @@ private static String handleFind(String[] tokens, TaskList taskList, TextUi text
}

private static LocalDateTime parseDate(String dateString) throws ChillChiefException {
assert dateString != null && !dateString.isEmpty() : "dateString should not be null or empty";

try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
return LocalDateTime.parse(dateString, formatter);
Expand All @@ -91,13 +95,15 @@ private static LocalDateTime parseDate(String dateString) throws ChillChiefExcep
}

private static String handleTodo(String[] tokens, TaskList tasks, TextUi textUi) {
assert tokens != null : "Tokens cannot be null";
String description = tokens[1].trim();
Todo todoTask = new Todo(description, false);
tasks.addToTaskList(todoTask);
return textUi.showTaskAdded(todoTask, tasks.getTaskListLength());
}

private static String handleDeadline(String[] tokens, TaskList tasks, TextUi textUi) throws ChillChiefException {
assert tokens != null : "Tokens cannot be null";
String descriptionAndBy = tokens[1].trim();
String deadlineDescription = descriptionAndBy.split(" /by ")[0].trim();
String when = descriptionAndBy.split(" /by ")[1].trim();
Expand All @@ -108,6 +114,7 @@ private static String handleDeadline(String[] tokens, TaskList tasks, TextUi tex
}

private static String handleEvent(String[] tokens, TaskList tasks, TextUi textUi) throws ChillChiefException {
assert tokens != null : "Tokens cannot be null";
String descriptionAndStartAndEnd = tokens[1].trim();
String[] parts = descriptionAndStartAndEnd.split(" /from | /to ");
String eventDescription = parts[0].trim();
Expand All @@ -119,21 +126,25 @@ private static String handleEvent(String[] tokens, TaskList tasks, TextUi textUi
}

private static String handleUnmark(String[] tokens, TaskList tasks, TextUi textUi) throws ChillChiefException {
assert tokens != null && tokens.length > 2 : "Invalid command format for unmark";
int index = Integer.parseInt(tokens[1]) - 1;
Task taskToUnMark = tasks.getTask(index);
tasks.getTask(index).markNotDone();
return textUi.showMarkedOrUnmarkMessage(taskToUnMark);
}

private static String handleMark(String[] tokens, TaskList tasks, TextUi textUi) throws ChillChiefException {
assert tokens != null && tokens.length > 2 : "Invalid command format for mark";
int index = Integer.parseInt(tokens[1]) - 1;
Task taskToMark = tasks.getTask(index);
taskToMark.markDone();
return textUi.showMarkedOrUnmarkMessage(taskToMark);
}

private static String handleDelete(String[] tokens, TaskList tasks, TextUi textUi) throws ChillChiefException {
assert tokens != null : "Tokens cannot be null";
int index = Integer.parseInt(tokens[1]) - 1;
assert index < tasks.getTaskListLength() : "Index for task to delete is out of bounds";
Task taskToDelete = tasks.getTask(index);
tasks.deleteTask(index);
return textUi.showDeletedTask(taskToDelete, tasks.getTaskListLength());
Expand Down