Browse Source

do not exit menu after wrong input

feature-implement-menu-control
binsky 3 years ago
parent
commit
fcccecae9e
  1. 18
      src/main/java/PasswordManager.java
  2. 3
      src/main/java/PasswordManagerInterface.java
  3. 24
      src/test/java/PasswordManagerTest.java

18
src/main/java/PasswordManager.java

@ -1,4 +1,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class PasswordManager implements PasswordManagerInterface {
@ -7,10 +10,11 @@ public class PasswordManager implements PasswordManagerInterface {
public static void main(String[] args) {
PasswordManager pm = new PasswordManager();
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
while (pm.running) {
InputStream inputStream = System.in;
pm.showMenu(inputStream);
pm.showMenu(inputStream, outputStream);
}
System.exit(0);
}
@ -21,7 +25,6 @@ public class PasswordManager implements PasswordManagerInterface {
@Override
public void listVaults() {
}
@Override
@ -30,16 +33,21 @@ public class PasswordManager implements PasswordManagerInterface {
}
@Override
public void showMenu(InputStream inputStream) {
public void showMenu(InputStream inputStream, OutputStream outputStream) {
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(inputStream);
running = true;
sb.append("ciip Gruppe 8 - Password Manager\n\n");
sb.append("Menu:\n");
sb.append("- exit: e\n");
System.out.println(sb + "\n");
try {
outputStream.write((sb + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
String input = scan.nextLine();

3
src/main/java/PasswordManagerInterface.java

@ -1,9 +1,10 @@
import java.io.InputStream;
import java.io.OutputStream;
public interface PasswordManagerInterface {
public static void main(String[] args) {
};
public void listVaults();
public void openVault();
public void showMenu(InputStream inputStream);
public void showMenu(InputStream inputStream, OutputStream outputStream);
}

24
src/test/java/PasswordManagerTest.java

@ -1,8 +1,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
@ -35,13 +34,28 @@ class PasswordManagerTest {
@Test
void showMenu() {
ByteArrayInputStream virtualConsoleInput = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(virtualConsoleInput));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream));
assertTrue(outputStream.toString().startsWith("ciip Gruppe 8"));
}
@Test
void exitMenu() {
pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)));
pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)), System.out);
assertFalse(pm.running);
}
@Test
void doNotExitMenuAfterWrongInput() {
pm.showMenu(new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)), System.out);
assertTrue(pm.running);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream));
assertTrue(outputStream.toString().startsWith("ciip Gruppe 8"));
}
private ByteArrayInputStream getEmptyStringInputStream() {
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
}
}
Loading…
Cancel
Save