Browse Source

implement exiting menu

feature-implement-menu-control
binsky 3 years ago
parent
commit
d3edeba81b
  1. 22
      src/main/java/PasswordManager.java
  2. 10
      src/test/java/PasswordManagerTest.java

22
src/main/java/PasswordManager.java

@ -1,14 +1,18 @@
import java.io.BufferedInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.Scanner;
public class PasswordManager implements PasswordManagerInterface { public class PasswordManager implements PasswordManagerInterface {
boolean running = true;
public static void main(String[] args) { public static void main(String[] args) {
PasswordManager pm = new PasswordManager(); PasswordManager pm = new PasswordManager();
while (true) {
while (pm.running) {
InputStream inputStream = System.in; InputStream inputStream = System.in;
pm.showMenu(inputStream); pm.showMenu(inputStream);
} }
System.exit(0);
} }
public PasswordManager() { public PasswordManager() {
@ -27,6 +31,20 @@ public class PasswordManager implements PasswordManagerInterface {
@Override @Override
public void showMenu(InputStream inputStream) { public void showMenu(InputStream inputStream) {
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(inputStream);
sb.append("ciip Gruppe 8 - Password Manager\n\n");
sb.append("Menu:\n");
sb.append("- exit: e\n");
System.out.println(sb + "\n");
String input = scan.nextLine();
if (input.equals("e")) {
running = false;
}
} }
} }

10
src/test/java/PasswordManagerTest.java

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -34,6 +35,13 @@ class PasswordManagerTest {
@Test @Test
void showMenu() { void showMenu() {
pm.showMenu(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)));
ByteArrayInputStream virtualConsoleInput = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(virtualConsoleInput));
}
@Test
void exitMenu() {
pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)));
assertFalse(pm.running);
} }
} }
Loading…
Cancel
Save