-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathOperator.java
More file actions
44 lines (36 loc) · 928 Bytes
/
Operator.java
File metadata and controls
44 lines (36 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package calcproject.engine;
import java.util.Arrays;
public enum Operator {
Plus("+", 0, (a, b) -> a + b),
Minus("-", 0, (a, b) -> a - b),
Multiply("*", 1, (a, b) -> a * b),
Divide("/", 1, (a, b) -> a / b),
UnSupportedOp("", -1, (a, b) -> Double.NaN);
private String operator;
private int priority;
private Calculable calculable;
Operator(String operator, int priority, Calculable calculable) {
this.operator = operator;
this.priority = priority;
this.calculable = calculable;
}
public static Operator opValueOf(String op) {
return Arrays.stream(values())
.filter(
value -> value
.getOperator()
.equals(op)
)
.findAny()
.orElse(Operator.UnSupportedOp);
}
public String getOperator() {
return this.operator;
}
public int getPriority() {
return this.priority;
}
public double calculate(double num1, double num2) {
return this.calculable.calculate(num1, num2);
}
}