From 88e5b9b6370056f3752107a33b7fbc7e0b7e4e7f Mon Sep 17 00:00:00 2001 From: Impala36 Date: Wed, 27 Jan 2021 00:19:28 +0800 Subject: [PATCH] Level-10 Changed all commands and UI to return Strings for GUI --- build.gradle | 18 ++++- src/main/java/Launcher.java | 11 +++ src/main/java/Main.java | 32 ++++++++ src/main/java/duke/DialogBox.java | 61 +++++++++++++++ src/main/java/duke/Duke.java | 35 ++++----- src/main/java/duke/MainWindow.java | 62 +++++++++++++++ src/main/java/duke/commands/Command.java | 2 +- .../java/duke/commands/DeadlineCommand.java | 23 +++--- .../java/duke/commands/DeleteCommand.java | 19 +++-- src/main/java/duke/commands/DoneCommand.java | 18 ++--- src/main/java/duke/commands/EventCommand.java | 19 +++-- src/main/java/duke/commands/ExitCommand.java | 4 +- src/main/java/duke/commands/FindCommand.java | 10 +-- .../java/duke/commands/InvalidCommand.java | 4 +- src/main/java/duke/commands/ListCommand.java | 9 +-- src/main/java/duke/commands/TodoCommand.java | 10 +-- src/main/java/duke/database/Database.java | 41 +++++----- src/main/java/duke/parser/Parser.java | 1 - src/main/java/duke/ui/Ui.java | 76 ++++++++----------- src/main/resources/view/DialogBox.fxml | 20 +++++ src/main/resources/view/MainWindow.fxml | 19 +++++ src/test/java/EventCommandTest.java | 2 +- 22 files changed, 342 insertions(+), 154 deletions(-) create mode 100644 src/main/java/Launcher.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/duke/DialogBox.java create mode 100644 src/main/java/duke/MainWindow.java create mode 100644 src/main/resources/view/DialogBox.fxml create mode 100644 src/main/resources/view/MainWindow.fxml diff --git a/build.gradle b/build.gradle index 495079b8..58420113 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,20 @@ dependencies { compile 'junit:junit:4.12' compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2' + String javaFxVersion = '11' + + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' + implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' } test { @@ -36,7 +50,7 @@ test { jar{ manifest { attributes( - 'Main-Class': 'main.java.duke.Duke' + 'Main-Class': 'Launcher' ) } } @@ -46,7 +60,7 @@ run { } application { - mainClassName = "main.java.duke.Duke" + mainClassName = "Launcher" } shadowJar { diff --git a/src/main/java/Launcher.java b/src/main/java/Launcher.java new file mode 100644 index 00000000..26034c56 --- /dev/null +++ b/src/main/java/Launcher.java @@ -0,0 +1,11 @@ +import javafx.application.Application; + +/** + * A launcher class to workaround classpath issues. + */ + +public class Launcher { + public static void main(String[] args) { + Application.launch(Main.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..99c82e88 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,32 @@ +import java.io.IOException; +import duke.Duke; +import duke.MainWindow; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +/** + * A GUI for Duke using FXML. + */ + +public class Main extends Application { + + private final Duke duke = new Duke(); + + @Override + public void start(Stage stage) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/MainWindow.fxml")); + AnchorPane ap = fxmlLoader.load(); + Scene scene = new Scene(ap); + stage.setScene(scene); + fxmlLoader.getController().startDuke(duke); + stage.show(); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/DialogBox.java b/src/main/java/duke/DialogBox.java new file mode 100644 index 00000000..d183f825 --- /dev/null +++ b/src/main/java/duke/DialogBox.java @@ -0,0 +1,61 @@ +package duke; + +import java.io.IOException; +import java.util.Collections; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; + +/** + * An example of a custom control using FXML. + * This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label + * containing text from the speaker. + */ +public class DialogBox extends HBox { + @FXML + private Label dialog; + @FXML + private ImageView displayPicture; + + private DialogBox(String text, Image img) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml")); + fxmlLoader.setController(this); + fxmlLoader.setRoot(this); + fxmlLoader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + + dialog.setText(text); + displayPicture.setImage(img); + } + + /** + * Flips the dialog box such that the ImageView is on the left and text on the right. + */ + private void flip() { + ObservableList tmp = FXCollections.observableArrayList(this.getChildren()); + Collections.reverse(tmp); + getChildren().setAll(tmp); + setAlignment(Pos.TOP_LEFT); + } + + public static DialogBox getUserDialog(String text, Image img) { + return new DialogBox(text, img); + } + + public static DialogBox getDukeDialog(String text, Image img) { + var db = new DialogBox(text, img); + db.flip(); + return db; + } +} \ No newline at end of file diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 5d5c03ba..86752d26 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,17 +1,17 @@ package duke; +import java.util.Scanner; import duke.commands.Command; -import duke.commands.ExitCommand; -import duke.ui.Ui; +import duke.database.Database; import duke.parser.Parser; import duke.task.TaskList; -import duke.database.Database; -import java.util.Scanner; +import duke.ui.Ui; + /** * A Personal Assistant Chatbot that helps a person to keep track of various things. * * @author Wang Zhenquan - * @version A-MoreOOP + * @version Level-10 * @since 20/08/2020 */ @@ -22,25 +22,16 @@ public class Duke { private final TaskList tasks = new TaskList(); private final Database database = new Database("duke.txt", tasks, ui); - public static void main(String[] args) { - new Duke().run(); - } - - private void run() { + String getGreetingsAndTasks() { System.setProperty("file.encoding", "utf-8"); - ui.printDukeLogo(); - ui.printGreeting(); - database.readDatabase(tasks, ui, database); - repeatedUserInput(); + return ui.printDukeLogo() + + ui.printGreeting() + + database.readDatabase(tasks, ui, database); } - private void repeatedUserInput() { - while (true) { - Command command = parser.parseInput(in.nextLine(), ui, tasks, database); - command.execute(); - if (ExitCommand.class.isAssignableFrom(command.getClass())) { - return; - } - } + String getResponse(String input) { + Command command = parser.parseInput(input, ui, tasks, database); + return command.execute(); } + } \ No newline at end of file diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java new file mode 100644 index 00000000..6b4d381d --- /dev/null +++ b/src/main/java/duke/MainWindow.java @@ -0,0 +1,62 @@ +package duke; + +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; + +/** + * Controller for duke.MainWindow. Provides the layout for the other controls. + */ + +public class MainWindow extends AnchorPane { + private final Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); + private final Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); + @FXML + private ScrollPane scrollPane; + @FXML + private VBox dialogContainer; + @FXML + private TextField userInput; + @FXML + private Button sendButton; + private Duke duke; + + @FXML + public void initialize() { + scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); + } + + public void startDuke(Duke d) { + duke = d; + initializeDuke(); + } + + /** + * Creates two dialog boxes, one echoing user input and the other containing Duke'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); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + + ); + userInput.clear(); + } + + @FXML + public void initializeDuke() { + String greetings = duke.getGreetingsAndTasks(); + dialogContainer.getChildren().addAll( + DialogBox.getDukeDialog(greetings, dukeImage) + ); + } +} \ No newline at end of file diff --git a/src/main/java/duke/commands/Command.java b/src/main/java/duke/commands/Command.java index 4d9cfa1f..54adf8ad 100644 --- a/src/main/java/duke/commands/Command.java +++ b/src/main/java/duke/commands/Command.java @@ -35,5 +35,5 @@ protected Command(Ui ui) { this.ui = ui; } - public abstract void execute(); + public abstract String execute(); } \ No newline at end of file diff --git a/src/main/java/duke/commands/DeadlineCommand.java b/src/main/java/duke/commands/DeadlineCommand.java index e772c3ee..6a0d717b 100644 --- a/src/main/java/duke/commands/DeadlineCommand.java +++ b/src/main/java/duke/commands/DeadlineCommand.java @@ -5,7 +5,6 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import duke.database.Database; -import duke.exception.DukeException; import duke.task.Deadline; import duke.task.TaskList; import duke.ui.Ui; @@ -37,34 +36,32 @@ public String reformatDateTime(String dateTime) throws DateTimeParseException { } @Override - public void execute() { + public String execute() { try { if (line.isEmpty() || !line.contains("/by")) { - throw new DukeException( - "It seems that you've missed out the task description or the /by segment!\n" - + "Please type in the 'deadline /by
' format."); + return ui.printRed("It seems that you've missed out the task description or the /by segment!\n" + + "Please type in the 'deadline /by
' format."); } String description = line.split("/by ")[0]; String dateTime = line.split("/by ")[1]; if (description.isEmpty() || dateTime.isEmpty()) { - throw new DukeException( - "It seems that you've missed out the task description or the /by segment!\n" - + "Please type in the 'deadline /by
' format."); + return ui.printRed("It seems that you've missed out the task description or the /by segment!\n" + + "Please type in the 'deadline /by
' format."); } line = description + "/by " + reformatDateTime(dateTime); line = reformatLine("[Deadline] ", "by", line); tasks.add(new Deadline(line)); - ui.printTaskAdded(tasks); database.updateDatabase(tasks); + return ui.printTaskAdded(tasks); - } catch (DukeException | IOException e) { - ui.printRedBorderlines(e.getMessage()); + } catch (IOException e) { + return ui.printRed(e.getMessage()); } catch (IndexOutOfBoundsException e) { - ui.printRedBorderlines("It seems that you've missed out the deadline time!\n" + return ui.printRed("It seems that you've missed out the deadline time!\n" + "Please type in something for
after 'deadline /by'."); } catch (DateTimeParseException e) { - ui.printRedBorderlines("It seems that you didn't enter the time in the right format!\n" + return ui.printRed("It seems that you didn't enter the time in the right format!\n" + "Please type in the 'deadline /by
' format."); } } diff --git a/src/main/java/duke/commands/DeleteCommand.java b/src/main/java/duke/commands/DeleteCommand.java index 12f6e6a8..20fbd9b7 100644 --- a/src/main/java/duke/commands/DeleteCommand.java +++ b/src/main/java/duke/commands/DeleteCommand.java @@ -2,7 +2,6 @@ import java.io.IOException; import duke.database.Database; -import duke.exception.DukeException; import duke.task.TaskList; import duke.ui.Ui; @@ -18,29 +17,29 @@ public DeleteCommand(String taskDescription, TaskList tasks, Ui ui, Database dat } @Override - public void execute() { + public String execute() { try { if (tasks.size() == 0) { - ui.printRedBorderlines("It appears that you have no tasks yet, so you can't delete any!\n" + return ui.printRed("It appears that you have no tasks yet, so you can't delete any!\n" + "Perhaps you should start creating one?"); - return; } if (line.isEmpty()) { - throw new DukeException("You almost typed a proper delete command, but you missed out the number!\n" + return ui.printRed("You almost typed a proper delete command, but you missed out the number!\n" + "Please type in the 'delete ' format."); } int index = Integer.parseInt(line); - ui.printTaskRemoved(tasks, index); + String message = ui.printTaskRemoved(tasks, index); tasks.remove(index); database.updateDatabase(tasks); + return message; - } catch (DukeException | IOException e) { - ui.printRedBorderlines(e.getMessage()); + } catch (IOException e) { + return ui.printRed(e.getMessage()); } catch (NumberFormatException e) { - ui.printRedBorderlines( + return ui.printRed( "I'm sorry, but the list goes numerically.\nPerhaps you could type a number for the index?"); } catch (IndexOutOfBoundsException e) { - ui.printRedBorderlines("It appears that you only have " + tasks.size() + " task(s) in your list,\n" + return ui.printRed("It appears that you only have " + tasks.size() + " task(s) in your list,\n" + "perhaps you might want to try typing an index number from 1 to " + tasks.size() + " instead?"); } } diff --git a/src/main/java/duke/commands/DoneCommand.java b/src/main/java/duke/commands/DoneCommand.java index baa1cda4..1a2f9f21 100644 --- a/src/main/java/duke/commands/DoneCommand.java +++ b/src/main/java/duke/commands/DoneCommand.java @@ -2,7 +2,6 @@ import java.io.IOException; import duke.database.Database; -import duke.exception.DukeException; import duke.task.TaskList; import duke.ui.Ui; @@ -18,29 +17,28 @@ public DoneCommand(String line, TaskList tasks, Ui ui, Database database) { } @Override - public void execute() { + public String execute() { try { if (tasks.size() == 0) { - ui.printRedBorderlines("It appears that you have no tasks yet, so you can't complete any!\n" + return ui.printRed("It appears that you have no tasks yet, so you can't complete any!\n" + "Perhaps you should start creating one?"); - return; } if (line.isEmpty()) { - throw new DukeException("You almost typed a proper done command, but you missed out the number!\n" + return ui.printRed("You almost typed a proper done command, but you missed out the number!\n" + "Please type in the 'done ' format."); } int index = Integer.parseInt(line); tasks.get(index - 1).setDone(); - ui.printTaskCompleted(index, tasks); database.updateDatabase(tasks); + return ui.printTaskCompleted(index, tasks); - } catch (DukeException | IOException e) { - ui.printRedBorderlines(e.getMessage()); + } catch (IOException e) { + return ui.printRed(e.getMessage()); } catch (NumberFormatException e) { - ui.printRedBorderlines( + return ui.printRed( "I'm sorry, but the list goes numerically.\nPerhaps you could type a number for the index?"); } catch (IndexOutOfBoundsException e) { - ui.printRedBorderlines("It appears that you only have " + tasks.size() + " task(s) in your list,\n" + return ui.printRed("It appears that you only have " + tasks.size() + " task(s) in your list,\n" + "perhaps you might want to try typing an index number from 1 to " + tasks.size() + " instead?"); } } diff --git a/src/main/java/duke/commands/EventCommand.java b/src/main/java/duke/commands/EventCommand.java index 022c3e8f..d1ed7607 100644 --- a/src/main/java/duke/commands/EventCommand.java +++ b/src/main/java/duke/commands/EventCommand.java @@ -5,7 +5,6 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import duke.database.Database; -import duke.exception.DukeException; import duke.task.Event; import duke.task.TaskList; import duke.ui.Ui; @@ -38,10 +37,10 @@ public String reformatTime(String time) throws DateTimeParseException { } @Override - public void execute() { + public String execute() { try { if (line.isEmpty() || !line.contains("/at") || !line.contains("/to")) { - throw new DukeException("It seems that you've missed out the required keyword(s)!\n" + return ui.printRed("It seems that you've missed out the required keyword(s)!\n" + "Please type in the 'event /at
/to ' format."); } String description = line.split("/at ")[0]; @@ -49,8 +48,8 @@ public void execute() { String endTime = line.split("/at ")[1].split(" /to ")[1]; if (description.isEmpty() || startDateTime.isEmpty() || endTime.isEmpty()) { - throw new DukeException( - "It seems that you've missed out the task description or the start and end duration!\n" + return ui + .printRed("It seems that you've missed out the task description or the start and end duration!\n" + "Please type in the 'event /at
/to ' format."); } startDateTime = reformatDateTime(startDateTime); @@ -58,16 +57,16 @@ public void execute() { line = description + "/at " + startDateTime + " /to " + endTime; line = reformatLine("[Event] ", "at", "to", line); tasks.add(new Event(line)); - ui.printTaskAdded(tasks); database.updateDatabase(tasks); + return ui.printTaskAdded(tasks); - } catch (DukeException | IOException e) { - ui.printRedBorderlines(e.getMessage()); + } catch (IOException e) { + return ui.printRed(e.getMessage()); } catch (IndexOutOfBoundsException e) { - ui.printRedBorderlines("It seems that you've missed out the event duration!\n" + return ui.printRed("It seems that you've missed out the event duration!\n" + "Please type in something for
after 'event /at'."); } catch (DateTimeParseException e) { - ui.printRedBorderlines("It seems that you didn't enter the time in the right format!\n" + return ui.printRed("It seems that you didn't enter the time in the right format!\n" + "Please type in the 'event /at
' format."); } } diff --git a/src/main/java/duke/commands/ExitCommand.java b/src/main/java/duke/commands/ExitCommand.java index ac16c73d..6e428766 100644 --- a/src/main/java/duke/commands/ExitCommand.java +++ b/src/main/java/duke/commands/ExitCommand.java @@ -11,7 +11,7 @@ public ExitCommand(Ui ui) { } @Override - public void execute() { - ui.printFarewell(); + public String execute() { + return ui.printFarewell(); } } \ No newline at end of file diff --git a/src/main/java/duke/commands/FindCommand.java b/src/main/java/duke/commands/FindCommand.java index 967de83f..29f0d1ef 100644 --- a/src/main/java/duke/commands/FindCommand.java +++ b/src/main/java/duke/commands/FindCommand.java @@ -15,15 +15,13 @@ public FindCommand(String line, TaskList tasks, Ui ui) { } @Override - public void execute() { + public String execute() { String searchWord = line.trim(); if (tasks.size() == 0) { - ui.printBorderlines("It appears that you have no tasks! Perhaps you should start creating one?"); - return; + return "It appears that you have no tasks! Perhaps you should start creating one?"; } if (searchWord.isEmpty()) { - ui.printBorderlines("Please type in the 'find ' format!"); - return; + return ui.printRed("Please type in the 'find ' format!"); } String matches = "Here are the tasks that matches '" + searchWord + "'!\n"; boolean hasMatch = false; @@ -37,6 +35,6 @@ public void execute() { matches = "It appears that are no matches for '" + searchWord + "'!\n" + "Perhaps you could try searching another word?"; } - ui.printBorderlines(matches); + return matches; } } \ No newline at end of file diff --git a/src/main/java/duke/commands/InvalidCommand.java b/src/main/java/duke/commands/InvalidCommand.java index e0daa910..1dd45e48 100644 --- a/src/main/java/duke/commands/InvalidCommand.java +++ b/src/main/java/duke/commands/InvalidCommand.java @@ -11,7 +11,7 @@ public InvalidCommand(Ui ui) { } @Override - public void execute() { - ui.printInvalidCommand(); + public String execute() { + return ui.printInvalidCommand(); } } \ No newline at end of file diff --git a/src/main/java/duke/commands/ListCommand.java b/src/main/java/duke/commands/ListCommand.java index c86410e5..7c40f658 100644 --- a/src/main/java/duke/commands/ListCommand.java +++ b/src/main/java/duke/commands/ListCommand.java @@ -14,15 +14,14 @@ public ListCommand(TaskList tasks, Ui ui) { } @Override - public void execute() { + public String execute() { if (tasks.size() == 0) { - ui.printBorderlines("It appears that you have no tasks! Perhaps you should start creating one?"); - return; + return ui.printRed("It appears that you have no tasks! Perhaps you should start creating one?"); } String total = "Here are the tasks that you currently have!\n"; for (int i = 0; i < tasks.size(); i++) { - total = total.concat((i + 1) + ". " + tasks.get(i).getDescription()) + "\n"; + total = total.concat((i + 1) + ". " + tasks.get(i).getDescription()) + System.lineSeparator(); } - ui.printBorderlines(total); + return total; } } diff --git a/src/main/java/duke/commands/TodoCommand.java b/src/main/java/duke/commands/TodoCommand.java index fe1a5943..cb3d867f 100644 --- a/src/main/java/duke/commands/TodoCommand.java +++ b/src/main/java/duke/commands/TodoCommand.java @@ -19,19 +19,19 @@ public TodoCommand(String line, TaskList tasks, Ui ui, Database database) { } @Override - public void execute() { + public String execute() { try { if (line.isEmpty()) { - throw new DukeException("It seems that you missed out the task description!\n" + return ui.printRed("It seems that you missed out the task description!\n" + "Please type in the 'todo ' format."); } line = "[Todo] " + line; tasks.add(new Todo(line)); - ui.printTaskAdded(tasks); database.updateDatabase(tasks); + return ui.printTaskAdded(tasks); - } catch (DukeException | IOException e) { - ui.printRedBorderlines(e.getMessage()); + } catch (IOException e) { + return ui.printRed(e.getMessage()); } } } \ No newline at end of file diff --git a/src/main/java/duke/database/Database.java b/src/main/java/duke/database/Database.java index 9b933de7..92a886ea 100644 --- a/src/main/java/duke/database/Database.java +++ b/src/main/java/duke/database/Database.java @@ -1,19 +1,18 @@ package duke.database; -import duke.commands.InvalidCommand; -import duke.commands.ListCommand; -import duke.task.TaskList; -import duke.task.Todo; -import duke.task.Deadline; -import duke.task.Event; -import duke.ui.Ui; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; +import duke.commands.ListCommand; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.TaskList; +import duke.task.Todo; +import duke.ui.Ui; + /** * Responsible for all interactions between the in-memory tasks and the tasks file. */ @@ -28,23 +27,22 @@ public Database(String fileName, TaskList tasks, Ui ui) { public void updateDatabase(TaskList tasks) throws IOException { FileWriter fw = new FileWriter("./" + fileName, false); for (int i = 0; i < tasks.size(); i++) { - String line = (i + 1) + ". " + tasks.get(i).getDescription() + "\n"; + String line = (i + 1) + ". " + tasks.get(i).getDescription() + System.lineSeparator(); fw.write(line); } fw.close(); } - public void readDatabase(TaskList tasks, Ui ui, Database database) { + public String readDatabase(TaskList tasks, Ui ui, Database database) { try { ArrayList lines = getLines(fileName); tasks = extractTasks(lines, tasks, ui, database); - ui.printFileExists(); - new ListCommand(tasks, ui).execute(); + return ui.printFileExists() + new ListCommand(tasks, ui).execute(); } catch (FileNotFoundException e) { - ui.printNoFileFound(); + return ui.printNoFileFound(); } catch (IOException e) { - ui.printRedBorderlines(ui.colorRed("IOException encountered: " + e.getMessage())); + return ui.printRed("IOException encountered: " + e.getMessage()); } } @@ -66,12 +64,15 @@ public TaskList extractTasks(ArrayList lines, TaskList tasks, Ui ui, Dat String taskDescription = line.split("]")[2].trim(); switch (taskType) { - case "Todo": tasks.add(new Todo("[Todo] " + taskDescription)); - break; - case "Deadline": tasks.add(new Deadline("[Deadline] " + taskDescription)); - break; - case "Event": tasks.add(new Event("[Event] " + taskDescription)); - break; + case "Todo": + tasks.add(new Todo("[Todo] " + taskDescription)); + break; + case "Deadline": + tasks.add(new Deadline("[Deadline] " + taskDescription)); + break; + case "Event": + tasks.add(new Event("[Event] " + taskDescription)); + break; default: ui.printInvalidTask(); break; diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 47f14028..e12395d4 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -40,7 +40,6 @@ public Command parseInput(String line, Ui ui, TaskList tasks, Database database) return new EventCommand(lineWithoutCommand, tasks, ui, database); case "bye": case "exit": - case "": // also exits when user input is empty return new ExitCommand(ui); default: return new InvalidCommand(ui); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 1a9b8136..f7a401d5 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -1,5 +1,6 @@ package duke.ui; +import duke.MainWindow; import duke.task.TaskList; /** @@ -7,44 +8,38 @@ */ public class Ui { + + private final MainWindow mw = new MainWindow(); /** * Colors an input string in red. */ + public String colorRed(String input) { - return "\033[31m" + input + "\033[0m"; + return input; //"\033[31m" + input + "\033[0m"; } /** * Colors an input string in green. */ + public String colorGreen(String input) { - return "\033[33m" + input + "\033[0m"; + return input; //"\033[33m" + input + "\033[0m"; } /** - * Prints the input string with borderlines. + * Prints the input string in red. */ - public void printBorderlines(String input) { - System.out.println(Style.BORDERLINE.getString() - + input - + Style.BORDERLINE.getString()); - } - /** - * Prints the input string with red colored borderlines. - */ - public void printRedBorderlines(String input) { - System.out.println(colorRed(Style.BORDERLINE.getString()) - + input - + colorRed(Style.BORDERLINE.getString())); + public String printRed(String input) { + return colorRed(input); } - public void printDukeLogo() { - System.out.println(Style.LOGO.getString()); + public String printDukeLogo() { + return Style.LOGO.getString(); } - public void printGreeting() { - String greeting = "Hello! I'm Duke.\n" + public String printGreeting() { + return "Hello! I'm Duke.\n" + "I can help you manage a list of tasks!\n" + "What you can tell me to do is listed below:\n" + " ⬡ Create a Todo task | " + colorGreen("todo \n") @@ -55,52 +50,45 @@ public void printGreeting() { + " ⬢ List down all tasks | " + colorGreen("list\n") + " ⬡ Find tasks with word | " + colorGreen("find \n") + " ⬢ Exit my program | " + colorGreen("bye or hit Enter"); - printBorderlines(greeting); } - public void printFarewell() { - String farewell = "Bye. Hope to see you again soon!"; - printBorderlines(farewell); + public String printFarewell() { + return "Bye. Hope to see you again soon!"; } - public void printTaskAdded(TaskList tasks) { - String added = "Great! You have entered a proper task as shown below:\n" + public String printTaskAdded(TaskList tasks) { + return "Great! You have entered a proper task as shown below:\n" + colorGreen(tasks.get(tasks.size() - 1).getDescription() + "\nWe now have " + tasks.size() + " task(s) in your list!"); - printBorderlines(added); } - public void printTaskRemoved(TaskList tasks, int index) { - String added = "Okay! We have removed the task as shown below:\n" + public String printTaskRemoved(TaskList tasks, int index) { + return "Okay! We have removed the task as shown below:\n" + colorRed(tasks.get(index - 1).getDescription() + colorGreen("\nWe now have " + (tasks.size() - 1) + " task(s) in your list!")); - printBorderlines(added); } - public void printTaskCompleted(int index, TaskList tasks) { - String done = "Good work there! Now we have:\n" - + "\033[33mTask [" + index + "] " - + tasks.get(index - 1).getDescription() + " set as completed!\033[0m"; - printBorderlines(done); + public String printTaskCompleted(int index, TaskList tasks) { + return "Good work there! Now we have:\n" + + colorGreen("Task [" + index + "] ") + + tasks.get(index - 1).getDescription() + colorGreen(" set as completed!"); } - public void printInvalidCommand() { - printRedBorderlines("It seems that you have typed something out of my unfortunately\n" + public String printInvalidCommand() { + return printRed("It seems that you have typed something out of my unfortunately\n" + "limited vocabulary. Can you try again?"); } - public void printInvalidTask() { - printRedBorderlines("Task found is not a Todo, Deadline or Event task. Please verify the task."); + public String printInvalidTask() { + return printRed("Task found is not a Todo, Deadline or Event task. Please verify the task."); } - public void printNoFileFound() { - String message = "It looks like it's your first time as I could not find any existing task file.\n" + public String printNoFileFound() { + return "\nIt looks like it's your first time as I could not find any existing task file.\n" + "Get started by creating a task!"; - System.out.println(message); } - public void printFileExists() { - String message = "An existing task file is found!"; - System.out.println(message); + public String printFileExists() { + return "\nAn existing task file is found!\n"; } } diff --git a/src/main/resources/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml new file mode 100644 index 00000000..1bd6586e --- /dev/null +++ b/src/main/resources/view/DialogBox.fxml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml new file mode 100644 index 00000000..cac0f4b7 --- /dev/null +++ b/src/main/resources/view/MainWindow.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + +