File tree 2 files changed +54
-1
lines changed
calculator/src/main/java/com/wonu606/calculator
2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
public enum CalculatorMessage {
4
4
INVALID_ORDER ("잘못된 순번입니다." ),
5
- INVALID_INPUT ("잘못된 입력입니다." );
5
+ INVALID_INPUT ("잘못된 입력입니다." ),
6
+ NOT_DIVISIBLE_BY_ZERO ("0으로 나눌 수 없습니다." );
6
7
7
8
public final String message ;
8
9
You can’t perform that action at this time.
0 commit comments