Skip to content

Commit edb334a

Browse files
committed
1 parent 6b6aeb9 commit edb334a

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
1-
# [simple calculator ](https://www.codewars.com/kata/simple-calculator "https://www.codewars.com/kata/5810085c533d69f4980001cf")
1+
# [Simple calculator ](https://www.codewars.com/kata/simple-calculator "https://www.codewars.com/kata/5810085c533d69f4980001cf")
22

33
You are required to create a simple calculator that returns the result of addition, subtraction, multiplication or division of two numbers.
44

5-
Your function will accept three arguments:<br>
6-
The first and second argument should be numbers.<br>
7-
The third argument should represent a sign indicating the operation to perform on these two numbers.
8-
```if-not:csharp,java
9-
if the variables are not numbers or the sign does not belong to the list above a message "unknown value" must be returned.
10-
```
11-
```if:csharp,java
5+
Your function will accept three arguments:
6+
- The first and second argument should be numbers.
7+
- The third argument should represent a sign indicating the operation to perform on these two numbers.
8+
129
If the sign is not a valid sign, throw an IllegalArgumentException (Java) or ArgumentException (C#).
13-
```
1410

1511
# Example:
1612

17-
~~~if-not:csharp,java
18-
```
19-
arguments: 1, 2, "+"
20-
should return 3
21-
22-
arguments: 1, 2, "&"
23-
should return "unknown value"
24-
25-
arguments: 1, "k", "*"
26-
should return "unknown value"
27-
```
28-
~~~
29-
~~~if:csharp,java
3013
```
3114
arguments: 1, 2, "+"
3215
should return 3
3316
3417
arguments: 1, 2, "&"
3518
should throw an IllegalArgumentException (Java) or ArgumentException (C#)
36-
```
37-
~~~
38-
39-
Good luck!
19+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface Calculator {
2+
static double calculate(double a, double b, String op) {
3+
return switch (op) {
4+
case "+" -> a + b;
5+
case "-" -> a - b;
6+
case "*" -> a * b;
7+
case "/" -> b != 0 ? a / b : 0;
8+
default -> throw new IllegalArgumentException();
9+
};
10+
}
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class SolutionTest {
8+
@ParameterizedTest
9+
@CsvSource(textBlock = """
10+
1, 2, +, 3
11+
6, 2, +, 8
12+
4, 3, -, 1
13+
5, 5, *, 25
14+
5, 4, /, 1.25
15+
""")
16+
void basicOperation(double a, double b, String op, double expected) {
17+
assertEquals(expected, Calculator.calculate(a, b, op), 1e-3);
18+
}
19+
20+
@ParameterizedTest
21+
@CsvSource(textBlock = """
22+
6, 2, &
23+
3, 5, \\
24+
5, 5, =
25+
6, 3, '\t'
26+
""")
27+
void unsupportedOperation(double a, double b, String op) {
28+
assertThrows(IllegalArgumentException.class, () -> Calculator.calculate(a, b, op));
29+
}
30+
}

0 commit comments

Comments
 (0)