-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExpression.java
More file actions
32 lines (25 loc) · 854 Bytes
/
Expression.java
File metadata and controls
32 lines (25 loc) · 854 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
package domain.expression;
import domain.expression.operator.Operator;
public class Expression {
private int left;
private int right;
private Operator operator;
private Expression(int left, int right, Operator operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
public static Expression of(int left, int right, String operator) {
return new Expression(left, right, Operator.from(operator));
}
public static Expression of(Expression left, int right, String operator) {
int leftResult = left.evaluate();
return new Expression(leftResult, right, Operator.from(operator));
}
public static Expression from(int left) {
return of(left, 0, "+");
}
public int evaluate() {
return operator.operate(left, right);
}
}