From 2e8fbd044b79097eb5db572148a90e6a9f1d7e27 Mon Sep 17 00:00:00 2001 From: Remi Date: Fri, 7 Jun 2019 21:36:07 +0200 Subject: [PATCH] global refactor --- .Rhistory | 0 rapport.md | 18 +++++++++--------- src/app/Application.java | 4 ++-- src/kernel/BinaryOperation.java | 40 ++++++++++++++++++++++++++++++++++++++++ src/kernel/Cell.java | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/Formula.java | 12 ++++++++++++ src/kernel/Grid.java | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/exception/CellNotFoundException.java | 4 ++++ src/kernel/exception/DivisionByZeroException.java | 5 +++++ src/kernel/function/Average.java | 31 +++++++++++++++++++++++++++++++ src/kernel/function/Function.java | 21 +++++++++++++++++++++ src/kernel/function/Sum.java | 29 +++++++++++++++++++++++++++++ src/kernel/operation/Addition.java | 16 ++++++++++++++++ src/kernel/operation/Division.java | 16 ++++++++++++++++ src/kernel/operation/Multiplication.java | 16 ++++++++++++++++ src/kernel/operation/Subtraction.java | 16 ++++++++++++++++ src/noyau/Addition.java | 34 ---------------------------------- src/noyau/Case.java | 66 ------------------------------------------------------------------ src/noyau/Division.java | 47 ----------------------------------------------- src/noyau/Fonctions.java | 25 ------------------------- src/noyau/Formule.java | 11 ----------- src/noyau/Grille.java | 77 ----------------------------------------------------------------------------- src/noyau/Moyenne.java | 35 ----------------------------------- src/noyau/Multiplication.java | 32 -------------------------------- src/noyau/OperationBinaire.java | 41 ----------------------------------------- src/noyau/Somme.java | 31 ------------------------------- src/noyau/Soustraction.java | 34 ---------------------------------- src/noyau/exception/CellNotFoundException.java | 4 ---- src/noyau/exception/DivisionByZeroException.java | 5 ----- 29 files changed, 366 insertions(+), 453 deletions(-) delete mode 100644 .Rhistory create mode 100644 src/kernel/BinaryOperation.java create mode 100644 src/kernel/Cell.java create mode 100644 src/kernel/Formula.java create mode 100644 src/kernel/Grid.java create mode 100644 src/kernel/exception/CellNotFoundException.java create mode 100644 src/kernel/exception/DivisionByZeroException.java create mode 100644 src/kernel/function/Average.java create mode 100644 src/kernel/function/Function.java create mode 100644 src/kernel/function/Sum.java create mode 100644 src/kernel/operation/Addition.java create mode 100644 src/kernel/operation/Division.java create mode 100644 src/kernel/operation/Multiplication.java create mode 100644 src/kernel/operation/Subtraction.java delete mode 100644 src/noyau/Addition.java delete mode 100644 src/noyau/Case.java delete mode 100644 src/noyau/Division.java delete mode 100644 src/noyau/Fonctions.java delete mode 100644 src/noyau/Formule.java delete mode 100644 src/noyau/Grille.java delete mode 100644 src/noyau/Moyenne.java delete mode 100644 src/noyau/Multiplication.java delete mode 100644 src/noyau/OperationBinaire.java delete mode 100644 src/noyau/Somme.java delete mode 100644 src/noyau/Soustraction.java delete mode 100644 src/noyau/exception/CellNotFoundException.java delete mode 100644 src/noyau/exception/DivisionByZeroException.java diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index e69de29..0000000 --- a/.Rhistory +++ /dev/null diff --git a/rapport.md b/rapport.md index 4718218..2c6c9f0 100644 --- a/rapport.md +++ b/rapport.md @@ -4,10 +4,10 @@ Le but du projet est de réaliser un tableur "basique" mais facilement extensible, l'application sera divisée en 2 parties : -* le noyau +* le kernel * la partie graphique -Le noyau s'occupera de toutes les opérations de notre grille, les cases +Le kernel s'occupera de toutes les opérations de notre grid, les cases ne pourront contenir que des réels ou des formules(opération binaire ou des fonctions acceptant des plages de cases). @@ -21,7 +21,7 @@ abstraites sont en italique : ### PSEUDO-JAVA CREATION GRILLE,CASES -Voici un exemple de création d'une grille et de l'ajout / modification / +Voici un exemple de création d'une grid et de l'ajout / modification / affichage de plusieurs types de case : ```java @@ -54,7 +54,7 @@ class Application { g.getValeur("a",1); //Affichera 100.0 g.getValeur("a",4); //Affichera (100+50+150)=100 g.getFormuleAsString("b",2); //Affichera MOYENNE(a4,a2,a3) - g.getFormuleDeveloppe("b",2); //Affichera MOYENNE(Somme(a1,a2,a3),a2,(a1+a2)) + g.getFormuleDeveloppe("b",2); Sum } } ``` @@ -62,7 +62,7 @@ class Application { ### CHOIX STRUCTURE DE DONNÉES Nous devons choisir une structure de donnée pour stocker les cases dans -notre grille, nous savons déjà que nous allons utiliser ne collection +notre grid, nous savons déjà que nous allons utiliser ne collection pour les stocker,voici celles que nous connaissons: - des tableaux - des listes @@ -70,7 +70,7 @@ pour les stocker,voici celles que nous connaissons: - des sets D'après le schéma UML ci-dessus, nous allons donc utiliser une `HashMap` -pour stocker les cases de notre grille : +pour stocker les cases de notre grid : * Pour rechercher une case et, effectuer des opérations dessus ce sera plus facile, la clé de la Map sera une chaine de caractère (String) qui représente la coordonnée de cette case (c'est-à-dire la concaténation @@ -180,12 +180,12 @@ class Case{ } } -// Exemple pour Moyenne +Average class Moyenne { List listCases = new ArrayList(); String getFormuleDeveloppe() { - return "Moyenne(" + listCases.stream().map(c -> c.getFormuleDeveloppe()).collect((Collectors).joining(", ")) + ")"; + return Average + listCases.stream().map(c -> c.getFormuleDeveloppe()).collect((Collectors).joining(", ")) + ")"; } } @@ -354,7 +354,7 @@ class Case { } } -// Exemple pour OperationBinaire +BinaryOperation class OperationBinaire { Case gauche; Case droite; diff --git a/src/app/Application.java b/src/app/Application.java index bce4c7e..9e5ea7d 100644 --- a/src/app/Application.java +++ b/src/app/Application.java @@ -1,11 +1,11 @@ package app; -import noyau.Grille; +import kernel.Grid; public class Application { public static void main(String[] args) { - Grille grille = new Grille(); + Grid grid = new Grid(); System.out.println("Hello world!"); } diff --git a/src/kernel/BinaryOperation.java b/src/kernel/BinaryOperation.java new file mode 100644 index 0000000..c61eae4 --- /dev/null +++ b/src/kernel/BinaryOperation.java @@ -0,0 +1,40 @@ +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 new file mode 100644 index 0000000..a7fbd0d --- /dev/null +++ b/src/kernel/Cell.java @@ -0,0 +1,72 @@ +package kernel; + +import java.util.ArrayList; +import java.util.List; + +public class Cell { + + private String column; + private Integer line; + private Double value; + private Formula formula; + private List usedIn = new ArrayList<>(); + + public Cell(String column, Integer line, Double value) { + this.column = column; + this.line = line; + this.value = value; + } + + public Cell(String column, Integer line, Formula formula) { + this.column = column; + this.line = line; + this.formula = formula; + } + + public Double getValue() { + return this.value; + } + + public Formula getFormula() { + return this.formula; + } + + public String getDevelopedFormula() { + return containFormula() ? this.formula.getDevelopedFormula() : this.toString(); + } + + public String getId() { + return this.column + this.line.toString(); + } + + public List getUsedIn() { + return this.usedIn; + } + + public String toString() { + return containFormula() ? this.formula.toString() : this.getId(); + } + + public void updateValue() { + this.value = this.formula.eval(); + } + + public Boolean containFormula() { + return this.formula != null; + } + + public void setFormula(Formula formula) { + this.formula = formula; + this.spread(); + } + + public void setValue(Double value) { + this.value = value; + this.spread(); + } + + private void spread() { + for (Cell cell : this.usedIn) + cell.updateValue(); + } +} diff --git a/src/kernel/Formula.java b/src/kernel/Formula.java new file mode 100644 index 0000000..611563b --- /dev/null +++ b/src/kernel/Formula.java @@ -0,0 +1,12 @@ +package kernel; + +abstract public class Formula { + + abstract public String getDevelopedFormula(); + + abstract public String toString(); + + abstract public Double eval(); + + abstract public Boolean createCycle(Cell cell); +} diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java new file mode 100644 index 0000000..4129652 --- /dev/null +++ b/src/kernel/Grid.java @@ -0,0 +1,77 @@ +package kernel; + +import kernel.exception.CellNotFoundException; + +import java.util.HashMap; +import java.util.Map; + +public class Grid { + + private Map cells = new HashMap<>(); + + public String createCell(String column, Integer line, Double value) { + String id = this.getId(column, line); + Cell cell = new Cell(column, line, value); + + this.cells.put(id, cell); + + return id; + } + + public String createCell(String column, Integer line, Formula formula) { + String id = this.getId(column, line); + Cell cell = new Cell(column, line, formula); + + this.cells.put(id, cell); + + return id; + } + + public void setValue(String column, Integer line, Double value) throws CellNotFoundException { + Cell cell = this.getCell(column, line); + + try { + cell.setValue(value); + } catch (NullPointerException exception) { + throw new CellNotFoundException(); + } + } + + public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException { + Cell cell = this.getCell(column, line); + + try { + cell.setFormula(formula); + } catch (NullPointerException exception) { + throw new CellNotFoundException(); + } + } + + public Cell getCell(String column, Integer line) { + return this.cells.get(this.getId(column, line)); + } + + public Double getValue(String column, Integer line) throws CellNotFoundException { + Cell cell = this.getCell(column, line); + + try { + return cell.getValue(); + } catch (NullPointerException exception) { + throw new CellNotFoundException(); + } + } + + public String getFormulaAsString(String column, Integer line) throws CellNotFoundException { + Cell cell = this.getCell(column, line); + + try { + return cell.getFormula().toString(); + } catch (NullPointerException exception) { + throw new CellNotFoundException(); + } + } + + private String getId(String column, Integer line) { + return column + line.toString(); + } +} diff --git a/src/kernel/exception/CellNotFoundException.java b/src/kernel/exception/CellNotFoundException.java new file mode 100644 index 0000000..3b059e4 --- /dev/null +++ b/src/kernel/exception/CellNotFoundException.java @@ -0,0 +1,4 @@ +package kernel.exception; + +public class CellNotFoundException extends Exception { +} diff --git a/src/kernel/exception/DivisionByZeroException.java b/src/kernel/exception/DivisionByZeroException.java new file mode 100644 index 0000000..58963fa --- /dev/null +++ b/src/kernel/exception/DivisionByZeroException.java @@ -0,0 +1,5 @@ +package kernel.exception; + +public class DivisionByZeroException extends Exception{ + +} diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java new file mode 100644 index 0000000..c495912 --- /dev/null +++ b/src/kernel/function/Average.java @@ -0,0 +1,31 @@ +package kernel.function; + +import kernel.Cell; + +import java.util.OptionalDouble; +import java.util.stream.Collectors; + +public class Average extends Function { + + public String toString() { + return "MOYENNE(" + this.listCells.stream() + .map(Cell::getId) + .collect(Collectors.joining(",")) + + ")"; + } + + public String getDevelopedFormula() { + return "MOYENNE(" + this.listCells.stream() + .map(Cell::getDevelopedFormula) + .collect(Collectors.joining(",")) + + ")"; + } + + public Double eval() { + OptionalDouble average = this.listCells.stream() + .mapToDouble(Cell::getValue) + .average(); + + return average.isPresent() ? average.getAsDouble() : 0.; + } +} diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java new file mode 100644 index 0000000..cf1c27a --- /dev/null +++ b/src/kernel/function/Function.java @@ -0,0 +1,21 @@ +package kernel.function; + +import kernel.Cell; +import kernel.Formula; + +import java.util.ArrayList; + +abstract public class Function extends Formula { + + protected ArrayList listCells = new ArrayList<>(); + + abstract public String getDevelopedFormula(); + + abstract public String toString(); + + abstract public Double eval(); + + public Boolean createCycle(Cell cell) { + return this.listCells.contains(cell); + } +} diff --git a/src/kernel/function/Sum.java b/src/kernel/function/Sum.java new file mode 100644 index 0000000..7f6a623 --- /dev/null +++ b/src/kernel/function/Sum.java @@ -0,0 +1,29 @@ +package kernel.function; + +import kernel.Cell; +import kernel.function.Function; + +import java.util.stream.Collectors; + +public class Sum extends Function { + + public String toString() { + return "SOMME(" + this.listCells.stream() + .map(Cell::getId) + .collect(Collectors.joining(",")) + + ")"; + } + + public String getDevelopedFormula() { + return "SOMME(" + this.listCells.stream() + .map(Cell::getDevelopedFormula) + .collect(Collectors.joining(",")) + + ")"; + } + + public Double eval() { + return this.listCells.stream() + .mapToDouble(Cell::getValue) + .sum(); + } +} diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java new file mode 100644 index 0000000..9b3d175 --- /dev/null +++ b/src/kernel/operation/Addition.java @@ -0,0 +1,16 @@ +package kernel.operation; + +import kernel.BinaryOperation; +import kernel.Cell; + +public class Addition extends BinaryOperation { + + public Addition(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + this.operator = "+"; + } + + public Double eval() { + return this.leftCell.getValue() + this.rightCell.getValue(); + } +} diff --git a/src/kernel/operation/Division.java b/src/kernel/operation/Division.java new file mode 100644 index 0000000..9b1a77e --- /dev/null +++ b/src/kernel/operation/Division.java @@ -0,0 +1,16 @@ +package kernel.operation; + +import kernel.BinaryOperation; +import kernel.Cell; + +public class Division extends BinaryOperation { + + public Division(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + this.operator = "/"; + } + + public Double eval() { + return this.leftCell.getValue() / this.rightCell.getValue(); + } +} diff --git a/src/kernel/operation/Multiplication.java b/src/kernel/operation/Multiplication.java new file mode 100644 index 0000000..bf92a2e --- /dev/null +++ b/src/kernel/operation/Multiplication.java @@ -0,0 +1,16 @@ +package kernel.operation; + +import kernel.BinaryOperation; +import kernel.Cell; + +public class Multiplication extends BinaryOperation { + + public Multiplication(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + this.operator = "*"; + } + + public Double eval() { + return this.leftCell.getValue() * this.rightCell.getValue(); + } +} diff --git a/src/kernel/operation/Subtraction.java b/src/kernel/operation/Subtraction.java new file mode 100644 index 0000000..7927239 --- /dev/null +++ b/src/kernel/operation/Subtraction.java @@ -0,0 +1,16 @@ +package kernel.operation; + +import kernel.BinaryOperation; +import kernel.Cell; + +public class Subtraction extends BinaryOperation { + + public Subtraction(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + this.operator = "-"; + } + + public Double eval() { + return this.leftCell.getValue() - this.rightCell.getValue(); + } +} diff --git a/src/noyau/Addition.java b/src/noyau/Addition.java deleted file mode 100644 index 8585bfc..0000000 --- a/src/noyau/Addition.java +++ /dev/null @@ -1,34 +0,0 @@ -package noyau; - -import java.util.stream.Collectors; - -public class Addition extends OperationBinaire { - - Addition(Case g,Case d){ - gauche=g; - droite=d; - } - - - public double eval() { - - if (!creerCycle(gauche) && !creerCycle(droite)) - return (double) gauche.getValue() + droite.getValue(); - else - return 0; - - } - - - public String toString(){ - - return gauche.getId() +" + " + droite.getId() ; - } - - - public String getFormuleDeveloppe(){ - - return ""; - } - -} diff --git a/src/noyau/Case.java b/src/noyau/Case.java deleted file mode 100644 index 1c519a9..0000000 --- a/src/noyau/Case.java +++ /dev/null @@ -1,66 +0,0 @@ -package noyau; - -import java.util.ArrayList; -import java.util.List; - -public class Case { - - private String column; - private Integer line; - private Double value; - private Formule formula; - private List usedIn = new ArrayList<>(); - - public Case(String column, Integer line, Double value) { - this.column = column; - this.line = line; - this.value = value; - } - - public Case(String column, Integer line, Formule formula) { - this.column = column; - this.line = line; - this.formula = formula; - } - - public double getValue() { - return this.value; - } - - public List getUsedIn() { - return this.usedIn; - } - - public void setValue(Double value) { - this.value = value; - this.spread(); - } - - public void updateValue() { - - } - - public String getId() { - return this.column + this.line.toString(); - } - - public Boolean containFormula() { - return this.formula != null; - } - - - - public Formule getFormula() { - return this.formula; - } - - public void setFormula(Formule formula) { - this.formula = formula; - this.spread(); - } - - private void spread() { - for (Case cell : this.usedIn) - cell.updateValue(); - } -} diff --git a/src/noyau/Division.java b/src/noyau/Division.java deleted file mode 100644 index db3461d..0000000 --- a/src/noyau/Division.java +++ /dev/null @@ -1,47 +0,0 @@ -package noyau; - -import noyau.exception.CellNotFoundException; -import noyau.exception.DivisionByZeroException; - -public class Division extends OperationBinaire{ - - Division(Case g,Case d){ - gauche=g; - droite=d; - } - - - - public double eval() throws DivisionByZeroException { - - try{ - if (!creerCycle(gauche) && !creerCycle(droite)) - - return gauche.getValue()/droite.getValue(); - else - return 0; - } - catch(ArithmeticException ex){ - throw new DivisionByZeroException(); - } - - - - - - } - - - public String toString(){ - - return gauche.getId() +" / " + droite.getId() ; - } - - - - public String getFormuleDeveloppe(){ - - return ""; - } - -} diff --git a/src/noyau/Fonctions.java b/src/noyau/Fonctions.java deleted file mode 100644 index 306a6ed..0000000 --- a/src/noyau/Fonctions.java +++ /dev/null @@ -1,25 +0,0 @@ -package noyau; - -import java.util.*; - -abstract public class Fonctions extends Formule { - - ArrayList listCases = new ArrayList(); - - abstract public String getFormuleDeveloppe(); - - - - public boolean creerCycle(Case uneCase){ - - return listCases.contains(uneCase); - - - - - - - } - - -} diff --git a/src/noyau/Formule.java b/src/noyau/Formule.java deleted file mode 100644 index cee025c..0000000 --- a/src/noyau/Formule.java +++ /dev/null @@ -1,11 +0,0 @@ -package noyau; - -import noyau.exception.DivisionByZeroException; - -abstract public class Formule { - - abstract public String toString(); - abstract public double eval() throws DivisionByZeroException; - abstract public boolean creerCycle(Case uneCase); - -} diff --git a/src/noyau/Grille.java b/src/noyau/Grille.java deleted file mode 100644 index 6380ac9..0000000 --- a/src/noyau/Grille.java +++ /dev/null @@ -1,77 +0,0 @@ -package noyau; - -import noyau.exception.CellNotFoundException; - -import java.util.HashMap; -import java.util.Map; - -public class Grille { - - private Map cases = new HashMap<>(); - - public String createCase(String column, Integer line, Double value) { - String id = this.getId(column, line); - Case cell = new Case(column, line, value); - - this.cases.put(id, cell); - - return id; - } - - public String createCase(String column, Integer line, Formule formula) { - String id = this.getId(column, line); - Case cell = new Case(column, line, formula); - - this.cases.put(id, cell); - - return id; - } - - public void setValue(String column, Integer line, Double value) throws CellNotFoundException { - Case cell = this.getCase(column, line); - - try { - cell.setValue(value); - } catch (NullPointerException exception) { - throw new CellNotFoundException(); - } - } - - public void setFormula(String column, Integer line, Formule formula) throws CellNotFoundException { - Case cell = this.getCase(column, line); - - try { - cell.setFormula(formula); - } catch (NullPointerException exception) { - throw new CellNotFoundException(); - } - } - - public Case getCase(String column, Integer line) { - return this.cases.get(this.getId(column, line)); - } - - public Double getValeur(String column, Integer line) throws CellNotFoundException { - Case cell = this.getCase(column, line); - - try { - return cell.getValue(); - } catch (NullPointerException exception) { - throw new CellNotFoundException(); - } - } - - public String getFormuleAsString(String column, Integer line) throws CellNotFoundException { - Case cell = this.getCase(column, line); - - try { - return cell.getValue(); - } catch (NullPointerException exception) { - throw new CellNotFoundException(); - } - } - - private String getId(String column, Integer line) { - return column + line.toString(); - } -} diff --git a/src/noyau/Moyenne.java b/src/noyau/Moyenne.java deleted file mode 100644 index e398aa7..0000000 --- a/src/noyau/Moyenne.java +++ /dev/null @@ -1,35 +0,0 @@ -package noyau; - -import java.util.stream.Collectors; - -public class Moyenne extends Fonctions{ - - - public String toString(){ - - return "MOYENNE(" + listCases.stream() - .map( n -> n.getId() ) - .collect( Collectors.joining( "," ) ) - +")"; - } - - public String getFormuleDeveloppe(){ - - return ""; - } - - public double eval() { - - double val=0.0; - - try{ - for(int i=0; i n.getId() ) - .collect( Collectors.joining( "," ) ) - +")"; - } - - public String getFormuleDeveloppe(){ - - return ""; - } - - public double eval() { - - double val=0; - - if (listCases.size() != 0) - for(int i=0; i