Jenkins
3 years ago
15 changed files with 423 additions and 102 deletions
-
2pom.xml
-
144src/main/java/Application/App.java
-
55src/main/java/Application/Cli.java
-
41src/main/java/Application/MenuManager.java
-
6src/main/java/Game/Game.java
-
2src/main/java/Game/TicTacToe/Board.java
-
13src/main/java/Main.java
-
53src/test/java/Application/AppTest.java
-
55src/test/java/Application/CliTest.java
-
50src/test/java/Application/MenuManagerTest.java
-
8src/test/java/Application/MenuTest.java
-
29src/test/java/Game/GameTest.java
-
2src/test/java/Game/TicTacToe/BoardTest.java
-
7src/test/java/Game/TictactoeTest.java
@ -0,0 +1,144 @@ |
|||
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 boolean inMenu = true; |
|||
private MenuManager menuManager; |
|||
|
|||
private Game currentGame; |
|||
|
|||
public App(Cli cli) { |
|||
this.cli = cli; |
|||
init(); |
|||
} |
|||
|
|||
private void init() { |
|||
menuManager = initMenuManager(); |
|||
|
|||
goToMenu(); |
|||
} |
|||
|
|||
public void start() { |
|||
isRunning = true; |
|||
while (isRunning) { |
|||
String input = cli.getScanner().nextLine(); |
|||
if (input.equals("exit")) { |
|||
stop(); |
|||
return; |
|||
} else { |
|||
if (inMenu) { |
|||
cli.clearConsole(); |
|||
selectMenuItem(input); |
|||
} else { |
|||
if (input.equals("q")) { |
|||
goToMenu(); |
|||
continue; |
|||
} |
|||
cli.clearConsole(); |
|||
currentGame.update(input); |
|||
currentGame.print(cli); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void stop() { |
|||
isRunning = false; |
|||
cli.getPrintStream().println("Stopping application..."); |
|||
} |
|||
|
|||
public boolean isRunning() { |
|||
return isRunning; |
|||
} |
|||
|
|||
public Game getCurrentGame() { |
|||
return this.currentGame; |
|||
} |
|||
|
|||
private MenuManager initMenuManager() { |
|||
MenuManager mm = new MenuManager(); |
|||
ArrayList<Menu> gameList = new ArrayList<>(); |
|||
|
|||
gameList.add(new Menu("Tic Tac 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 (input.equals("q") && !menuManager.inRootMenu()) { |
|||
goToMenu(); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
|||
} |
|||
printMenu(); |
|||
} |
|||
|
|||
private void setCurrentGame(Game game) { |
|||
inMenu = false; |
|||
currentGame = game; |
|||
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()); |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
package Application; |
|||
|
|||
import java.io.InputStream; |
|||
import java.io.PrintStream; |
|||
import java.util.Scanner; |
|||
|
|||
public class Cli { |
|||
|
|||
private boolean isClearConsoleActive = true; |
|||
|
|||
private PrintStream printStream; |
|||
private InputStream inputStream; |
|||
private Scanner scanner; |
|||
|
|||
public Cli(PrintStream printStream, InputStream inputStream) { |
|||
this.printStream = printStream; |
|||
this.inputStream = inputStream; |
|||
this.scanner = new Scanner(inputStream); |
|||
isClearConsoleActive = true; |
|||
} |
|||
|
|||
public PrintStream getPrintStream() { |
|||
return this.printStream; |
|||
} |
|||
|
|||
public Scanner getScanner(){ |
|||
return this.scanner; |
|||
} |
|||
|
|||
public void clearConsole(){ |
|||
if(!isClearConsoleActive) |
|||
return; |
|||
try{ |
|||
String operatingSystem = System.getProperty("os.name");//Check the current operating system |
|||
|
|||
if(operatingSystem.contains("Windows")){ |
|||
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls"); |
|||
Process startProcess = pb.inheritIO().start(); |
|||
startProcess.waitFor(); |
|||
} else { |
|||
ProcessBuilder pb = new ProcessBuilder("clear"); |
|||
Process startProcess = pb.inheritIO().start(); |
|||
|
|||
startProcess.waitFor(); |
|||
} |
|||
}catch(Exception e){ |
|||
this.getPrintStream().println(e); |
|||
} |
|||
} |
|||
|
|||
public void setClearConsoleActive(boolean clearConsoleActive) { |
|||
isClearConsoleActive = clearConsoleActive; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,53 @@ |
|||
package Application; |
|||
|
|||
import org.junit.jupiter.api.AfterEach; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.PrintStream; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class AppTest { |
|||
|
|||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
|||
private final ByteArrayInputStream inContent = new ByteArrayInputStream("1\n2\nexit\n".getBytes()); |
|||
|
|||
App app; |
|||
Cli cli; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
cli = new Cli(new PrintStream(outContent), inContent); |
|||
cli.setClearConsoleActive(false); |
|||
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\nexit\n".getBytes()); |
|||
cli = new Cli(new PrintStream(outContent), input); |
|||
cli.setClearConsoleActive(false); |
|||
app = new App(cli); |
|||
app.start(); |
|||
assertFalse(app.isRunning()); |
|||
} |
|||
|
|||
@Test |
|||
void selectMenuItem() { |
|||
assertNull(app.getCurrentGame()); |
|||
app.selectMenuItem("1"); |
|||
assertNull(app.getCurrentGame()); |
|||
app.selectMenuItem("2"); |
|||
assertNotNull(app.getCurrentGame()); |
|||
app.selectMenuItem("q"); |
|||
assertNull(app.getCurrentGame()); |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
package Application; |
|||
|
|||
import org.junit.jupiter.api.AfterEach; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.InputStream; |
|||
import java.io.PrintStream; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertTrue; |
|||
|
|||
class CliTest { |
|||
|
|||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
|||
private final PrintStream originalOut = System.out; |
|||
private final ByteArrayInputStream inContent = new ByteArrayInputStream("Test".getBytes()); |
|||
private final InputStream originalIn = System.in; |
|||
|
|||
Cli cli; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
System.setOut(new PrintStream(outContent)); |
|||
System.setIn(inContent); |
|||
cli = new Cli(System.out, System.in); |
|||
} |
|||
|
|||
@AfterEach |
|||
void tearDown() { |
|||
System.setOut(originalOut); |
|||
System.setIn(originalIn); |
|||
} |
|||
|
|||
//Only tests correct stream integration, other methods are provided |
|||
@Test |
|||
void getPrintStream() { |
|||
cli.getPrintStream().println("Hello World!"); |
|||
assertEquals("Hello World!\n", outContent.toString().replaceAll("\r", "")); |
|||
} |
|||
|
|||
//Only tests correct stream integration, other methods are provided |
|||
@Test |
|||
void getScanner() { |
|||
assertEquals(cli.getScanner().next(), "Test"); |
|||
} |
|||
|
|||
//Cannot test because it uses external processes |
|||
@Test |
|||
void clearConsole() { |
|||
assertTrue(true); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue