From 20220af27862a5e8f3f0dcd4e29eed855a518f9e Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 27 Jan 2022 19:45:47 +0100 Subject: [PATCH] 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