Skip to content

Commit

Permalink
Refactor code for shorter methods and follow Java Checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
freddychenyouren2 committed Sep 20, 2023
1 parent 6ba4bca commit e403757
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 43 deletions.
2 changes: 1 addition & 1 deletion data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
D | 0 | test dates correct deadline | 2023-10-21 | null
E | 1 | correct event timings | 2222-11-11 | 2222-11-13
E | 0 | correct event timings | 2222-11-11 | 2222-11-13
D | 1 | Dead Package bruh | 1111-11-11 | null
T | 0 | After Checkstyle | null | null
D | 0 | VIEWSCHEDULE BRO | 2023-09-14 | null
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private DialogBox(String text, Image img) {

dialog.setText(text);
dialog.setMinHeight(Region.USE_PREF_SIZE);
Circle circleClip = new Circle(40,30,30);
Circle circleClip = new Circle(40, 30, 30);
displayPicture.setImage(img);
displayPicture.setClip(circleClip);
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/duke/util/Parser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package duke.util;

import java.io.IOException;
import java.time.LocalDate;

import duke.Duke;
import duke.exception.EmptyDescriptionException;
import duke.exception.InvalidCommandException;
import duke.exception.InvalidDateException;
Expand All @@ -22,8 +20,6 @@
* @author Freddy Chen You Ren
*/
public class Parser {
//60 underscores.
private static final String HORIZONTAL_LINE = " ____________________________________________________________";
private TaskList taskList;
private Ui ui;

Expand Down Expand Up @@ -68,9 +64,10 @@ private String getCommand(String line) {
* - find (keyword}: to find the list of tasks that contains the corresponding keyword.
* - deadline {taskname} /by {time}: to add a new task as a deadline task.
* - todo {taskname}: to add a new task as a to-do item. (No need to provide time).
* - event {taskname} /from {starttime} /to {endtime}: to add a new task as an event task (with given start time and end time).
* - event {taskname} /from {starttime} /to {endtime}: to add a new task as an event task.
* - view {date}: list down the tasks that are to be done on that day.
* Note that the commands are not case-sensitive. For instance: "BYE", "ByE", "bYe" will all be treated as the "bye" command.
* Note that the commands are not case-sensitive.
* For instance: "BYE", "ByE", "bYe" will all be treated as the "bye" command.
*
* @param userInput The user's input to be parsed.
*/
Expand Down Expand Up @@ -138,11 +135,12 @@ private String handleFindTask(String userInput) {
}
private String handleException(Exception e) {
if (e instanceof IllegalArgumentException) {
InvalidCommandException exception = new InvalidCommandException("I'm sorry, but I don't know what that means :-(");
InvalidCommandException exception = new InvalidCommandException(
"I'm sorry, but I don't know what that means :-(");
return exception.toString();
}
if (e instanceof EmptyDescriptionException ||
e instanceof IOException || e instanceof InvalidDateException) {
if (e instanceof EmptyDescriptionException || e instanceof IOException
|| e instanceof InvalidDateException) {
return e.toString();
} else {
return "Very Invalid command! Please enter valid commands";
Expand Down
1 change: 0 additions & 1 deletion src/main/java/duke/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down
38 changes: 13 additions & 25 deletions src/main/java/duke/util/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TaskList {
//60 underscores.
protected static String HORIZONTAL_LINE = " ____________________________________________________________";
protected Storage storage;
protected ArrayList<Task> listOfTasks;
protected ArrayList<Task> listOfTasks;

/**
* Instantiates a TaskList with the given storage.
Expand Down Expand Up @@ -70,12 +70,11 @@ protected String listAllTasks() {
*
* @param deleteInput The input representing the delete command.
* It should contain task index to be deleted.
* @return The message to indicate which task has been deleted after successful deletion.
* @throws EmptyDescriptionException If there is no task index provided.
* @throws IOException If there is an error updating the data file.
* @return The message to indicate which task has been deleted after successful deletion.
*/
public String deleteTask(String deleteInput)
throws EmptyDescriptionException, IOException {
public String deleteTask(String deleteInput) throws EmptyDescriptionException, IOException {
String[] words = deleteInput.split("\\s+"); // Split input by space, put into array
StringBuilder message = new StringBuilder();

Expand All @@ -84,31 +83,23 @@ public String deleteTask(String deleteInput)
throw new EmptyDescriptionException("Please provide the task index to be deleted.");
}

//Try parsing into integer to get deleteIndex
try {
int deleteIndex = Integer.parseInt(words[1]) - 1;
if (deleteIndex >= 0 && deleteIndex < listOfTasks.size()) {
Task removedTask = listOfTasks.remove(deleteIndex);
storage.clearAllData();
storage.updateData();

assert !listOfTasks.contains(removedTask) : "Task should have been removed!";

message.append("Noted. I've removed this task:\n");
message.append(String.format("%s\n", removedTask.toString()));
message.append(String.format("Now you have %d task(s) in the list.\n", listOfTasks.size()));
System.out.println(" Noted. I've removed this Task:");
System.out.printf(" %s\n", removedTask);
System.out.printf(" Now you have %d task(s) in the list.\n", listOfTasks.size());
} else {
message.append("OOPS!!! The task index is invalid.\n");
message.append(String.format("You currently have %d task(s).\n", listOfTasks.size()));
System.out.printf(" You currently have %d task(s).\n", listOfTasks.size());
}
return message.toString();
} catch (NumberFormatException e) {
message.append("OOPS!!! Please enter the index after 'delete' command.");
message.append("For example: delete 5");
message.append("OOPS!!! Please enter the index after 'delete' command.\nFor example: delete 5");
message.append("This will remove Task 5 from your Task List, assuming you have at least 5 tasks.");
return message.toString();
}
Expand All @@ -120,8 +111,8 @@ public String deleteTask(String deleteInput)
* Prints out an error message if index of the task given is out of range or invalid.
*
* @param taskIndex The index of the Task to be marked as done.
* @throws IOException If there is an issue with updating the data in the storage file.
* @return The message to indicate that the task is marked as done.
* @throws IOException If there is an issue with updating the data in the storage file.
*/
protected String markTask(int taskIndex) throws IOException {
StringBuilder message = new StringBuilder();
Expand All @@ -130,16 +121,12 @@ protected String markTask(int taskIndex) throws IOException {
} else {
Task task = listOfTasks.get(taskIndex);
task.markAsDone();

assert listOfTasks.get(taskIndex).getStatus() : "Task must be marked done.";

storage.clearAllData();
storage.updateData();

message.append("Nice! I've marked this Task as done:\n");
message.append(String.format("\t[%s] %s\n", task.getStatusIcon(), task.getDescription()));
System.out.println(" Nice! I've marked this Task as done:");
System.out.printf(" [%s] %s\n", task.getStatusIcon(), task.getDescription());
}
return message.toString();
}
Expand All @@ -150,8 +137,8 @@ protected String markTask(int taskIndex) throws IOException {
* Prints out an error message if index of the task given is out of range or invalid.
*
* @param taskIndex The index of the Task to be marked as not done yet.
* @throws IOException If there is an issue with updating the data in the storage file.
* @return The message to indicate that the task is marked as not done yet.
* @throws IOException If there is an issue with updating the data in the storage file.
*/
protected String unmarkTask(int taskIndex) throws IOException {
StringBuilder message = new StringBuilder();
Expand All @@ -160,9 +147,7 @@ protected String unmarkTask(int taskIndex) throws IOException {
} else {
Task task = listOfTasks.get(taskIndex);
task.markAsNotDone();

assert !listOfTasks.get(taskIndex).getStatus() : "Task must be marked NOT done yet.";

storage.clearAllData();
storage.updateData();

Expand Down Expand Up @@ -222,6 +207,9 @@ public static boolean isValidDate(String testDate) {
* @return The message representing the tasks found using the keyword provided.
*/
protected String findTask(String matchingKeyword) {
if (matchingKeyword.trim().isEmpty()) {
return "Please provide the keyword you wish to find :)";
}
if (listOfTasks.isEmpty()) {
System.out.println("\t You currently have no tasks so I can't find any matching tasks :/.");
return "\t You currently have no tasks so I can't find any matching tasks :(";
Expand All @@ -237,7 +225,7 @@ protected String findTask(String matchingKeyword) {
taskCount++;
}
}

//Output matching tasks
if (taskCount > 0) {
return matchingTasks.toString();
Expand All @@ -253,7 +241,7 @@ protected String viewSchedule(String userInput) throws InvalidDateException {
if (details.length <= 1) {
return "Invalid input. Please key in `view YYYY-MM-DD` format.";
}
String requestedDate = details[1].trim(); // Get requested date
String requestedDate = details[1].trim(); // Get requested date

if (!isValidDate(requestedDate)) {
throw new InvalidDateException();
Expand All @@ -262,8 +250,8 @@ protected String viewSchedule(String userInput) throws InvalidDateException {
String requestedDateString = LocalDate.parse(requestedDate).format(DateTimeFormatter.ofPattern("MMM d yyyy"));
ArrayList<Task> scheduledTasks = printSchedule(LocalDate.parse(requestedDate));
if (scheduledTasks.size() == 0) {
return String.format("There is no tasks happening on %s. ", requestedDateString) +
"Please double check your list of tasks.";
return String.format("There is no tasks happening on %s. ", requestedDateString)
+ "Please double check your list of tasks.";
}

StringBuilder message = new StringBuilder(
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/duke/util/Ui.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package duke.util;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import duke.Duke;
import javafx.application.Platform;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
* Represents a user interface for SeeWhyAre bot.
Expand Down Expand Up @@ -33,10 +34,6 @@ protected String printHorizontalLine() {
return HORIZONTAL_LINE;
}

public Ui getUi() {
return this;
}

/**
* Displays a greeting message to the user.
*/
Expand Down

0 comments on commit e403757

Please sign in to comment.