Skip to content

Commit

Permalink
Add javadoc for helper classes.
Browse files Browse the repository at this point in the history
Refactor constants for helper classes.
  • Loading branch information
TheDinos committed Oct 10, 2024
1 parent 42d0bbc commit bfcea71
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 36 deletions.
29 changes: 19 additions & 10 deletions src/main/java/jeff/helper/Parser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jeff.helper;

import jeff.command.*;
import java.io.IOException;

/**
* A utility class responsible for parsing user input commands and creating
Expand All @@ -11,6 +10,16 @@
*/
public class Parser {

private static final String TODO_COMMAND = "todo";
private static final String DEADLINE_COMMAND = "deadline";
private static final String EVENT_COMMAND = "event";
private static final String MARK_COMMAND = "mark";
private static final String UNMARK_COMMAND = "unmark";
private static final String LIST_COMMAND = "list";
private static final String DELETE_COMMAND = "delete";
private static final String FIND_COMMAND = "find";
private static final String BYE_COMMAND = "bye";

/**
* Returns the first word of a given string.
*
Expand Down Expand Up @@ -41,20 +50,20 @@ public static Command parse(String line) {

//Carry out different actions based on the first word
switch (firstWord) {
case "todo":
case "deadline":
case "event":
case TODO_COMMAND:
case DEADLINE_COMMAND:
case EVENT_COMMAND:
return new AddCommand(firstWord, line);
case "list":
case LIST_COMMAND:
return new ListCommand(firstWord, line);
case "mark":
case "unmark":
case MARK_COMMAND:
case UNMARK_COMMAND:
return new MarkCommand(firstWord, line);
case "delete":
case DELETE_COMMAND:
return new DeleteCommand(firstWord, line);
case "find":
case FIND_COMMAND:
return new FindCommand(firstWord, line);
case "bye":
case BYE_COMMAND:
return new ExitCommand();
default:
return new InvalidCommand();
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/jeff/helper/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
/**
* The <code>Storage</code> class is responsible for loading and saving tasks
* to and from a specified text file. It handles the parsing of task data
* and ensures that tasks are correctly formatted before they are added
* to the task list.
* from the text file. It also ensures that tasks are correctly formatted before
* they are added to the task list.
*/
public class Storage {

Expand All @@ -31,27 +31,31 @@ public class Storage {
private static final int FILE_TASK_FIELD1_INDEX = 3;
private static final int FILE_TASK_FIELD2_INDEX = 4;

private static final String TODO_STRING = "T";
private static final String DEADLINE_STRING = "D";
private static final String EVENT_STRING = "E";

/**
* Constructs a <code>Storage</code> object with the specified file path.
*
* @param filePath The path to the file where tasks are stored.
* @param filePath The path to the text file where tasks are stored.
*/
public Storage(String filePath){
this.filePath = filePath;
}

/**
* Processes a Todo task from the given details.
*
* @param taskDetails An array containing the details of the Todo task.
* @return A <code>Todo</code> object representing the task.
* @throws InvalidFormatException If the task details are invalid.
*
* @param taskDetails An array containing the details of the Todo task.
* @return A <code>Todo</code> object representing the task.
* @throws InvalidFormatException If the task details are invalid.
*/
private Task processFileTodo(String[] taskDetails) throws InvalidFormatException {
if (taskDetails.length == TODO_FILE_FIELD_LENGTH) {
return new Todo(taskDetails[FILE_TASK_DESC_INDEX]);
} else {
throw new InvalidFormatException("todo");
throw new InvalidFormatException("Todo description missing");
}
}

Expand All @@ -66,7 +70,7 @@ private Task processFileDeadline(String[] taskDetails) throws InvalidFormatExcep
if (taskDetails.length == DEADLINE_FILE_FIELD_LENGTH) {
return new Deadline(taskDetails[FILE_TASK_DESC_INDEX], taskDetails[FILE_TASK_FIELD1_INDEX]);
} else {
throw new InvalidFormatException("deadline");
throw new InvalidFormatException("Deadline field(s) missing");
}
}

Expand All @@ -82,7 +86,7 @@ private Task processFileEvent(String[] taskDetails) throws InvalidFormatExceptio
return new Event(taskDetails[FILE_TASK_DESC_INDEX], taskDetails[FILE_TASK_FIELD1_INDEX],
taskDetails[FILE_TASK_FIELD2_INDEX]);
} else {
throw new InvalidFormatException("event");
throw new InvalidFormatException("Event field(s) missing");
}
}

Expand All @@ -93,15 +97,15 @@ private Task processFileEvent(String[] taskDetails) throws InvalidFormatExceptio
* @return A <code>Task</code> object representing the task.
* @throws InvalidFormatException If the task type is unknown or invalid.
*/
private Task processFileTaskTypes(String[] taskDetails) throws InvalidFormatException {
private Task processFileTaskType(String[] taskDetails) throws InvalidFormatException {
String taskType = taskDetails[FILE_TASK_TASK_INDEX];

switch (taskType) {
case "T":
case TODO_STRING:
return processFileTodo(taskDetails);
case "D":
case DEADLINE_STRING:
return processFileDeadline(taskDetails);
case "E":
case EVENT_STRING:
return processFileEvent(taskDetails);
default:
throw new InvalidFormatException("Unknown task type");
Expand Down Expand Up @@ -138,11 +142,11 @@ private String[] getFileTaskDetails(String taskLine) throws InvalidFormatExcepti
*/
private void processFileTasks(String taskLine, ArrayList<Task> taskList) throws InvalidFormatException {
try {
//Splits the taskDetails and ensures the fields are not empty
//Get details of the task and ensures the fields are not empty
String[] taskDetails = getFileTaskDetails(taskLine);

//Processes different the different task types and add them to taskList if valid
taskList.add(processFileTaskTypes(taskDetails));
//Processes the task and add them to taskList if valid
taskList.add(processFileTaskType(taskDetails));

if (taskDetails[FILE_TASK_MARK_INDEX].equals("1")) {
taskList.get(taskList.size() - 1).setIsDone(true);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/jeff/helper/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,64 @@
import jeff.task.Task;
import java.util.ArrayList;


/**
* TaskList class is responsible for managing a list of tasks.
* It allows adding, removing, and retrieving tasks from its list.
*/
public class TaskList {

private ArrayList<Task> taskList;

/**
* Default constructor that initializes an empty task list.
*/
public TaskList() {
this.taskList = new ArrayList<>();
}

/**
* Constructor that initializes the task list with an existing list of tasks.
*
* @param tasks an ArrayList of Task objects to initialize the task list with.
*/
public TaskList(ArrayList<Task> tasks){
this.taskList = tasks;
}

/**
* Retrieves the task at the specified index.
*
* @param index the index of the task to retrieve.
* @return the Task object at the given index.
*/
public Task getTask(int index){
return taskList.get(index);
}

/**
* Returns the number of tasks in the task list.
*
* @return the number of tasks in the list.
*/
public int getCount(){
return taskList.size();
}

/**
* Adds a task to the task list.
*
* @param task the Task object to add to the list.
*/
public void addTask(Task task) {
taskList.add(task);
}

/**
* Removes the task at the specified index from the task list.
*
* @param index the index of the task to remove.
*/
public void removeTask(int index) {
taskList.remove(index);
}
Expand Down
44 changes: 35 additions & 9 deletions src/main/java/jeff/helper/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.util.Scanner;

/**
* The Ui class handles interactions with the user by providing messages
* such as welcome, exit, errors, and capturing user input.
*/
public class Ui {
//Constants
public final String DIVIDER = "____________________________________________________________";
public final String INTRO_TEXT = """

public static final String DIVIDER = "____________________________________________________________";
public static final String INTRO_TEXT = """
____________________________________________________________
Hello! I'm JEFF!!!
Expand All @@ -18,45 +22,67 @@ public class Ui {
Type 'list' to display everything you've said!
Type 'mark'/'unmark' to change the status of inputted tasks!
Type 'delete' to delete a task in the list!
Type 'find' to search for a keyword in the task list!
Type 'bye' to exit!
Follow the above formats closely!
If you give me nonsense, I will call you out!
Buuuuttt I will give you some indication of your error!
If there's an error, I will try and give you some indication!
____________________________________________________________
""";

public final String EXIT_TEXT = """
public static final String EXIT_TEXT = """
Bye. Hope to see you again soon!
""";

/**
* Displays the introductory message to welcome the user to the application.
*/
public void showWelcome(){
System.out.print(INTRO_TEXT);
}

/**
* Displays the exit message when the user quits the application.
*/
public void showExit(){
System.out.print(EXIT_TEXT);
}

/**
* Displays a divider to separate sections in the user interface.
*/
public void showDivider(){
System.out.print(DIVIDER);
}

/**
* Displays a new line in the user interface.
*/
public void showNewLine(){
System.out.print(System.lineSeparator());
}

/**
* Displays an error message if there is an issue loading data.
*
* @param errorMessage the specific error message to display.
*/
public void showLoadingError(String errorMessage){
System.out.print(this.DIVIDER + System.lineSeparator() + "An error occurred while loading the data file"
+ System.lineSeparator() + errorMessage);
System.out.print(DIVIDER + System.lineSeparator() + "An error occurred while loading the data file"
+ System.lineSeparator() + errorMessage + System.lineSeparator());
}

/**
* Reads the command inputted by the user and returns it as a string.
*
* @return the user's command as a string.
*/
public String readCommand(){
Scanner in = new Scanner(System.in);
System.out.print("You say:" + System.lineSeparator());
String line = in.nextLine();
//Divider that comes after user input
System.out.print(this.DIVIDER + System.lineSeparator());
System.out.print(DIVIDER + System.lineSeparator());
return line;
}
}

0 comments on commit bfcea71

Please sign in to comment.