diff --git a/src/ch/zhaw/ads/ADS1_2_test.java b/src/ch/zhaw/ads/ADS1_2_test.java index c1ac217..72bbc00 100644 --- a/src/ch/zhaw/ads/ADS1_2_test.java +++ b/src/ch/zhaw/ads/ADS1_2_test.java @@ -47,5 +47,10 @@ public class ADS1_2_test { public void testEmptyPop() { assertNull(stack.pop()); } + + @Test + public void testEmptyPeek() { + assertNull(stack.peek()); + } } diff --git a/src/ch/zhaw/ads/ADS1_3_test.java b/src/ch/zhaw/ads/ADS1_3_test.java index d57f168..b467686 100644 --- a/src/ch/zhaw/ads/ADS1_3_test.java +++ b/src/ch/zhaw/ads/ADS1_3_test.java @@ -1,4 +1,4 @@ -/* + package ch.zhaw.ads; import org.junit.jupiter.api.BeforeEach; @@ -29,4 +29,3 @@ public class ADS1_3_test { } } - */ \ No newline at end of file diff --git a/src/ch/zhaw/ads/BracketServer.java b/src/ch/zhaw/ads/BracketServer.java new file mode 100644 index 0000000..71cf497 --- /dev/null +++ b/src/ch/zhaw/ads/BracketServer.java @@ -0,0 +1,46 @@ +package ch.zhaw.ads; + +public class BracketServer implements CommandExecutor { + + + + @Override + public String execute(String command) throws Exception { + return checkBrackets(command) ? "true" : "false"; + } + + public boolean checkBrackets(String command) { + Stack stack = new ListStack(); + char[] chars = command.toCharArray(); + for (char x: chars) { + Character closingBracket = getClosingBracketForOpening(x); + if(closingBracket != null){ + stack.push(closingBracket); + } else if(isClosingBracket(x) && stack.pop() != (Character)x) { + return false; + } + } + if(stack.isEmpty()){ + return true; + } else { + return false; + } + } + + private Character getClosingBracketForOpening(char openingBracket) { + switch (openingBracket){ + case '(': + return ')'; + case '[': + return ']'; + case '{': + return '}'; + } + return null; + } + + private boolean isClosingBracket(char x) { + return x == ')' || x == ']' || x == '}'; + } + +} \ No newline at end of file diff --git a/src/ch/zhaw/ads/ListStack.java b/src/ch/zhaw/ads/ListStack.java index f1fa8ba..facdb16 100644 --- a/src/ch/zhaw/ads/ListStack.java +++ b/src/ch/zhaw/ads/ListStack.java @@ -13,10 +13,7 @@ public class ListStack implements Stack { @Override public Object pop() { - if(arrayList.isEmpty()){ - return null; - } - return arrayList.remove(0); + return arrayList.isEmpty() ? null : arrayList.remove(0); } @Override @@ -26,7 +23,7 @@ public class ListStack implements Stack { @Override public Object peek() { - return arrayList.get(0); + return arrayList.isEmpty() ? null : arrayList.get(0); } @Override