Browse Source

Added Menu navigation to App class with weak test case (depending on implementation)

feature-app
Nick Stolbov 3 years ago
parent
commit
daa43a4856
  1. 87
      src/main/java/Application/App.java
  2. 15
      src/test/java/Application/AppTest.java

87
src/main/java/Application/App.java

@ -1,13 +1,20 @@
package Application; package Application;
import Game.Game;
import Game.Tictactoe; import Game.Tictactoe;
import java.util.ArrayList;
import java.util.Scanner;
public class App { public class App {
private boolean isRunning = false; private boolean isRunning = false;
private Cli cli; private Cli cli;
private Tictactoe ttt;
private boolean inMenu = true;
private MenuManager menuManager;
private Game currentGame;
public App(Cli cli) { public App(Cli cli) {
this.cli = cli; this.cli = cli;
@ -15,23 +22,33 @@ public class App {
} }
private void init() { private void init() {
ttt = new Tictactoe();
menuManager = initMenuManager();
cli.clearConsole();
cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); cli.getPrintStream().println("Welcome to the Cli Arcade Service!");
cli.getPrintStream().println("Press 'q' at any time to stop the application"); 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() { public void start() {
isRunning = true; isRunning = true;
while (isRunning) { while (isRunning) {
String input = cli.getScanner().next();
String input = cli.getScanner().nextLine();
if (input.equals("q")) { if (input.equals("q")) {
stop(); stop();
return; return;
} else { } 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() { public boolean isRunning() {
return 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"));
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);
}
} }

15
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.BeforeEach;
import org.junit.jupiter.api.Test; 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.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AppTest { class AppTest {
@ -41,4 +40,12 @@ class AppTest {
assertFalse(app.isRunning()); assertFalse(app.isRunning());
} }
@Test
void selectMenuItem() {
assertNull(app.getCurrentGame());
app.selectMenuItem("1");
assertNull(app.getCurrentGame());
app.selectMenuItem("2");
assertNotNull(app.getCurrentGame());
}
} }
Loading…
Cancel
Save