Skip to content

Commit

Permalink
Merge pull request #3 from AlWo223/branch-A-JavaDoc
Browse files Browse the repository at this point in the history
JavaDoc Comments
  • Loading branch information
AlWo223 authored Oct 1, 2023
2 parents d712611 + 0cb9ba7 commit b301570
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 29 deletions.
74 changes: 66 additions & 8 deletions src/main/java/duke/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,31 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

/**
* Executes the commands provided by the user.
*/
public class Command {

private String command;
private String argument;

/**
* Constructor of Command.
* @parm command Command to be executed.
* @parm argument Arguments required for the specific command.
* */
public Command(String command, String argument) {
this.command = command;
this.argument = argument;
}

/**
* Executes the given command.
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the list of tasks with the applied modifications.
* @throws InvalidCommandException If the command is not included in the program or if there is a typo.
* */
public TaskList executeCommand(TaskList tasks, Ui ui) throws InvalidCommandException {
switch (command) {
case "list":
Expand Down Expand Up @@ -50,19 +65,37 @@ public TaskList executeCommand(TaskList tasks, Ui ui) throws InvalidCommandExcep
return tasks;
}

/**
* Marks or unmarks a given task.
* If the index is out of bounds or if the argument is not a number, it will print an error message.
* @parm argument Index of the task to be edited.
* @parm done New status of the task.
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the list of tasks with the applied mark modifications.
* */
public TaskList editTask(String argument, boolean done, TaskList tasks, Ui ui){
try {
int index = Integer.parseInt(argument);
tasks.getTasks().get(index - 1).setDone(done);
ui.printMark(done, tasks, index);
int taskIndex = Integer.parseInt(argument);
tasks.getTasks().get(taskIndex - 1).setDone(done);
ui.printMark(done, tasks, taskIndex);
} catch (IndexOutOfBoundsException | NumberFormatException e){
ui.printInvalidTaskIdMessage();
}
return tasks;
}

/**
* Adds a new Todo task with the description specified in argument.
* If the description is empty, it will print an error message.
* @parm argument Description of the task.
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the list of tasks with the new Todo.
* @see Todo
* */
public TaskList addToDo(String argument, TaskList tasks, Ui ui){
if(argument==null || argument.isEmpty()){
if (argument==null || argument.isEmpty()) {
ui.printEmptyTodoMessage();
}
Task todo = new Todo(argument);
Expand All @@ -71,6 +104,15 @@ public TaskList addToDo(String argument, TaskList tasks, Ui ui){
return tasks;
}

/**
* Adds a new Deadline task with the description and due date specified in argument.
* If the description or due date is empty, it will print an error message.
* @parm argument Description and due date of the task.
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the list of tasks with the newly added deadline.
* @see Deadline
* */
public TaskList addDeadline(String argument, TaskList tasks, Ui ui){
try {
LocalDateTime dueDate = LocalDateTime.parse(argument.split(" /by ")[1].replace(" ", "T"));
Expand All @@ -88,6 +130,15 @@ public TaskList addDeadline(String argument, TaskList tasks, Ui ui){
return tasks;
}

/**
* Adds a new Event task with the description, start date and end date specified in argument.
* If the description, start date or end date is empty, it will print an error message.
* @parm argument Description, start date and end date of the task.
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the list of tasks with the newly added Event.
* @see Event
* */
public TaskList addEvent(String argument, TaskList tasks, Ui ui){
try {
String description = argument.split(" /from ")[0];
Expand All @@ -99,9 +150,9 @@ public TaskList addEvent(String argument, TaskList tasks, Ui ui){
Task event = new Event(description, startDate, endDate);
tasks.add(event);
ui.printTaskAddedMessage(event, tasks.getTasks());
} catch (ArrayIndexOutOfBoundsException e){
} catch (ArrayIndexOutOfBoundsException e) {
ui.printInvalidEventMessage();
} catch (NullPointerException e){
} catch (NullPointerException e) {
ui.printEmptyEventMessage();
} catch (DateTimeParseException e){
ui.printInvalidDateTimeMessage();
Expand All @@ -111,16 +162,23 @@ public TaskList addEvent(String argument, TaskList tasks, Ui ui){
return tasks;
}

/**
* Deletes a task from the list of tasks based on the specified position within the list starting at index 1.
* If the index is out of bounds or if the argument is not a number, it will print an error message.
* @parm argument Index of the task to be deleted (>= 1).
* @parm tasks TaskList object containing the list of tasks.
* @parm ui Ui object to interact with the user.
* @return TaskList object containing the updated list of tasks.
* */
public TaskList deleteTask(String argument, TaskList tasks, Ui ui) {
try {
int index = Integer.parseInt(argument);
Task task = tasks.getTasks().get(index - 1);
tasks.remove(index - 1);
ui.printTaskRemovedMessage(task, tasks);
} catch (IndexOutOfBoundsException | NumberFormatException e){
} catch (IndexOutOfBoundsException | NumberFormatException e) {
ui.printInvalidTaskIdMessage();
}
return tasks;
}

}
18 changes: 17 additions & 1 deletion src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Represents a deadline task which has a description and a due date.
*/
public class Deadline extends Task{

private LocalDateTime dueDate;
private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("MMM-dd-yyyy HH:mm");

public Deadline(String description, LocalDateTime dueDate){
/**
* Constructor for Deadline
* @param description description of the deadline
* @param dueDate due date of the described task
*/
public Deadline(String description, LocalDateTime dueDate) {
super(description);
this.dueDate = dueDate;

}

@Override
/**
* Returns the single line representation of the deadline which is used by the UI.
* @return string representation of the deadline.
*/
public String toString(){
return "[D]" + super.toString() + " (by: " + dueDate.format(DTF) + ")";
}

@Override
/**
* Returns the single line representation of the deadline which is used for the file.
* @return string representation of the deadline.
*/
public String toFileString() {
return ("D | " + super.toFileString() + " | " + dueDate.format(DTF));
}
Expand Down
46 changes: 35 additions & 11 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,58 @@

import duke.exceptions.InvalidCommandException;

/**
* Main class of the Duke program.
* Duke is a chatbot that helps the user to keep track of tasks.
* The user can add tasks, mark tasks as done, delete tasks and view the list of tasks.
* The bot supports three types of tasks: Todo, Deadline and Event.
* The tasks are saved in a file and loaded automatically from the file when the program is started again.
* The file is saved in the data folder.
* The user can exit the program by typing "bye".
* */
public class Duke {

private TaskList tasks;
private Storage storage;
private Ui ui;
private Parser parser;
public Duke(String filePath){
ui = new Ui();
storage = new Storage();
tasks = new TaskList(storage.loadData());

/**
* Constructor of Duke. Initializes the Ui, Storage and Tasks list.
* @parm filePath Path to the file where the data is stored. Default path is data/duke.txt.
* */
public Duke(String filePath) {
this.ui = new Ui();
this.storage = new Storage(filePath);
this.tasks = new TaskList(storage.loadData());
}

public void run(){
ui.introduceBot(tasks);
handleCommands();
ui.farewellBot();
/**
* Runs the Duke program.
*/
public void run() {
this.ui.introduceBot(tasks);
this.handleCommands();
this.ui.farewellBot();
}

/**
* Main method of the Duke program.
* @param args
*/
public static void main(String[] args) {
new Duke("data/duke.txt").run();
}

/**
* Handles the user interaction loop and executes the commands.
* Method will continue to ask for user input until its termination on the "bye" command.
* @see Command
*/
public void handleCommands() {
String input;

do {
input = ui.readCommand();
input = this.ui.readCommand();
Command c = Parser.extractCommand(input);
try {
c.executeCommand(tasks, ui);
Expand All @@ -39,5 +64,4 @@ public void handleCommands() {
ui.printHorizontalLine();
} while (!input.equalsIgnoreCase("bye"));
}

}
17 changes: 17 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Represents an event task which is a task with a start date and an end date.
*/
public class Event extends Task {

private LocalDateTime startDate;
private LocalDateTime endDate;
private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("MMM-dd-yyyy HH:mm");

/**
* Constructor for Event class.
* @param description Description of the event.
* @param startDate Start date of the event.
* @param endDate End date of the event.
*/
public Event(String description, LocalDateTime startDate, LocalDateTime endDate){
super(description);
this.startDate = startDate;
this.endDate = endDate;
}

@Override
/**
* Returns the single line representation of the event which is used by the UI.
* @return string representation of the event.
*/
public String toString(){
return "[E]" + super.toString() + " (from: " + startDate.format(DTF) + " to: " + endDate.format(DTF) + ")";
}

@Override
/**
* Returns the single line representation of the event which is used for the file.
* @return string representation of the event.
*/
public String toFileString(){
return ("E | " + super.toFileString() + " | " + startDate.format(DTF) + " | " + endDate.format(DTF));
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package duke;

/**
* Deals with making sense of the user command.
*/
public class Parser {

public Parser(){

}

/**
* Extracts the command and argument from the input string provided by the user.
* @param input user input describing the command and argument
* @return Command object containing the extracted command and argument
*/
public static Command extractCommand(String input) {
String[] parts = input.split(" ", 2);
String command = parts[0].toLowerCase();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* Handles the loading and saving of data from and to the data file.
*/
public class Storage {

private String path;
private ArrayList<Task> tasks;

private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("MMM-dd-yyyy'T'HH:mm");

/**
* Default Constructor for Storage with path set to "data/duke.txt".
*/
public Storage() {
this.path = "data/duke.txt";
}

/**
* Constructor for Storage with path set to the given path.
* @param path Path to the file which the Storage will read from and write to.
*/
public Storage(String path) {
this.path = path;
}

/**
* Loads data from the file specified by the path into a task list.
* If the file does not exist, an empty task list will be returned.
* @return ArrayList of Tasks read from the file.
*/
public ArrayList<Task> loadData(){
try {
tasks = new ArrayList<Task>();
Expand All @@ -38,6 +55,13 @@ public ArrayList<Task> loadData(){
return tasks;
}

/**
* Reads one line of data from the file and adds the corresponding task to the task list.
* If the input formatting of the line is incorrect, the line will be skipped and the user will be notified about
* the
* file corruption.
* @param input Line of data from the file.
*/
public void readDataLine(String input) {
String[] parts = input.split(" \\| ");
try {
Expand Down Expand Up @@ -66,6 +90,11 @@ public void readDataLine(String input) {
}
}

/**
* Store the given task list to the file specified by the path.
* If the file does not exist, a new file will be created.
* @param tasks Task list to be saved.
*/
public void saveData(ArrayList<Task> tasks) {
try {
this.tasks = tasks;
Expand Down
Loading

0 comments on commit b301570

Please sign in to comment.