forked from next-step/java-racingcar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathSymbol.java
45 lines (37 loc) · 1.33 KB
/
MathSymbol.java
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.Arrays;
import static calculator.ExceptionMessage.IS_NOT_MATH_EXPRESSION_MESSAGE;
public enum MathSymbol {
PLUS("+"),
MINUS("-"),
DIVIDE("/"),
MULTIPLE("*");
private final String mathSymbol;
MathSymbol(String mathSymbol) {
this.mathSymbol = mathSymbol;
}
public String getMathSymbol() {
return mathSymbol;
}
public static MathSymbol findValidatedSymbol(String operator) {
return Arrays.stream(MathSymbol.values())
.filter(mathSymbol -> mathSymbol.getMathSymbol().equals(operator))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("잘못된 연산자 입니다."));
}
public double operate(double firstNumber, double secondNumber) {
if (PLUS.getMathSymbol() == this.getMathSymbol()) {
return firstNumber + secondNumber;
}
if (MINUS.getMathSymbol() == this.getMathSymbol()) {
return firstNumber - secondNumber;
}
if (MULTIPLE.getMathSymbol() == this.getMathSymbol()) {
return firstNumber * secondNumber;
}
if (DIVIDE.getMathSymbol() == this.getMathSymbol()) {
return firstNumber / secondNumber;
}
throw new IllegalArgumentException(IS_NOT_MATH_EXPRESSION_MESSAGE);
}
}