Skip to content

Commit

Permalink
A-CodeQuality, refactored code to follow good coding practices.
Browse files Browse the repository at this point in the history
  • Loading branch information
raydenlim committed Sep 15, 2023
1 parent e80e73b commit 7340752
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 52 deletions.
7 changes: 2 additions & 5 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[A][ ] hehe
[D][ ] (by: now)
[A][ ] airubg
[D][ ] ugbia (by: efuba woubjs)
[A][ ] ff
[D][ ] uabrgiub (by: 12 Dec 1212 00:00)
[A][ ] iguiugbf
2 changes: 1 addition & 1 deletion src/main/java/Command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DeleteCommand(int taskIndex) {
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
try {
assert taskIndex > 0 && taskIndex < taskList.getTaskCount() : "Invalid task index/ out of range";
assert taskIndex >= 0 && taskIndex <= taskList.getTaskCount() : "Invalid task index/ out of range";
Task removedTask = taskList.deleteTask(taskIndex);
storage.saveTask(taskList.getTasks());
return ui.showDeletedTask(removedTask, taskList.getTaskCount());
Expand Down
238 changes: 203 additions & 35 deletions src/main/java/Parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,9 @@ public static Task parseTask(String taskData) throws FileNotFoundException, Duke
taskToAdd = new ToDo(taskInfo);
break;
case "D":
taskInfo = taskInfo.substring(0, taskInfo.indexOf("(") - 1);
String xtraInfo = taskData.substring(taskData.indexOf("(") + 1, taskData.indexOf(")"));
String[] deadLineInfo = xtraInfo.split(": ");
String by = deadLineInfo[1];
taskToAdd = new DeadLine(taskInfo, by);
break;
return createDeadLineTask(taskInfo);
case "E":
taskInfo = taskInfo.substring(0, taskInfo.indexOf('(') - 1);
String addInfo = taskData.substring(taskData.indexOf('(') + 1, taskData.indexOf(')'));
String[] eventInfo = addInfo.split(": ");
String from = eventInfo[1].substring(0, eventInfo[1].length() - 2).trim();
String to = eventInfo[2].trim();
taskToAdd = new Event(taskInfo, from, to);
break;
return createEventTask(taskInfo);
default:
throw new DukeException("File is corrupted!");
}
Expand All @@ -83,6 +72,44 @@ public static Task parseTask(String taskData) throws FileNotFoundException, Duke
return null;
}

/**
* Creates a DeadLine task based on task information.
*
* @param taskInfo The task information string containing the task description and deadline.
* @return A DeadLine task instance based on the task information.
* @throws DukeException If there is an issue parsing the task information.
*/
private static Task createDeadLineTask(String taskInfo) throws DukeException {
try {
String[] parts = taskInfo.split(" \\(by: ");
String description = parts[0];
String by = parts[1].substring(0, parts[1].length() - 1);
return new DeadLine(description, by);
} catch (Exception e) {
throw new DukeException("Error parsing Deadline task: " + taskInfo);
}
}

/**
* Creates an Event task based on task information.
*
* @param taskInfo The task information string containing the task description and event details.
* @return An Event task instance based on the task information.
* @throws DukeException If there is an issue parsing the task information.
*/
private static Task createEventTask(String taskInfo) throws DukeException {
try {
String[] parts = taskInfo.split(" \\(from: ");
String description = parts[0];
String[] dateParts = parts[1].split(" to ");
String from = dateParts[0];
String to = dateParts[1].substring(0, dateParts[1].length() - 1);
return new Event(description, from, to);
} catch (Exception e) {
throw new DukeException("Error parsing Event task: " + taskInfo);
}
}

/**
* Parses a user input string and creates the corresponding `Command` object.
*
Expand All @@ -100,27 +127,100 @@ public static Command parse(String input) throws DukeException {
case "list":
return new ListCommand();
case "todo":
String todoDesc = input.substring(5).trim(); // Extract the description
return createToDoCommand(input);
case "add":
return createAddCommand(input);
case "deadline":
return createDeadLineCommand(input);
case "event":
return createEventCommand(input);
case "unmark":
return createUnmarkCommand(input);
case "mark":
return createMarkCommand(input);
case "echo":
return createEchoCommand(input);
case "delete":
return createDeleteCommand(input);
case "help":
return new HelpCommand();
case "find":
return createFindCommand(input);
default:
throw new DukeException("I'm sorry, but I don't understand that command.");
}
}

/**
* Creates a ToDoCommand based on user input.
*
* @param input The user input string containing the command and description.
* @return A ToDoCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static ToDoCommand createToDoCommand(String input) throws DukeException {
try {
String todoDesc = input.substring(5).trim();
Task todoTask = new ToDo(todoDesc);
return new ToDoCommand(todoTask);
case "add":
} catch (Exception e) {
throw new DukeException("Error parsing ToDo command: " + input);
}
}

/**
* Creates an AddCommand based on user input.
*
* @param input The user input string containing the command and description.
* @return An AddCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static AddCommand createAddCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please specify a task to add.");
}
String addDesc = input.substring(4).trim();
Task addTask = new Add(addDesc);
return new AddCommand(addTask);
case "deadline":
String deadlineDesc = input.substring(8).trim(); // Extract the description
} catch (Exception e) {
throw new DukeException("Error parsing Add command: " + input);
}
}

/**
* Creates a DeadLineCommand based on user input.
*
* @param input The user input string containing the command and description.
* @return A DeadLineCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static DeadLineCommand createDeadLineCommand(String input) throws DukeException {
try {
String deadlineDesc = input.substring(8).trim();
if (!deadlineDesc.contains("/by")) {
throw new DukeException("The deadline command should include '/by'.");
}
String[] deadlineParts = deadlineDesc.split("/by");
String description = deadlineParts[0].trim();
String by = deadlineParts[1].trim();
return new DeadLineCommand(description, by);
case "event":
String eventDesc = input.substring(5).trim(); // Extract the description
} catch (Exception e) {
throw new DukeException("Error parsing Deadline command: " + input);
}
}

/**
* Creates an EventCommand based on user input.
*
* @param input The user input string containing the command and event description.
* @return An EventCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static EventCommand createEventCommand(String input) throws DukeException {
try {
String eventDesc = input.substring(5).trim();
if (!eventDesc.contains("/from") || !eventDesc.contains("/to")) {
throw new DukeException("The event command should include '/from' and '/to'.");
}
Expand All @@ -130,40 +230,108 @@ public static Command parse(String input) throws DukeException {
String from = dateParts[0].trim();
String to = dateParts[1].trim();
return new EventCommand(eventDescription, from, to);
case "unmark":
if (words.length < 2) {
throw new DukeException("Please specify a task number to unmark.");
}
int taskNumberToUnmark = Integer.parseInt(words[1]) - 1; // Assuming tasks are 1-based
return new UnmarkCommand(taskNumberToUnmark);
case "mark":
} catch (Exception e) {
throw new DukeException("Error parsing Event command: " + input);
}
}

/**
* Creates a MarkCommand based on user input.
*
* @param input The user input string containing the command and task number to mark as done.
* @return A MarkCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static MarkCommand createMarkCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please specify a task number to mark as done.");
}
int taskNumberToMark = Integer.parseInt(words[1]) - 1; // Assuming tasks are 1-based
int taskNumberToMark = Integer.parseInt(words[1]) - 1;
return new MarkCommand(taskNumberToMark);
case "echo":
} catch (Exception e) {
throw new DukeException("Error parsing Mark command: " + input);
}
}

/**
* Creates an UnmarkCommand based on user input.
*
* @param input The user input string containing the command and task number to unmark.
* @return An UnmarkCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static UnmarkCommand createUnmarkCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please specify a task number to unmark.");
}
int taskNumberToUnmark = Integer.parseInt(words[1]) - 1;
return new UnmarkCommand(taskNumberToUnmark);
} catch (Exception e) {
throw new DukeException("Error parsing Unmark command: " + input);
}
}

/**
* Creates an EchoCommand based on user input.
*
* @param input The user input string containing the command and text for the echo.
* @return An EchoCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static EchoCommand createEchoCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please provide text for the echo command.");
}
String echoText = input.substring(5).trim(); // Extract the text
String echoText = input.substring(5).trim();
return new EchoCommand(echoText);
case "delete":
} catch (Exception e) {
throw new DukeException("Error parsing Echo command: " + input);
}
}

/**
* Creates a DeleteCommand based on user input.
*
* @param input The user input string containing the command and task index to delete.
* @return A DeleteCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static DeleteCommand createDeleteCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please provide me a task index to delete");
}
int taskNumToDel = Integer.parseInt(words[1]) - 1;
return new DeleteCommand(taskNumToDel);
case "help":
return new HelpCommand();
case "find":
} catch (Exception e) {
throw new DukeException("Error parsing Delete command: " + input);
}
}

/**
* Creates a FindCommand based on user input.
*
* @param input The user input string containing the command and keyword for searching.
* @return A FindCommand instance based on the user input.
* @throws DukeException If there is an issue parsing the input.
*/
private static FindCommand createFindCommand(String input) throws DukeException {
try {
String[] words = input.split(" ");
if (words.length < 2) {
throw new DukeException("Please provide a keyword for the find command.");
}
String keyword = input.substring(5).trim();
return new FindCommand(keyword);
default:
throw new DukeException("I'm sorry, but I don't understand that command.");
} catch (Exception e) {
throw new DukeException("Error parsing Find command: " + input);
}
}
}
25 changes: 14 additions & 11 deletions src/main/java/Ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
*/
public class Ui {

private static final String HELP_DESK = "List of Commands: Add, Deadline, Event, Todo, Echo,"
+ "Mark, Unmark, Delete, Find, Bye\n"
+ "1. Add - Add a task to the list\n"
+ "2. Deadline - Add a task with a deadline\n"
+ "3. Event - Add an event task\n"
+ "4. Todo - Add a todo task\n"
+ "5. Echo - Echo a message\n"
+ "6. Mark - Mark a task as done\n"
+ "7. Unmark - Unmark a task as done\n"
+ "8. Delete - Delete a task\n"
+ "9. Find - Find tasks by keyword\n"
+ "10. Bye - Exit the program";

/**
* Displays a welcome message to the user when the application starts.
*
Expand Down Expand Up @@ -49,17 +62,7 @@ public String showError(DukeException e) {
* @return A list of available commands.
*/
public String showCommands() {
return "List of Commands: Add, Deadline, Event, Todo, Echo, Mark, Unmark, Delete, Find, Bye\n"
+ "1. Add - Add a task to the list\n"
+ "2. Deadline - Add a task with a deadline\n"
+ "3. Event - Add an event task\n"
+ "4. Todo - Add a todo task\n"
+ "5. Echo - Echo a message\n"
+ "6. Mark - Mark a task as done\n"
+ "7. Unmark - Unmark a task as done\n"
+ "8. Delete - Delete a task\n"
+ "9. Find - Find tasks by keyword\n"
+ "10. Bye - Exit the program";
return HELP_DESK;
}

/**
Expand Down

0 comments on commit 7340752

Please sign in to comment.