-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInterpretorMain.java
More file actions
49 lines (40 loc) · 2.28 KB
/
InterpretorMain.java
File metadata and controls
49 lines (40 loc) · 2.28 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
46
47
48
49
package behavioral.interpretor;
import behavioral.interpretor.expressions.Expression;
import behavioral.interpretor.expressions.impl.compute.AddExpression;
import behavioral.interpretor.expressions.impl.compute.MultiplyExpression;
import behavioral.interpretor.expressions.impl.compute.NumberExpression;
import behavioral.interpretor.expressions.impl.translate.EnglishTranslationContext;
import behavioral.interpretor.expressions.impl.translate.FrenchTranslationContext;
import behavioral.interpretor.expressions.impl.translate.WordExpression;
public class InterpretorMain {
public static void main(String[] args) {
computeExpression();
translateExpression();
}
private static void computeExpression() {
Expression<Integer> expr1 = new NumberExpression(10);
Expression<Integer> expr2 = new NumberExpression(5);
Expression<Integer> expr3 = new NumberExpression(3);
// Construction de l'expression (5 + 3) * 10
Expression<Integer> multiplication = new MultiplyExpression(
new AddExpression(expr2, expr3),
expr1
);
int result = multiplication.interpret();
System.out.println("Result: " + result); // Output: Result: 80
}
private static void translateExpression() {
// Création des expressions avec différents contextes de traduction
Expression<String> expr1 = new WordExpression("hello", new EnglishTranslationContext());
Expression<String> expr2 = new WordExpression("world", new EnglishTranslationContext());
Expression<String> expr3 = new WordExpression("java", new EnglishTranslationContext());
Expression<String> expr4 = new WordExpression("hello", new FrenchTranslationContext());
Expression<String> expr5 = new WordExpression("world", new FrenchTranslationContext());
Expression<String> expr6 = new WordExpression("java", new FrenchTranslationContext());
// Interprétation des expressions dans différentes langues
System.out.println("English Translation:");
System.out.println(expr1.interpret() + " " + expr2.interpret() + " " + expr3.interpret());
System.out.println("French Translation:");
System.out.println(expr4.interpret() + " " + expr5.interpret() + " " + expr6.interpret());
}
}