Menu.java
8.45 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package app;
import kernel.Cell;
import kernel.Formula;
import kernel.Grid;
import kernel.LanguageEnum;
import kernel.exception.BadSyntaxException;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import kernel.function.Average;
import kernel.function.Sum;
import kernel.operation.Addition;
import kernel.operation.Division;
import kernel.operation.Multiplication;
import kernel.operation.Subtraction;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Menu {
public static void main(String[] args) throws IOException, CellNotFoundException {
File fichier = new File("grid.data");
// ouverture d'un flux sur un fichier
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier));
Grid grid = new Grid();
Scanner in = new Scanner(System.in);
int choice;
String column;
int line;
myLanguage();
do {
menu();
System.out.print("VOTRE CHOIX? ");
choice = in.nextInt();
switch (choice) {
case 1:
try {
createCase(grid);
} catch (CreateCycleException | CellNotFoundException | BadSyntaxException e) {
e.getMessage();
}
break;
case 2:
try {
setCase(grid);
} catch (CreateCycleException | CellNotFoundException | BadSyntaxException e) {
e.getMessage();
}
break;
case 3:
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println(grid.getCell(column, line).getId() + ": " + grid.getCell(column, line).getDevelopedFormula());
break;
case 4:
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println(grid.getCell(column, line).getId() + ": " + grid.getCell(column, line).toString());
break;
case 5:
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println(grid.getCell(column, line).getId() + ": " + grid.getCell(column, line).getValue());
break;
case 6:
gridPrint(grid);
break;
case 0: // quitter
}
} while (choice != 0);
oos.writeObject(grid);
oos.close();
System.out.println("\n Adiós");
}
private static void menu() {
System.out.println("\n1: CREER UNE CASE \n2: MODIFIER UNE CASE\n3:AFFICHER FORMULE DEVELOPPEEE CASE\n4:AFFICHER FORMULE CASE\n5:AFFICHER VALEUR CASE\n6:AFFICHER GRILLE\n0: QUITTER");
}
private static void createCase(Grid grid) throws CreateCycleException, CellNotFoundException, BadSyntaxException {
Scanner in = new Scanner(System.in);
String column;
String formula;
int line;
int choice;
double value;
do {
System.out.println("\n1: Case avec valeur\n2: Case avec formule\n0: Quitter");
System.out.print("votre choix? ");
choice = in.nextInt();
switch (choice) {
case 1:
try {
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println("\n Entrez la valeur de la case");
value = in.nextDouble();
grid.createCell(column, line, value);
} catch (InvalidIntervalException exception) {
System.out.println(exception.getMessage());
}
break;
case 2:
try {
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println("\n Entrez la formule (Ex: A1+A2 ou SOMME(A1,A2)");
formula = in.next();
grid.createCell(column, line, generateFormulaWithString(formula, grid));
} catch (InvalidIntervalException exception) {
System.out.println(exception.getMessage());
}
break;
case 0: // quitter
}
} while (choice != 0);
}
private static void gridPrint(Grid grid) {
Scanner in = new Scanner(System.in);
List<Cell> cells = grid.getCells();
int choice;
do {
System.out.println("\n1: Afficher cases avec valeurs\n2: Afficher cases avec formules\n3: Afficher cases avec formules dévéloppées\n0: Quitter");
System.out.print("votre choix? ");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.println("Affichage des valeurs :");
for (Cell cell : cells)
System.out.println(cell.getId() + ": " + cell.getValue());
break;
case 2:
System.out.println("Affichage des formules :");
for (Cell cell : cells)
System.out.println(cell.getId() + ": " + cell.toString());
break;
case 3:
System.out.println("Affichage des formules développées :");
for (Cell cell : cells)
System.out.println(cell.getId() + ": " + cell.getDevelopedFormula());
break;
case 0: // quitter
}
} while (choice != 0);
}
private static void setCase(Grid grid) throws CreateCycleException, CellNotFoundException, BadSyntaxException {
Scanner in = new Scanner(System.in);
String column;
String formula;
int line;
int choice;
double value;
do {
System.out.println("\n1: Modifier case avec valeur\n2: Modifier case avec formule\n0: QUITTER");
System.out.print("votre choix? ");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println("\n Entrez la valeur de la case");
value = in.nextDouble();
grid.setValue(column, line, value);
break;
case 2:
System.out.println("\n Entrez la colonne de la case");
column = in.next();
System.out.println("\n Entrez la ligne de la case");
line = in.nextInt();
System.out.println("\n Entrez la formule (Ex: (A1+A2) ou SOMME(A1,A2)");
formula = in.next();
grid.setFormula(column, line, generateFormulaWithString(formula, grid));
break;
case 0: // quitter
}
} while (choice != 0);
}
private static Formula generateFormulaWithString(String input, Grid grid) throws CellNotFoundException, BadSyntaxException {
Pattern functionPattern = Pattern.compile("([A-Z]+)\\(([A-Z0-9,]+)\\)");
Matcher functionMatcher = functionPattern.matcher(input);
if (functionMatcher.matches()) {
List<Cell> cells = Arrays.stream(functionMatcher.group(2).split(","))
.map(c -> grid.getCell(c))
.collect(Collectors.toList());
if (cells.contains(null))
throw new CellNotFoundException("Une des cellules demandées n'existe pas.");
cells = cells.stream().filter(Objects::nonNull).collect(Collectors.toList());
switch (functionMatcher.group(1)) {
case "SUM":
case "SOMME":
return new Sum(cells);
case "AVERAGE":
case "MOYENNE":
return new Average(cells);
}
} else {
Pattern binaryOperationPattern = Pattern.compile("([A-Z]+[0-9]+)([+\\-*/])([A-Z]+[0-9]+)");
Matcher binaryOperationMatcher = binaryOperationPattern.matcher(input);
if (!binaryOperationMatcher.matches())
throw new BadSyntaxException();
Cell leftCell = grid.getCell(binaryOperationMatcher.group(1));
Cell rightCell = grid.getCell(binaryOperationMatcher.group(3));
if (leftCell == null || rightCell == null)
throw new CellNotFoundException("Une des cellules demandées n'existe pas.");
switch (binaryOperationMatcher.group(2)) {
case "+":
return new Addition(leftCell, rightCell);
case "-":
return new Subtraction(leftCell, rightCell);
case "*":
return new Multiplication(leftCell, rightCell);
case "/":
return new Division(leftCell, rightCell);
}
}
return null;
}
private static void myLanguage() {
Scanner in = new Scanner(System.in);
int choix = 0;
System.out.println("\n1: Francais/French\n2: Anglais/English");
System.out.println("votre choix? ");
choix = in.nextInt();
switch (choix) {
case 1:
Grid.language = LanguageEnum.FR;
break;
case 2:
Grid.language = LanguageEnum.EN;
break;
}
}
}