Skip to content

Commit

Permalink
Merge pull request #1 from NereusWB922/branch-A-Assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
NereusWB922 authored Sep 10, 2023
2 parents 9468916 + d63e83f commit 6b83a4d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ shadowJar {

run{
standardInput = System.in
enableAssertions = true
}
3 changes: 3 additions & 0 deletions src/main/java/corgi/Corgi.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public String getIntro() {
* Starts the chatbot - Corgi.
*/
public String getResponse(String input) {
assert !input.equals("") : "Input cannot be empty";

Command cmd = null;

try {
cmd = new CommandParser().parse(input);
assert cmd != null : "Command returned from parser cannot be null";
return cmd.execute(this.tasks, this.renderer, this.storage);
} catch (InvalidCommandFormatException e) {
return this.renderer.showError(e.getClass().getSimpleName(), e.getMessage());
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/corgi/parsers/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ private Command newDateCommand(String[] inputs) throws InvalidCommandFormatExcep
throw new InvalidCommandFormatException("Invalid date format!");
}

assert target != null : "Target date cannot be null.";

return new FindTasksOnDateCommand(target);
}

Expand All @@ -172,14 +174,16 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC
if (type == CommandType.TODO) {
Task target = new ToDo(taskInfo);

assert target != null : "New Todo task cannot be null.";

return new AddTaskCommand(target, CommandType.TODO);

} else if (type == CommandType.DEADLINE) {
// todo: check number of /by
String[] deadlineInfos = taskInfo.split(" /by ");

if (deadlineInfos.length == 1) {
throw new InvalidCommandFormatException("Missing deadline!" + "\nFormat: "
throw new InvalidCommandFormatException("Invalid format!" + "\nFormat: "
+ type.getCommandFormat());
}

Expand All @@ -193,8 +197,12 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC
+ type.getCommandFormat());
}

assert by != null : "Date cannot be null.";

Task target = new Deadline(deadlineDesc, by);

assert target != null : "New Deadline task cannot be null.";

return new AddTaskCommand(target, CommandType.DEADLINE);

} else {
Expand Down Expand Up @@ -231,8 +239,13 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC
+ type.getCommandFormat());
}

assert from != null : "Date cannot be null.";
assert to != null : "Date cannot be null.";

Task target = new Event(eventDesc, from, to);

assert target != null : "New Event task cannot be null.";

return new AddTaskCommand(target, CommandType.EVENT);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/corgi/parsers/TaskParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public Task parse(String s) throws ParsingException {
throw new InvalidParsingFormatException("Wrong format for date!");
}

assert by != null : "LocalDate object cannot be null";

task = new Deadline(status, desc, by);

break;
Expand All @@ -87,12 +89,17 @@ public Task parse(String s) throws ParsingException {
throw new InvalidParsingFormatException("Wrong format for date!");
}

assert from != null : "LocalDate object cannot be null";
assert to != null : "LocalDate object cannot be null";

task = new Event(status, desc, from, to);
break;
default:
throw new InvalidParsingTypeException("Invalid task type!");
}

assert task != null : "Task object cannot be null.";

return task;
}
}
7 changes: 6 additions & 1 deletion src/main/java/corgi/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public void setCorgi(Corgi c) {
*/
@FXML
private void handleUserInput() {
String input = userInput.getText();
String input = userInput.getText().trim();

if (input.equals("")) {
return;
}

String response = corgi.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
Expand Down

0 comments on commit 6b83a4d

Please sign in to comment.