Commit 5731029f5105ddcc2b3a00debad1bf4a725e27e1
1 parent
0c16a45a
merge
Showing
14 changed files
with
95 additions
and
71 deletions
Show diff stats
.gitignore
.project deleted
@@ -1,17 +0,0 @@ | @@ -1,17 +0,0 @@ | ||
1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
2 | -<projectDescription> | ||
3 | - <name>projetPPO_taniel_andjembe</name> | ||
4 | - <comment></comment> | ||
5 | - <projects> | ||
6 | - </projects> | ||
7 | - <buildSpec> | ||
8 | - <buildCommand> | ||
9 | - <name>org.eclipse.jdt.core.javabuilder</name> | ||
10 | - <arguments> | ||
11 | - </arguments> | ||
12 | - </buildCommand> | ||
13 | - </buildSpec> | ||
14 | - <natures> | ||
15 | - <nature>org.eclipse.jdt.core.javanature</nature> | ||
16 | - </natures> | ||
17 | -</projectDescription> |
src/app/Application.java
@@ -3,6 +3,7 @@ package app; | @@ -3,6 +3,7 @@ package app; | ||
3 | import kernel.Cell; | 3 | import kernel.Cell; |
4 | import kernel.Grid; | 4 | import kernel.Grid; |
5 | import kernel.exception.CellNotFoundException; | 5 | import kernel.exception.CellNotFoundException; |
6 | +import kernel.exception.CreateCycleException; | ||
6 | import kernel.function.Average; | 7 | import kernel.function.Average; |
7 | import kernel.function.Sum; | 8 | import kernel.function.Sum; |
8 | 9 | ||
@@ -34,6 +35,8 @@ public class Application { | @@ -34,6 +35,8 @@ public class Application { | ||
34 | grid.createCell("B", 2, new Average(averageList)); | 35 | grid.createCell("B", 2, new Average(averageList)); |
35 | } catch (CellNotFoundException exception) { | 36 | } catch (CellNotFoundException exception) { |
36 | System.out.println(exception.getMessage()); | 37 | System.out.println(exception.getMessage()); |
38 | + } catch (CreateCycleException exception) { | ||
39 | + System.out.println(exception.toString()); | ||
37 | } | 40 | } |
38 | 41 | ||
39 | // Affichage | 42 | // Affichage |
@@ -50,5 +53,16 @@ public class Application { | @@ -50,5 +53,16 @@ public class Application { | ||
50 | System.out.println("Affichage des formules développées :"); | 53 | System.out.println("Affichage des formules développées :"); |
51 | for (Cell cell : cells) | 54 | for (Cell cell : cells) |
52 | System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); | 55 | System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); |
56 | + | ||
57 | + // Propagation | ||
58 | + try { | ||
59 | + grid.setValue("A", 1, 20.); | ||
60 | + } catch (CellNotFoundException exception) { | ||
61 | + System.out.println("exception"); | ||
62 | + } | ||
63 | + | ||
64 | + System.out.println("Affichage des valeurs après modification :"); | ||
65 | + for (Cell cell : cells) | ||
66 | + System.out.println(cell.getId() + ": " + cell.getValue()); | ||
53 | } | 67 | } |
54 | } | 68 | } |
src/kernel/Cell.java
1 | package kernel; | 1 | package kernel; |
2 | 2 | ||
3 | +import kernel.exception.CreateCycleException; | ||
4 | + | ||
3 | import java.util.ArrayList; | 5 | import java.util.ArrayList; |
4 | import java.util.List; | 6 | import java.util.List; |
5 | 7 | ||
@@ -14,10 +16,10 @@ public class Cell { | @@ -14,10 +16,10 @@ public class Cell { | ||
14 | public Cell(String column, Integer line, Double value) { | 16 | public Cell(String column, Integer line, Double value) { |
15 | this.column = column; | 17 | this.column = column; |
16 | this.line = line; | 18 | this.line = line; |
17 | - this.value = value; | 19 | + this.setValue(value); |
18 | } | 20 | } |
19 | 21 | ||
20 | - public Cell(String column, Integer line, Formula formula) { | 22 | + public Cell(String column, Integer line, Formula formula) throws CreateCycleException { |
21 | this.column = column; | 23 | this.column = column; |
22 | this.line = line; | 24 | this.line = line; |
23 | this.setFormula(formula); | 25 | this.setFormula(formula); |
@@ -48,27 +50,36 @@ public class Cell { | @@ -48,27 +50,36 @@ public class Cell { | ||
48 | } | 50 | } |
49 | 51 | ||
50 | public void updateValue() { | 52 | public void updateValue() { |
51 | - this.value = this.formula.eval(); | 53 | + if (this.containFormula()) |
54 | + this.value = this.formula.eval(); | ||
52 | } | 55 | } |
53 | 56 | ||
54 | public Boolean containFormula() { | 57 | public Boolean containFormula() { |
55 | return this.formula != null; | 58 | return this.formula != null; |
56 | } | 59 | } |
57 | 60 | ||
58 | - public void setFormula(Formula formula) { | ||
59 | - | ||
60 | - this.formula = formula; | ||
61 | - this.updateValue(); | ||
62 | - this.spread(); | 61 | + public void setFormula(Formula formula) throws CreateCycleException { |
62 | + if (formula.createCycle(this)) | ||
63 | + throw new CreateCycleException(); | ||
64 | + else { | ||
65 | + this.formula = formula; | ||
66 | + for (Cell cell : this.formula.getUtilisedCells()) | ||
67 | + cell.usedIn.add(this); | ||
68 | + this.updateValue(); | ||
69 | + this.spreadValue(); | ||
70 | + } | ||
63 | } | 71 | } |
64 | 72 | ||
65 | public void setValue(Double value) { | 73 | public void setValue(Double value) { |
66 | this.value = value; | 74 | this.value = value; |
67 | - this.spread(); | 75 | + this.formula = null; |
76 | + this.spreadValue(); | ||
68 | } | 77 | } |
69 | 78 | ||
70 | - private void spread() { | ||
71 | - for (Cell cell : this.usedIn) | 79 | + private void spreadValue() { |
80 | + for (Cell cell : this.usedIn) { | ||
72 | cell.updateValue(); | 81 | cell.updateValue(); |
82 | + cell.spreadValue(); | ||
83 | + } | ||
73 | } | 84 | } |
74 | } | 85 | } |
src/kernel/Formula.java
1 | package kernel; | 1 | package kernel; |
2 | 2 | ||
3 | -abstract public class Formula { | 3 | +import java.util.List; |
4 | 4 | ||
5 | - abstract public String getDevelopedFormula(); | 5 | +public interface Formula { |
6 | 6 | ||
7 | - abstract public String toString(); | 7 | + String getDevelopedFormula(); |
8 | 8 | ||
9 | - abstract public Double eval(); | 9 | + String toString(); |
10 | 10 | ||
11 | - abstract public Boolean createCycle(Cell cell); | 11 | + Double eval(); |
12 | + | ||
13 | + Boolean createCycle(Cell cell); | ||
14 | + | ||
15 | + List<Cell> getUtilisedCells(); | ||
12 | } | 16 | } |
src/kernel/Grid.java
1 | package kernel; | 1 | package kernel; |
2 | 2 | ||
3 | import kernel.exception.CellNotFoundException; | 3 | import kernel.exception.CellNotFoundException; |
4 | +import kernel.exception.CreateCycleException; | ||
4 | 5 | ||
5 | import java.util.ArrayList; | 6 | import java.util.ArrayList; |
6 | import java.util.HashMap; | 7 | import java.util.HashMap; |
@@ -21,7 +22,7 @@ public class Grid { | @@ -21,7 +22,7 @@ public class Grid { | ||
21 | return id; | 22 | return id; |
22 | } | 23 | } |
23 | 24 | ||
24 | - public String createCell(String column, Integer line, Formula formula) { | 25 | + public String createCell(String column, Integer line, Formula formula) throws CreateCycleException { |
25 | String id = this.getCellId(column, line); | 26 | String id = this.getCellId(column, line); |
26 | Cell cell = new Cell(column, line, formula); | 27 | Cell cell = new Cell(column, line, formula); |
27 | 28 | ||
@@ -34,7 +35,8 @@ public class Grid { | @@ -34,7 +35,8 @@ public class Grid { | ||
34 | this.getCell(column, line).setValue(value); | 35 | this.getCell(column, line).setValue(value); |
35 | } | 36 | } |
36 | 37 | ||
37 | - public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException { | 38 | + public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException, |
39 | + CreateCycleException { | ||
38 | this.getCell(column, line).setFormula(formula); | 40 | this.getCell(column, line).setFormula(formula); |
39 | } | 41 | } |
40 | 42 |
src/kernel/exception/DivisionByZeroException.java deleted
src/kernel/function/Function.java
@@ -6,16 +6,15 @@ import kernel.Grid; | @@ -6,16 +6,15 @@ import kernel.Grid; | ||
6 | import kernel.LanguageEnum; | 6 | import kernel.LanguageEnum; |
7 | 7 | ||
8 | import java.util.HashMap; | 8 | import java.util.HashMap; |
9 | +import java.util.Iterator; | ||
9 | import java.util.List; | 10 | import java.util.List; |
10 | import java.util.Map; | 11 | import java.util.Map; |
11 | import java.util.stream.Collectors; | 12 | import java.util.stream.Collectors; |
12 | -import java.util.ArrayList; | ||
13 | -import java.util.Iterator; | ||
14 | 13 | ||
15 | -abstract public class Function extends Formula { | 14 | +abstract public class Function implements Formula { |
16 | 15 | ||
17 | - protected Map<LanguageEnum, String> names = new HashMap<>(); | ||
18 | - protected List<Cell> listCells; | 16 | + public Map<LanguageEnum, String> names = new HashMap<>(); |
17 | + public List<Cell> listCells; | ||
19 | 18 | ||
20 | public Function(List<Cell> listCells) { | 19 | public Function(List<Cell> listCells) { |
21 | this.listCells = listCells; | 20 | this.listCells = listCells; |
@@ -38,21 +37,21 @@ abstract public class Function extends Formula { | @@ -38,21 +37,21 @@ abstract public class Function extends Formula { | ||
38 | } | 37 | } |
39 | 38 | ||
40 | public Boolean createCycle(Cell cell) { | 39 | public Boolean createCycle(Cell cell) { |
41 | - | ||
42 | - boolean cycle=false; | ||
43 | - if (!this.listCells.contains(cell)){ | ||
44 | - Iterator<Cell> it=listCells.iterator(); | ||
45 | - while (it.hasNext() &&!cycle){ | ||
46 | - if (it.next().containFormula()){ | ||
47 | - cycle=it.next().getFormula().createCycle(cell); | ||
48 | - } | ||
49 | - } | ||
50 | - return cycle; | ||
51 | - | ||
52 | - } | ||
53 | - else | ||
54 | - return true; | ||
55 | - | ||
56 | - | 40 | + boolean cycle = false; |
41 | + if (!this.listCells.contains(cell)) { | ||
42 | + Iterator<Cell> it = listCells.iterator(); | ||
43 | + while (it.hasNext() && !cycle) { | ||
44 | + Cell currentCell = it.next(); | ||
45 | + if (currentCell.containFormula()) { | ||
46 | + cycle = currentCell.getFormula().createCycle(cell); | ||
47 | + } | ||
48 | + } | ||
49 | + return cycle; | ||
50 | + } else | ||
51 | + return true; | ||
52 | + } | ||
53 | + | ||
54 | + public List<Cell> getUtilisedCells() { | ||
55 | + return this.listCells; | ||
57 | } | 56 | } |
58 | } | 57 | } |
src/kernel/operation/Addition.java
src/kernel/BinaryOperation.java renamed to src/kernel/operation/BinaryOperation.java
1 | -package kernel; | 1 | +package kernel.operation; |
2 | 2 | ||
3 | -abstract public class BinaryOperation extends Formula { | 3 | +import kernel.Cell; |
4 | +import kernel.Formula; | ||
5 | + | ||
6 | +import java.util.ArrayList; | ||
7 | +import java.util.List; | ||
8 | + | ||
9 | +abstract public class BinaryOperation implements Formula { | ||
4 | 10 | ||
5 | protected Cell leftCell; | 11 | protected Cell leftCell; |
6 | protected Cell rightCell; | 12 | protected Cell rightCell; |
@@ -11,16 +17,16 @@ abstract public class BinaryOperation extends Formula { | @@ -11,16 +17,16 @@ abstract public class BinaryOperation extends Formula { | ||
11 | this.rightCell = rightCell; | 17 | this.rightCell = rightCell; |
12 | } | 18 | } |
13 | 19 | ||
20 | + abstract public Double eval(); | ||
21 | + | ||
14 | public String getDevelopedFormula() { | 22 | public String getDevelopedFormula() { |
15 | - return this.leftCell.getDevelopedFormula() + " " + this.operator + " " + this.rightCell.getDevelopedFormula(); | 23 | + return this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula(); |
16 | } | 24 | } |
17 | 25 | ||
18 | public String toString() { | 26 | public String toString() { |
19 | - return this.leftCell.toString() + " " + this.operator + " " + this.rightCell.toString(); | 27 | + return this.leftCell.getId() + this.operator + this.rightCell.getId(); |
20 | } | 28 | } |
21 | 29 | ||
22 | - abstract public Double eval(); | ||
23 | - | ||
24 | public Boolean createCycle(Cell cell) { | 30 | public Boolean createCycle(Cell cell) { |
25 | if (this.leftCell.containFormula() && !this.rightCell.containFormula()) | 31 | if (this.leftCell.containFormula() && !this.rightCell.containFormula()) |
26 | return this.leftCell.getFormula().createCycle(cell); | 32 | return this.leftCell.getFormula().createCycle(cell); |
@@ -37,4 +43,12 @@ abstract public class BinaryOperation extends Formula { | @@ -37,4 +43,12 @@ abstract public class BinaryOperation extends Formula { | ||
37 | 43 | ||
38 | } | 44 | } |
39 | } | 45 | } |
46 | + | ||
47 | + public List<Cell> getUtilisedCells() { | ||
48 | + List<Cell> cells = new ArrayList<>(); | ||
49 | + cells.add(this.leftCell); | ||
50 | + cells.add(this.rightCell); | ||
51 | + | ||
52 | + return cells; | ||
53 | + } | ||
40 | } | 54 | } |
src/kernel/operation/Division.java
src/kernel/operation/Multiplication.java
src/kernel/operation/Subtraction.java