Commit 080a0e68dfee9e542a95e2dd8463c8aa522afffc
Committed by
unknown
1 parent
453ac322
change return
Showing
20 changed files
with
166 additions
and
64 deletions
Show diff stats
.gitignore
grid.data
No preview for this file type
src/ihm/TablooProto.java
... | ... | @@ -277,7 +277,7 @@ public class TablooProto extends JPanel { |
277 | 277 | // Exécution de l'interface graphique a partir d'un terminal. |
278 | 278 | public static void main(String[] args) throws ClassNotFoundException, IOException { |
279 | 279 | try { |
280 | - grid = Grid.load(); | |
280 | + grid = Grid.load("grid.data"); | |
281 | 281 | } catch (IOException | ClassNotFoundException e) { |
282 | 282 | grid = new Grid(); |
283 | 283 | } |
... | ... | @@ -298,7 +298,7 @@ public class TablooProto extends JPanel { |
298 | 298 | @Override |
299 | 299 | public void windowClosing(WindowEvent we) { |
300 | 300 | try { |
301 | - grid.save(); | |
301 | + grid.save("grid.data"); | |
302 | 302 | } catch (IOException e) { |
303 | 303 | System.out.println(e.getMessage()); |
304 | 304 | } | ... | ... |
src/kernel/Cell.java
... | ... | @@ -5,12 +5,11 @@ import kernel.exception.CreateCycleException; |
5 | 5 | import java.io.Serializable; |
6 | 6 | import java.util.ArrayList; |
7 | 7 | import java.util.List; |
8 | +import java.util.Objects; | |
8 | 9 | |
9 | 10 | public class Cell implements Serializable { |
10 | 11 | |
11 | 12 | private static final long serialVersionUID = 1L; |
12 | - | |
13 | - | |
14 | 13 | private String column; |
15 | 14 | private int line; |
16 | 15 | private double value; |
... | ... | @@ -72,7 +71,7 @@ public class Cell implements Serializable { |
72 | 71 | |
73 | 72 | public void setFormula(Formula formula) throws CreateCycleException { |
74 | 73 | if (formula.createCycle(this)) |
75 | - throw new CreateCycleException(); | |
74 | + throw new CreateCycleException("L'assignation de la formule " + formula.toString() + " créée un cycle."); | |
76 | 75 | |
77 | 76 | this.formula = formula; |
78 | 77 | for (Cell cell : this.formula.getUtilisedCells()) |
... | ... | @@ -94,5 +93,20 @@ public class Cell implements Serializable { |
94 | 93 | } |
95 | 94 | } |
96 | 95 | |
96 | + @Override | |
97 | + public boolean equals(Object o) { | |
98 | + if (this == o) | |
99 | + return true; | |
100 | + if (!(o instanceof Cell)) | |
101 | + return false; | |
102 | + | |
103 | + Cell cell = (Cell) o; | |
104 | + | |
105 | + return line == cell.line && column.equals(cell.column); | |
106 | + } | |
97 | 107 | |
108 | + @Override | |
109 | + public int hashCode() { | |
110 | + return Objects.hash(column, line); | |
111 | + } | |
98 | 112 | } | ... | ... |
src/kernel/Formula.java
src/kernel/Grid.java
... | ... | @@ -20,46 +20,42 @@ public class Grid implements Serializable { |
20 | 20 | public static LanguageEnum language = LanguageEnum.FR; |
21 | 21 | |
22 | 22 | public void createCell(String column, int line, double value) throws InvalidIntervalException { |
23 | - column = column.toUpperCase(); | |
24 | - | |
25 | 23 | if (!validateInterval(column, line)) |
26 | 24 | throw new InvalidIntervalException(); |
25 | + | |
27 | 26 | String id = this.getCellId(column, line); |
28 | 27 | Cell cell = new Cell(column, line, value); |
29 | - this.cells.put(id, cell); | |
30 | 28 | |
29 | + this.cells.put(id, cell); | |
31 | 30 | } |
32 | 31 | |
33 | 32 | public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException { |
34 | - column = column.toUpperCase(); | |
35 | - | |
36 | 33 | if (!validateInterval(column, line)) |
37 | 34 | throw new InvalidIntervalException(); |
35 | + | |
38 | 36 | String id = this.getCellId(column, line); |
39 | 37 | Cell cell = new Cell(column, line, formula); |
40 | - this.cells.put(id, cell); | |
41 | 38 | |
39 | + this.cells.put(id, cell); | |
42 | 40 | } |
43 | 41 | |
44 | 42 | public void setValue(String column, int line, double value) throws CellNotFoundException { |
45 | - column = column.toUpperCase(); | |
46 | 43 | this.getCell(column, line).setValue(value); |
47 | 44 | } |
48 | 45 | |
49 | 46 | public void setFormula(String column, int line, Formula formula) throws CellNotFoundException, |
50 | 47 | CreateCycleException { |
51 | - column = column.toUpperCase(); | |
52 | 48 | this.getCell(column, line).setFormula(formula); |
53 | 49 | } |
54 | 50 | |
55 | 51 | public Cell getCell(String column, int line) throws CellNotFoundException { |
56 | - column = column.toUpperCase(); | |
57 | - Cell cell = this.cells.get(this.getCellId(column, line)); | |
52 | + String id = this.getCellId(column, line); | |
53 | + Cell cell = this.cells.get(id); | |
58 | 54 | |
59 | 55 | if (cell != null) |
60 | 56 | return cell; |
61 | 57 | else |
62 | - throw new CellNotFoundException(); | |
58 | + throw new CellNotFoundException("La cellule " + id + " n'existe pas."); | |
63 | 59 | } |
64 | 60 | |
65 | 61 | public Cell getCell(String id) { |
... | ... | @@ -71,26 +67,21 @@ public class Grid implements Serializable { |
71 | 67 | } |
72 | 68 | |
73 | 69 | public double getValue(String column, int line) throws CellNotFoundException { |
74 | - column = column.toUpperCase(); | |
75 | - return this.getCell(column, line).getValue(); | |
70 | + return this.getCell(column.toUpperCase(), line).getValue(); | |
76 | 71 | } |
77 | 72 | |
78 | 73 | public String getFormulaAsString(String column, int line) throws CellNotFoundException { |
79 | - column = column.toUpperCase(); | |
80 | - return this.getCell(column, line).toString(); | |
74 | + return this.getCell(column.toUpperCase(), line).toString(); | |
81 | 75 | } |
82 | 76 | |
83 | 77 | public String getDevelopedFormula(String column, int line) throws CellNotFoundException { |
84 | - column = column.toUpperCase(); | |
85 | - return this.getCell(column, line).getDevelopedFormula(); | |
78 | + return this.getCell(column.toUpperCase(), line).getDevelopedFormula(); | |
86 | 79 | } |
87 | 80 | |
88 | 81 | private String getCellId(String column, int line) { |
89 | - column = column.toUpperCase(); | |
90 | - return column + line; | |
82 | + return column.toUpperCase() + line; | |
91 | 83 | } |
92 | 84 | |
93 | - | |
94 | 85 | public int getTotalColumn() { |
95 | 86 | return MAX_COLUMNS.charAt(0) - (int) 'A' + 1; |
96 | 87 | } |
... | ... | @@ -114,10 +105,10 @@ public class Grid implements Serializable { |
114 | 105 | Cell cell = this.getCell(id); |
115 | 106 | |
116 | 107 | if (!cell.getUsedIn().isEmpty()) |
117 | - throw new CannotDeleteCellException(); | |
108 | + throw new CannotDeleteCellException("La cellule " + id + " est utilisée dans une autre case."); | |
118 | 109 | |
119 | 110 | cell.updateUsedIn(); |
120 | - this.cells.remove(this.getCellId(column, line)); | |
111 | + this.cells.remove(id); | |
121 | 112 | } |
122 | 113 | } |
123 | 114 | |
... | ... | @@ -131,20 +122,21 @@ public class Grid implements Serializable { |
131 | 122 | return ascii; |
132 | 123 | } |
133 | 124 | |
134 | - public void save() throws IOException { | |
135 | - File file = new File("grid.data"); | |
125 | + public void save(String fileName) throws IOException { | |
126 | + File file = new File(fileName); | |
136 | 127 | |
137 | 128 | ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file)); |
138 | 129 | stream.writeObject(this); |
139 | 130 | stream.close(); |
140 | 131 | } |
141 | 132 | |
142 | - public static Grid load() throws IOException, ClassNotFoundException { | |
143 | - File file = new File("grid.data"); | |
133 | + public static Grid load(String fileName) throws IOException, ClassNotFoundException { | |
134 | + File file = new File(fileName); | |
144 | 135 | |
145 | 136 | ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file)); |
137 | + Grid grid = (Grid) stream.readObject(); | |
138 | + stream.close(); | |
146 | 139 | |
147 | - return (Grid) stream.readObject(); | |
140 | + return grid; | |
148 | 141 | } |
149 | - | |
150 | 142 | } | ... | ... |
src/kernel/exception/BadSyntaxException.java
... | ... | @@ -3,6 +3,18 @@ package kernel.exception; |
3 | 3 | public class BadSyntaxException extends Exception { |
4 | 4 | |
5 | 5 | public BadSyntaxException() { |
6 | - super("Erreur de syntaxe."); | |
6 | + super(); | |
7 | + } | |
8 | + | |
9 | + public BadSyntaxException(String message) { | |
10 | + super(message); | |
11 | + } | |
12 | + | |
13 | + public BadSyntaxException(Throwable throwable) { | |
14 | + super(throwable); | |
15 | + } | |
16 | + | |
17 | + public BadSyntaxException(String message, Throwable throwable) { | |
18 | + super(message, throwable); | |
7 | 19 | } |
8 | 20 | } | ... | ... |
src/kernel/exception/CannotDeleteCellException.java
... | ... | @@ -3,6 +3,18 @@ package kernel.exception; |
3 | 3 | public class CannotDeleteCellException extends Exception { |
4 | 4 | |
5 | 5 | public CannotDeleteCellException() { |
6 | - super("Cette cellule est utilisée dans une autre cellule."); | |
6 | + super(); | |
7 | + } | |
8 | + | |
9 | + public CannotDeleteCellException(String message) { | |
10 | + super(message); | |
11 | + } | |
12 | + | |
13 | + public CannotDeleteCellException(Throwable throwable) { | |
14 | + super(throwable); | |
15 | + } | |
16 | + | |
17 | + public CannotDeleteCellException(String message, Throwable throwable) { | |
18 | + super(message, throwable); | |
7 | 19 | } |
8 | 20 | } | ... | ... |
src/kernel/exception/CellNotFoundException.java
... | ... | @@ -3,6 +3,18 @@ package kernel.exception; |
3 | 3 | public class CellNotFoundException extends Exception { |
4 | 4 | |
5 | 5 | public CellNotFoundException() { |
6 | - super("Vous voulez utiliser une cellule qui n'est pas encore créée."); | |
6 | + super(); | |
7 | + } | |
8 | + | |
9 | + public CellNotFoundException(String message) { | |
10 | + super(message); | |
11 | + } | |
12 | + | |
13 | + public CellNotFoundException(Throwable throwable) { | |
14 | + super(throwable); | |
15 | + } | |
16 | + | |
17 | + public CellNotFoundException(String message, Throwable throwable) { | |
18 | + super(message, throwable); | |
7 | 19 | } |
8 | 20 | } | ... | ... |
src/kernel/exception/CreateCycleException.java
1 | 1 | package kernel.exception; |
2 | 2 | |
3 | 3 | public class CreateCycleException extends Exception { |
4 | + | |
4 | 5 | public CreateCycleException() { |
5 | - super("L'aasignation de cette formule créée un cycle."); | |
6 | + super(); | |
7 | + } | |
8 | + | |
9 | + public CreateCycleException(String message) { | |
10 | + super(message); | |
11 | + } | |
12 | + | |
13 | + public CreateCycleException(Throwable throwable) { | |
14 | + super(throwable); | |
15 | + } | |
16 | + | |
17 | + public CreateCycleException(String message, Throwable throwable) { | |
18 | + super(message, throwable); | |
6 | 19 | } |
7 | 20 | } | ... | ... |
src/kernel/exception/InvalidIntervalException.java
1 | 1 | package kernel.exception; |
2 | 2 | |
3 | 3 | public class InvalidIntervalException extends Exception { |
4 | + | |
5 | + public InvalidIntervalException() { | |
6 | + super(); | |
7 | + } | |
8 | + | |
9 | + public InvalidIntervalException(String message) { | |
10 | + super(message); | |
11 | + } | |
12 | + | |
13 | + public InvalidIntervalException(Throwable throwable) { | |
14 | + super(throwable); | |
15 | + } | |
16 | + | |
17 | + public InvalidIntervalException(String message, Throwable throwable) { | |
18 | + super(message, throwable); | |
19 | + } | |
4 | 20 | } | ... | ... |
src/kernel/function/Average.java
... | ... | @@ -10,12 +10,13 @@ public class Average extends Function { |
10 | 10 | |
11 | 11 | public Average(List<Cell> listCells) { |
12 | 12 | super(listCells); |
13 | - this.names.put(LanguageEnum.FR, "MOYENNE"); | |
14 | - this.names.put(LanguageEnum.EN, "AVERAGE"); | |
13 | + this.getNames().put(LanguageEnum.FR, "MOYENNE"); | |
14 | + this.getNames().put(LanguageEnum.EN, "AVERAGE"); | |
15 | 15 | } |
16 | 16 | |
17 | + @Override | |
17 | 18 | public double eval() { |
18 | - OptionalDouble average = this.listCells.stream().mapToDouble(Cell::getValue).average(); | |
19 | + OptionalDouble average = this.getUtilisedCells().stream().mapToDouble(Cell::getValue).average(); | |
19 | 20 | |
20 | 21 | return average.isPresent() ? average.getAsDouble() : 0.; |
21 | 22 | } | ... | ... |
src/kernel/function/Function.java
... | ... | @@ -14,8 +14,8 @@ import java.util.stream.Collectors; |
14 | 14 | abstract public class Function implements Formula, Serializable { |
15 | 15 | |
16 | 16 | private static final long serialVersionUID = 1L; |
17 | - public Map<LanguageEnum, String> names = new HashMap<>(); | |
18 | - public List<Cell> listCells; | |
17 | + private Map<LanguageEnum, String> names = new HashMap<>(); | |
18 | + private List<Cell> listCells; | |
19 | 19 | |
20 | 20 | public Function(List<Cell> listCells) { |
21 | 21 | this.listCells = listCells; |
... | ... | @@ -23,6 +23,11 @@ abstract public class Function implements Formula, Serializable { |
23 | 23 | |
24 | 24 | abstract public double eval(); |
25 | 25 | |
26 | + public Map<LanguageEnum, String> getNames() { | |
27 | + return this.names; | |
28 | + } | |
29 | + | |
30 | + @Override | |
26 | 31 | public String getDevelopedFormula() { |
27 | 32 | return names.get(Grid.language) + "(" |
28 | 33 | + this.listCells.stream() |
... | ... | @@ -30,6 +35,7 @@ abstract public class Function implements Formula, Serializable { |
30 | 35 | .collect(Collectors.joining(",")) + ")"; |
31 | 36 | } |
32 | 37 | |
38 | + @Override | |
33 | 39 | public String toString() { |
34 | 40 | return names.get(Grid.language) + "(" |
35 | 41 | + this.listCells.stream() |
... | ... | @@ -37,18 +43,21 @@ abstract public class Function implements Formula, Serializable { |
37 | 43 | .collect(Collectors.joining(",")) + ")"; |
38 | 44 | } |
39 | 45 | |
46 | + @Override | |
40 | 47 | public boolean createCycle(Cell cell) { |
41 | - if (!this.listCells.contains(cell)) { | |
42 | - for (Cell currentCell : this.listCells) | |
43 | - if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) | |
44 | - return true; | |
45 | - } else | |
46 | - return true; | |
47 | - | |
48 | - return false; | |
48 | + return this.listCells.contains(cell) || this.generateIndirectCycle(cell); | |
49 | 49 | } |
50 | 50 | |
51 | + @Override | |
51 | 52 | public List<Cell> getUtilisedCells() { |
52 | 53 | return this.listCells; |
53 | 54 | } |
55 | + | |
56 | + private boolean generateIndirectCycle(Cell cell) { | |
57 | + for (Cell currentCell : this.listCells) | |
58 | + if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) | |
59 | + return true; | |
60 | + | |
61 | + return false; | |
62 | + } | |
54 | 63 | } | ... | ... |
src/kernel/function/Sum.java
... | ... | @@ -9,11 +9,12 @@ public class Sum extends Function { |
9 | 9 | |
10 | 10 | public Sum(List<Cell> listCells) { |
11 | 11 | super(listCells); |
12 | - this.names.put(LanguageEnum.FR, "SOMME"); | |
13 | - this.names.put(LanguageEnum.EN, "SUM"); | |
12 | + this.getNames().put(LanguageEnum.FR, "SOMME"); | |
13 | + this.getNames().put(LanguageEnum.EN, "SUM"); | |
14 | 14 | } |
15 | 15 | |
16 | + @Override | |
16 | 17 | public double eval() { |
17 | - return this.listCells.stream().mapToDouble(Cell::getValue).sum(); | |
18 | + return this.getUtilisedCells().stream().mapToDouble(Cell::getValue).sum(); | |
18 | 19 | } |
19 | 20 | } | ... | ... |
src/kernel/operation/Addition.java
... | ... | @@ -8,11 +8,13 @@ public class Addition extends BinaryOperation { |
8 | 8 | super(leftCell, rightCell); |
9 | 9 | } |
10 | 10 | |
11 | + @Override | |
11 | 12 | public String getOperator() { |
12 | 13 | return "+"; |
13 | 14 | } |
14 | 15 | |
16 | + @Override | |
15 | 17 | public double eval() { |
16 | - return this.leftCell.getValue() + this.rightCell.getValue(); | |
18 | + return this.getLeftCell().getValue() + this.getRightCell().getValue(); | |
17 | 19 | } |
18 | 20 | } | ... | ... |
src/kernel/operation/BinaryOperation.java
... | ... | @@ -10,8 +10,8 @@ import java.util.List; |
10 | 10 | abstract public class BinaryOperation implements Formula, Serializable { |
11 | 11 | |
12 | 12 | private static final long serialVersionUID = 1L; |
13 | - protected Cell leftCell; | |
14 | - protected Cell rightCell; | |
13 | + private Cell leftCell; | |
14 | + private Cell rightCell; | |
15 | 15 | |
16 | 16 | public BinaryOperation(Cell leftCell, Cell rightCell) { |
17 | 17 | this.leftCell = leftCell; |
... | ... | @@ -22,14 +22,25 @@ abstract public class BinaryOperation implements Formula, Serializable { |
22 | 22 | |
23 | 23 | abstract public String getOperator(); |
24 | 24 | |
25 | + public Cell getLeftCell() { | |
26 | + return this.leftCell; | |
27 | + } | |
28 | + | |
29 | + public Cell getRightCell() { | |
30 | + return this.rightCell; | |
31 | + } | |
32 | + | |
33 | + @Override | |
25 | 34 | public String getDevelopedFormula() { |
26 | 35 | return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")"; |
27 | 36 | } |
28 | 37 | |
38 | + @Override | |
29 | 39 | public String toString() { |
30 | 40 | return "(" + this.leftCell.getId() + this.getOperator() + this.rightCell.getId() + ")"; |
31 | 41 | } |
32 | 42 | |
43 | + @Override | |
33 | 44 | public boolean createCycle(Cell cell) { |
34 | 45 | if (this.leftCell.containFormula() && !this.rightCell.containFormula()) |
35 | 46 | return this.leftCell.getFormula().createCycle(cell); |
... | ... | @@ -43,6 +54,7 @@ abstract public class BinaryOperation implements Formula, Serializable { |
43 | 54 | return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); |
44 | 55 | } |
45 | 56 | |
57 | + @Override | |
46 | 58 | public List<Cell> getUtilisedCells() { |
47 | 59 | List<Cell> cells = new ArrayList<>(); |
48 | 60 | cells.add(this.leftCell); | ... | ... |
src/kernel/operation/Division.java
... | ... | @@ -8,11 +8,13 @@ public class Division extends BinaryOperation { |
8 | 8 | super(leftCell, rightCell); |
9 | 9 | } |
10 | 10 | |
11 | + @Override | |
11 | 12 | public String getOperator() { |
12 | 13 | return "/"; |
13 | 14 | } |
14 | 15 | |
16 | + @Override | |
15 | 17 | public double eval() { |
16 | - return this.leftCell.getValue() / this.rightCell.getValue(); | |
18 | + return this.getLeftCell().getValue() / this.getRightCell().getValue(); | |
17 | 19 | } |
18 | 20 | } | ... | ... |
src/kernel/operation/Multiplication.java
... | ... | @@ -8,11 +8,13 @@ public class Multiplication extends BinaryOperation { |
8 | 8 | super(leftCell, rightCell); |
9 | 9 | } |
10 | 10 | |
11 | + @Override | |
11 | 12 | public String getOperator() { |
12 | 13 | return "*"; |
13 | 14 | } |
14 | 15 | |
16 | + @Override | |
15 | 17 | public double eval() { |
16 | - return this.leftCell.getValue() * this.rightCell.getValue(); | |
18 | + return this.getLeftCell().getValue() * this.getRightCell().getValue(); | |
17 | 19 | } |
18 | 20 | } | ... | ... |
src/kernel/operation/Subtraction.java
... | ... | @@ -8,11 +8,13 @@ public class Subtraction extends BinaryOperation { |
8 | 8 | super(leftCell, rightCell); |
9 | 9 | } |
10 | 10 | |
11 | + @Override | |
11 | 12 | public String getOperator() { |
12 | 13 | return "-"; |
13 | 14 | } |
14 | 15 | |
16 | + @Override | |
15 | 17 | public double eval() { |
16 | - return this.leftCell.getValue() - this.rightCell.getValue(); | |
18 | + return this.getLeftCell().getValue() - this.getRightCell().getValue(); | |
17 | 19 | } |
18 | 20 | } | ... | ... |
src/kernel/test/GridTest.java
... | ... | @@ -213,9 +213,9 @@ public class GridTest { |
213 | 213 | CreateCycleException { |
214 | 214 | this.createCellsWithFormula(); |
215 | 215 | |
216 | - this.grid.save(); | |
216 | + this.grid.save("test.data"); | |
217 | 217 | |
218 | - Grid g = Grid.load(); | |
218 | + Grid g = Grid.load("test.data"); | |
219 | 219 | |
220 | 220 | Cell cell = g.getCell("A", 1); |
221 | 221 | ... | ... |