Skip to content

Commit

Permalink
Merge branch 'hotfix-v0.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasting98 committed Sep 16, 2021
2 parents a0097d8 + 9d8c2d3 commit bc35ddf
Show file tree
Hide file tree
Showing 20 changed files with 122 additions and 72 deletions.
6 changes: 4 additions & 2 deletions src/main/java/chad/ChadChatBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
public class ChadChatBot extends Application {

private static final String APP_WINDOW_TITLE = "ChadBot";
private static final String FXML_PATH = "/view/Ui.fxml";
private static final String APP_ICON_PATH = "/images/yeschad_toleft.png";

private Ui ui;
private CommandParser commandParser;
Expand Down Expand Up @@ -55,7 +57,7 @@ public void readInput(String input) {
@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(ChadChatBot.class.getResource("/view/Ui.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(ChadChatBot.class.getResource(FXML_PATH));
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
initialiseStage(stage, scene);
Expand Down Expand Up @@ -89,7 +91,7 @@ private void initialiseComponents(FXMLLoader fxmlLoader) throws IOException {

private void initialiseStage(Stage stage, Scene scene) {
stage.setTitle(APP_WINDOW_TITLE);
Image appIcon = new Image("/images/yeschad_toleft.png");
Image appIcon = new Image(APP_ICON_PATH);
stage.getIcons().add(appIcon);
stage.setScene(scene);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/chad/command/AddTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
public abstract class AddTaskCommand extends Command implements UndoableCommand {

private static final String ADD_TASK_SUCCESSFUL_MESSAGE = "Got it. I've added this task:";
private static final String MISSING_TASK_DESCRIPTION_ERROR_TEMPLATE =
"A description is required for \"%s\" commands.";

private Task task;
private String taskDescription;
private int taskIndex;
Expand All @@ -37,7 +41,7 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
taskHandler.addTask(task);
int numberOfTasks = taskHandler.getNumberOfTasks();
ui.startMessage()
.addLine("Got it. I've added this task:")
.addLine(ADD_TASK_SUCCESSFUL_MESSAGE)
.addTask(task)
.addTasksListLength(numberOfTasks)
.displayMessage();
Expand Down Expand Up @@ -75,9 +79,9 @@ private String parseTaskDescription(String[] tokens) {
return getTokenSequence(tokens, 1, tokens.length);
}

private void checkTaskDescriptionLength(String taskDescription) throws ChadInvalidCommandException {
void checkTaskDescriptionLength(String taskDescription) throws ChadInvalidCommandException {
if (taskDescription.length() == 0) {
throw new ChadInvalidCommandException(String.format("A description is required for \"%s\" commands.",
throw new ChadInvalidCommandException(String.format(MISSING_TASK_DESCRIPTION_ERROR_TEMPLATE,
getCommandType().getCommandDescription()));
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/chad/command/AddTemporalTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public abstract class AddTemporalTaskCommand extends AddTaskCommand {

private static final String DATE_TIME_FORMAT_PATTERN = "yyyy/M/d HHmm";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_PATTERN);
private static final String TIME_RELATION_TEMPLATE = "/%s";
private static final String MISSING_DATE_TIME_ERROR_TEMPLATE = "A date and time is required for \"%s\" commands.";
private static final String INVALID_DATE_TIME_FORMAT_ERROR_TEMPLATE =
"The date and time should be in the \"%s\" format.";

private LocalDateTime time;

Expand Down Expand Up @@ -45,7 +49,7 @@ LocalDateTime getTaskTime() {
}

private int findTimeRelationIndex(String[] tokens) {
String timeRelationToken = String.format("/%s", getTimeRelation());
String timeRelationToken = String.format(TIME_RELATION_TEMPLATE, getTimeRelation());
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals(timeRelationToken)) {
Expand All @@ -55,16 +59,9 @@ private int findTimeRelationIndex(String[] tokens) {
return tokens.length;
}

private void checkTaskDescriptionLength(String taskDescription) throws ChadInvalidCommandException {
if (taskDescription.length() == 0) {
throw new ChadInvalidCommandException(String.format("A description is required for \"%s\" commands.",
getCommandType().getCommandDescription()));
}
}

private void checkTimeStringLength(String timeStr) throws ChadInvalidCommandException {
if (timeStr.length() == 0) {
throw new ChadInvalidCommandException(String.format("A date and time is required for \"%s\" commands.",
throw new ChadInvalidCommandException(String.format(MISSING_DATE_TIME_ERROR_TEMPLATE,
getCommandType().getCommandDescription()));
}
}
Expand All @@ -74,7 +71,7 @@ private void parseTime(String timeStr) throws ChadInvalidCommandException {
time = LocalDateTime.parse(timeStr, DATE_TIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new ChadInvalidCommandException(String
.format("The date and time should be in the \"%s\" format.", DATE_TIME_FORMAT_PATTERN));
.format(INVALID_DATE_TIME_FORMAT_ERROR_TEMPLATE, DATE_TIME_FORMAT_PATTERN));
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/chad/command/ApplyOnTaskNumberCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
public abstract class ApplyOnTaskNumberCommand extends Command implements UndoableCommand {

private static final String MISSING_TASK_NUMBER_ERROR_TEMPLATE = "A task number is required for \"%s\" commands.";
private static final String INVALID_TASK_NUMBER_ERROR_MESSAGE = "The task number is not a number.";

private int taskIndex;

/**
Expand All @@ -27,7 +30,7 @@ void parseCommand(String[] tokens) throws ChadInvalidCommandException {

private void checkTaskNumberStringLength(String taskNumberString) throws ChadInvalidCommandException {
if (taskNumberString.length() == 0) {
throw new ChadInvalidCommandException(String.format("A task number is required for \"%s\" commands.",
throw new ChadInvalidCommandException(String.format(MISSING_TASK_NUMBER_ERROR_TEMPLATE,
getCommandType().getCommandDescription()));
}
}
Expand All @@ -51,7 +54,7 @@ private void parseTaskIndex(String taskNumberString) throws ChadInvalidCommandEx
try {
taskIndex = Integer.parseInt(taskNumberString) - 1;
} catch (NumberFormatException e) {
throw new ChadInvalidCommandException("The task number is not a number.");
throw new ChadInvalidCommandException(INVALID_TASK_NUMBER_ERROR_MESSAGE);
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/chad/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public abstract class Command {

private static final boolean MUST_EXIT = false;
private static final String TOKEN_DELIMITER = " ";

private String command;

Expand All @@ -21,8 +22,8 @@ public abstract class Command {
*/
public Command(String command) throws ChadInvalidCommandException {
this.command = command;
String[] tokens = command.strip().split(" ");
assert tokens.length != 0 && tokens[0].length() != 0 : "The String command cannot be empty";
String[] tokens = command.strip().split(TOKEN_DELIMITER);
assert tokens.length != 0 && tokens[0].length() != 0 : "The String command cannot be empty.";
String commandName = tokens[0];
CommandType commandType = getCommandType();
assert commandName.equals(commandType.getCommandName()) : String.format("This command is not \"%s\".",
Expand Down Expand Up @@ -62,7 +63,7 @@ String getTokenSequence(String[] tokens, int inclusiveStart, int exclusiveEnd) {
StringBuilder tokenSequenceSb = new StringBuilder();
for (int i = inclusiveStart; i < exclusiveEnd; i++) {
String token = tokens[i];
tokenSequenceSb.append(token).append(" ");
tokenSequenceSb.append(token).append(TOKEN_DELIMITER);
}
return tokenSequenceSb.toString().strip();
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/chad/command/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class CommandParser {
private static final String DELETE_TASK_COMMAND = "delete";
private static final String FIND_TASKS_COMMAND = "find";
private static final String UNDO_COMMAND = "undo";
private static final String EMPTY_COMMAND_ERROR_MESSAGE = "This command is empty.";
private static final String NO_EXISTING_COMMAND_ERROR_MESSAGE = "This command does not exist.";

/**
* Parses the command to get the Command instance.
Expand All @@ -27,7 +29,7 @@ public class CommandParser {
public Command getCommandInstance(String command) throws ChadInvalidCommandException {
String[] tokens = command.strip().split(" ");
if (tokens.length == 0 || tokens[0].length() == 0) {
throw new ChadInvalidCommandException("This command is empty.");
throw new ChadInvalidCommandException(EMPTY_COMMAND_ERROR_MESSAGE);
}
String commandName = tokens[0];
switch (commandName) {
Expand All @@ -50,7 +52,7 @@ public Command getCommandInstance(String command) throws ChadInvalidCommandExcep
case UNDO_COMMAND:
return new UndoCommand(command);
default:
throw new ChadInvalidCommandException("This command does not exist.");
throw new ChadInvalidCommandException(NO_EXISTING_COMMAND_ERROR_MESSAGE);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/chad/command/DeleteTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public class DeleteTaskCommand extends ApplyOnTaskNumberCommand {

private static final CommandType COMMAND_TYPE = CommandType.DELETE_TASK;
private static final String NO_EXISTING_TASK_NUMBER_ERROR_MESSAGE = "The task number does not exist.";
private static final String DELETE_TASK_SUCCESSFUL_MESSAGE = "Noted. I've removed this task:";

private Task deletedTask;

Expand All @@ -29,10 +31,10 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
try {
deletedTask = taskHandler.deleteTask(getTaskIndex());
} catch (IndexOutOfBoundsException e) {
throw new ChadInvalidCommandException("The task number does not exist.");
throw new ChadInvalidCommandException(NO_EXISTING_TASK_NUMBER_ERROR_MESSAGE);
}
ui.startMessage()
.addLine("Noted. I've removed this task:")
.addLine(DELETE_TASK_SUCCESSFUL_MESSAGE)
.addTask(deletedTask)
.addTasksListLength(taskHandler.getNumberOfTasks())
.displayMessage();
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/chad/command/FindTasksCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
public class FindTasksCommand extends Command {

private static final CommandType COMMAND_TYPE = CommandType.FIND_TASKS;
private static final String MISSING_QUERY_ERROR_TEMPLATE = "A query is required for \"%s\" commands.";
private static final String NO_MATCH_MESSAGE = "No matching tasks were found.";
private static final String ONE_MATCH_MESSAGE = "Here is the 1 matching task in your list:";
private static final String MULTIPLE_MATCHES_TEMPLATE = "Here are the %d matching tasks in your list:";

private String queryTaskDescription;

Expand All @@ -35,18 +39,18 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
switch (n) {
case 0:
ui.startMessage()
.addLine("No matching tasks were found.")
.addLine(NO_MATCH_MESSAGE)
.displayMessage();
break;
case 1:
ui.startMessage()
.addLine("Here is the 1 matching task in your list:")
.addLine(ONE_MATCH_MESSAGE)
.addFindTasksResultsList(queryResults)
.displayMessage();
break;
default:
ui.startMessage()
.addLine(String.format("Here are the %d matching tasks in your list:", n))
.addLine(String.format(MULTIPLE_MATCHES_TEMPLATE, n))
.addFindTasksResultsList(queryResults)
.displayMessage();
break;
Expand All @@ -62,7 +66,7 @@ void parseCommand(String[] tokens) throws ChadInvalidCommandException {

private void checkQueryTaskDescriptionLength(String queryTaskDescription) throws ChadInvalidCommandException {
if (queryTaskDescription.length() == 0) {
throw new ChadInvalidCommandException(String.format("A query is required for \"%s\" commands.",
throw new ChadInvalidCommandException(String.format(MISSING_QUERY_ERROR_TEMPLATE,
getCommandType().getCommandDescription()));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/chad/command/ListTasksCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
public class ListTasksCommand extends Command {

private static final CommandType COMMAND_TYPE = CommandType.LIST_TASKS;
private static final String NO_TASKS_MESSAGE = "There are no tasks in your list.";
private static final String SOME_TASKS_MESSAGE = "Here are the tasks in your list:";
private static final String UNNECESSARY_ARGUMENTS_ERROR_MESSAGE = "There were unnecessary arguments.";

/**
* Creates an ListTasksCommand instance.
Expand All @@ -25,11 +28,11 @@ public ListTasksCommand(String command) throws ChadInvalidCommandException {
public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandException {
if (taskHandler.getNumberOfTasks() == 0) {
ui.startMessage()
.addLine("You have no tasks in the list.")
.addLine(NO_TASKS_MESSAGE)
.displayMessage();
} else {
ui.startMessage()
.addLine("Here are the tasks in your list:")
.addLine(SOME_TASKS_MESSAGE)
.addTasksList(taskHandler.getTasks())
.displayMessage();
}
Expand All @@ -38,7 +41,7 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
@Override
void parseCommand(String[] tokens) throws ChadInvalidCommandException {
if (tokens.length > 1) {
throw new ChadInvalidCommandException("There were unnecessary arguments.");
throw new ChadInvalidCommandException(UNNECESSARY_ARGUMENTS_ERROR_MESSAGE);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/chad/command/MarkTaskDoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public class MarkTaskDoneCommand extends ApplyOnTaskNumberCommand {

private static final CommandType COMMAND_TYPE = CommandType.MARK_TASK_DONE;
private static final String NO_EXISTING_TASK_NUMBER_ERROR_MESSAGE = "The task number does not exist.";
private static final String MARK_TASK_DONE_SUCCESSFUL_MESSAGE = "Nice! I've marked this task as done:";

/**
* Creates an MarkTaskDoneCommand instance.
Expand All @@ -28,10 +30,10 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
try {
task = taskHandler.markTaskDone(getTaskIndex());
} catch (IndexOutOfBoundsException e) {
throw new ChadInvalidCommandException("The task number does not exist.");
throw new ChadInvalidCommandException(NO_EXISTING_TASK_NUMBER_ERROR_MESSAGE);
}
ui.startMessage()
.addLine("Nice! I've marked this task as done:")
.addLine(MARK_TASK_DONE_SUCCESSFUL_MESSAGE)
.addTask(task)
.displayMessage();
CommandHandler commandHandler = CommandHandler.getInstance();
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/chad/command/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
public class UndoCommand extends Command {

private static final CommandType COMMAND_TYPE = CommandType.UNDO;
private static final String NO_UNDOABLE_COMMANDS_LEFT_MESSAGE = "There are no commands (left over) to undo.";
private static final String UNDO_SUCCESSFUL_MESSAGE = "Okay, I have undone the following command:";
private static final String UNNECESSARY_ARGUMENTS_ERROR_MESSAGE = "There were unnecessary arguments.";

/**
* Creates an UndoCommand instance.
Expand All @@ -27,11 +30,11 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
Command command = commandHandler.undo(taskHandler);
if (command == null) {
ui.startMessage()
.addLine("There are no commands (left over) to undo.")
.addLine(NO_UNDOABLE_COMMANDS_LEFT_MESSAGE)
.displayMessage();
} else {
ui.startMessage()
.addLine("Okay, I have undone the following command:")
.addLine(UNDO_SUCCESSFUL_MESSAGE)
.addCommand(command)
.displayMessage();
}
Expand All @@ -40,7 +43,7 @@ public void execute(TaskHandler taskHandler, Ui ui) throws ChadInvalidCommandExc
@Override
void parseCommand(String[] tokens) throws ChadInvalidCommandException {
if (tokens.length > 1) {
throw new ChadInvalidCommandException("There were unnecessary arguments.");
throw new ChadInvalidCommandException(UNNECESSARY_ARGUMENTS_ERROR_MESSAGE);
}
}

Expand Down

0 comments on commit bc35ddf

Please sign in to comment.