+ KeyboardReader

+ JUnit Tests
This commit is contained in:
Johannes Theiner 2018-11-08 16:49:52 +01:00
parent 0765e6243c
commit b58c50cc54
3 changed files with 70 additions and 0 deletions

19
pom.xml
View File

@ -17,8 +17,27 @@
<target>8</target> <target>8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,29 @@
package xyz.joethei.studium.algodat.vorlesung;
import java.util.Stack;
class KeyboardReader {
private Stack<Character> 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);
}
}
}

View File

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