From 77588f8a37a5a1e01f25f6093b22cd542aad1eb5 Mon Sep 17 00:00:00 2001 From: Linus <53803800+zupey@users.noreply.github.com> Date: Wed, 7 Sep 2022 17:24:35 +0800 Subject: [PATCH] feat: closes GUI when user inputs `bye` Duke now closes when user inputs bye. --- src/main/java/duke/Duke.java | 13 +++++++++++++ src/main/java/duke/MainWindow.java | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ed09ca1d0c..7cff876bca 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -17,6 +17,7 @@ */ public class Duke { + private boolean isClosed; private final TaskList taskList; private final CommandHandlerFactory commandHandlerFactory; @@ -34,6 +35,15 @@ public Duke() { Storage db = new Storage("./data/duke.txt"); taskList = db.load(); commandHandlerFactory = new CommandHandlerFactory(); + isClosed = false; + } + + /** + * Checks if duke has terminated. + * @return whether duke has terminated. + */ + public boolean isClosed() { + return isClosed; } /** @@ -42,6 +52,9 @@ public Duke() { * @return Duke's response to the user. */ public String getResponse(String input) { + if (input.equals("bye")) { + this.isClosed = true; + } try { CommandHandler commandHandler = commandHandlerFactory.getHandler(input); return commandHandler.handle(taskList); diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java index a5028b803b..cc64ff3d04 100644 --- a/src/main/java/duke/MainWindow.java +++ b/src/main/java/duke/MainWindow.java @@ -1,5 +1,6 @@ package duke; +import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; @@ -50,5 +51,9 @@ private void handleUserInput() { DialogBox.getDukeDialog(response, dukeImage) ); userInput.clear(); + + if (duke.isClosed()) { + Platform.exit(); + } } }