diff --git a/src/main/java/ic/doc/IntOperator.java b/src/main/java/ic/doc/IntOperator.java index 698c16c..35ebd9c 100644 --- a/src/main/java/ic/doc/IntOperator.java +++ b/src/main/java/ic/doc/IntOperator.java @@ -4,10 +4,12 @@ import java.util.List; import java.util.function.Function; public class IntOperator { + private final String symbol; private final int operands; private final Function, Integer> operator; - IntOperator(int operands, Function, Integer> operator) { + IntOperator(String symbol, int operands, Function, Integer> operator) { + this.symbol = symbol; this.operands = operands; this.operator = operator; } @@ -24,4 +26,9 @@ public class IntOperator { } return operator.apply(arguments); } + + @Override + public String toString() { + return symbol; + } } diff --git a/src/main/java/ic/doc/ReversePolishStack.java b/src/main/java/ic/doc/ReversePolishStack.java index b74c5e1..058c1aa 100644 --- a/src/main/java/ic/doc/ReversePolishStack.java +++ b/src/main/java/ic/doc/ReversePolishStack.java @@ -1,6 +1,7 @@ package ic.doc; import java.util.Stack; +import java.util.stream.Collectors; public class ReversePolishStack { private final Stack stack = new Stack<>(); @@ -34,7 +35,12 @@ public class ReversePolishStack { if (!stack.isEmpty()) { throw new ArithmeticException("Invalid notation, too many operators"); } - stack.push(new IntOperator(0, args -> result)); + stack.push(new IntOperator(Integer.toString(result), 0, args -> result)); return result; } + + @Override + public String toString() { + return stack.stream().map(IntOperator::toString).collect(Collectors.joining(" ")); + } } diff --git a/src/test/java/ic/doc/IntOperatorTest.java b/src/test/java/ic/doc/IntOperatorTest.java index b0244d3..027bdb6 100644 --- a/src/test/java/ic/doc/IntOperatorTest.java +++ b/src/test/java/ic/doc/IntOperatorTest.java @@ -11,13 +11,13 @@ import static org.junit.Assert.fail; public class IntOperatorTest { @Test public void intOperatorCanEvaluate() { - IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1)); + IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1)); assertThat(intOperator.evaluate(List.of(1, 2)), is(3)); } @Test public void intOperatorThrowsOnInvalidArguments() { - IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1)); + IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1)); try { intOperator.evaluate(List.of(1)); fail("Expected IllegalArgumentException to be thrown on invalid number of arguments"); @@ -28,7 +28,13 @@ public class IntOperatorTest { @Test public void canGetOperatorOperandsCount() { - IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1)); + IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1)); assertThat(intOperator.getOperands(), is(2)); } + + @Test + public void operatorToStringReturnsSymbol() { + IntOperator intOperator = new IntOperator("symbol", 0, args -> 0); + assertThat(intOperator.toString(), is("symbol")); + } } diff --git a/src/test/java/ic/doc/ReversePolishStackTest.java b/src/test/java/ic/doc/ReversePolishStackTest.java index 562e46e..ec4419f 100644 --- a/src/test/java/ic/doc/ReversePolishStackTest.java +++ b/src/test/java/ic/doc/ReversePolishStackTest.java @@ -16,25 +16,25 @@ public class ReversePolishStackTest { @Test public void canPushToReversePolishStack() { - reversePolishStack.push(new IntOperator(0, args -> 1)); + reversePolishStack.push(new IntOperator("1", 0, args -> 1)); assertThat(reversePolishStack.getSize(), is(1)); - reversePolishStack.push(new IntOperator(0, args -> 2)); + reversePolishStack.push(new IntOperator("2", 0, args -> 2)); assertThat(reversePolishStack.getSize(), is(2)); } @Test public void canEvaluateReversePolishStack() { - reversePolishStack.push(new IntOperator(0, args -> 10)); - reversePolishStack.push(new IntOperator(0, args -> 25)); - reversePolishStack.push(new IntOperator(2, args -> args.get(0) + args.get(1))); + reversePolishStack.push(new IntOperator("10", 0, args -> 10)); + reversePolishStack.push(new IntOperator("25", 0, args -> 25)); + reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1))); assertThat(reversePolishStack.evaluate(), is(35)); } @Test public void evaluatePushesBack() { - reversePolishStack.push(new IntOperator(0, args -> 10)); - reversePolishStack.push(new IntOperator(0, args -> 25)); - reversePolishStack.push(new IntOperator(2, args -> args.get(0) + args.get(1))); + reversePolishStack.push(new IntOperator("10", 0, args -> 10)); + reversePolishStack.push(new IntOperator("25", 0, args -> 25)); + reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1))); reversePolishStack.evaluate(); assertThat(reversePolishStack.getSize(), is(1)); assertThat(reversePolishStack.evaluate(), is(35)); @@ -42,8 +42,8 @@ public class ReversePolishStackTest { @Test public void evaluateThrowsOnInvalidNotation() { - reversePolishStack.push(new IntOperator(0, args -> 10)); - reversePolishStack.push(new IntOperator(0, args -> 25)); + reversePolishStack.push(new IntOperator("10", 0, args -> 10)); + reversePolishStack.push(new IntOperator("25", 0, args -> 25)); try { reversePolishStack.evaluate(); fail("Expected ArithmeticException to be thrown when too many operators"); @@ -51,4 +51,16 @@ public class ReversePolishStackTest { // good } } + + @Test + public void testReversePolishStackString() { + reversePolishStack.push(new IntOperator("10", 0, args -> 10)); + reversePolishStack.push(new IntOperator("25", 0, args -> 25)); + reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1))); + reversePolishStack.push(new IntOperator("1", 0, args -> 10)); + reversePolishStack.push(new IntOperator("9", 0, args -> 25)); + reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1))); + reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1))); + assertThat(reversePolishStack.toString(), is("10 25 + 1 9 - *")); + } }