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.ArrayList; 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, CreateCycleException, BadSyntaxException { File fichier = new File("grille.ser"); // 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 choix=0; String column=""; String formula=""; int line; myLanguage(); do { menu(); System.out.print("VOTRE CHOIX? "); choix = in.nextInt(); switch (choix) { 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 (choix!=0); oos.writeObject(grid); oos.close(); System.out.println("\n Adiós"); } 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"); } static void createCase(Grid grid) throws CreateCycleException, CellNotFoundException, BadSyntaxException { Scanner in=new Scanner(System.in); String column=""; String formula=""; int line; int choix=0; double value; do { System.out.println("\n1: Case avec valeur\n2: Case avec formule\n0: Quitter"); System.out.print("votre choix? "); choix = in.nextInt(); switch (choix) { 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 (choix!=0); } static void gridPrint(Grid grid) throws CreateCycleException, CellNotFoundException, BadSyntaxException { Scanner in=new Scanner(System.in); List cells = grid.getCells(); int choix=0; 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? "); choix = in.nextInt(); switch (choix) { 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 (choix!=0); } static void setCase(Grid grid) throws CreateCycleException, CellNotFoundException, BadSyntaxException { Scanner in=new Scanner(System.in); String column=""; String formula=""; int line; int choix=0; double value; do { System.out.println("\n1: Modifier case avec valeur\n2: Modifier case avec formule\n0: QUITTER"); System.out.print("votre choix? "); choix = in.nextInt(); switch (choix) { 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 (choix!=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 cells = Arrays.stream(functionMatcher.group(2).split(",")) .map(c -> grid.getCell(c)) .collect(Collectors.toList()); if (cells.contains(null)) throw new CellNotFoundException(); 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(); 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; } 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; } } }