diff --git a/src/main/java/Application/App.java b/src/main/java/Application/App.java index 86c4865..db7c8c5 100644 --- a/src/main/java/Application/App.java +++ b/src/main/java/Application/App.java @@ -1,13 +1,20 @@ package Application; +import Game.Game; import Game.Tictactoe; +import java.util.ArrayList; +import java.util.Scanner; + public class App { private boolean isRunning = false; private Cli cli; - private Tictactoe ttt; + private boolean inMenu = true; + private MenuManager menuManager; + + private Game currentGame; public App(Cli cli) { this.cli = cli; @@ -15,23 +22,33 @@ public class App { } private void init() { - ttt = new Tictactoe(); + 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"); - ttt.print(cli); + cli.getPrintStream().println("Select a item by typing the number next to it"); + + cli.getPrintStream().print(menuManager.getFormattedMenuList()); } public void start() { isRunning = true; while (isRunning) { - String input = cli.getScanner().next(); + String input = cli.getScanner().nextLine(); if (input.equals("q")) { stop(); return; } else { - ttt.update(input); - cli.clearConsole(); - ttt.print(cli); + if (inMenu) { + cli.clearConsole(); + cli.getPrintStream().println("Select a item by typing the number next to it"); + selectMenuItem(input); + } else { + cli.clearConsole(); + currentGame.update(input); + currentGame.print(cli); + } } } } @@ -44,4 +61,60 @@ public class App { public boolean isRunning() { return isRunning; } + + public Game getCurrentGame() { + return this.currentGame; + } + + private MenuManager initMenuManager() { + MenuManager mm = new MenuManager(); + ArrayList gameList = new ArrayList<>(); + + gameList.add(new Menu("Tic Tac Toe")); + gameList.add(new Menu("Tic Toe")); + + Menu gameMenu = new Menu("Games"); + + gameMenu.addMenu(new Menu("Back")); + for (Menu game : gameList) { + game.addMenu(new Menu("Back")); + gameMenu.addMenu(game); + } + mm.addMenu(gameMenu); + + return mm; + } + + protected void selectMenuItem(String input) { + Scanner scanner = new Scanner(input); + if (scanner.hasNextInt()) { + int index = scanner.nextInt() - 1; + menuManager.select(index); + if (menuManager.getCurrentMenu() != null) { + switch (menuManager.getCurrentMenu().getName()) { + case "Back": + try { + menuManager.back(); + menuManager.back(); + } catch (Exception e) { + e.printStackTrace(); + } + break; + case "Tic Tac Toe": + setCurrentGame(new Tictactoe()); + return; + default: + break; + } + } + } + cli.getPrintStream().print(menuManager.getFormattedMenuList()); + } + + private void setCurrentGame(Game game) { + inMenu = false; + currentGame = game; + cli.clearConsole(); + currentGame.print(cli); + } } diff --git a/src/test/java/Application/AppTest.java b/src/test/java/Application/AppTest.java index c3b25e2..13f9bac 100644 --- a/src/test/java/Application/AppTest.java +++ b/src/test/java/Application/AppTest.java @@ -4,12 +4,11 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.*; -import java.nio.charset.StandardCharsets; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class AppTest { @@ -41,4 +40,12 @@ class AppTest { assertFalse(app.isRunning()); } + @Test + void selectMenuItem() { + assertNull(app.getCurrentGame()); + app.selectMenuItem("1"); + assertNull(app.getCurrentGame()); + app.selectMenuItem("2"); + assertNotNull(app.getCurrentGame()); + } } \ No newline at end of file