Browse Source

Change stop command to "exit" and added "q" command for going back for better navigation

feature-app
Nick Stolbov 3 years ago
parent
commit
838b335323
  1. 44
      src/main/java/Application/App.java
  2. 6
      src/test/java/Application/AppTest.java

44
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<Menu> 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());
}
}

6
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());
}
}
Loading…
Cancel
Save