From 20220af27862a5e8f3f0dcd4e29eed855a518f9e Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 27 Jan 2022 19:45:47 +0100 Subject: [PATCH 1/4] Added application main loop for updating, printing the games and quiting at anytime with test for correctly terminating. --- src/main/java/Application/App.java | 46 ++++++++++++++++++++++++++ src/main/java/Main.java | 10 ++---- src/test/java/Application/AppTest.java | 42 +++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/main/java/Application/App.java create mode 100644 src/test/java/Application/AppTest.java diff --git a/src/main/java/Application/App.java b/src/main/java/Application/App.java new file mode 100644 index 0000000..da2f9bd --- /dev/null +++ b/src/main/java/Application/App.java @@ -0,0 +1,46 @@ +package Application; + +import Game.Tictactoe; + +public class App { + + private boolean isRunning = false; + private Cli cli; + + private Tictactoe ttt; + + public App(Cli cli) { + this.cli = cli; + init(); + } + + private void init() { + ttt = new Tictactoe(); + cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); + cli.getPrintStream().println("Press 'q' at any time to stop the application"); + ttt.print(cli); + } + + public void start() { + isRunning = true; + while (isRunning) { + String input = cli.getScanner().next(); + if (input.equals("q")) { + stop(); + return; + } else { + ttt.update(input); + ttt.print(cli); + } + } + } + + public void stop() { + isRunning = false; + cli.getPrintStream().println("Stopping application..."); + } + + public boolean isRunning() { + return isRunning; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index d286686..7c937c5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,4 @@ +import Application.App; import Application.Cli; import Game.Tictactoe; @@ -6,13 +7,8 @@ import java.util.Scanner; public class Main { public static void main(String[] args) { - Tictactoe ttt = new Tictactoe(); Cli cli = new Cli(System.out, System.in); - Scanner scan = cli.getScanner(); - ttt.print(cli); - while (scan.hasNext()) { - ttt.update(scan.next()); - ttt.print(cli); - } + App app = new App(cli); + app.start(); } } diff --git a/src/test/java/Application/AppTest.java b/src/test/java/Application/AppTest.java new file mode 100644 index 0000000..4813ea6 --- /dev/null +++ b/src/test/java/Application/AppTest.java @@ -0,0 +1,42 @@ +package Application; + +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 static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AppTest { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayInputStream inContent = new ByteArrayInputStream("1\n2\nq\n".getBytes()); + + App app; + Cli cli; + + @BeforeEach + void setUp() { + cli = new Cli(new PrintStream(outContent), inContent); + app = new App(cli); + } + + @AfterEach + void tearDown() { + } + + //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()); + cli = new Cli(new PrintStream(outContent), input); + app = new App(cli); + app.start(); + assertFalse(app.isRunning()); + } + +} \ No newline at end of file From 71bce709ab5ccb62f279d706c7cfbd246205a274 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 27 Jan 2022 20:54:41 +0100 Subject: [PATCH 2/4] Added deactivation for clearConsole() method because it would throw warnings otherwise. --- src/main/java/Application/App.java | 1 + src/main/java/Application/Cli.java | 10 ++++++++++ src/test/java/Application/AppTest.java | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/main/java/Application/App.java b/src/main/java/Application/App.java index da2f9bd..86c4865 100644 --- a/src/main/java/Application/App.java +++ b/src/main/java/Application/App.java @@ -30,6 +30,7 @@ public class App { return; } else { ttt.update(input); + cli.clearConsole(); ttt.print(cli); } } diff --git a/src/main/java/Application/Cli.java b/src/main/java/Application/Cli.java index 2b93962..0defedf 100644 --- a/src/main/java/Application/Cli.java +++ b/src/main/java/Application/Cli.java @@ -6,6 +6,8 @@ import java.util.Scanner; public class Cli { + private boolean isClearConsoleActive = true; + private PrintStream printStream; private InputStream inputStream; private Scanner scanner; @@ -14,6 +16,7 @@ public class Cli { this.printStream = printStream; this.inputStream = inputStream; this.scanner = new Scanner(inputStream); + isClearConsoleActive = true; } public PrintStream getPrintStream() { @@ -25,6 +28,8 @@ public class Cli { } public void clearConsole(){ + if(!isClearConsoleActive) + return; try{ String operatingSystem = System.getProperty("os.name");//Check the current operating system @@ -42,4 +47,9 @@ public class Cli { this.getPrintStream().println(e); } } + + public void setClearConsoleActive(boolean clearConsoleActive) { + isClearConsoleActive = clearConsoleActive; + } + } diff --git a/src/test/java/Application/AppTest.java b/src/test/java/Application/AppTest.java index 4813ea6..c3b25e2 100644 --- a/src/test/java/Application/AppTest.java +++ b/src/test/java/Application/AppTest.java @@ -22,6 +22,7 @@ class AppTest { @BeforeEach void setUp() { cli = new Cli(new PrintStream(outContent), inContent); + cli.setClearConsoleActive(false); app = new App(cli); } @@ -34,6 +35,7 @@ class AppTest { void stop() { ByteArrayInputStream input = new ByteArrayInputStream("1\n2\nq\n".getBytes()); cli = new Cli(new PrintStream(outContent), input); + cli.setClearConsoleActive(false); app = new App(cli); app.start(); assertFalse(app.isRunning()); From daa43a48564c2e8db20f596aff0f3bd1a33b2eaa Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 00:08:15 +0100 Subject: [PATCH 3/4] Added Menu navigation to App class with weak test case (depending on implementation) --- src/main/java/Application/App.java | 87 +++++++++++++++++++++++--- src/test/java/Application/AppTest.java | 15 +++-- 2 files changed, 91 insertions(+), 11 deletions(-) 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 From 838b3353238016f7dea203ad9699477a6357391b Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 12:44:41 +0100 Subject: [PATCH 4/4] Change stop command to "exit" and added "q" command for going back for better navigation --- src/main/java/Application/App.java | 44 ++++++++++++++++++++------ src/test/java/Application/AppTest.java | 6 ++-- 2 files changed, 38 insertions(+), 12 deletions(-) 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