diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java index 08b0f9f40273..c6d395cb04d5 100644 --- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java @@ -1,18 +1,14 @@ package com.thealgorithms.others; -import java.util.*; +import java.util.Scanner; +import java.util.Stack; -public class StackPostfixNotation { - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" - System.out.println(postfixEvaluate(post)); - scanner.close(); +public final class StackPostfixNotation { + private StackPostfixNotation() { } // Evaluates the given postfix expression string and returns the result. - public static int postfixEvaluate(String exp) { + public static int postfixEvaluate(final String exp) { Stack s = new Stack(); Scanner tokens = new Scanner(exp); @@ -28,12 +24,16 @@ public static int postfixEvaluate(String exp) { case "+" -> s.push(num1 + num2); case "-" -> s.push(num1 - num2); case "*" -> s.push(num1 * num2); - default -> s.push(num1 / num2); + case "/" -> s.push(num1 / num2); + default -> throw new IllegalArgumentException("exp contains an unknown operation."); } // "+", "-", "*", "/" } } tokens.close(); + if (s.size() != 1) { + throw new IllegalArgumentException("exp is not a proper postfix expression."); + } return s.pop(); } } diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java new file mode 100644 index 000000000000..4894b403ea06 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.others; + +import static java.util.Map.entry; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Map; +import org.junit.jupiter.api.Test; + +public class StackPostfixNotationTest { + @Test + public void testEvaluate() { + final Map testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("-5 -2 -", -3), entry("5 2 + 3 *", 21), entry("-5", -5)); + for (final var tc : testCases.entrySet()) { + assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); + } + } + + @Test + public void testIfEvaluateThrowsExceptionForEmptyInput() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("")); + } + + @Test + public void testIfEvaluateThrowsExceptionForInproperInput() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3")); + } + + @Test + public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !")); + } +}