diff --git a/pom.xml b/pom.xml index 4ef10ea..ea16109 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,27 @@ 8 + + maven-surefire-plugin + 2.22.0 + + + + org.junit.jupiter + junit-jupiter-api + RELEASE + test + + + org.junit.jupiter + junit-jupiter-engine + RELEASE + test + + + \ No newline at end of file diff --git a/src/main/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReader.java b/src/main/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReader.java new file mode 100644 index 0000000..1df8d23 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReader.java @@ -0,0 +1,29 @@ +package xyz.joethei.studium.algodat.vorlesung; + +import java.util.Stack; + +class KeyboardReader { + + private Stack stack = new Stack<>(); + + private void input(char c) { + if(c == '#') stack.pop(); + else if(c == '~') stack.clear(); + else stack.push(c); + } + + String getString() { + StringBuilder stringBuilder = new StringBuilder(); + for (char c : stack) { + stringBuilder.append(c); + } + return stringBuilder.toString(); + } + + void read(String input) { + for (char c: input.toCharArray()) { + input(c); + } + } + +} \ No newline at end of file diff --git a/src/test/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReaderTest.java b/src/test/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReaderTest.java new file mode 100644 index 0000000..a49fdd4 --- /dev/null +++ b/src/test/java/xyz/joethei/studium/algodat/vorlesung/KeyboardReaderTest.java @@ -0,0 +1,22 @@ +package xyz.joethei.studium.algodat.vorlesung; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class KeyboardReaderTest { + + @Test + void druckeTest() { + KeyboardReader reader = new KeyboardReader(); + reader.read("D#dr-+~druk#ckeh#"); + assertEquals("drucke", reader.getString()); + } + + @Test + void blaTest() { + KeyboardReader reader = new KeyboardReader(); + reader.read("grwgafmrag~grsegsthbrshfb~b#blq#a"); + assertEquals("bla", reader.getString()); + } +} \ No newline at end of file