Skip to content

Commit

Permalink
added comments to the purpose of each method and class
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertybox123 committed Sep 21, 2023
1 parent 3ef36d5 commit c6b907c
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 120 deletions.
1 change: 1 addition & 0 deletions src/main/java/duke/Command.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package duke;
//list of possible commands by the user
public enum Command {
TODO,
DEADLINE,
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/duke/DateConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateConverter {
// converts the input string from MMM dd yyyy to a Date object with "YYYY/MM/DD"
public static String convertDate(String inputDateStr) {
try {
// Define a SimpleDateFormat for parsing the input date
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ public class Deadline extends Task {
private LocalDate deadline;



// constructor of deadline takes in a string, whether it is marked and a deadline
public Deadline(String name, boolean isMarked, LocalDate deadline) {
super(name, isMarked);
this.deadline = deadline;
}



// gets the deadline of the Deadline object
public LocalDate getDeadline() {

return deadline;
}

// sets the deadline of the Deadline Object
public void setDeadline(LocalDate deadline) {
this.deadline = deadline;
}

// returns a String depending on if it is marked as [D] <marked/or not> <name of deadline task> by:<Date>
@Override
public String toString() {
String status = isMarked ? "[X]" : "[ ]";
Expand Down
256 changes: 154 additions & 102 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,130 +30,182 @@ public void run() {
Command command = Parser.parseCommand(userInput);
String description = Parser.parseDescription(userInput);
switch (command) {
//if user wants to exit, tasks are saved and exit message is shown
case EXIT:
storage.save(tasks, "tasks.txt");
ui.closeScanner();
ui.showExitMessage();
handleExit();
return;
// lists all the tasks out
case LIST:
ui.showTaskList(tasks);
handleList();
break;
case FIND:
handleFind(description);
break;
// unmarks task
case UNMARK:
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.unmarkTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
handleUnmark(description);
break;
//marks the task
case MARK:
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.markTaskAsDone(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
handleMark(description);
break;
// if user wants to add a todo object
case TODO:
try {
if (description.isEmpty()) {
throw new EmptyTodoException();
}
Todo todo = new Todo(description, false);
tasks.addTask(todo);

} catch (EmptyTodoException e) {
System.out.println(e.getMessage());
}
handleTodo(description);
break;
// if user wants to input deadline
case DEADLINE:

if (description.isEmpty()) {
try {
throw new EmptyDeadlineException();
} catch (EmptyDeadlineException e) {
System.out.println(e.getMessage());
}
} else {
// Find the index of the deadline separator "/"
int separatorIndex = description.indexOf('/');

if (separatorIndex != -1) { // Ensure the separator exists in the input
// Extract the task description and deadline

String descriptionString = description.substring(0, separatorIndex).trim();
String deadline = description.substring(separatorIndex + 4).trim();
String pattern = "\\d{4}/\\d{2}/\\d{2}";
Pattern datePattern = Pattern.compile(pattern);
Matcher matcher = datePattern.matcher(deadline);
if (matcher.find()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
Deadline deadlineTask = new Deadline(descriptionString, false, localDateDeadline);
tasks.addTask(deadlineTask);

} else {
System.out.println("Please input your deadline in YYYY/MM/DD format");
}
} else {
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
}
}
handleDeadline(description);
break;
// if user wants to input an event
case EVENT:
if (description.isEmpty()) {
try {
throw new EmptyEventException();
} catch (EmptyEventException e) {
System.out.println(e.getMessage());
}
} else {
// Find the indices of the time separators
int fromIndex = description.indexOf("/from");
int toIndex = description.indexOf("/to");

if (fromIndex != -1 && toIndex != -1) {
// Extract the task description, startTime, and endTime
String descriptionString = description.substring(0, fromIndex).trim();
String startTime = description.substring(fromIndex + 5, toIndex).trim();
String endTime = description.substring(toIndex + 3).trim();

// Create a new Event object
Event eventTask = new Event(descriptionString, false, startTime, endTime);
tasks.addTask(eventTask);

} else {
System.out.println("Invalid input format for event command.");
}
}
handleEvent(description);
break;
// if user wants to delete existing task
case DELETE:
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.deleteTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
handleDelete(description);
break;
// if user just enters a completely invalid command
case INVALID:
ui.showError("Invalid command. Please try again.");
handleInvalid();
break;
}
}
}

private void handleExit() {
storage.save(tasks, "tasks.txt");
ui.closeScanner();
ui.showExitMessage();
}

private void handleList() {
ui.showTaskList(tasks);
}

private void handleFind(String description) {
tasks.findTasksContainingKeyword(description);
}

private void handleUnmark(String description) {
// if user inputs task number, check if it is even an integer, and whether it is within range
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.unmarkTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}

} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
}

private void handleMark(String description) {
// if user inputs task number, check if it is even an integer, and whether it is within range
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.markTaskAsDone(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
}
private void handleTodo(String description) {
try {
if (description.isEmpty()) {
throw new EmptyTodoException();
}
Todo todo = new Todo(description, false);
tasks.addTask(todo);

} catch (EmptyTodoException e) {
System.out.println(e.getMessage());
}
}
private void handleDeadline(String description) {
if (description.isEmpty()) {
try {
throw new EmptyDeadlineException();
} catch (EmptyDeadlineException e) {
System.out.println(e.getMessage());
}
} else {
// Find the index of the deadline separator "/"
int separatorIndex = description.indexOf('/');

if (separatorIndex != -1) { // Ensure the separator exists in the input
// Extract the task description and deadline

String descriptionString = description.substring(0, separatorIndex).trim();
String deadline = description.substring(separatorIndex + 4).trim();
String pattern = "\\d{4}/\\d{2}/\\d{2}";
Pattern datePattern = Pattern.compile(pattern);
Matcher matcher = datePattern.matcher(deadline);
if (matcher.find()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
Deadline deadlineTask = new Deadline(descriptionString, false, localDateDeadline);
tasks.addTask(deadlineTask);

} else {
System.out.println("Please input your deadline in YYYY/MM/DD format");
}
} else {
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
}
}
}

private void handleEvent(String description) {
if (description.isEmpty()) {
try {
throw new EmptyEventException();
} catch (EmptyEventException e) {
System.out.println(e.getMessage());
}
} else {
// Find the indices of the time separators
int fromIndex = description.indexOf("/from");
int toIndex = description.indexOf("/to");

if (fromIndex != -1 && toIndex != -1) {
// Extract the task description, startTime, and endTime
String descriptionString = description.substring(0, fromIndex).trim();
String startTime = description.substring(fromIndex + 5, toIndex).trim();
String endTime = description.substring(toIndex + 3).trim();

// Create a new Event object
Event eventTask = new Event(descriptionString, false, startTime, endTime);
tasks.addTask(eventTask);

} else {
System.out.println("Invalid input format for event command.");
}
}
}

private void handleDelete(String description) {
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.deleteTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
}

private void handleInvalid() {
ui.showError("Invalid command. Please try again.");
}

public static void main(String[] args) {
new Duke("tasks.txt").run();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/EmptyDeadlineException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package duke;
// if the description of a deadline is empty, return error message
public class EmptyDeadlineException extends Exception {
public EmptyDeadlineException() {
super("OOPS!!! The description of a deadline cannot be empty.");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/EmptyEventException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package duke;
// if the description of an event is empty, return error message
public class EmptyEventException extends Exception {
public EmptyEventException() {
super("OOPS!!! The description of an event cannot be empty.");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/EmptyTodoException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package duke;
// if the description of a todo is empty, return error message
public class EmptyTodoException extends Exception {
public EmptyTodoException() {
super("OOPS!!! The description of a todo cannot be empty.");
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package duke;

public class Event extends Task {
private String startTime;
private String endTime;

//Event objects inherit from tasks and has additional attributes start and end time

public Event(String name, boolean isMarked, String startTime, String endTime) {
super(name, isMarked);
this.startTime = startTime;
this.endTime = endTime;
}

//getter of start time of event
public String getStartTime() {
return startTime;
}

// setter of start time for event
public void setStartTime(String startTime) {
this.startTime = startTime;
}

// getter of end time for event
public String getEndTime() {
return endTime;
}

// setter of end time for event
public void setEndTime(String endTime) {
this.endTime = endTime;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/duke/Exit.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package duke;

// when user enters bye and wants to exit the bot , this message is shown
public class Exit {
public String exitMessage() {
return "Bye. Hope to see you again soon!";
Expand Down
Loading

0 comments on commit c6b907c

Please sign in to comment.