From 3301a1626ff1a05714f440e5d21a9881a200b7ff Mon Sep 17 00:00:00 2001 From: Gavzzz Date: Tue, 20 Sep 2022 01:28:47 +0800 Subject: [PATCH] GUI bug fixed Changed return type to String so return type can be more easily accessed by the GUI. --- build.gradle | 6 -- src/main/java/Duke/Duke.java | 54 +++++++----------- src/main/java/Duke/commands/ByeCommands.java | 5 +- src/main/java/Duke/commands/Command.java | 3 +- .../java/Duke/commands/DeadlineCommands.java | 16 +++--- .../java/Duke/commands/DeleteCommands.java | 16 +++--- .../commands/DetectDuplicateCommands.java | 14 +++-- .../java/Duke/commands/EventCommands.java | 12 ++-- src/main/java/Duke/commands/Executor.java | 4 +- src/main/java/Duke/commands/FindCommands.java | 12 ++-- src/main/java/Duke/commands/ListCommands.java | 10 ++-- src/main/java/Duke/commands/MarkCommands.java | 10 ++-- src/main/java/Duke/commands/TaskCommands.java | 11 ++-- src/main/java/Duke/commands/TodoCommands.java | 12 ++-- .../java/Duke/commands/UnmarkCommands.java | 10 ++-- src/main/java/Duke/gui/DialogBox.java | 30 +++------- src/main/java/Duke/gui/Main.java | 10 +++- src/main/java/Duke/gui/MainWindow.java | 49 ++++++---------- .../resource => resources}/images/DaDuke.png | Bin .../resource => resources}/images/DaUser.png | Bin .../view/DialogBox.fxml | 0 .../view/MainWindow.fxml | 4 +- 22 files changed, 129 insertions(+), 159 deletions(-) rename src/main/{java/Duke/resource => resources}/images/DaDuke.png (100%) rename src/main/{java/Duke/resource => resources}/images/DaUser.png (100%) rename src/main/{java/Duke/resource => resources}/view/DialogBox.fxml (100%) rename src/main/{java/Duke/resource => resources}/view/MainWindow.fxml (95%) diff --git a/build.gradle b/build.gradle index f13e452297..d57f3750ad 100644 --- a/build.gradle +++ b/build.gradle @@ -8,11 +8,6 @@ repositories { mavenCentral() } -test { - useJUnitPlatform() -} - - dependencies { @@ -61,5 +56,4 @@ shadowJar { run { standardInput = System.in - enableAssertions = true } diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index 816279874f..c53bbc7533 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -1,55 +1,41 @@ package Duke; import Duke.commands.Command; +import java.util.ArrayList; import java.util.List; - +import Duke.gui.Main; +import javafx.application.Application; import java.io.IOException; - +/* Inspiration taken from Bag Devesh Kumar and Zizheng ip to solve some prevalent issues + */ public class Duke { - private final Storage storage; - private final TaskList tasks; - private final Ui ui; - + private Storage storage; + private TaskList tasks; + private Ui ui; - public Duke() throws IOException { + public Duke() throws IOException, DukeException { ui = new Ui(); storage = new Storage(); tasks = new TaskList(storage.fileToList()); } - public void run() { - ui.showLogo(); - boolean isExit = false; - while (!isExit) { - try { - String fullInput = ui.readInput(); - ui.printDash(); - Command c = Parser.parse(fullInput); - List reply = c.execute(tasks, ui, storage); - for (String line : reply) { - ui.printLine(line); - } - isExit = c.isExit(); - } catch (IOException | DukeException e) { - ui.printError(e.getMessage()); - } finally { - ui.printDash(); - } - } - } - - public static void main(String[] args) { - try { - new Duke().run(); - } catch (IOException e) { - System.out.println(e.getMessage()); - } + public String getResponse(String input) throws DukeException, IOException { + Command c = Parser.parse(input); + String message = c.execute(tasks, ui, storage); + return message; } } + + + + + + + diff --git a/src/main/java/Duke/commands/ByeCommands.java b/src/main/java/Duke/commands/ByeCommands.java index db7f1f892c..6c5fc86090 100644 --- a/src/main/java/Duke/commands/ByeCommands.java +++ b/src/main/java/Duke/commands/ByeCommands.java @@ -10,8 +10,9 @@ public class ByeCommands extends Command { @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { - return List.of(ui.printBye()); + public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { + String message = ui.printBye(); + return message; } @Override diff --git a/src/main/java/Duke/commands/Command.java b/src/main/java/Duke/commands/Command.java index 105ec336ed..99980d80aa 100644 --- a/src/main/java/Duke/commands/Command.java +++ b/src/main/java/Duke/commands/Command.java @@ -1,5 +1,6 @@ package Duke.commands; +import java.util.ArrayList; import java.util.List; import Duke.DukeException; import Duke.Storage; @@ -25,7 +26,7 @@ public abstract class Command { * @throws DukeException when there is inappropriate input or save file issues. */ - public abstract List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException; + public abstract String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException; /** diff --git a/src/main/java/Duke/commands/DeadlineCommands.java b/src/main/java/Duke/commands/DeadlineCommands.java index 683aab3a37..ed31eed0a7 100644 --- a/src/main/java/Duke/commands/DeadlineCommands.java +++ b/src/main/java/Duke/commands/DeadlineCommands.java @@ -12,6 +12,8 @@ public class DeadlineCommands extends Executor { + private String input; + private final static Pattern deadlinePattern = Pattern.compile("(?.*) /by (?.*)"); /** @@ -38,16 +40,16 @@ public DeadlineCommands(String input) { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { - ArrayList text = new ArrayList<>(); - Matcher match = deadlinePattern.matcher(description); + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + Matcher match = deadlinePattern.matcher(input); if (!match.matches()) { throw new DukeException("No deadline was given, try again"); } - text.add("I've added this task:"); + String line1 = "I've added this task:"; Task addedTask = tasks.addTask(new Deadline(match.group("taskName"), match.group("by"))); - text.add(addedTask.toString()); - text.addAll(super.execute(tasks, ui, storage)); - return text; + String line2 = addedTask.toString(); + String line3 = super.execute(tasks, ui, storage); + String reply = line1 + "\n" + line2 + "\n" + line3; + return reply; } } diff --git a/src/main/java/Duke/commands/DeleteCommands.java b/src/main/java/Duke/commands/DeleteCommands.java index 8cd80bc085..3bbf682803 100644 --- a/src/main/java/Duke/commands/DeleteCommands.java +++ b/src/main/java/Duke/commands/DeleteCommands.java @@ -9,8 +9,10 @@ public class DeleteCommands extends TaskCommands { - public DeleteCommands(String fullInput) throws DukeException { - super(fullInput); + private String input; + + public DeleteCommands(String input) throws DukeException { + super(input); } /** @@ -24,13 +26,13 @@ public DeleteCommands(String fullInput) throws DukeException { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { if (tasks.isValidTaskNumber(taskNumber)) { - ArrayList text = new ArrayList<>(); - text.add("I've removed this task: "); - text.add(tasks.getTaskToString(taskNumber)); + String line1 = "I've removed this task: "; + String line2 = tasks.getTaskToString(taskNumber); tasks.removeTask(taskNumber); - return text; + String reply = line1 + line2; + return reply; } else { throw new DukeException("This task does not exists"); } diff --git a/src/main/java/Duke/commands/DetectDuplicateCommands.java b/src/main/java/Duke/commands/DetectDuplicateCommands.java index 57d3e3c467..dddc0e276d 100644 --- a/src/main/java/Duke/commands/DetectDuplicateCommands.java +++ b/src/main/java/Duke/commands/DetectDuplicateCommands.java @@ -20,21 +20,23 @@ public class DetectDuplicateCommands extends Command { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { + public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { int counter = 0; - ArrayList text = new ArrayList<>(); for (int i = 0; i < tasks.getTaskListSize() -1 ; i++) { for (int j = i + 1; j < tasks.getTaskListSize(); j++) { if (tasks.getTask(i) == tasks.getTask(j)) { - text.add("There seems to be a duplicate task in your planner"); - text.add("The duplicate task is " + tasks.getTaskToString(i)); counter++; break; } } } - text.add("You have " + counter + " duplicate tasks"); - return text; + if (counter == 0) { + String reply = "You have no duplicate tasks"; + return reply; + } else { + String reply = "You have " + counter + " duplicate tasks"; + return reply; + } } } diff --git a/src/main/java/Duke/commands/EventCommands.java b/src/main/java/Duke/commands/EventCommands.java index 4dae5bd6b2..35b80cd8a9 100644 --- a/src/main/java/Duke/commands/EventCommands.java +++ b/src/main/java/Duke/commands/EventCommands.java @@ -38,17 +38,17 @@ public EventCommands(String input) { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { - ArrayList text = new ArrayList<>(); + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { Matcher match = eventPattern.matcher(description); if (!match.matches()) { throw new DukeException("No event time was given, try again"); } - text.add("I've added this task:"); + String line1 = "I've added this task:"; Task addedTask = tasks.addTask(new Event(match.group("taskName"), match.group("at"))); - text.add(addedTask.toString()); + String line2 = addedTask.toString(); storage.addTask(addedTask); - text.addAll(super.execute(tasks, ui, storage)); - return text; + String line3 = super.execute(tasks, ui, storage); + String reply = line1 + "\n" + line2 + "\n" + line3; + return reply; } } diff --git a/src/main/java/Duke/commands/Executor.java b/src/main/java/Duke/commands/Executor.java index 3f48fcd886..d39164fff4 100644 --- a/src/main/java/Duke/commands/Executor.java +++ b/src/main/java/Duke/commands/Executor.java @@ -33,7 +33,7 @@ public Executor(String input) { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { - return List.of(tasks.getSizeToString()); + public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { + return tasks.getSizeToString(); } } diff --git a/src/main/java/Duke/commands/FindCommands.java b/src/main/java/Duke/commands/FindCommands.java index 76878802d1..4b13e16f15 100644 --- a/src/main/java/Duke/commands/FindCommands.java +++ b/src/main/java/Duke/commands/FindCommands.java @@ -23,17 +23,17 @@ public FindCommands(String input) throws DukeException { } @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { - ArrayList text = new ArrayList<>(); + public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { TaskList matches = tasks.filter(Keywords); - text.add(String.format("Here are the matching tasks \"%s\":", - String.join("\", \"", Keywords))); + String line1 = String.format("Here are the matching tasks \"%s\":", + String.join("\", \"", Keywords)); for (int i = 1; matches.isValidTaskNumber(i); i++) { - text.add(String.format("%d. %s", i + 1, matches.getTaskToString(i))); + String line2 = String.format("%d. %s", i + 1, matches.getTaskToString(i)); + line1 += line2; } - return text; + return line1; } diff --git a/src/main/java/Duke/commands/ListCommands.java b/src/main/java/Duke/commands/ListCommands.java index a945297d89..7d8f3144de 100644 --- a/src/main/java/Duke/commands/ListCommands.java +++ b/src/main/java/Duke/commands/ListCommands.java @@ -23,12 +23,12 @@ public class ListCommands extends Command { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { - ArrayList text = new ArrayList<>(); - text.add("Here are the tasks in your list:"); + public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException { + String line1 = "Here are the tasks in your list:"; for (int i = 1; tasks.isValidTaskNumber(i); i++) { - text.add(i + ". " + tasks.getTaskToString(i)); + String line2 = i + ". " + tasks.getTaskToString(i); + line1 += "\n" + line2; } - return text; + return line1; } } diff --git a/src/main/java/Duke/commands/MarkCommands.java b/src/main/java/Duke/commands/MarkCommands.java index f27c3b52a2..550cd9611a 100644 --- a/src/main/java/Duke/commands/MarkCommands.java +++ b/src/main/java/Duke/commands/MarkCommands.java @@ -29,14 +29,14 @@ public MarkCommands(String input) throws DukeException { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { if (tasks.isValidTaskNumber(taskNumber)) { - ArrayList text = new ArrayList<>(); - text.add("I've marked this task as done!"); + String line1 = "I've marked this task as done!"; tasks.markAsDone(taskNumber); storage.updateTask(taskNumber, Constants.MARK); - text.add(tasks.getTaskToString(taskNumber)); - return text; + String line2 = tasks.getTaskToString(taskNumber); + String reply = line1 + line2; + return reply; } else { throw new DukeException("This task does not exist."); } diff --git a/src/main/java/Duke/commands/TaskCommands.java b/src/main/java/Duke/commands/TaskCommands.java index f3690a4294..81959e7b6b 100644 --- a/src/main/java/Duke/commands/TaskCommands.java +++ b/src/main/java/Duke/commands/TaskCommands.java @@ -13,8 +13,8 @@ public abstract class TaskCommands extends Command { protected final int taskNumber; - public TaskCommands(String fullInput) throws DukeException { - String[] commands = fullInput.split(" "); + public TaskCommands(String input) throws DukeException { + String[] commands = input.split(" "); try { taskNumber = Integer.parseInt(commands[1]); } catch (NumberFormatException e) { @@ -23,11 +23,10 @@ public TaskCommands(String fullInput) throws DukeException { } @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { if (tasks.isValidTaskNumber(taskNumber)) { - ArrayList text = new ArrayList<>(); - text.add(tasks.getTaskToString(taskNumber)); - return text; + String line1 = tasks.getTaskToString(taskNumber); + return line1; } else { throw new DukeException("No such task exist."); } diff --git a/src/main/java/Duke/commands/TodoCommands.java b/src/main/java/Duke/commands/TodoCommands.java index bc5417166d..374b4cbdf5 100644 --- a/src/main/java/Duke/commands/TodoCommands.java +++ b/src/main/java/Duke/commands/TodoCommands.java @@ -38,13 +38,13 @@ public TodoCommands(String input) { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { - ArrayList text = new ArrayList<>(); - text.add("I've added this task:"); + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + String line1 = "I've added this task:"; Task addedTask = tasks.addTask(new Todo(description)); - text.add(addedTask.toString()); + String line2 = addedTask.toString(); storage.addTask(addedTask); - text.addAll(super.execute(tasks, ui, storage)); - return text; + String line3 = super.execute(tasks, ui, storage); + String reply = line1 + "\n" + line2 + "\n" + line3; + return reply; } } diff --git a/src/main/java/Duke/commands/UnmarkCommands.java b/src/main/java/Duke/commands/UnmarkCommands.java index 54475b3c99..d2411fb6e5 100644 --- a/src/main/java/Duke/commands/UnmarkCommands.java +++ b/src/main/java/Duke/commands/UnmarkCommands.java @@ -32,14 +32,14 @@ public UnmarkCommands(String input) throws DukeException { */ @Override - public List execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException { if (tasks.isValidTaskNumber(taskNumber)) { - ArrayList text = new ArrayList<>(); - text.add("I've marked this task as not done (yet ;))"); + String line1 = "I've marked this task as not done (yet ;))"; tasks.markAsNotDone(taskNumber); storage.updateTask(taskNumber, Constants.UNMARK); - text.add(tasks.getTaskToString(taskNumber)); - return text; + String line2 = tasks.getTaskToString(taskNumber); + String reply = line1 + line2; + return reply; } else { throw new DukeException("tasks.Task does not exist."); } diff --git a/src/main/java/Duke/gui/DialogBox.java b/src/main/java/Duke/gui/DialogBox.java index a5fdfedf01..b9aefde065 100644 --- a/src/main/java/Duke/gui/DialogBox.java +++ b/src/main/java/Duke/gui/DialogBox.java @@ -15,21 +15,13 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; -public class DialogBox extends AnchorPane { - - @FXML - private Label name; - +public class DialogBox extends HBox { @FXML private Label dialog; - - @FXML - private ImageView profilePicture; - @FXML - private HBox boxHeader; + private ImageView displayPicture; - private DialogBox(String name, String text, Image img) { + private DialogBox(String text, Image img) { try { FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml")); fxmlLoader.setController(this); @@ -38,9 +30,9 @@ private DialogBox(String name, String text, Image img) { } catch (IOException e) { e.printStackTrace(); } - this.name.setText(name); + dialog.setText(text); - profilePicture.setImage(img); + displayPicture.setImage(img); } /** @@ -49,22 +41,18 @@ private DialogBox(String name, String text, Image img) { private void flip() { ObservableList tmp = FXCollections.observableArrayList(this.getChildren()); Collections.reverse(tmp); - boxHeader.getChildren().setAll(tmp); - boxHeader.setAlignment(Pos.TOP_LEFT); + getChildren().setAll(tmp); + setAlignment(Pos.TOP_LEFT); } public static DialogBox getUserDialog(String text, Image img) { - return new DialogBox("My dude", text, img); + return new DialogBox(text, img); } public static DialogBox getDukeDialog(String text, Image img) { - var db = new DialogBox("Drake", text, img); + var db = new DialogBox(text, img); db.flip(); return db; } - - - - } diff --git a/src/main/java/Duke/gui/Main.java b/src/main/java/Duke/gui/Main.java index c289633866..c222a7d108 100644 --- a/src/main/java/Duke/gui/Main.java +++ b/src/main/java/Duke/gui/Main.java @@ -13,7 +13,12 @@ public class Main extends Application { - + private Duke duke = new Duke(); + + + public Main() throws IOException, DukeException { + } + @Override public void start(Stage stage) { @@ -22,8 +27,9 @@ public void start(Stage stage) { AnchorPane ap = fxmlLoader.load(); Scene scene = new Scene(ap); stage.setScene(scene); - stage.show(); stage.setTitle("Duke"); + fxmlLoader.getController().setDuke(duke); + stage.show(); } catch (IOException e) { System.out.println(e.getMessage()); diff --git a/src/main/java/Duke/gui/MainWindow.java b/src/main/java/Duke/gui/MainWindow.java index c89d70434d..24f85911e6 100644 --- a/src/main/java/Duke/gui/MainWindow.java +++ b/src/main/java/Duke/gui/MainWindow.java @@ -25,6 +25,8 @@ public class MainWindow extends AnchorPane { @FXML private Button sendButton; + private Duke duke; + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); @@ -38,49 +40,34 @@ public MainWindow() throws DukeException, IOException { } @FXML - public void initialize() throws IOException { - + public void initialize() { scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); - List messages = new ArrayList<>(ui.chatBox(ui.showLogo())); - - taskList = new TaskList(storage.fileToList()); - storage = new Storage(); + } - for (String message : messages) { - dialogContainer.getChildren().add(DialogBox.getDukeDialog(message, dukeImage)); - } + public void setDuke(Duke d) { + duke = d; } + /** * Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to * the dialog container. */ - @FXML - private void handleUserInput() throws DukeException, IOException { - String input = userInput.getText(); - Command command = Parser.parse(input); - List reply = command.execute(taskList, ui, storage); - dialogContainer.getChildren().add(DialogBox.getUserDialog(input, userImage)); - List text = new ArrayList<>(); - for (String line : reply) { - text.add(line); - if (text.size() >= 5) { - dialogContainer.getChildren().add( - DialogBox.getDukeDialog(String.join("\n", text), dukeImage)); - text.clear(); - } - } - if (text.size() > 0) { - dialogContainer.getChildren().add( - DialogBox.getDukeDialog(String.join("\n", text), dukeImage)); - } - - userInput.clear(); - } + @FXML + private void handleUserInput() throws DukeException, IOException { + String input = userInput.getText(); + String response = duke.getResponse(input); + dialogContainer.getChildren().addAll( + DialogBox.getUserDialog(input, userImage), + DialogBox.getDukeDialog(response, dukeImage) + ); + userInput.clear(); + } } + diff --git a/src/main/java/Duke/resource/images/DaDuke.png b/src/main/resources/images/DaDuke.png similarity index 100% rename from src/main/java/Duke/resource/images/DaDuke.png rename to src/main/resources/images/DaDuke.png diff --git a/src/main/java/Duke/resource/images/DaUser.png b/src/main/resources/images/DaUser.png similarity index 100% rename from src/main/java/Duke/resource/images/DaUser.png rename to src/main/resources/images/DaUser.png diff --git a/src/main/java/Duke/resource/view/DialogBox.fxml b/src/main/resources/view/DialogBox.fxml similarity index 100% rename from src/main/java/Duke/resource/view/DialogBox.fxml rename to src/main/resources/view/DialogBox.fxml diff --git a/src/main/java/Duke/resource/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml similarity index 95% rename from src/main/java/Duke/resource/view/MainWindow.fxml rename to src/main/resources/view/MainWindow.fxml index 0bacf7bc15..68fb0a7e6a 100644 --- a/src/main/java/Duke/resource/view/MainWindow.fxml +++ b/src/main/resources/view/MainWindow.fxml @@ -1,3 +1,5 @@ + + @@ -14,4 +16,4 @@ - + \ No newline at end of file