diff --git a/src/main/java/Application/App.java b/src/main/java/Application/App.java index db7c8c5..751218f 100644 --- a/src/main/java/Application/App.java +++ b/src/main/java/Application/App.java @@ -24,27 +24,25 @@ public class App { private void init() { menuManager = initMenuManager(); - cli.clearConsole(); - cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); - cli.getPrintStream().println("Press 'q' at any time to stop the application"); - cli.getPrintStream().println("Select a item by typing the number next to it"); - - cli.getPrintStream().print(menuManager.getFormattedMenuList()); + goToMenu(); } public void start() { isRunning = true; while (isRunning) { String input = cli.getScanner().nextLine(); - if (input.equals("q")) { + if (input.equals("exit")) { stop(); return; } else { if (inMenu) { cli.clearConsole(); - cli.getPrintStream().println("Select a item by typing the number next to it"); selectMenuItem(input); } else { + if (input.equals("q")) { + goToMenu(); + continue; + } cli.clearConsole(); currentGame.update(input); currentGame.print(cli); @@ -71,7 +69,6 @@ public class App { ArrayList gameList = new ArrayList<>(); gameList.add(new Menu("Tic Tac Toe")); - gameList.add(new Menu("Tic Toe")); Menu gameMenu = new Menu("Games"); @@ -87,6 +84,11 @@ public class App { protected void selectMenuItem(String input) { Scanner scanner = new Scanner(input); + + if (input.equals("q") && !menuManager.inRootMenu()) { + goToMenu(); + } + if (scanner.hasNextInt()) { int index = scanner.nextInt() - 1; menuManager.select(index); @@ -108,7 +110,7 @@ public class App { } } } - cli.getPrintStream().print(menuManager.getFormattedMenuList()); + printMenu(); } private void setCurrentGame(Game game) { @@ -117,4 +119,26 @@ public class App { cli.clearConsole(); currentGame.print(cli); } + + private void goToMenu() { + inMenu = true; + currentGame = null; + while (!menuManager.inRootMenu()) { + try { + menuManager.back(); + } catch (Exception e) { + e.printStackTrace(); + } + } + printMenu(); + } + + private void printMenu() { + cli.clearConsole(); + cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); + cli.getPrintStream().println("Type 'exit' at any time to stop the application"); + cli.getPrintStream().println("Select a item by typing the number next to it"); + + cli.getPrintStream().print(menuManager.getFormattedMenuList()); + } } diff --git a/src/test/java/Application/AppTest.java b/src/test/java/Application/AppTest.java index 13f9bac..8593c9d 100644 --- a/src/test/java/Application/AppTest.java +++ b/src/test/java/Application/AppTest.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*; class AppTest { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayInputStream inContent = new ByteArrayInputStream("1\n2\nq\n".getBytes()); + private final ByteArrayInputStream inContent = new ByteArrayInputStream("1\n2\nexit\n".getBytes()); App app; Cli cli; @@ -32,7 +32,7 @@ class AppTest { //inContent has the 'q' at the end, to terminated the loop and set isRunning to false @Test void stop() { - ByteArrayInputStream input = new ByteArrayInputStream("1\n2\nq\n".getBytes()); + ByteArrayInputStream input = new ByteArrayInputStream("1\n2\nexit\n".getBytes()); cli = new Cli(new PrintStream(outContent), input); cli.setClearConsoleActive(false); app = new App(cli); @@ -47,5 +47,7 @@ class AppTest { assertNull(app.getCurrentGame()); app.selectMenuItem("2"); assertNotNull(app.getCurrentGame()); + app.selectMenuItem("q"); + assertNull(app.getCurrentGame()); } } \ No newline at end of file