-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComandoIf.java
More file actions
80 lines (73 loc) · 2.24 KB
/
ComandoIf.java
File metadata and controls
80 lines (73 loc) · 2.24 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList;
import java.util.Scanner;
public class ComandoIf extends Comando{
public static final int IF_MODE = 1;
public static final int ELSE_MODE = 2;
private ArrayList<Comando> commandList;
private ArrayList<Comando> elseList;
private String logicalExpr;
private int mode;
public ComandoIf(){
this.commandList = new ArrayList<Comando>();
this.elseList = new ArrayList<Comando>();
this.mode = IF_MODE;
}
public void setLogicalExpr(String expr){
this.logicalExpr = expr;
}
public void addCommand(Comando comando){
if (mode == IF_MODE)
this.commandList.add(comando);
else
this.elseList.add(comando);
}
public void changeMode(int mode){
this.mode = mode;
}
public void run(){
Scanner teclado = new Scanner(System.in);
System.out.println("A expr é verdadeira? 1 / 0");
int opcao = teclado.nextInt();
if (opcao == 1){
for (Comando c: commandList){
c.run();
}
}
}
public String writeCode(){
StringBuilder str = new StringBuilder();
str.append("<if expr=\"").append(logicalExpr).append("\">\n");
str.append("<commandList>\n");
for(Comando c: commandList){
str.append(c.writeCode());
}
str.append("</commandList>\n");
if (!elseList.isEmpty()){
str.append("<elseList>\n");
for (Comando c: elseList){
str.append(c.writeCode());
}
str.append("</elseList>\n");
}
str.append("</if>\n");
return str.toString();
}
public String writeJava(){
StringBuilder str = new StringBuilder();
str.append("if (");
str.append(logicalExpr);
str.append("){\n");
for (Comando c: commandList){
str.append(c.writeJava());
}
str.append("}\n");
if (!elseList.isEmpty()){
str.append("else{\n");
for (Comando c: elseList){
str.append(c.writeJava());
}
str.append("}\n");
}
return str.toString();
}
}