diff --git a/.gitignore b/.gitignore
index 0d97dcc..3373e25 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,5 @@
/out
/bin
/.metadata
+.project
+.classpath
diff --git a/.project b/.project
deleted file mode 100644
index 7bc1cc9..0000000
--- a/.project
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
- projetPPO_taniel_andjembe
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
-
- org.eclipse.jdt.core.javanature
-
-
diff --git a/src/app/Application.java b/src/app/Application.java
index 93c1f7d..cd5caff 100644
--- a/src/app/Application.java
+++ b/src/app/Application.java
@@ -3,6 +3,7 @@ package app;
import kernel.Cell;
import kernel.Grid;
import kernel.exception.CellNotFoundException;
+import kernel.exception.CreateCycleException;
import kernel.function.Average;
import kernel.function.Sum;
@@ -34,6 +35,8 @@ public class Application {
grid.createCell("B", 2, new Average(averageList));
} catch (CellNotFoundException exception) {
System.out.println(exception.getMessage());
+ } catch (CreateCycleException exception) {
+ System.out.println(exception.toString());
}
// Affichage
@@ -50,5 +53,16 @@ public class Application {
System.out.println("Affichage des formules développées :");
for (Cell cell : cells)
System.out.println(cell.getId() + ": " + cell.getDevelopedFormula());
+
+ // Propagation
+ try {
+ grid.setValue("A", 1, 20.);
+ } catch (CellNotFoundException exception) {
+ System.out.println("exception");
+ }
+
+ System.out.println("Affichage des valeurs après modification :");
+ for (Cell cell : cells)
+ System.out.println(cell.getId() + ": " + cell.getValue());
}
}
diff --git a/src/kernel/BinaryOperation.java b/src/kernel/BinaryOperation.java
deleted file mode 100644
index c61eae4..0000000
--- a/src/kernel/BinaryOperation.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package kernel;
-
-abstract public class BinaryOperation extends Formula {
-
- protected Cell leftCell;
- protected Cell rightCell;
- protected String operator;
-
- public BinaryOperation(Cell leftCell, Cell rightCell) {
- this.leftCell = leftCell;
- this.rightCell = rightCell;
- }
-
- public String getDevelopedFormula() {
- return this.leftCell.getDevelopedFormula() + " " + this.operator + " " + this.rightCell.getDevelopedFormula();
- }
-
- public String toString() {
- return this.leftCell.toString() + " " + this.operator + " " + this.rightCell.toString();
- }
-
- abstract public Double eval();
-
- public Boolean createCycle(Cell cell) {
- if (this.leftCell.containFormula() && !this.rightCell.containFormula())
- return this.leftCell.getFormula().createCycle(cell);
- else {
- if (!this.leftCell.containFormula() && this.rightCell.containFormula())
- return this.rightCell.getFormula().createCycle(cell);
- else {
- if (this.leftCell.containFormula() && this.rightCell.containFormula())
- return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
- else {
- return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
- }
- }
-
- }
- }
-}
diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java
index 11e9530..c2f9a46 100644
--- a/src/kernel/Cell.java
+++ b/src/kernel/Cell.java
@@ -1,5 +1,7 @@
package kernel;
+import kernel.exception.CreateCycleException;
+
import java.util.ArrayList;
import java.util.List;
@@ -14,10 +16,10 @@ public class Cell {
public Cell(String column, Integer line, Double value) {
this.column = column;
this.line = line;
- this.value = value;
+ this.setValue(value);
}
- public Cell(String column, Integer line, Formula formula) {
+ public Cell(String column, Integer line, Formula formula) throws CreateCycleException {
this.column = column;
this.line = line;
this.setFormula(formula);
@@ -48,27 +50,36 @@ public class Cell {
}
public void updateValue() {
- this.value = this.formula.eval();
+ if (this.containFormula())
+ this.value = this.formula.eval();
}
public Boolean containFormula() {
return this.formula != null;
}
- public void setFormula(Formula formula) {
-
- this.formula = formula;
- this.updateValue();
- this.spread();
+ public void setFormula(Formula formula) throws CreateCycleException {
+ if (formula.createCycle(this))
+ throw new CreateCycleException();
+ else {
+ this.formula = formula;
+ for (Cell cell : this.formula.getUtilisedCells())
+ cell.usedIn.add(this);
+ this.updateValue();
+ this.spreadValue();
+ }
}
public void setValue(Double value) {
this.value = value;
- this.spread();
+ this.formula = null;
+ this.spreadValue();
}
- private void spread() {
- for (Cell cell : this.usedIn)
+ private void spreadValue() {
+ for (Cell cell : this.usedIn) {
cell.updateValue();
+ cell.spreadValue();
+ }
}
}
diff --git a/src/kernel/Formula.java b/src/kernel/Formula.java
index 611563b..121a136 100644
--- a/src/kernel/Formula.java
+++ b/src/kernel/Formula.java
@@ -1,12 +1,16 @@
package kernel;
-abstract public class Formula {
+import java.util.List;
- abstract public String getDevelopedFormula();
+public interface Formula {
- abstract public String toString();
+ String getDevelopedFormula();
- abstract public Double eval();
+ String toString();
- abstract public Boolean createCycle(Cell cell);
+ Double eval();
+
+ Boolean createCycle(Cell cell);
+
+ List getUtilisedCells();
}
diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java
index aafcee2..1116a7e 100644
--- a/src/kernel/Grid.java
+++ b/src/kernel/Grid.java
@@ -1,6 +1,7 @@
package kernel;
import kernel.exception.CellNotFoundException;
+import kernel.exception.CreateCycleException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -21,7 +22,7 @@ public class Grid {
return id;
}
- public String createCell(String column, Integer line, Formula formula) {
+ public String createCell(String column, Integer line, Formula formula) throws CreateCycleException {
String id = this.getCellId(column, line);
Cell cell = new Cell(column, line, formula);
@@ -34,7 +35,8 @@ public class Grid {
this.getCell(column, line).setValue(value);
}
- public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException {
+ public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException,
+ CreateCycleException {
this.getCell(column, line).setFormula(formula);
}
diff --git a/src/kernel/exception/CreateCycleException.java b/src/kernel/exception/CreateCycleException.java
new file mode 100644
index 0000000..8e20268
--- /dev/null
+++ b/src/kernel/exception/CreateCycleException.java
@@ -0,0 +1,4 @@
+package kernel.exception;
+
+public class CreateCycleException extends Exception {
+}
diff --git a/src/kernel/exception/DivisionByZeroException.java b/src/kernel/exception/DivisionByZeroException.java
deleted file mode 100644
index 58963fa..0000000
--- a/src/kernel/exception/DivisionByZeroException.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package kernel.exception;
-
-public class DivisionByZeroException extends Exception{
-
-}
diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java
index f735b4e..5455f71 100644
--- a/src/kernel/function/Function.java
+++ b/src/kernel/function/Function.java
@@ -6,16 +6,15 @@ import kernel.Grid;
import kernel.LanguageEnum;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
-import java.util.ArrayList;
-import java.util.Iterator;
-abstract public class Function extends Formula {
+abstract public class Function implements Formula {
- protected Map names = new HashMap<>();
- protected List listCells;
+ public Map names = new HashMap<>();
+ public List listCells;
public Function(List listCells) {
this.listCells = listCells;
@@ -38,21 +37,21 @@ abstract public class Function extends Formula {
}
public Boolean createCycle(Cell cell) {
-
- boolean cycle=false;
- if (!this.listCells.contains(cell)){
- Iterator it=listCells.iterator();
- while (it.hasNext() &&!cycle){
- if (it.next().containFormula()){
- cycle=it.next().getFormula().createCycle(cell);
- }
- }
- return cycle;
-
- }
- else
- return true;
-
-
+ boolean cycle = false;
+ if (!this.listCells.contains(cell)) {
+ Iterator it = listCells.iterator();
+ while (it.hasNext() && !cycle) {
+ Cell currentCell = it.next();
+ if (currentCell.containFormula()) {
+ cycle = currentCell.getFormula().createCycle(cell);
+ }
+ }
+ return cycle;
+ } else
+ return true;
+ }
+
+ public List getUtilisedCells() {
+ return this.listCells;
}
}
diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java
index 5e3d7d7..f5b91fb 100644
--- a/src/kernel/operation/Addition.java
+++ b/src/kernel/operation/Addition.java
@@ -1,6 +1,5 @@
package kernel.operation;
-import kernel.BinaryOperation;
import kernel.Cell;
public class Addition extends BinaryOperation {
diff --git a/src/kernel/operation/BinaryOperation.java b/src/kernel/operation/BinaryOperation.java
new file mode 100644
index 0000000..f30b41f
--- /dev/null
+++ b/src/kernel/operation/BinaryOperation.java
@@ -0,0 +1,54 @@
+package kernel.operation;
+
+import kernel.Cell;
+import kernel.Formula;
+
+import java.util.ArrayList;
+import java.util.List;
+
+abstract public class BinaryOperation implements Formula {
+
+ protected Cell leftCell;
+ protected Cell rightCell;
+ protected String operator;
+
+ public BinaryOperation(Cell leftCell, Cell rightCell) {
+ this.leftCell = leftCell;
+ this.rightCell = rightCell;
+ }
+
+ abstract public Double eval();
+
+ public String getDevelopedFormula() {
+ return this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula();
+ }
+
+ public String toString() {
+ return this.leftCell.getId() + this.operator + this.rightCell.getId();
+ }
+
+ public Boolean createCycle(Cell cell) {
+ if (this.leftCell.containFormula() && !this.rightCell.containFormula())
+ return this.leftCell.getFormula().createCycle(cell);
+ else {
+ if (!this.leftCell.containFormula() && this.rightCell.containFormula())
+ return this.rightCell.getFormula().createCycle(cell);
+ else {
+ if (this.leftCell.containFormula() && this.rightCell.containFormula())
+ return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
+ else {
+ return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
+ }
+ }
+
+ }
+ }
+
+ public List getUtilisedCells() {
+ List cells = new ArrayList<>();
+ cells.add(this.leftCell);
+ cells.add(this.rightCell);
+
+ return cells;
+ }
+}
diff --git a/src/kernel/operation/Division.java b/src/kernel/operation/Division.java
index 9b1a77e..121e3be 100644
--- a/src/kernel/operation/Division.java
+++ b/src/kernel/operation/Division.java
@@ -1,6 +1,5 @@
package kernel.operation;
-import kernel.BinaryOperation;
import kernel.Cell;
public class Division extends BinaryOperation {
diff --git a/src/kernel/operation/Multiplication.java b/src/kernel/operation/Multiplication.java
index bf92a2e..24327db 100644
--- a/src/kernel/operation/Multiplication.java
+++ b/src/kernel/operation/Multiplication.java
@@ -1,6 +1,5 @@
package kernel.operation;
-import kernel.BinaryOperation;
import kernel.Cell;
public class Multiplication extends BinaryOperation {
diff --git a/src/kernel/operation/Subtraction.java b/src/kernel/operation/Subtraction.java
index 7927239..23d5d9b 100644
--- a/src/kernel/operation/Subtraction.java
+++ b/src/kernel/operation/Subtraction.java
@@ -1,6 +1,5 @@
package kernel.operation;
-import kernel.BinaryOperation;
import kernel.Cell;
public class Subtraction extends BinaryOperation {
--
libgit2 0.21.2 | | | | | | | | |