Skip to content

Commit

Permalink
Added javadocs to most methods/classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jellywaiyan committed Sep 6, 2023
1 parent f296862 commit f424f95
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/main/java/Jelly/main/Jelly.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import Jelly.commands.Command;
import Jelly.exceptions.JellyException;


/**
* The main class which is responsible in running the Jelly chat bot.
*/
public class Jelly {
private static final String FILE_PATH = "./taskData/jelly.txt";
private final Scanner scanner = new Scanner(System.in);
Expand All @@ -13,6 +15,12 @@ public class Jelly {
private Ui ui;
private Storage storage;

/**
* Constructor for an instance of Jelly.
*
* @param filePath The file path used when saving or starting up the bot. Contains a list of tasks(if any).
* @throws JellyException If there are any errors while starting up Jelly.
*/
private Jelly(String filePath) throws JellyException {
this.storage = new Storage(filePath);
this.ui = new Ui();
Expand All @@ -24,11 +32,21 @@ private Jelly(String filePath) throws JellyException {
System.out.println(e.getMessage());
}
}

/**
* The main initialiser for the Jelly Chat bot.
*
* @param args
* @throws JellyException
*/
public static void main(String[] args) throws JellyException {
Jelly jelly = new Jelly(FILE_PATH);
jelly.run();
}

/**
* Runs the commands given to the Jelly Chat bot.
*/
private void run() {
ui.startUpMessage();
boolean running = true;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/Jelly/main/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
import Jelly.exceptions.JellyException;
import Jelly.exceptions.JellyUnknownCommandException;

/**
* Responsible for parsing commands from the user, creates a Command object.
*/
public class Parser {

/**
* Parses the command inputted.
*
* @param command The user's input etc. list, bye.
* @return An instance of Command that matches the user's input.
* @throws JellyException If the input is invalid.
*/
public static Command parse(String command) throws JellyException {
try {
String[] stringArray = command.split(" ");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/Jelly/main/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* Stores and loads tasked based on the specified file path.
*/
public class Storage {
private static final String FILE_PATH = "./taskData/jelly.txt";

private String filePath;

/**
* Constructor for Storage, based on the specified file path.
*
* @param filePath The file path used when saving or starting up the bot. Contains a list of tasks(if any).
*/
public Storage(String filePath) {
this.filePath = filePath;
}

/**
* Starts up the Jelly Bot by loading the tasks from the specified file path.
*
* @return An ArrayList with all the tasks from the file.
* @throws JellyException If there is an error while loading up the file.
*/
public ArrayList<Task> startUp() throws JellyException {
ArrayList<Task> storage = new ArrayList<>();
try {
Expand Down Expand Up @@ -61,6 +75,11 @@ public ArrayList<Task> startUp() throws JellyException {
}
}

/**
* Saves all task data into the file.
*
* @param taskList
*/
public void saveAndExit(TaskList taskList) {
try {
FileWriter fileWriter = new FileWriter(filePath);
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/Jelly/main/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,88 @@

import java.util.ArrayList;

/**
* Contains all the commands regarding the list of tasks.
*/
public class TaskList {

private ArrayList<Task> taskList;

/**
* Constructor for a new empty tasklist.
*/
public TaskList() {
this.taskList = new ArrayList<>();
}

/**
* Constructor for a tasklist with existing tasks.
*
* @param taskList
*/
public TaskList(ArrayList<Task> taskList) {
this.taskList = taskList;
}

/**
* Gets the total number of tasks in the list.
*
* @return The number of tasks in the list.
*/
public int size() {
return taskList.size();
}

/**
* Adds a specified task into the list.
*
* @param task The task to be added into the list.
*/
public void add(Task task) {
taskList.add(task);
}

/**
* Removes the task at the specified index in the list.
*
* @param index The index of the task to be removed.
*/
public void delete(int index) {
taskList.remove(index);
}

/**
* Retrieves the task at the specified index in the list.
*
* @param index The index of the task to be retrieved.
* @return The task that was retrieved.
*/
public Task get(int index) {
return taskList.get(index);
}

/**
* Getter for the list of tasks.
* @return The list of tasks.
*/
public ArrayList<Task> getTasks() {
return taskList;
}

/**
* Marks a task at the specified index of the list, as done.
*
* @param index The index of the task to be marked as done.
*/
public void markAsDone(int index) {
taskList.get(index).markAsDone();
}

/**
* Marks a task at the specified index of the list, as not done.
*
* @param index The index of the task to be marked as not done.
*/
public void markAsUndone(int index) {
taskList.get(index).markAsUndone();
}
Expand Down
45 changes: 42 additions & 3 deletions src/main/java/Jelly/main/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,81 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* Responsible for the interface of the Jelly Chat Bot.
*/
public class Ui {
private Scanner sc;

/**
* Constructor for Ui, takes in user input from the keyboard.
*/
public Ui() {
sc = new Scanner(System.in);
}

/**
* Prints out a welcome message when the Chat Bot is booted up.
*/
public void startUpMessage() {
System.out.println("Hello! I'm Jelly");
System.out.println("What can I do for you?");
}

/**
* Reads the command inputted by the user.
*
* @return A string of the command inputted.
*/
public String commandMe() {
return sc.nextLine();
}

/**
* If there is an error, diplay it to the user.
*
* @param message The error message.
*/
public void displayErrorMessage(String message) {
System.out.println(message);
}

/**
* Displays the list of tasks in the storage.
*
* @param storage The tasklist that is in the storage.
*/
public void printList(ArrayList<Task> storage) {
for (int i = 0; i < storage.size(); i++) {
System.out.println((i + 1) + "." + storage.get(i).toString());
}
}

public void addedTaskMessage(Task task, int noOfTasks) {
System.out.println("Ok! I've added this task: \n" + task.toString());
/**
* Displays a completion message after successfully adding a task to the list.
*
* @param addedTask The task that was added.
* @param noOfTasks The total number of tasks in the list after adding.
*/
public void addedTaskMessage(Task addedTask, int noOfTasks) {
System.out.println("Ok! I've added this task: \n" + addedTask.toString());
System.out.println("Now you have " + noOfTasks + " tasks in the list.");
}

/**
* Displays a completion message after successfully deleting a task from the list.
*
* @param deletedTask The task that was deleted.
* @param noOfTasks The total number of tasks in the list after deletion.
*/
public void deleteMessage(Task deletedTask, int noOfTasks) {
System.out.println("Okay, I've removed this task: \n" + deletedTask);
System.out.println("Now you have " + noOfTasks + " in the list.");
}


/**
* Displays a final message to the user before the Chat Bot shuts down.
*/
public void byeMessage() {
System.out.println("Bye mate! Have a nice day :]");
}
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/Jelly/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.ArrayList;
import java.util.List;

/**
* Corresponds to a task with a deadline(date or date and time).
*/
public class Deadline extends Task {

protected String by;
Expand All @@ -15,6 +18,12 @@ public class Deadline extends Task {

protected LocalDateTime deadLineDateAndTime;

/**
* Constructor for a deadline task.
*
* @param description The description of the task.
* @param by When the task is due by.
*/
public Deadline(String description, String by) {
super(description);
this.deadlineDate = parseDate(by);
Expand All @@ -40,6 +49,12 @@ public String writeToFile() {
return printedStuff + this.by;
}

/**
* Parses the date inputted that corresponds to the formats accepted.
*
* @param date The due date of the task.
* @return The parsed date, or null if the date is in a format that isn't accepted.
*/
protected LocalDate parseDate(String date) {
List<DateTimeFormatter> formats = new ArrayList<>();
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Expand All @@ -56,7 +71,13 @@ protected LocalDate parseDate(String date) {
return null;
}

protected LocalDateTime parseDateTime(String date) {
/**
* Parses the date and time inputted that corresponds to the formats accepted.
*
* @param dateTime The due date and time of the task.
* @return The parsed date and time, or null if incorrect format.
*/
protected LocalDateTime parseDateTime(String dateTime) {
List<DateTimeFormatter> formats = new ArrayList<>();
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"));
formats.add(DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"));
Expand All @@ -65,7 +86,7 @@ protected LocalDateTime parseDateTime(String date) {

for (int i = 0; i < formats.size(); i++) {
try {
return LocalDateTime.parse(date, formats.get(i));
return LocalDateTime.parse(dateTime, formats.get(i));
} catch (DateTimeParseException e) {

}
Expand Down
Loading

0 comments on commit f424f95

Please sign in to comment.