Skip to content

Commit b214d18

Browse files
committed
feat: 연산자 구현
1 parent e97894a commit b214d18

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.wonu606.calculator.model;
2+
3+
import com.wonu606.util.CalculatorMessage;
4+
5+
public enum Operator {
6+
ADD("+", 2){
7+
@Override
8+
public double apply(double a, double b) {
9+
return a + b;
10+
}
11+
},
12+
SUBTRACT("-", 2) {
13+
@Override
14+
public double apply(double a, double b) {
15+
return a - b;
16+
}
17+
},
18+
MULTIPLY("*", 1) {
19+
@Override
20+
public double apply(double a, double b) {
21+
return a * b;
22+
}
23+
},
24+
DIVIDE("*", 1) {
25+
@Override
26+
public double apply(double a, double b) {
27+
if (b == 0) {
28+
throw new ArithmeticException(CalculatorMessage.NOT_DIVISIBLE_BY_ZERO.message);
29+
}
30+
return a / b;
31+
}
32+
};
33+
34+
public final String symbol;
35+
public final int precedence;
36+
37+
Operator(String symbol, int precedence) {
38+
this.symbol = symbol;
39+
this.precedence = precedence;
40+
}
41+
42+
public abstract double apply(double a, double b);
43+
44+
public static Operator get(String symbol) {
45+
for (Operator operator : values()) {
46+
if (operator.symbol.equals(symbol)) {
47+
return operator;
48+
}
49+
}
50+
return null;
51+
}
52+
}

calculator/src/main/java/com/wonu606/calculator/util/CalculatorMessage.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public enum CalculatorMessage {
44
INVALID_ORDER("잘못된 순번입니다."),
5-
INVALID_INPUT("잘못된 입력입니다.");
5+
INVALID_INPUT("잘못된 입력입니다."),
6+
NOT_DIVISIBLE_BY_ZERO("0으로 나눌 수 없습니다.");
67

78
public final String message;
89

0 commit comments

Comments
 (0)