Commit 94ba86ffeebd3703045bea479e6d0ecd00becda3
1 parent
6ccf7ee5
Auto stash before merge of "master" and "origin/master"
Showing
7 changed files
with
112 additions
and
65 deletions
Show diff stats
src/app/Application.java
1 | 1 | package app; |
2 | 2 | |
3 | +import kernel.Cell; | |
3 | 4 | import kernel.Grid; |
5 | +import kernel.exception.CellNotFoundException; | |
6 | +import kernel.function.Average; | |
7 | +import kernel.function.Sum; | |
8 | + | |
9 | +import java.util.ArrayList; | |
10 | +import java.util.List; | |
4 | 11 | |
5 | 12 | public class Application { |
6 | 13 | |
7 | 14 | public static void main(String[] args) { |
8 | 15 | Grid grid = new Grid(); |
9 | 16 | |
10 | - System.out.println("Hello world!"); | |
17 | + // Création | |
18 | + System.out.println("Création de quelques cases..."); | |
19 | + grid.createCell("A", 1, 10.); | |
20 | + grid.createCell("B", 1, 0.); | |
21 | + grid.createCell("A", 2, 5.); | |
22 | + | |
23 | + try { | |
24 | + List<Cell> sumList = new ArrayList<>(); | |
25 | + sumList.add(grid.getCell("A", 1)); | |
26 | + sumList.add(grid.getCell("A", 2)); | |
27 | + | |
28 | + grid.createCell("A", 3, new Sum(sumList)); | |
29 | + | |
30 | + List<Cell> averageList = new ArrayList<>(); | |
31 | + averageList.add(grid.getCell("A", 3)); | |
32 | + averageList.add(grid.getCell("B", 1)); | |
33 | + | |
34 | + grid.createCell("B", 2, new Average(averageList)); | |
35 | + } catch (CellNotFoundException exception) { | |
36 | + System.out.println(exception.getMessage()); | |
37 | + } | |
38 | + | |
39 | + // Affichage | |
40 | + List<Cell> cells = grid.getCells(); | |
41 | + | |
42 | + System.out.println("Affichage des valeurs :"); | |
43 | + for (Cell cell : cells) | |
44 | + System.out.println(cell.getId() + ": " + cell.getValue()); | |
45 | + | |
46 | + System.out.println("Affichage des formules :"); | |
47 | + for (Cell cell : cells) | |
48 | + System.out.println(cell.getId() + ": " + cell.toString()); | |
49 | + | |
50 | + System.out.println("Affichage des formules développées :"); | |
51 | + for (Cell cell : cells) | |
52 | + System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); | |
11 | 53 | } |
12 | 54 | } | ... | ... |
src/kernel/Cell.java
... | ... | @@ -20,7 +20,7 @@ public class Cell { |
20 | 20 | public Cell(String column, Integer line, Formula formula) { |
21 | 21 | this.column = column; |
22 | 22 | this.line = line; |
23 | - this.formula = formula; | |
23 | + this.setFormula(formula); | |
24 | 24 | } |
25 | 25 | |
26 | 26 | public Double getValue() { |
... | ... | @@ -32,7 +32,7 @@ public class Cell { |
32 | 32 | } |
33 | 33 | |
34 | 34 | public String getDevelopedFormula() { |
35 | - return containFormula() ? this.formula.getDevelopedFormula() : this.toString(); | |
35 | + return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId(); | |
36 | 36 | } |
37 | 37 | |
38 | 38 | public String getId() { |
... | ... | @@ -44,7 +44,7 @@ public class Cell { |
44 | 44 | } |
45 | 45 | |
46 | 46 | public String toString() { |
47 | - return containFormula() ? this.formula.toString() : this.getId(); | |
47 | + return this.containFormula() ? this.formula.toString() : this.getId(); | |
48 | 48 | } |
49 | 49 | |
50 | 50 | public void updateValue() { |
... | ... | @@ -57,6 +57,7 @@ public class Cell { |
57 | 57 | |
58 | 58 | public void setFormula(Formula formula) { |
59 | 59 | this.formula = formula; |
60 | + this.updateValue(); | |
60 | 61 | this.spread(); |
61 | 62 | } |
62 | 63 | ... | ... |
src/kernel/Grid.java
... | ... | @@ -2,15 +2,18 @@ package kernel; |
2 | 2 | |
3 | 3 | import kernel.exception.CellNotFoundException; |
4 | 4 | |
5 | +import java.util.ArrayList; | |
5 | 6 | import java.util.HashMap; |
7 | +import java.util.List; | |
6 | 8 | import java.util.Map; |
7 | 9 | |
8 | 10 | public class Grid { |
9 | 11 | |
10 | 12 | private Map<String, Cell> cells = new HashMap<>(); |
13 | + public static LanguageEnum language = LanguageEnum.FR; | |
11 | 14 | |
12 | 15 | public String createCell(String column, Integer line, Double value) { |
13 | - String id = this.getId(column, line); | |
16 | + String id = this.getCellId(column, line); | |
14 | 17 | Cell cell = new Cell(column, line, value); |
15 | 18 | |
16 | 19 | this.cells.put(id, cell); |
... | ... | @@ -19,7 +22,7 @@ public class Grid { |
19 | 22 | } |
20 | 23 | |
21 | 24 | public String createCell(String column, Integer line, Formula formula) { |
22 | - String id = this.getId(column, line); | |
25 | + String id = this.getCellId(column, line); | |
23 | 26 | Cell cell = new Cell(column, line, formula); |
24 | 27 | |
25 | 28 | this.cells.put(id, cell); |
... | ... | @@ -28,50 +31,39 @@ public class Grid { |
28 | 31 | } |
29 | 32 | |
30 | 33 | public void setValue(String column, Integer line, Double value) throws CellNotFoundException { |
31 | - Cell cell = this.getCell(column, line); | |
32 | - | |
33 | - try { | |
34 | - cell.setValue(value); | |
35 | - } catch (NullPointerException exception) { | |
36 | - throw new CellNotFoundException(); | |
37 | - } | |
34 | + this.getCell(column, line).setValue(value); | |
38 | 35 | } |
39 | 36 | |
40 | 37 | public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException { |
41 | - Cell cell = this.getCell(column, line); | |
38 | + this.getCell(column, line).setFormula(formula); | |
39 | + } | |
40 | + | |
41 | + public Cell getCell(String column, Integer line) throws CellNotFoundException { | |
42 | + Cell cell = this.cells.get(this.getCellId(column, line)); | |
42 | 43 | |
43 | - try { | |
44 | - cell.setFormula(formula); | |
45 | - } catch (NullPointerException exception) { | |
44 | + if (cell != null) | |
45 | + return cell; | |
46 | + else | |
46 | 47 | throw new CellNotFoundException(); |
47 | - } | |
48 | 48 | } |
49 | 49 | |
50 | - public Cell getCell(String column, Integer line) { | |
51 | - return this.cells.get(this.getId(column, line)); | |
50 | + public List<Cell> getCells() { | |
51 | + return new ArrayList<>(this.cells.values()); | |
52 | 52 | } |
53 | 53 | |
54 | 54 | public Double getValue(String column, Integer line) throws CellNotFoundException { |
55 | - Cell cell = this.getCell(column, line); | |
56 | - | |
57 | - try { | |
58 | - return cell.getValue(); | |
59 | - } catch (NullPointerException exception) { | |
60 | - throw new CellNotFoundException(); | |
61 | - } | |
55 | + return this.getCell(column, line).getValue(); | |
62 | 56 | } |
63 | 57 | |
64 | 58 | public String getFormulaAsString(String column, Integer line) throws CellNotFoundException { |
65 | - Cell cell = this.getCell(column, line); | |
59 | + return this.getCell(column, line).toString(); | |
60 | + } | |
66 | 61 | |
67 | - try { | |
68 | - return cell.getFormula().toString(); | |
69 | - } catch (NullPointerException exception) { | |
70 | - throw new CellNotFoundException(); | |
71 | - } | |
62 | + public String getDevelopedFormula(String column, Integer line) throws CellNotFoundException { | |
63 | + return this.getCell(column, line).getDevelopedFormula(); | |
72 | 64 | } |
73 | 65 | |
74 | - private String getId(String column, Integer line) { | |
66 | + private String getCellId(String column, Integer line) { | |
75 | 67 | return column + line.toString(); |
76 | 68 | } |
77 | 69 | } | ... | ... |
src/kernel/function/Average.java
1 | 1 | package kernel.function; |
2 | 2 | |
3 | 3 | import kernel.Cell; |
4 | +import kernel.LanguageEnum; | |
4 | 5 | |
6 | +import java.util.List; | |
5 | 7 | import java.util.OptionalDouble; |
6 | -import java.util.stream.Collectors; | |
7 | 8 | |
8 | 9 | public class Average extends Function { |
9 | 10 | |
10 | - public String toString() { | |
11 | - return "MOYENNE(" + this.listCells.stream() | |
12 | - .map(Cell::getId) | |
13 | - .collect(Collectors.joining(",")) | |
14 | - + ")"; | |
15 | - } | |
16 | - | |
17 | - public String getDevelopedFormula() { | |
18 | - return "MOYENNE(" + this.listCells.stream() | |
19 | - .map(Cell::getDevelopedFormula) | |
20 | - .collect(Collectors.joining(",")) | |
21 | - + ")"; | |
11 | + public Average(List<Cell> listCells) { | |
12 | + super(listCells); | |
13 | + this.names.put(LanguageEnum.FR, "MOYENNE"); | |
14 | + this.names.put(LanguageEnum.EN, "AVERAGE"); | |
22 | 15 | } |
23 | 16 | |
24 | 17 | public Double eval() { | ... | ... |
src/kernel/function/Function.java
... | ... | @@ -2,20 +2,41 @@ package kernel.function; |
2 | 2 | |
3 | 3 | import kernel.Cell; |
4 | 4 | import kernel.Formula; |
5 | +import kernel.Grid; | |
6 | +import kernel.LanguageEnum; | |
5 | 7 | |
8 | +import java.util.HashMap; | |
9 | +import java.util.List; | |
10 | +import java.util.Map; | |
11 | +import java.util.stream.Collectors; | |
6 | 12 | import java.util.ArrayList; |
7 | 13 | import java.util.Iterator; |
8 | 14 | |
9 | 15 | abstract public class Function extends Formula { |
10 | 16 | |
11 | - protected ArrayList<Cell> listCells = new ArrayList<>(); | |
17 | + protected Map<LanguageEnum, String> names = new HashMap<>(); | |
18 | + protected List<Cell> listCells; | |
12 | 19 | |
13 | - abstract public String getDevelopedFormula(); | |
14 | - | |
15 | - abstract public String toString(); | |
20 | + public Function(List<Cell> listCells) { | |
21 | + this.listCells = listCells; | |
22 | + } | |
16 | 23 | |
17 | 24 | abstract public Double eval(); |
18 | 25 | |
26 | + public String getDevelopedFormula() { | |
27 | + return names.get(Grid.language) + "(" | |
28 | + + this.listCells.stream() | |
29 | + .map(Cell::getDevelopedFormula) | |
30 | + .collect(Collectors.joining(",")) + ")"; | |
31 | + } | |
32 | + | |
33 | + public String toString() { | |
34 | + return names.get(Grid.language) + "(" | |
35 | + + this.listCells.stream() | |
36 | + .map(Cell::getId) | |
37 | + .collect(Collectors.joining(",")) + ")"; | |
38 | + } | |
39 | + | |
19 | 40 | public Boolean createCycle(Cell cell) { |
20 | 41 | |
21 | 42 | boolean cycle=false; | ... | ... |
src/kernel/function/Sum.java
1 | 1 | package kernel.function; |
2 | 2 | |
3 | 3 | import kernel.Cell; |
4 | -import kernel.function.Function; | |
4 | +import kernel.LanguageEnum; | |
5 | 5 | |
6 | -import java.util.stream.Collectors; | |
6 | +import java.util.List; | |
7 | 7 | |
8 | 8 | public class Sum extends Function { |
9 | 9 | |
10 | - public String toString() { | |
11 | - return "SOMME(" + this.listCells.stream() | |
12 | - .map(Cell::getId) | |
13 | - .collect(Collectors.joining(",")) | |
14 | - + ")"; | |
15 | - } | |
16 | - | |
17 | - public String getDevelopedFormula() { | |
18 | - return "SOMME(" + this.listCells.stream() | |
19 | - .map(Cell::getDevelopedFormula) | |
20 | - .collect(Collectors.joining(",")) | |
21 | - + ")"; | |
10 | + public Sum(List<Cell> listCells) { | |
11 | + super(listCells); | |
12 | + this.names.put(LanguageEnum.FR, "SOMME"); | |
13 | + this.names.put(LanguageEnum.EN, "SUM"); | |
22 | 14 | } |
23 | 15 | |
24 | 16 | public Double eval() { | ... | ... |