Skip to content

Commit

Permalink
Update files for error handling
Browse files Browse the repository at this point in the history
Bugs in using deadline and event command were fixed.
Error handling updated in several files.
mainClass in build.gradle was updated to fix JavaFX error.
  • Loading branch information
gongg21 committed Sep 23, 2023
1 parent 1313eda commit 200c3dd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test {
}

application {
mainClass.set("gbot.Main")
mainClass.set("gbot.Launcher")
}

shadowJar {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gbot/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gbot;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
Expand All @@ -19,6 +20,8 @@ public class MainWindow extends AnchorPane {
private VBox dialogContainer;
@FXML
private TextField userInput;
@FXML
private Button sendButton;

private GBot gbot;

Expand All @@ -37,6 +40,17 @@ public void setGBot(GBot g) {
gbot = g;
}

/**
* Creates a dialog box, containing message from Zean and appending it to the dialog container.
*
* @param msg The message to be displayed.
*/
public void showMessage(String msg) {
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(msg, gbotImage)
);
}

/**
* Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to
* the dialog container. Clears the user input after processing.
Expand Down
56 changes: 40 additions & 16 deletions src/main/java/gbot/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import exceptions.*;

import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import tasks.Deadline;
import tasks.Event;
Expand All @@ -14,8 +15,12 @@
* @author Gallen Ong
*/
public class Parser {
private static final String INVALIDTASK = "Although you might not be wrong, I simply do not understand...\n" +
private static final String INVALID_TASK = "Although you might not be wrong, I simply do not understand...\n" +
"Kindly enter a valid task number.";
private static final String INVALID_INDEX = "I apologise, that task does not exist.";
private static final String INVALID_FORMAT = "I must apologise for correcting you.\n" +
"Kindly enter a valid date in the YYYY-MM-DD format.";

/**
* Returns corresponding methods after parsing user inputs.
*
Expand Down Expand Up @@ -82,12 +87,14 @@ private static String checkPrefix(String message, TaskList tasks) throws Runtime
private static String parseMark(String message, TaskList tasks) {
String[] words = message.split(" ");
if (words.length != 2) {
return INVALIDTASK;
return INVALID_TASK;
}
try {
return tasks.markTask(Integer.parseInt(words[1]));
} catch (NumberFormatException e) {
throw new GBotException(INVALIDTASK);
throw new GBotException(INVALID_TASK);
} catch (TaskException e) {
throw new GBotException(INVALID_INDEX);
}
}

Expand All @@ -100,12 +107,14 @@ private static String parseMark(String message, TaskList tasks) {
private static String parseUnmark(String message, TaskList tasks) {
String[] words = message.split(" ");
if (words.length != 2) {
return INVALIDTASK;
return INVALID_TASK;
}
try {
return tasks.unmarkTask(Integer.parseInt(words[1]));
} catch (NumberFormatException e) {
throw new GBotException(INVALIDTASK);
throw new GBotException(INVALID_TASK);
} catch (TaskException e) {
throw new GBotException(INVALID_INDEX);
}
}

Expand All @@ -120,7 +129,7 @@ private static String parseTodo(String message, TaskList tasks) {
if (desc.isBlank()) {
return "I apologise. Kindly input a task description.";
}
return tasks.addTodo(desc);
return tasks.addTodo(desc.strip());
}

/**
Expand All @@ -130,13 +139,19 @@ private static String parseTodo(String message, TaskList tasks) {
* @param tasks The TaskList object that stores tasks.
*/
private static String parseDeadline(String message, TaskList tasks) {
String[] taskDesc = message.split(" /by ");
String details = message.substring(8);
String[] taskDesc = details.split(" /by ");
if (taskDesc.length != 2) {
return "I apologise for correcting you. Kindly follow the following:\n" +
"deadline (task) /by (deadline in YYYY-MM-DD)\n" +
"eg. deadline return book /by 2023-09-21";
}
return tasks.addDeadline(taskDesc[0].substring(8), taskDesc[1]);

try {
return tasks.addDeadline(taskDesc[0].strip(), taskDesc[1].strip());
} catch (DateTimeParseException e) {
throw new GBotException(INVALID_FORMAT);
}
}

/**
Expand All @@ -146,29 +161,36 @@ private static String parseDeadline(String message, TaskList tasks) {
* @param tasks The TaskList object that stores tasks.
*/
private static String parseEvent(String message, TaskList tasks) {
String[] inputSplitByFrom = message.split(" /from ");
String[] inputSplitByTo = message.split(" /to ");
String details = message.substring(5);
String[] inputSplitByFrom = details.split(" /from");
String[] inputSplitByTo = details.split(" /to");
if (inputSplitByFrom.length != 2 || inputSplitByTo.length != 2) {
return "I apologise for correcting you. Kindly follow the following:\n" +
"event (task) /from (start in YYYY-MM-DD) /to (end in YYYY-MM-DD)\n" +
"eg. event attend meeting /from 2023-09-21 /to 2023-09-22";
}

String[] inputAfterFrom = inputSplitByFrom[1].split(" /to ");
String[] inputAfterFrom = inputSplitByFrom[1].split(" /to");
String desc;
String fromDate;
String toDate;

if (inputAfterFrom.length == 2) {
System.out.println(inputAfterFrom.length);
desc = inputSplitByFrom[0];
fromDate = inputAfterFrom[0];
toDate = inputAfterFrom[1];
} else {
desc = inputSplitByTo[0];
fromDate = inputSplitByTo[1].split(" /from ")[1];
toDate = inputSplitByTo[1].split(" /from ")[0];
fromDate = inputSplitByTo[1].split(" /from")[1];
toDate = inputSplitByTo[1].split(" /from")[0];
}

try {
return tasks.addEvent(desc.strip(), fromDate.strip(), toDate.strip());
} catch (DateTimeParseException e) {
throw new GBotException(INVALID_FORMAT);
}
return tasks.addEvent(desc, fromDate, toDate);
}

/**
Expand All @@ -180,12 +202,14 @@ private static String parseEvent(String message, TaskList tasks) {
private static String parseDelete(String message, TaskList tasks) {
String[] words = message.split(" ");
if (words.length != 2) {
return INVALIDTASK;
return INVALID_TASK;
}
try {
return tasks.deleteTask(Integer.parseInt(words[1]));
} catch (NumberFormatException e) {
throw new GBotException(INVALIDTASK);
throw new GBotException(INVALID_TASK);
} catch (TaskException e) {
throw new GBotException(INVALID_INDEX);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gbot/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gbot;

import exceptions.TaskException;

import java.time.LocalDate;
import java.util.ArrayList;

import tasks.Deadline;
Expand Down Expand Up @@ -131,7 +133,7 @@ public String addDeadline(String desc, String deadline) {
return "It appears you may be missing some details, do kindly enter.";
}

Deadline newDeadline = new Deadline(desc, deadline);
Deadline newDeadline = new Deadline(desc, deadline.strip());
list.add(newDeadline);
storage.writeToFile(newDeadline.toStringForFile());
taskCount++;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class Deadline extends Task {
* Initialises a Deadline task with a description and deadline.
*
* @param description The task description.
* @param by The task deadline.
* @param deadline The task deadline.
*/
public Deadline(String description, String by) {
public Deadline(String description, String deadline) {
super(description.strip());
this.deadline = LocalDate.parse(by.strip());
this.deadline = LocalDate.parse(deadline);
}

/**
Expand Down
36 changes: 33 additions & 3 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,63 @@ public void parseUnmark_invalidTask_errorMessageShown() {
}
}
@Test
public void parseTodo_emptyField_todoExceptionThrown() {
public void parseTodo_emptyField_GBotExceptionThrown() {
assertEquals("I apologise. Kindly input a task description.",
Parser.parse("todo ", new TaskList()));
}
@Test
public void parseDeadline_emptyField_deadlineExceptionThrown() {
public void parseDeadline_emptyField_GBotExceptionThrown() {
assertEquals("I apologise for correcting you. Kindly follow the following:\n" +
"deadline (task) /by (deadline in YYYY-MM-DD)\n" +
"eg. deadline return book /by 2023-09-21",
Parser.parse("deadline ", new TaskList()));
}
@Test
public void parseDeadline_invalidFormat_GBotExceptionThrown() {
try {
assertEquals("", Parser.parse("deadline return /by tomorrow", new TaskList()));
fail();
} catch (GBotException e) {
assertEquals("I must apologise for correcting you.\n" +
"Kindly enter a valid date in the YYYY-MM-DD format.", e.getMessage());
}
}
@Test
public void parseDeadline_missingDetails_errorMessageShown() {
assertEquals("It appears you may be missing some details, do kindly enter.",
Parser.parse("deadline /by tomorrow", new TaskList()));
}
@Test
public void parseEvent_emptyField_eventExceptionThrown() {
assertEquals("I apologise for correcting you. Kindly follow the following:\n" +
"event (task) /from (start in YYYY-MM-DD) /to (end in YYYY-MM-DD)\n" +
"eg. event attend meeting /from 2023-09-21 /to 2023-09-22",
Parser.parse("event ", new TaskList()));
}
@Test
public void parseEvent_invalidFormat_GBotExceptionThrown() {
try {
assertEquals("", Parser.parse("event meeting /from now /to later", new TaskList()));
fail();
} catch (GBotException e) {
assertEquals("I must apologise for correcting you.\n" +
"Kindly enter a valid date in the YYYY-MM-DD format.", e.getMessage());
}
}
@Test
public void parseEvent_missingDetails_errorMessageShown() {
assertEquals("It appears you may be missing some details, do kindly enter.",
Parser.parse("event /from 2023-09-21 /to ", new TaskList()));
}
@Test
public void parseDelete_emptyField_errorMessageShown() {
assertEquals("Although you might not be wrong, I simply do not understand...\n" +
"Kindly enter a valid task number.", Parser.parse("delete ", new TaskList()));
}
@Test
public void parseDelete_invalidTask_errorMessageShown() {
try {
assertEquals("", Parser.parse("delete invalid ", new TaskList()));
assertEquals("", Parser.parse("delete invalid", new TaskList()));
fail();
} catch (GBotException e) {
assertEquals("Although you might not be wrong, I simply do not understand...\n" +
Expand Down

0 comments on commit 200c3dd

Please sign in to comment.