solved ex3

This commit is contained in:
navid.sassan 2020-09-28 16:06:59 +02:00
parent 241094e268
commit 4ceedbab2a
2 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,48 @@
package ch.zhaw.ads;
import java.util.Map;
import static java.util.Map.entry;
public class BracketServer implements CommandExecutor {
@Override
public String execute(String command) {
StringBuffer result = new StringBuffer(100);
result.append("Die Eingabe war \"");
result.append(command);
result.append("\"\n");
return result.toString();
}
private Map<Character, Character> openingBrackets = Map.ofEntries(
entry('(', ')'),
entry('[', ']'),
entry('{', '}')
);
private Map<Character, Character> closingBrackets = Map.ofEntries(
entry(')', '('),
entry(']', '['),
entry('}', '{')
);
public boolean checkBrackets(String input) {
ListStack<Character> bracketStack = new ListStack<>();
for (int i = 0; i < input.length(); i++) {
char cur_char = input.charAt(i);
if (openingBrackets.containsKey(cur_char)) {
bracketStack.push(cur_char);
}
if (closingBrackets.containsKey(cur_char)) {
if (bracketStack.isEmpty()) {
return false;
}
Character last = (Character) bracketStack.pop();
if (last != closingBrackets.get(cur_char)) {
return false;
}
}
}
return bracketStack.isEmpty();
}
}

View File

@ -3,9 +3,10 @@ package ch.zhaw.ads;
import org.junit.Test; import org.junit.Test;
import org.junit.Before; import org.junit.Before;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class BracketServerTest { public class BracketServerTest {
/* BracketServer bs; BracketServer bs;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -24,6 +25,4 @@ public class BracketServerTest {
test("[(3 +3)* 35 +3]* {3 +2}",true); test("[(3 +3)* 35 +3]* {3 +2}",true);
test("[({3 +3)* 35} +3]* {3 +2}",false); test("[({3 +3)* 35} +3]* {3 +2}",false);
} }
*/
} }