diff --git a/kata/8-kyu/index.md b/kata/8-kyu/index.md index 81ee98c9..9c7d0caa 100644 --- a/kata/8-kyu/index.md +++ b/kata/8-kyu/index.md @@ -180,6 +180,7 @@ - [Rock Paper Scissors!](rock-paper-scissors "5672a98bdbdd995fad00000f") - [Sentence Smash](sentence-smash "53dc23c68a0c93699800041d") - [Short Long Short](short-long-short "50654ddff44f800200000007") +- [simple calculator ](simple-calculator "5810085c533d69f4980001cf") - [Simple Fun #1: Seats in Theater](simple-fun-number-1-seats-in-theater "588417e576933b0ec9000045") - [Simple multiplication](simple-multiplication "583710ccaa6717322c000105") - [Simple validation of a username with regex](simple-validation-of-a-username-with-regex "56a3f08aa9a6cc9b75000023") diff --git a/kata/8-kyu/simple-calculator/README.md b/kata/8-kyu/simple-calculator/README.md new file mode 100644 index 00000000..f196fe33 --- /dev/null +++ b/kata/8-kyu/simple-calculator/README.md @@ -0,0 +1,19 @@ +# [Simple calculator ](https://www.codewars.com/kata/simple-calculator "https://www.codewars.com/kata/5810085c533d69f4980001cf") + +You are required to create a simple calculator that returns the result of addition, subtraction, multiplication or division of two numbers. + +Your function will accept three arguments: + - The first and second argument should be numbers. +- The third argument should represent a sign indicating the operation to perform on these two numbers. + +If the sign is not a valid sign, throw an IllegalArgumentException (Java) or ArgumentException (C#). + +# Example: + +``` +arguments: 1, 2, "+" +should return 3 + +arguments: 1, 2, "&" +should throw an IllegalArgumentException (Java) or ArgumentException (C#) +``` \ No newline at end of file diff --git a/kata/8-kyu/simple-calculator/main/Calculator.java b/kata/8-kyu/simple-calculator/main/Calculator.java new file mode 100644 index 00000000..61073244 --- /dev/null +++ b/kata/8-kyu/simple-calculator/main/Calculator.java @@ -0,0 +1,11 @@ +interface Calculator { + static double calculate(double a, double b, String op) { + return switch (op) { + case "+" -> a + b; + case "-" -> a - b; + case "*" -> a * b; + case "/" -> a / b; + default -> throw new IllegalArgumentException(); + }; + } +} \ No newline at end of file diff --git a/kata/8-kyu/simple-calculator/test/SolutionTest.java b/kata/8-kyu/simple-calculator/test/SolutionTest.java new file mode 100644 index 00000000..3ad8f647 --- /dev/null +++ b/kata/8-kyu/simple-calculator/test/SolutionTest.java @@ -0,0 +1,30 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 1, 2, +, 3 + 6, 2, +, 8 + 4, 3, -, 1 + 5, 5, *, 25 + 5, 4, /, 1.25 + """) + void basicOperation(double a, double b, String op, double expected) { + assertEquals(expected, Calculator.calculate(a, b, op), 1e-3); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + 6, 2, & + 3, 5, \\ + 5, 5, = + 6, 3, '\t' + """) + void unsupportedOperation(double a, double b, String op) { + assertThrows(IllegalArgumentException.class, () -> Calculator.calculate(a, b, op)); + } +} \ No newline at end of file