Skip to content

Commit

Permalink
Add more JavaDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
QX-CHEN committed Sep 29, 2020
1 parent bb493cc commit 81504cb
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public static void main(String[] args) {
run();
}


/**
* Run the main program and stop when a bye command is given.
*/
private static void run() {
Scanner scanner = new Scanner(System.in);
Ui.greet();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/commands/ByeCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands;

import data.TaskList;
import ui.Ui;

import static common.Message.BYE_MESSAGE;

Expand All @@ -15,6 +14,7 @@ public class ByeCommand extends Command{
* Executes the command by printing a bye message.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
return new CommandResult(BYE_MESSAGE);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public abstract class Command {
/**
* Execute the command.
* @throws InvalidTaskNumberException task number out of range.
* @throws TaskDoneException done command given to a task that is already done.
*/
public abstract CommandResult execute(TaskList tasks) throws InvalidTaskNumberException, TaskDoneException;
}
48 changes: 48 additions & 0 deletions src/main/java/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,92 @@
import data.TaskList;
import tasks.Task;

/**
* CommandResult represents a result after command execution.
* it is pass to Ui class for printing.
*/
public class CommandResult {
private final String message;
private TaskList tasks = null;
private Task task = null;
private int numOfTasks = -1;

/**
* Creates a CommandResult with message, task and number of tasks.
*
* @param message to be displayed.
* @param task that the command operated on, created or deleted.
* @param numOfTasks task count after operation.
*/
public CommandResult(String message, Task task, int numOfTasks) {
this.message = message;
this.task = task;
this.numOfTasks = numOfTasks;
}

/**
* Creates a CommandResult with message and task.
*
* @param message to be displayed.
* @param task that the command operated on, created or deleted.
*/
public CommandResult(String message, Task task) {
this.message = message;
this.task = task;
}

/**
* Creates a CommandResult with message and tasks.
*
* @param message to be displayed.
* @param tasks to be displayed.
*/
public CommandResult(String message, TaskList tasks) {
this.message = message;
this.tasks = tasks;
}

/**
* Creates a CommandResult with message.
*
* @param message to be displayed.
*/
public CommandResult(String message) {
this.message = message;
}

/**
* Get the message from CommandResult.
*
* @return message string.
*/
public String getMessage() {
return message;
}

/**
* Get the tasks from CommandResult.
*
* @return a TaskList of tasks.
*/
public TaskList getTasks() {
return tasks;
}

/**
* Get the task from CommandResult.
*
* @return a task.
*/
public Task getTask() {
return task;
}

/**
* Get the task count from CommandResult.
*
* @return number of tasks.
*/
public int getNumOfTasks() {
return numOfTasks;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/commands/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DeadlineCommand extends Command {
private final String time;

/**
* Creates a AddDeadline Command with trimmed input.
* Creates a DeadlineCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException if the trimmed input format does not match with the pattern.
Expand All @@ -44,6 +44,7 @@ public DeadlineCommand(String trimmedInput) throws InvalidCommandException {
* Executes the command by creating a Deadline task and add it to TaskList.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
Deadline deadline = new Deadline(description, date, time);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DeleteCommand extends Command{
* Creates a DeleteCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException input format does not match with the command word.
*/
public DeleteCommand(String trimmedInput) throws InvalidCommandException {
Matcher matcher = COMMAND_PATTERN.matcher(trimmedInput);
Expand All @@ -38,6 +39,8 @@ public DeleteCommand(String trimmedInput) throws InvalidCommandException {
* Executes the command by deleting task of given index.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
* @throws InvalidTaskNumberException task number out of range.
*/
public CommandResult execute(TaskList tasks) throws InvalidTaskNumberException {
Task task = tasks.deleteTask(taskNum);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/commands/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static common.Message.*;
import static common.Message.MARK_AS_DONE_MESSAGE;

/**
* Representation of command that marks a task in TaskList as done.
Expand All @@ -24,6 +24,7 @@ public class DoneCommand extends Command{
* Creates a DoneCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException input format does not match with the command word.
*/
public DoneCommand(String trimmedInput) throws InvalidCommandException {
Matcher matcher = COMMAND_PATTERN.matcher(trimmedInput);
Expand All @@ -38,6 +39,8 @@ public DoneCommand(String trimmedInput) throws InvalidCommandException {
* Executes the command by marking task of given index as done.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
* @throws InvalidTaskNumberException task number out of range.
*/
public CommandResult execute(TaskList tasks) throws InvalidTaskNumberException, TaskDoneException {
Task task = tasks.markAsDone(taskNum);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EventCommand extends Command {
private final String end;

/**
* Creates a AddEvent Command with trimmed input.
* Creates a EventCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException if the trimmed input format does not match with the pattern.
Expand All @@ -46,6 +46,7 @@ public EventCommand(String trimmedInput) throws InvalidCommandException {
* Executes the command by creating a Event task and add it to TaskList.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
Event event = new Event(description, date, start, end);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@

import static common.Message.FIND_MESSAGE;

/**
* Representation of command that finds relevant task(s) from TaskList.
*/
public class FindCommand extends Command {
public static final String COMMAND_WORD = "find";
private static final Pattern COMMAND_PATTERN = Pattern.compile(COMMAND_WORD + "(?<keyword>.*\\s.*)");

private final String keyword;

/**
* Creates a FindCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException if the trimmed input format does not match with the pattern.
*/
public FindCommand(String trimmedInput) throws InvalidCommandException {
Matcher matcher = COMMAND_PATTERN.matcher(trimmedInput);
if (matcher.find()) {
Expand All @@ -23,6 +32,12 @@ public FindCommand(String trimmedInput) throws InvalidCommandException {
}
}

/**
* Executes the command by searching all tasks in TaskList with given keyword.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
TaskList filteredTasks = new TaskList();
for (int i = 0; i < tasks.size(); i++) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ListCommand extends Command{
* Executes the command by listing all the tasks.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
return new CommandResult(LIST_MESSAGE, tasks);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/TodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public class TodoCommand extends Command {
private final String description;

/**
* Creates a AddTodo Command with trimmed input.
* Creates a TodoCommand with trimmed input.
*
* @param trimmedInput raw input without leading and trailing white space.
* @throws InvalidCommandException input format does not match with the command word.
*/
public TodoCommand(String trimmedInput) throws InvalidCommandException {
Matcher matcher = COMMAND_PATTERN.matcher(trimmedInput);
Expand All @@ -37,6 +38,7 @@ public TodoCommand(String trimmedInput) throws InvalidCommandException {
* Executes the command by creating a Todo task and add it to TaskList.
*
* @param tasks runtime storage of tasks.
* @return CommandResult that pass printing info to Ui class.
*/
public CommandResult execute(TaskList tasks) {
Todo todo = new Todo(description);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/common/Message.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package common;

/**
* Message class contains messages to be printed by Ui class.
*/
public class Message {
public static final String HORIZONTAL_LINE = "\t______________________________" +
"______________________________";
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
* Utils class contains utility functions used by the program.
*/
public class Utils {


/**
* Convert a date from String to LocalDate.
*
* @param dateString String representation of a date.
* @return LocalDate that converts from dateString.
*/
public static LocalDate dateStringToDate(String dateString) {
if (dateString == null) {
return null;
Expand All @@ -22,6 +32,12 @@ public static LocalDate dateStringToDate(String dateString) {
}
}

/**
* Convert a time from String to LocalDate.
*
* @param timeString String representation of a time.
* @return LocalTime that converts from timeString.
*/
public static LocalTime timeStringToTime(String timeString) {
if (timeString == null) {
return null;
Expand All @@ -32,6 +48,12 @@ public static LocalTime timeStringToTime(String timeString) {
return LocalTime.of(hour, minute);
}

/**
* Convert a date from LocalDate to String.
*
* @param date LocalDate object.
* @return Formatted String that converts from LocalDate.
*/
public static String dateToString(LocalDate date) {
if (date == null) {
return "";
Expand All @@ -40,6 +62,12 @@ public static String dateToString(LocalDate date) {
}
}

/**
* Convert a time from LocalTime to String.
*
* @param time LocalTime object.
* @return Formatted String that converts from LocalTime.
*/
public static String timeToString(LocalTime time) {
if (time == null) {
return "";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/data/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public Task get(int index) {

/**
* Remove task from TaskList using index.
*
* @return task that is removed from the TaskList.
*/
public Task deleteTask(int taskNum) throws InvalidTaskNumberException {
if (taskNum <= 0 || taskNum > tasks.size()) {
Expand All @@ -51,6 +53,8 @@ public Task deleteTask(int taskNum) throws InvalidTaskNumberException {

/**
* Mark task as done using index.
*
* @return task that is marked as done.
*/
public Task markAsDone(int taskNum) throws InvalidTaskNumberException, TaskDoneException {
if (taskNum <= 0 || taskNum > tasks.size()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/exceptions/InvalidCommandException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package exceptions;

/**
* InvalidCommandException is thrown when input format
* does not match with available commands.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/exceptions/InvalidTaskNumberException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package exceptions;

/**
* InvalidTaskNumberException is thrown when task number
* is out of range.
*/
public class InvalidTaskNumberException extends Exception {
}
4 changes: 4 additions & 0 deletions src/main/java/exceptions/TaskDoneException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package exceptions;

/**
* TaskDoneException is thrown when done command is
* given to a task that is already done.
*/
public class TaskDoneException extends Exception {
}
8 changes: 8 additions & 0 deletions src/main/java/exceptions/UnknownDateFormatException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package exceptions;

/**
* UnknownDateFormatException is thrown when input date format
* is not recognised.
*/
public class UnknownDateFormatException extends Exception {
}
Loading

0 comments on commit 81504cb

Please sign in to comment.