From 54290c6325b7a3c4bdcc80dd79b9c08a83808e96 Mon Sep 17 00:00:00 2001 From: TyrusLye Date: Mon, 23 Oct 2023 16:35:30 +0800 Subject: [PATCH 1/2] Adding more OOP --- build.gradle | 4 +- docs/README.md | 22 +-- .../java/{ => URBOI_PACKIN}/Launcher.java | 2 + src/main/java/{ => URBOI_PACKIN}/Main.java | 9 +- .../ResponseController.java} | 158 ++---------------- src/main/java/URBOI_PACKIN/Task.java | 48 ++++++ .../java/URBOI_PACKIN/TaskTypes/Deadline.java | 26 +++ .../java/URBOI_PACKIN/TaskTypes/Event.java | 27 +++ .../java/URBOI_PACKIN/TaskTypes/Todo.java | 22 +++ .../java/{ => URBOI_PACKIN/UI}/DialogBox.java | 2 + .../{ => URBOI_PACKIN/UI}/MainWindow.java | 18 +- src/main/resources/view/MainWindow.fxml | 2 +- tasks.txt | Bin 1616 -> 60 bytes text-ui-test/runtest.bat | 2 +- 14 files changed, 168 insertions(+), 174 deletions(-) rename src/main/java/{ => URBOI_PACKIN}/Launcher.java (90%) rename src/main/java/{ => URBOI_PACKIN}/Main.java (70%) rename src/main/java/{Duke.java => URBOI_PACKIN/ResponseController.java} (60%) create mode 100644 src/main/java/URBOI_PACKIN/Task.java create mode 100644 src/main/java/URBOI_PACKIN/TaskTypes/Deadline.java create mode 100644 src/main/java/URBOI_PACKIN/TaskTypes/Event.java create mode 100644 src/main/java/URBOI_PACKIN/TaskTypes/Todo.java rename src/main/java/{ => URBOI_PACKIN/UI}/DialogBox.java (98%) rename src/main/java/{ => URBOI_PACKIN/UI}/MainWindow.java (87%) diff --git a/build.gradle b/build.gradle index 8f5858f168..ba99492fd9 100644 --- a/build.gradle +++ b/build.gradle @@ -43,8 +43,8 @@ test { } application { - mainClass.set("seedu.duke.Duke") - mainClassName= "Launcher" + mainClass.set("URBOI_PACKIN") + mainClassName= "URBOI_PACKIN.Launcher" } shadowJar { diff --git a/docs/README.md b/docs/README.md index 06ae17be5c..d2bd60de06 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,23 +8,23 @@ URBOI is a simple command-line chatbot designed to help you manage your tasks - [Getting Started](#getting-started) - [Adding Tasks](#adding-tasks) - [Adding a To-Do](#adding-a-to-do) - - [Adding a Deadline](#adding-a-deadline) - - [Adding an Event](#adding-an-event) + - [Adding a URBOI_PACKIN.TaskTypes.Deadline](#adding-a-deadline) + - [Adding an URBOI_PACKIN.TaskTypes.Event](#adding-an-event) - [Listing Tasks](#listing-tasks) - [Marking Tasks as Done](#marking-tasks-as-done) - [Deleting Tasks](#deleting-tasks) - [Searching for Tasks](#searching-for-tasks) -- [Exiting Duke](#exiting-duke) +- [Exiting URBOI_PACKIN.ResponseController](#exiting-duke) - [Saving Your Tasks](#saving-your-tasks) ## Getting Started 1. Clone or download the URBOI project code from [GitHub](https://github.com/TyrusLye/ip/releases/tag/A-Release). -2. Run the `URBOI.java` file to start the Duke chatbot. +2. Run the `URBOI.java` file to start the URBOI_PACKIN.ResponseController chatbot. ## Adding Tasks -Duke allows you to add three types of tasks: To-Do, Deadline, and Event. +URBOI_PACKIN.ResponseController allows you to add three types of tasks: To-Do, URBOI_PACKIN.TaskTypes.Deadline, and URBOI_PACKIN.TaskTypes.Event. ### Adding a To-Do @@ -36,7 +36,7 @@ todo [description] Replace `[task description]` with a brief description of the to-do task you want to add. -### Adding a Deadline +### Adding a URBOI_PACKIN.TaskTypes.Deadline To add a deadline task, use the following command: @@ -46,7 +46,7 @@ deadline [task description] /by [date and time] Replace `[task description]` with a description of the deadline task and `[date and time]` with the date and time the task is due in the format `d/M/yyyy HHmm`. -### Adding an Event +### Adding an URBOI_PACKIN.TaskTypes.Event To add an event task, use the following command: @@ -103,14 +103,14 @@ To search for tasks containing a specific keyword, use the following command: find [keyword] ``` -Replace `[keyword]` with the keyword you want to search for. Duke will display a list of tasks matching your keyword. +Replace `[keyword]` with the keyword you want to search for. URBOI_PACKIN.ResponseController will display a list of tasks matching your keyword. -## 7. Exiting Duke +## 7. Exiting URBOI_PACKIN.ResponseController -To exit Duke, simply enter: +To exit URBOI_PACKIN.ResponseController, simply enter: ```plaintext bye ``` -Duke will bid you farewell and close. \ No newline at end of file +URBOI_PACKIN.ResponseController will bid you farewell and close. \ No newline at end of file diff --git a/src/main/java/Launcher.java b/src/main/java/URBOI_PACKIN/Launcher.java similarity index 90% rename from src/main/java/Launcher.java rename to src/main/java/URBOI_PACKIN/Launcher.java index 11dbf00c62..962ac2fe78 100644 --- a/src/main/java/Launcher.java +++ b/src/main/java/URBOI_PACKIN/Launcher.java @@ -1,3 +1,5 @@ +package URBOI_PACKIN; + import javafx.application.Application; /** diff --git a/src/main/java/Main.java b/src/main/java/URBOI_PACKIN/Main.java similarity index 70% rename from src/main/java/Main.java rename to src/main/java/URBOI_PACKIN/Main.java index 318bd74bbb..6196259ef1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/URBOI_PACKIN/Main.java @@ -1,5 +1,8 @@ +package URBOI_PACKIN; + import java.io.IOException; +import URBOI_PACKIN.UI.MainWindow; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -7,11 +10,11 @@ import javafx.stage.Stage; /** - * A GUI for Duke using FXML. + * A GUI for URBOI_PACKIN.ResponseController using FXML. */ public class Main extends Application { - private Duke duke = new Duke(); + private ResponseController responseController = new ResponseController(); @Override public void start(Stage stage) { @@ -20,7 +23,7 @@ public void start(Stage stage) { AnchorPane ap = fxmlLoader.load(); Scene scene = new Scene(ap); stage.setScene(scene); - fxmlLoader.getController().setDuke(duke); + fxmlLoader.getController().setDuke(responseController); stage.show(); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/Duke.java b/src/main/java/URBOI_PACKIN/ResponseController.java similarity index 60% rename from src/main/java/Duke.java rename to src/main/java/URBOI_PACKIN/ResponseController.java index 288e77d7f2..8c95df38fe 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/URBOI_PACKIN/ResponseController.java @@ -1,44 +1,18 @@ -import java.time.LocalDate; +package URBOI_PACKIN; + +import URBOI_PACKIN.TaskTypes.Deadline; +import URBOI_PACKIN.TaskTypes.Event; +import URBOI_PACKIN.TaskTypes.Todo; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Scanner; import java.util.ArrayList; import java.io.*; -public class Duke { +public class ResponseController { //private static final String FILE_PATH = "src/main/java/tasks.txt"; private static final String FILE_PATH = "tasks.txt/"; - private static String response= ""; - /** - * Main method to start the Duke application. - * - * @param args Command-line arguments. - */ - public static void main(String[] args) { - //Scanner scanner = new Scanner(System.in); - String logo = "UUUUUUUU UUUUUUUURRRRRRRRRRRRRRRRR BBBBBBBBBBBBBBBBB OOOOOOOOO IIIIIIIIII\n" - +"U::::::U U::::::UR::::::::::::::::R B::::::::::::::::B OO:::::::::OO I::::::::I\n" - +"U::::::U U::::::UR::::::RRRRRR:::::R B::::::BBBBBB:::::B OO:::::::::::::OO I::::::::I\n" - +"UU:::::U U:::::UURR:::::R R:::::RBB:::::B B:::::BO:::::::OOO:::::::OII::::::II\n" - +" U:::::U U:::::U R::::R R:::::R B::::B B:::::BO::::::O O::::::O I::::I \n" - +" U:::::D D:::::U R::::R R:::::R B::::B B:::::BO:::::O O:::::O I::::I \n" - +" U:::::D D:::::U R::::RRRRRR:::::R B::::BBBBBB:::::B O:::::O O:::::O I::::I \n" - +" U:::::D D:::::U R:::::::::::::RR B:::::::::::::BB O:::::O O:::::O I::::I \n" - +" U:::::D D:::::U R::::RRRRRR:::::R B::::BBBBBB:::::B O:::::O O:::::O I::::I \n" - +" U:::::D D:::::U R::::R R:::::R B::::B B:::::BO:::::O O:::::O I::::I \n" - +" U:::::D D:::::U R::::R R:::::R B::::B B:::::BO:::::O O:::::O I::::I \n" - +" U::::::U U::::::U R::::R R:::::R B::::B B:::::BO::::::O O::::::O I::::I \n" - +" U:::::::UUU:::::::U RR:::::R R:::::RBB:::::BBBBBB::::::BO:::::::OOO:::::::OII::::::II\n" - +" UU:::::::::::::UU R::::::R R:::::RB:::::::::::::::::B OO:::::::::::::OO I::::::::I\n" - +" UU:::::::::UU R::::::R R:::::RB::::::::::::::::B OO:::::::::OO I::::::::I\n" - +" UUUUUUUUU RRRRRRRR RRRRRRRBBBBBBBBBBBBBBBBB OOOOOOOOO IIIIIIIIII\n"; - - System.out.println("Wazzup! It's\n" + logo); - System.out.println("What can I do for you mah man?"); - System.out.println("____________________________________________________________"); - - } - /** * Process user input and generate a response. @@ -53,7 +27,6 @@ public String getResponse(String command) { try{ if (command.equalsIgnoreCase("bye")) { response.append("Bye. Hope to see you again soon!"); - } else if (command.equalsIgnoreCase("list")) { response.append("Here are the tasks in your list:\n"); for (int i = 0; i < tasks.size(); i++) { @@ -71,7 +44,7 @@ public String getResponse(String command) { // Parse the date and time in the format d/M/yyyy HHmm String[] parts = command.split(" /by "); if (parts.length < 2) { - throw new DukeException("Deadline command must include a date."); + throw new DukeException("URBOI_PACKIN.TaskTypes.Deadline command must include a date."); } String description = parts[0].substring(9).trim(); LocalDateTime dateTime = LocalDateTime.parse(parts[1], DateTimeFormatter.ofPattern("d/M/yyyy HHmm")); @@ -124,9 +97,6 @@ public String getResponse(String command) { return response.toString(); } - private static String formatDate(LocalDate date) { - return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); - } /** * Loads tasks from a file into the task list. * @@ -153,10 +123,10 @@ private static void loadTasksFromFile(ArrayList tasks) { } /** - * Creates a Task object from a line of text in the specified format. + * Creates a URBOI_PACKIN.Task object from a line of text in the specified format. * * @param line The line of text containing task details. - * @return A Task object representing the task described in the line, or null if parsing fails. + * @return A URBOI_PACKIN.Task object representing the task described in the line, or null if parsing fails. */ private static Task createTaskFromLine(String line) { String[] parts = line.split(" \\| "); @@ -234,114 +204,8 @@ private static void saveTasksToFile(ArrayList tasks) { } - -/** - * Represents a task that can be added to the task list. - */ -class Task { - protected String description; - protected boolean isDone; - - protected LocalDateTime date; - - public String toFileString() { - return ""; - } - public String formatDate() { - return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); - } - public Task(String description, LocalDateTime date) { - this.description = description; - this.isDone = false; - this.date = date; - } - public String getStatusIcon() { - return (isDone ? "[X]" : "[ ]"); // Return a tick or cross symbol cuz im lazy like that, or its easier. idk - } - /** - * Get the description of the task. - * - * @return The description of the task. - */ - public String getDescription() { - return description; - } - public boolean isDone() { - return isDone; - } - public void markDone() { - isDone = true; - } - public void markNotDone() { - isDone = false; - } - @Override - public String toString() { - return "[" + (isDone ? "X" : " ") + "] " + description; - } -} - -/** - * Represents a Todo task. - */ -class Todo extends Task { - public Todo(String description) { - super(description,null); - } - @Override - public String toFileString() { - return "T | " + (isDone ? "1" : "0") + " | " + description; - } - - @Override - public String toString() { - return "[T]" + super.toString(); - } -} - -/** - * Represents a Deadline task. - */ -class Deadline extends Task { - protected String by; - - public Deadline(String description, LocalDateTime date) { - super(description, date); - } - @Override - public String toFileString() { - return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + date; - } - @Override - public String toString() { - return "[D]" + super.toString() + " (by: " + date + ")"; - } -} - -/** - * Represents an Event task. - */ -class Event extends Task { - protected String from; - protected String to; - - public Event(String description, String from, String to) { - super(description,null); - this.from = from; - this.to = to; - } - @Override - public String toFileString() { - return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; - } - @Override - public String toString() { - return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; - } -} - /** - * Custom exception class for Duke-specific exceptions. + * Custom exception class for URBOI_PACKIN.ResponseController-specific exceptions. */ class DukeException extends Exception { public DukeException(String message) { diff --git a/src/main/java/URBOI_PACKIN/Task.java b/src/main/java/URBOI_PACKIN/Task.java new file mode 100644 index 0000000000..ba45c7bc5f --- /dev/null +++ b/src/main/java/URBOI_PACKIN/Task.java @@ -0,0 +1,48 @@ +package URBOI_PACKIN; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class Task { + + protected String description; + protected boolean isDone; + + protected LocalDateTime date; + + public String toFileString() { + return ""; + } + public String formatDate() { + return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); + } + public Task(String description, LocalDateTime date) { + this.description = description; + this.isDone = false; + this.date = date; + } + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // Return a tick or cross symbol cuz im lazy like that, or its easier. idk + } + /** + * Get the description of the task. + * + * @return The description of the task. + */ + public String getDescription() { + return description; + } + public boolean isDone() { + return isDone; + } + public void markDone() { + isDone = true; + } + public void markNotDone() { + isDone = false; + } + @Override + public String toString() { + return "[" + (isDone ? "X" : " ") + "] " + description; + } +} diff --git a/src/main/java/URBOI_PACKIN/TaskTypes/Deadline.java b/src/main/java/URBOI_PACKIN/TaskTypes/Deadline.java new file mode 100644 index 0000000000..100ddd64c2 --- /dev/null +++ b/src/main/java/URBOI_PACKIN/TaskTypes/Deadline.java @@ -0,0 +1,26 @@ +package URBOI_PACKIN.TaskTypes; + +import URBOI_PACKIN.Task; + +import java.time.LocalDateTime; + +/** + * Represents a Deadline task. + */ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, LocalDateTime date) { + super(description, date); + } + + @Override + public String toFileString() { + return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + date; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + date + ")"; + } +} diff --git a/src/main/java/URBOI_PACKIN/TaskTypes/Event.java b/src/main/java/URBOI_PACKIN/TaskTypes/Event.java new file mode 100644 index 0000000000..f635b5fdfd --- /dev/null +++ b/src/main/java/URBOI_PACKIN/TaskTypes/Event.java @@ -0,0 +1,27 @@ +package URBOI_PACKIN.TaskTypes; + +import URBOI_PACKIN.Task; + +/** + * Represents an Event task. + */ +public class Event extends Task { + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description, null); + this.from = from; + this.to = to; + } + + @Override + public String toFileString() { + return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; + } +} diff --git a/src/main/java/URBOI_PACKIN/TaskTypes/Todo.java b/src/main/java/URBOI_PACKIN/TaskTypes/Todo.java new file mode 100644 index 0000000000..62d45989b9 --- /dev/null +++ b/src/main/java/URBOI_PACKIN/TaskTypes/Todo.java @@ -0,0 +1,22 @@ +package URBOI_PACKIN.TaskTypes; + +import URBOI_PACKIN.Task; + +/** + * Represents a Todo task. + */ +public class Todo extends Task { + public Todo(String description) { + super(description, null); + } + + @Override + public String toFileString() { + return "T | " + (isDone ? "1" : "0") + " | " + description; + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} diff --git a/src/main/java/DialogBox.java b/src/main/java/URBOI_PACKIN/UI/DialogBox.java similarity index 98% rename from src/main/java/DialogBox.java rename to src/main/java/URBOI_PACKIN/UI/DialogBox.java index 60636ac988..0b3355fd9d 100644 --- a/src/main/java/DialogBox.java +++ b/src/main/java/URBOI_PACKIN/UI/DialogBox.java @@ -1,3 +1,5 @@ +package URBOI_PACKIN.UI; + import java.io.IOException; import java.util.Collections; diff --git a/src/main/java/MainWindow.java b/src/main/java/URBOI_PACKIN/UI/MainWindow.java similarity index 87% rename from src/main/java/MainWindow.java rename to src/main/java/URBOI_PACKIN/UI/MainWindow.java index dd51e1b8e3..5f9e90bb31 100644 --- a/src/main/java/MainWindow.java +++ b/src/main/java/URBOI_PACKIN/UI/MainWindow.java @@ -1,3 +1,7 @@ +package URBOI_PACKIN.UI; + +import URBOI_PACKIN.ResponseController; +import URBOI_PACKIN.UI.DialogBox; import javafx.animation.PauseTransition; import javafx.application.Platform; import javafx.fxml.FXML; @@ -7,12 +11,8 @@ import javafx.scene.image.Image; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; -import javafx.scene.image.ImageView; -import javafx.stage.Stage; import javafx.util.Duration; -import java.util.Objects; - /** * Controller for MainWindow. Provides the layout for the other controls. */ @@ -27,7 +27,7 @@ public class MainWindow extends AnchorPane { private Button sendButton; - private Duke duke; + private ResponseController responseController; private Image userImage = new Image(this.getClass().getResourceAsStream("/images/crying.png")); private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/screaming.png")); @@ -47,19 +47,19 @@ public void initialize() { + "What can I do for you mah man?"; dialogContainer.getChildren().addAll(DialogBox.getDukeDialog(greetingMessage, dukeImage)); } - public void setDuke(Duke d) { - duke = d; + public void setDuke(ResponseController d) { + responseController = d; } /** - * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to + * Creates two dialog boxes, one echoing user input and the other containing URBOI_PACKIN.ResponseController's reply and then appends them to * the dialog container. Clears the user input after processing. */ @FXML private void handleUserInput() { String input = userInput.getText(); - String response = duke.getResponse(input); + String response = responseController.getResponse(input); if (input.equalsIgnoreCase("bye")) { dialogContainer.getChildren().addAll( DialogBox.getUserDialog(input, userImage), diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml index 030f7eae54..fc257e29c5 100644 --- a/src/main/resources/view/MainWindow.fxml +++ b/src/main/resources/view/MainWindow.fxml @@ -6,7 +6,7 @@ - +