-
Notifications
You must be signed in to change notification settings - Fork 114
[DarkDragoon2002] iP #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
90c8452
6027a2d
0d164dd
dded6bf
d0d0bda
a43c3b2
6a7f020
0d50273
cb16bf3
913e641
f0c0fab
36dbaff
2d8b65a
5b59c6f
c4ca544
f0b78b9
0941170
9672976
2d5e67e
c88fc8c
e2f954e
1e844eb
44b8d55
43e739f
075bdcf
8eff0ee
01d0c9d
cdf58d0
8d0bb25
4e5f9fa
2935ec3
e439f74
936c109
42cbb2c
364eab3
e64f9dc
16bf1b5
97be193
45abc71
4175b23
98d3df5
246fbcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package CustomExceptions; // Package for custom exceptions | ||
| import TaskChildren.Task; // Import Task class to manage task deletion | ||
|
|
||
| // Custom exception class for handling errors in Deadline task construction | ||
| public class DeadlineConstructorException extends Exception { | ||
|
|
||
| // Constructor that takes an error message as input | ||
| public DeadlineConstructorException(String message) { | ||
| // Call the superclass (Exception) constructor with a detailed error message | ||
| super("DEADLINE CONSTRUCTOR EXCEPTION: " + errorMessage(message)); | ||
|
|
||
| // If an error occurs, the latest task added is deleted to prevent invalid tasks | ||
| Task.deleteLatestTask(); | ||
| } | ||
|
|
||
| // Private helper method to interpret the error message | ||
| // It checks for specific missing components in the deadline command | ||
| private static String errorMessage(String message) { | ||
| // Check if the "/by" keyword is missing, which is needed for deadline tasks | ||
| if (!(message.contains(" /by "))) { | ||
| return "MISSING BY COMMAND"; // Return specific error if "/by" is not found | ||
| } | ||
|
|
||
| // Split the task input string into task description and deadline parts | ||
| String[] taskStringBreakdown = message.replace("deadline ", "").split(" /by "); | ||
|
|
||
| // Check if the task description before "/by" is empty | ||
| if (taskStringBreakdown[0].isEmpty()) { | ||
| return "MISSING TASK STATEMENT"; // Return specific error if no task description | ||
| } | ||
|
|
||
| // If none of the specific errors match, return an unknown error | ||
| return "UNKNOWN ERROR"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package CustomExceptions; // Package for custom exceptions | ||
| import TaskChildren.Task; // Import Task class to manage task deletion | ||
|
|
||
| // Custom exception class for handling errors in Event task construction | ||
| public class EventConstructorException extends Exception { | ||
|
|
||
| // Constructor that takes an error message as input | ||
| public EventConstructorException(String message) { | ||
| // Call the superclass (Exception) constructor with a detailed error message | ||
| super("EVENT CONSTRUCTOR EXCEPTION: " + errorMessage(message)); | ||
|
|
||
| // If an error occurs, the latest task added is deleted to prevent invalid tasks | ||
| Task.deleteLatestTask(); | ||
| } | ||
|
|
||
| // Private helper method to interpret the error message | ||
| // It checks for specific missing components in the event command | ||
| private static String errorMessage(String message) { | ||
| // Check if the "/from" and "/to" keywords are missing, which are needed for event tasks | ||
| if (!(message.contains(" /from ") || message.contains(" /to "))) { | ||
| return "MISSING FROM/TO COMMANDS"; // Return specific error if either "/from" or "/to" is missing | ||
| } | ||
|
|
||
| // Split the task input string into task description and the time period starting with "/from" | ||
| String[] taskStringBreakdown = message.replace("event ", "").split(" /from "); | ||
|
|
||
| // Check if the task description before "/from" is empty | ||
| if (taskStringBreakdown[0].isEmpty()) { | ||
| return "MISSING TASK STATEMENT"; // Return specific error if no task description is provided | ||
| } | ||
|
|
||
| // Further split the time period into "from" and "to" parts | ||
| String[] fromToStringBreakdown = taskStringBreakdown[1].split(" /to "); | ||
|
|
||
| // Check if both "from" and "to" components are present | ||
| if (fromToStringBreakdown.length != 2) { | ||
| return "MISSING FROM/TO DATES"; // Return specific error if either "from" or "to" date is missing | ||
| } | ||
|
|
||
| // If none of the specific errors match, return an unknown error | ||
| return "UNKNOWN ERROR"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package CustomExceptions; // Package for custom exceptions | ||
| import TaskChildren.Task; // Import Task class to manage task deletion | ||
|
|
||
| // Custom exception class for handling errors in ToDo task construction | ||
| public class ToDoConstructorException extends Exception { | ||
|
|
||
| // Constructor that takes an error message as input | ||
| public ToDoConstructorException(String message) { | ||
| // Call the superclass (Exception) constructor with a detailed error message | ||
| super("TODO CONSTRUCTOR EXCEPTION: " + errorMessage(message)); | ||
|
|
||
| // If an error occurs, the latest task added is deleted to prevent invalid tasks | ||
| Task.deleteLatestTask(); | ||
| } | ||
|
|
||
| // Private helper method to interpret the error message | ||
| // It checks for specific missing components in the todo command | ||
| private static String errorMessage(String message) { | ||
|
|
||
| // Remove the "todo" keyword from the message to isolate the task statement | ||
| String taskString = message.replace("todo ", ""); | ||
|
|
||
| // Check if the task statement is empty | ||
| if (taskString.isEmpty()) { | ||
| return "MISSING TASK STATEMENT"; // Return specific error if the task description is missing | ||
| } | ||
|
|
||
| // If none of the specific errors match, return an unknown error | ||
| return "UNKNOWN ERROR"; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import CustomExceptions.*; // Import custom exception classes | ||
| import TaskChildren.*; // Import task-related classes like ToDo, Deadline, and Event | ||
|
|
||
| import java.util.Scanner; // Import Scanner for user input | ||
|
|
||
| public class Juan { | ||
| // Constant for common error message to improve code readability and reusability | ||
| private final static String porFavor = "Por Favor?\n"; | ||
|
|
||
| // Main entry point for the application | ||
| public static void main(String[] args) { | ||
| // Display initial line and welcome message | ||
| lineMessage(); | ||
| helloMessage(); | ||
| lineMessage(); | ||
|
|
||
| // Continue chatting as long as user doesn't exit | ||
| boolean continueChatting = true; | ||
| while (continueChatting) { | ||
| // Chat feature to handle user input | ||
| continueChatting = chatFeature(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can consider extracting this part further to match same level of abstraction in this method. versus |
||
|
|
||
| // Display goodbye message when the chat ends | ||
| byeMessage(); | ||
| lineMessage(); | ||
| } | ||
|
|
||
| // Handles user input and executes corresponding actions | ||
| public static boolean chatFeature() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is very long, you can consider extracting the details for each case into separate methods. It will be better to keep each method short.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback just updated this! I extracted it into another class to make it easier to read |
||
|
|
||
| Scanner scanner = new Scanner(System.in); // Initialize the scanner for reading user input | ||
| String line = scanner.nextLine(); // Read user input | ||
| lineMessage(); // Print separator line | ||
|
|
||
| // Handle "bye" command to end chat | ||
| if (line.equals("bye")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May the command words also be variables, for later use or easier management? |
||
| return false; // End the conversation | ||
| } | ||
| // Handle "list" command to display the list of tasks | ||
| else if (line.equals("list")) { | ||
| Task.printTasksList(); // Print task list from Task class | ||
| } | ||
| // Handle "mark" command to mark a task as completed | ||
| else if (line.startsWith("mark ")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to avoid magic literals(e.g. numbers, strings), you can consider using named constants instead. It will be useful especially when the particular literals are used multiple times in the code.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback just updated this! |
||
| try { | ||
| int taskIndex = Integer.parseInt(line.replace("mark ", "")) - 1; // Parse the task index | ||
| Task.mark(taskIndex); // Mark the task as done | ||
| } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | ||
| // Handle invalid index or format exceptions | ||
| System.out.println(porFavor + "MARK EXCEPTION: INVALID TASK INDEX"); | ||
| } catch (NullPointerException e) { | ||
| // Handle null task case | ||
| System.out.println(porFavor + "MARK EXCEPTION: NULL TASK INDEX"); | ||
| } | ||
| } | ||
| // Handle "unmark" command to unmark a task as completed | ||
| else if (line.startsWith("unmark ")) { | ||
| try { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to keep else if and closing curly bracket on the same line.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback just updated this! |
||
| int taskIndex = Integer.parseInt(line.replace("unmark ", "")) - 1; // Parse the task index | ||
| Task.unmark(taskIndex); // Unmark the task | ||
| } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | ||
| // Handle invalid index or format exceptions | ||
| System.out.println(porFavor + "UNMARK EXCEPTION: INVALID TASK INDEX"); | ||
| } catch (NullPointerException e) { | ||
| // Handle null task case | ||
| System.out.println(porFavor + "UNMARK EXCEPTION: NULL TASK INDEX"); | ||
| } | ||
|
|
||
| } | ||
| // Handle "todo" command to create a new ToDo task | ||
| else if (line.startsWith("todo ")) { | ||
| try { | ||
| new ToDo(line); // Create a new ToDo object | ||
| } catch (ToDoConstructorException e) { | ||
| // Handle custom ToDo exception | ||
| System.out.println(porFavor + e.getMessage()); | ||
| } | ||
| } | ||
| // Handle "deadline" command to create a new Deadline task | ||
| else if (line.startsWith("deadline ")) { | ||
| try { | ||
| new Deadline(line); // Create a new Deadline object | ||
| } catch (DeadlineConstructorException e) { | ||
| // Handle custom Deadline exception | ||
| System.out.println(porFavor + e.getMessage()); | ||
| } | ||
| } | ||
| // Handle "event" command to create a new Event task | ||
| else if (line.startsWith("event ")) { | ||
| try { | ||
| new Event(line); // Create a new Event object | ||
| } catch (EventConstructorException e) { | ||
| // Handle custom Event exception | ||
| System.out.println(porFavor + e.getMessage()); | ||
| } | ||
| } | ||
| // Default case for unrecognized commands | ||
| else { | ||
| System.out.println(porFavor + "UNRECOGNIZED REQUEST"); // Inform user about an unrecognized command | ||
| } | ||
|
|
||
| lineMessage(); // Print separator line | ||
| return true; // Continue conversation | ||
| } | ||
|
|
||
| // Utility function to print a separator line for clean output formatting | ||
| public static void lineMessage() { | ||
| String line = "____________________________________________________________\n"; | ||
| System.out.print(line); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the messages, should it be static variables instead and what the methods do is just printing it out? |
||
|
|
||
| // Displays a welcome message when the program starts | ||
| public static void helloMessage() { | ||
| String greeting = | ||
| " ._-'-_ .\n" + | ||
| " . ' /_-_-_\\ ` .\n" + | ||
| " .' |-_-_-_-| `.\n" + | ||
| " ( `.-_-_-.' )\n" + | ||
| " !`. .'!\n" + | ||
| " ! ` . . ' !\n" + | ||
| " ! ! ! ! ! ! ! ! !\n" + | ||
| " / / \\ \\\n" + | ||
| " _-| \\___ ___/ /-_\n" + | ||
| " (_ )__\\_)\\(_/__( _)\n" + | ||
| " ))))\\X\\ ((((\n" + | ||
| " \\/ \\/ \n" + | ||
| "Hola Amigo, I am Juan Cervantes Salamanca from Michoacan \n" + | ||
| "Welcome to la familia \n" + | ||
| "How can we help you? \n"; | ||
| System.out.print(greeting); // Print the welcome message | ||
| } | ||
|
|
||
| // Displays a goodbye message when the program ends | ||
| public static void byeMessage() { | ||
| String bye = "Adios amigo, la familia will miss you\n"; | ||
| System.out.print(bye); // Print the goodbye message | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package TaskChildren; // Package for Task-related classes | ||
|
|
||
| import CustomExceptions.DeadlineConstructorException; // Import custom exception for Deadline tasks | ||
|
|
||
| // Deadline class, a child class of Task, represents a task with a specific deadline | ||
| public class Deadline extends Task { | ||
|
|
||
| // Variable to store the deadline date/time string | ||
| private String deadlineString; | ||
|
|
||
| // Constructor for creating a Deadline task | ||
| public Deadline(String inputString) throws DeadlineConstructorException { | ||
| // Call the parent class (Task) constructor with the task description (before the "/by" keyword) | ||
| super(inputString.replace("deadline ", "").split(" /by ")[0]); | ||
|
|
||
| // Check if the "/by" keyword is missing or if the task description is empty | ||
| if (!(inputString.contains(" /by ")) || this.taskString.isEmpty()) { | ||
| throw new DeadlineConstructorException(inputString); // Throw custom exception if validation fails | ||
| } | ||
|
|
||
| // Extract the deadline portion of the input string (after the "/by" keyword) | ||
| this.deadlineString = inputString.replace("deadline ", "").split(" /by ")[1]; | ||
|
|
||
| // Display a confirmation message when a Deadline task is successfully created | ||
| constructorMessage(); | ||
| } | ||
|
|
||
| // Override the checkboxString method to include the "[D]" tag and the deadline information | ||
| @Override | ||
| public String checkboxString() { | ||
| // Call the parent method and add the "[D]" tag and deadline string to the checkbox format | ||
| return "[D]" + super.checkboxString() + " (by: " + deadlineString + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package TaskChildren; // Package for Task-related classes | ||
|
|
||
| import CustomExceptions.EventConstructorException; // Import custom exception for Event tasks | ||
|
|
||
| // Event class, a child class of Task, represents a task with a specific start and end time | ||
| public class Event extends Task { | ||
|
|
||
| // Variables to store the start and end times of the event | ||
| private String fromString; | ||
| private String toString; | ||
|
|
||
| // Constructor for creating an Event task | ||
| public Event(String inputString) throws EventConstructorException { | ||
| // Call the parent class (Task) constructor with the task description (before the "/from" keyword) | ||
| super(inputString.replace("event ", "").split(" /from ")[0]); | ||
|
|
||
| // Validate if both "/from" and "/to" keywords are present, and the task description is not empty | ||
| if (!(inputString.contains(" /from ") & inputString.contains(" /to ")) || this.taskString.isEmpty()) { | ||
| throw new EventConstructorException(inputString); // Throw custom exception if validation fails | ||
| } | ||
|
|
||
| // Split the input to extract the start and end times | ||
| String[] fromToStrings = inputString.replace("event ", "").split(" /from ")[1].split(" /to "); | ||
|
|
||
| // Check if both "from" and "to" times are provided | ||
| if (fromToStrings.length != 2) { | ||
| throw new EventConstructorException(inputString); // Throw custom exception if either is missing | ||
| } | ||
|
|
||
| // Set the from and to times for the event | ||
| this.fromString = fromToStrings[0]; | ||
| this.toString = fromToStrings[1]; | ||
|
|
||
| // Display a confirmation message when an Event task is successfully created | ||
| constructorMessage(); | ||
| } | ||
|
|
||
| // Override the checkboxString method to include the "[E]" tag, start time, and end time | ||
| @Override | ||
| public String checkboxString() { | ||
| // Call the parent method and add the "[E]" tag along with the event's start and end times | ||
| return "[E]" + super.checkboxString() + " (from: " + fromString + " to: " + toString + ")"; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean variables/methods should be named to sound like booleans.
The same error is present in other parts of the code.