From f9062b881f49b2d4a3a68d555465987b4b4bbe3b Mon Sep 17 00:00:00 2001 From: macareonie Date: Sun, 11 Feb 2024 22:29:15 +0800 Subject: [PATCH] Ensure code compliance with checkstyle guidelines Code is unable to pass Continuous Integration automated build checks due to violations of checkstyle requirements. Let's, * Rectify all checkstyle violations. --- src/main/java/tsundere/Tsundere.java | 7 ++- .../tsundere/exception/GeneralException.java | 3 ++ src/main/java/tsundere/gui/MainWindow.java | 30 ++++++----- src/main/java/tsundere/task/Deadline.java | 6 ++- src/main/java/tsundere/task/Event.java | 6 +-- src/main/java/tsundere/task/Storage.java | 14 ++---- src/main/java/tsundere/task/Task.java | 5 +- src/main/java/tsundere/task/TaskList.java | 50 +++++++++---------- src/main/java/tsundere/task/ToDo.java | 2 +- src/main/java/tsundere/ui/Parser.java | 5 +- src/test/java/tsundere/task/DeadlineTest.java | 4 +- src/test/java/tsundere/task/EventTest.java | 4 +- src/test/java/tsundere/task/TaskTest.java | 3 +- src/test/java/tsundere/task/ToDoTest.java | 3 +- 14 files changed, 75 insertions(+), 67 deletions(-) diff --git a/src/main/java/tsundere/Tsundere.java b/src/main/java/tsundere/Tsundere.java index 097942db7d..afc15c7349 100644 --- a/src/main/java/tsundere/Tsundere.java +++ b/src/main/java/tsundere/Tsundere.java @@ -1,7 +1,6 @@ package tsundere; import javafx.application.Application; - import tsundere.task.Storage; /** @@ -9,7 +8,7 @@ */ public class Tsundere { - public static Storage storage; + private static Storage storage; /** * Initializes Tsundere with Storage. @@ -18,6 +17,10 @@ public Tsundere() { storage = new Storage(); } + public static Storage getStorage() { + return Tsundere.storage; + } + /** * Launches the GUI application. */ diff --git a/src/main/java/tsundere/exception/GeneralException.java b/src/main/java/tsundere/exception/GeneralException.java index 4c463fbd43..81d574f671 100644 --- a/src/main/java/tsundere/exception/GeneralException.java +++ b/src/main/java/tsundere/exception/GeneralException.java @@ -1,5 +1,8 @@ package tsundere.exception; +/** + * Encapsulates an Exception that contains a general message. + */ public class GeneralException extends Exception { protected final String msg; diff --git a/src/main/java/tsundere/gui/MainWindow.java b/src/main/java/tsundere/gui/MainWindow.java index d9ca9163f3..11ce663a37 100644 --- a/src/main/java/tsundere/gui/MainWindow.java +++ b/src/main/java/tsundere/gui/MainWindow.java @@ -1,5 +1,9 @@ package tsundere.gui; +import java.io.IOException; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -11,12 +15,6 @@ import tsundere.Tsundere; import tsundere.ui.Ui; -import java.io.IOException; -import java.util.Objects; -import java.util.concurrent.TimeUnit; - - - /** * Controller for MainWindow. Provides the layout for the other controls. */ @@ -30,10 +28,10 @@ public class MainWindow extends AnchorPane { @FXML private Button sendButton; - private final Image USER_IMAGE = new Image(Objects.requireNonNull(this.getClass(). - getResourceAsStream("/images/ok.png"))); - private final Image TSUN_IMAGE = new Image(Objects.requireNonNull(this.getClass(). - getResourceAsStream("/images/chitoge.png"))); + private Image userImage = new Image(Objects.requireNonNull(this.getClass() + .getResourceAsStream("/images/ok.png"))); + private Image tsunImage = new Image(Objects.requireNonNull(this.getClass() + .getResourceAsStream("/images/chitoge.png"))); /** * Creates GUI with an initial dialog box with the greeting statement. @@ -43,7 +41,7 @@ public void initialize() { scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); dialogContainer.getChildren().addAll( DialogBox.getTsundereDialog("Don't get the wrong idea!\n" - + "I'm not doing this to help you or anything!\n", TSUN_IMAGE) + + "I'm not doing this to help you or anything!\n", tsunImage) ); } @@ -57,7 +55,7 @@ public void setTsundere(Tsundere t) { * @throws IOException if savefile cannot be created. */ private void exit() throws InterruptedException, IOException { - Tsundere.storage.saveTasksToFile(); + Tsundere.getStorage().saveTasksToFile(); TimeUnit.SECONDS.sleep(1); Platform.exit(); System.exit(0); @@ -74,9 +72,9 @@ private void handleUserInput() { if (input.equalsIgnoreCase("bye")) { dialogContainer.getChildren().addAll( - DialogBox.getUserDialog(input, USER_IMAGE), + DialogBox.getUserDialog(input, userImage), DialogBox.getTsundereDialog("Don't forget about me!\n" - + "You better come back soon!\n", TSUN_IMAGE) + + "You better come back soon!\n", tsunImage) ); try { this.exit(); @@ -89,8 +87,8 @@ private void handleUserInput() { Ui ui = new Ui(); String response = ui.chat(input); dialogContainer.getChildren().addAll( - DialogBox.getUserDialog(input, USER_IMAGE), - DialogBox.getTsundereDialog(response, TSUN_IMAGE) + DialogBox.getUserDialog(input, userImage), + DialogBox.getTsundereDialog(response, tsunImage) ); userInput.clear(); } diff --git a/src/main/java/tsundere/task/Deadline.java b/src/main/java/tsundere/task/Deadline.java index f15ec73193..7f5a13cbb5 100644 --- a/src/main/java/tsundere/task/Deadline.java +++ b/src/main/java/tsundere/task/Deadline.java @@ -1,15 +1,17 @@ package tsundere.task; -import java.time.format.DateTimeFormatter; import java.time.LocalDate; +import java.time.format.DateTimeFormatter; /** * Encapsulates a Deadline object. */ public class Deadline extends Task { + + private static final String TYPE = "D"; protected String by; protected LocalDate date; - protected static final String TYPE = "D"; + /** * Initializes Deadline task with its name. diff --git a/src/main/java/tsundere/task/Event.java b/src/main/java/tsundere/task/Event.java index 99074ac635..597018e211 100644 --- a/src/main/java/tsundere/task/Event.java +++ b/src/main/java/tsundere/task/Event.java @@ -4,11 +4,11 @@ * Encapsulates an Event object. */ public class Event extends Task { + + private static final String TYPE = "E"; protected String from; protected String to; - protected static final String TYPE = "E"; - /** * Initializes Event task with its name. * @@ -39,6 +39,6 @@ public String toSaveString() { */ @Override public String toString() { - return "[E]" + super.toString() + " (from: " + from + " to: " + to +")"; + return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; } } diff --git a/src/main/java/tsundere/task/Storage.java b/src/main/java/tsundere/task/Storage.java index 47457ec1ec..d3edc4d32d 100644 --- a/src/main/java/tsundere/task/Storage.java +++ b/src/main/java/tsundere/task/Storage.java @@ -5,7 +5,6 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; - import java.util.ArrayList; import java.util.Scanner; @@ -17,9 +16,6 @@ public class Storage { private static final String FILEPATH = "./data/data.txt"; - private final String SAVED_SUCCESS_MSG = "Your session has been saved."; - private final String LOADED_SUCCESS_MSG = "Your previous session has been restored. Enjoy your chat."; - private final String LOADED_FAILURE_MSG = "Something went wrong with loading your previous session data!"; /** * Initializes Storage. @@ -30,7 +26,7 @@ public Storage() { new File("./data").mkdirs(); TaskList.taskList = loadTasksFromFile(); } catch (IOException | GeneralException e) { - System.out.println(LOADED_FAILURE_MSG); + System.out.println("Something went wrong with loading your previous session data!"); } } @@ -48,7 +44,7 @@ public void saveTasksToFile() throws IOException { for (Task task : TaskList.taskList) { pw.println(task.toSaveString()); } - System.out.println(SAVED_SUCCESS_MSG); + System.out.println("Your session has been saved."); } catch (FileNotFoundException e) { @@ -56,7 +52,7 @@ public void saveTasksToFile() throws IOException { if (file.createNewFile()) { saveTasksToFile(); - System.out.println(SAVED_SUCCESS_MSG); + System.out.println("Your session has been saved."); } else { throw new IOException("Unable to create the file: " + FILEPATH); } @@ -86,7 +82,7 @@ private ArrayList loadTasksFromFile() throws IOException, GeneralException Task task = parseTaskFromSaveString(line); tasks.add(task); } - System.out.println(LOADED_SUCCESS_MSG); + System.out.println("Your previous session has been restored. Enjoy your chat."); } return tasks; @@ -154,4 +150,4 @@ private static Task parseTaskFromSaveString(String saveString) throws GeneralExc } return task; } -} \ No newline at end of file +} diff --git a/src/main/java/tsundere/task/Task.java b/src/main/java/tsundere/task/Task.java index 873a1739c3..7e2bbfb4c1 100644 --- a/src/main/java/tsundere/task/Task.java +++ b/src/main/java/tsundere/task/Task.java @@ -1,8 +1,9 @@ package tsundere.task; -import tsundere.exception.GeneralException; import java.util.ArrayList; +import tsundere.exception.GeneralException; + /** * Encapsulates a Task object. */ @@ -123,4 +124,4 @@ public String toString() { return "[" + this.getStatusIcon() + "] " + this.description + tags; } -} \ No newline at end of file +} diff --git a/src/main/java/tsundere/task/TaskList.java b/src/main/java/tsundere/task/TaskList.java index 6b8f0f04e3..58e4d514ef 100644 --- a/src/main/java/tsundere/task/TaskList.java +++ b/src/main/java/tsundere/task/TaskList.java @@ -1,22 +1,22 @@ package tsundere.task; +import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; -import java.time.LocalDate; - import java.util.ArrayList; -import tsundere.ui.Parser; import tsundere.exception.GeneralException; +import tsundere.ui.Parser; /** * Encapsulates a TaskList object that contains all Task objects. */ public class TaskList { - public static ArrayList taskList = new ArrayList<>(); + protected static ArrayList taskList = new ArrayList<>(); + protected static String name = Parser.getName(); - private static final String INVALID_TASK_NUMBER_INPUTTED_MSG = "Im pretty sure that's the wrong task number! " + - "Check again!"; + private static final String INVALID_TASK_NUMBER_INPUTTED_MSG = "Im pretty sure that's the wrong task number! " + + "Check again!"; /** * Sets selected Task's status to undone. @@ -30,17 +30,17 @@ public static String unmarkTask() throws GeneralException { throw new GeneralException("What you tryna unmark huh?"); } try { - Task t = TaskList.taskList.get(Integer.parseInt(Parser.name.substring(7, 8)) - 1); + Task t = TaskList.taskList.get(Integer.parseInt(TaskList.name.substring(7, 8)) - 1); if (t.getStatusIcon().equals(" ")) { - return("You haven't even started this task dummy!"); + return "You haven't even started this task dummy!"; } else { t.unMarkTask(); - return(t.toString()); + return t.toString(); } } catch (NumberFormatException e) { throw new GeneralException("Can't you even remember the proper format for this?\n" + "unmark [task no.]"); - } catch (IndexOutOfBoundsException e ) { + } catch (IndexOutOfBoundsException e) { throw new GeneralException(INVALID_TASK_NUMBER_INPUTTED_MSG); } @@ -58,12 +58,12 @@ public static String markTask() throws GeneralException { throw new GeneralException("What you tryna mark huh?"); } try { - Task t = TaskList.taskList.get(Integer.parseInt(Parser.name.substring(5, 6)) - 1); + Task t = TaskList.taskList.get(Integer.parseInt(TaskList.name.substring(5, 6)) - 1); if (t.isDone) { - return("You already finished this!"); + return "You already finished this!"; } else { t.markTaskAsDone(); - return(t.toString()); + return t.toString(); } } catch (NumberFormatException e) { throw new GeneralException("Can't you even remember the proper format for this?\n" @@ -87,7 +87,7 @@ public static String deleteTask() throws GeneralException { } try { - int idx = Integer.parseInt(Parser.name.substring(7, 8)) - 1; + int idx = Integer.parseInt(TaskList.name.substring(7, 8)) - 1; Task t = TaskList.taskList.get(idx); TaskList.taskList.remove(idx); return getListSize("deleted", t); @@ -129,7 +129,7 @@ public static String listTasks() throws GeneralException { public static String addToDoTask() throws GeneralException { try { - String todo = Parser.name.split(" ", 2)[1]; + String todo = TaskList.name.split(" ", 2)[1]; Task t = new ToDo(todo); TaskList.taskList.add(t); @@ -150,7 +150,7 @@ public static String addToDoTask() throws GeneralException { public static String addEventTask() throws GeneralException { try { - String event = Parser.name.split(" ", 2)[1]; + String event = TaskList.name.split(" ", 2)[1]; String[] x = event.split(","); Task t = new Event(x[0], x[1].trim().split(" ", 2)[1], x[2].trim().split(" ", 2)[1]); @@ -172,7 +172,7 @@ public static String addEventTask() throws GeneralException { public static String addDeadlineTask() throws GeneralException { try { - String deadline = Parser.name.split(" ", 2)[1]; + String deadline = TaskList.name.split(" ", 2)[1]; String[] x = deadline.split(","); LocalDate d1 = LocalDate.parse(x[1].trim().split(" ", 2)[1]); String date = d1.format(DateTimeFormatter.ofPattern("MMM d yyyy")); @@ -203,7 +203,7 @@ public static String findTasks() throws GeneralException { StringBuilder response = new StringBuilder(); try { int count = 0; - String keyword = Parser.name.split(" ", 2)[1]; + String keyword = TaskList.name.split(" ", 2)[1]; for (int i = 0; i < size; i++) { Task t = TaskList.taskList.get(i); if (t.description.contains(keyword)) { @@ -212,7 +212,7 @@ public static String findTasks() throws GeneralException { } } if (count == 0) { - return("I couldn't find anything related to that!"); + return "I couldn't find anything related to that!"; } else { return response.toString(); } @@ -233,7 +233,7 @@ public static String tagTask() throws GeneralException { throw new GeneralException("Theres's nothing to find here!"); } try { - String[] x = Parser.name.split(" "); + String[] x = TaskList.name.split(" "); int idx = Integer.parseInt(x[1]) - 1; String tag = x[2]; Task t = TaskList.taskList.get(idx); @@ -258,7 +258,7 @@ public static String untagTask() throws GeneralException { throw new GeneralException("Theres's nothing to find here!"); } try { - String[] x = Parser.name.split(" "); + String[] x = TaskList.name.split(" "); int idx = Integer.parseInt(x[1]) - 1; String tag = x[2]; Task t = TaskList.taskList.get(idx); @@ -280,13 +280,13 @@ public static String untagTask() throws GeneralException { public static String getListSize(String str, Task t) { String response = ""; int size = TaskList.taskList.size(); - response += ("Noted..."); - response += (" " + t.toString() + " has been " + str + "\n"); + response += "Noted..."; + response += " " + t.toString() + " has been " + str + "\n"; if (size > 0) { - response += ("Get to work! You still have " + size + " " + (size > 1 ? "tasks" : "task") + " left!"); + response += "Get to work! You still have " + size + " " + (size > 1 ? "tasks" : "task") + " left!"; } else { - response += ("You finally have free time?"); + response += "You finally have free time?"; } return response; diff --git a/src/main/java/tsundere/task/ToDo.java b/src/main/java/tsundere/task/ToDo.java index 7835d2e561..38d9fb7806 100644 --- a/src/main/java/tsundere/task/ToDo.java +++ b/src/main/java/tsundere/task/ToDo.java @@ -5,7 +5,7 @@ */ public class ToDo extends Task { - protected static final String TYPE = "T"; + private static final String TYPE = "T"; /** * Initializes ToDo task with its name. diff --git a/src/main/java/tsundere/ui/Parser.java b/src/main/java/tsundere/ui/Parser.java index b96c964749..35d8fc0735 100644 --- a/src/main/java/tsundere/ui/Parser.java +++ b/src/main/java/tsundere/ui/Parser.java @@ -8,11 +8,14 @@ */ public class Parser { - public static String name; + private static String name; public Parser(String name) { Parser.name = name; } + public static String getName() { + return Parser.name; + } /** * Parses command given and executes corresponding action. * diff --git a/src/test/java/tsundere/task/DeadlineTest.java b/src/test/java/tsundere/task/DeadlineTest.java index f9e2d1c3ff..66fc6390bf 100644 --- a/src/test/java/tsundere/task/DeadlineTest.java +++ b/src/test/java/tsundere/task/DeadlineTest.java @@ -1,9 +1,9 @@ package tsundere.task; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + public class DeadlineTest { @Test public void deadlineToString_success() { diff --git a/src/test/java/tsundere/task/EventTest.java b/src/test/java/tsundere/task/EventTest.java index 4fcc9f563b..59307b6ff9 100644 --- a/src/test/java/tsundere/task/EventTest.java +++ b/src/test/java/tsundere/task/EventTest.java @@ -1,9 +1,9 @@ package tsundere.task; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + public class EventTest { @Test public void eventToString_success() { diff --git a/src/test/java/tsundere/task/TaskTest.java b/src/test/java/tsundere/task/TaskTest.java index fb492c98c6..3b71e4fa05 100644 --- a/src/test/java/tsundere/task/TaskTest.java +++ b/src/test/java/tsundere/task/TaskTest.java @@ -1,8 +1,9 @@ package tsundere.task; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; public class TaskTest { @Test diff --git a/src/test/java/tsundere/task/ToDoTest.java b/src/test/java/tsundere/task/ToDoTest.java index d4f6ea7c68..5b8ebd6545 100644 --- a/src/test/java/tsundere/task/ToDoTest.java +++ b/src/test/java/tsundere/task/ToDoTest.java @@ -1,8 +1,9 @@ package tsundere.task; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; public class ToDoTest { @Test