-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperator.java
More file actions
45 lines (36 loc) · 1.36 KB
/
Operator.java
File metadata and controls
45 lines (36 loc) · 1.36 KB
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
45
package calculator;
import java.util.function.BiFunction;
public enum Operator {
PLUS("+" , Integer::sum),
MINUS("-" , (previousValue, nextValue) -> previousValue - nextValue),
MULTIPLY("*" , (previousValue , nextValue) -> previousValue * nextValue),
DIVISION("/" , (previousValue , nextValue) -> {
divisionValidationCheck(nextValue);
return previousValue / nextValue;
});
private String operator;
private BiFunction<Integer , Integer , Integer> calculate;
Operator(String operator , BiFunction<Integer , Integer , Integer> calculate) {
this.operator = operator;
this.calculate = calculate;
}
public String getOperator() {
return operator;
}
public int calculate(int previousValue , int nextValue) {
return calculate.apply(previousValue , nextValue);
}
public static Operator toEnum(String operator) {
for (Operator operationOperator : Operator.values()) {
if (operationOperator.getOperator().equals(operator)) {
return operationOperator;
}
}
throw new IllegalArgumentException("잘못된 기호입니다.");
}
private static void divisionValidationCheck(int divisionValue) {
if (divisionValue == 0) {
throw new IllegalArgumentException("0으로 나눌수 없습니다.");
}
}
}