From e0bdcd838a5cc919297b084b9ea02b67535c77fd Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 11 Jun 2019 16:47:54 +0200 Subject: [PATCH] refacto --- src/app/Application.java | 42 +++++++----------------------------------- src/kernel/Cell.java | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------- src/kernel/Grid.java | 8 ++++---- src/kernel/function/Average.java | 5 ++--- src/kernel/function/Function.java | 38 ++++++++++---------------------------- src/kernel/function/Sum.java | 5 ++--- src/kernel/operation/Addition.java | 16 +++++++--------- src/kernel/operation/BinaryOperation.java | 22 +++++++++------------- src/kernel/operation/Division.java | 17 ++++++----------- src/kernel/operation/Multiplication.java | 14 ++++++-------- src/kernel/operation/Subtraction.java | 14 ++++++-------- 11 files changed, 149 insertions(+), 216 deletions(-) diff --git a/src/app/Application.java b/src/app/Application.java index d117c45..b09adb7 100644 --- a/src/app/Application.java +++ b/src/app/Application.java @@ -12,31 +12,22 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; +public class Application { -public class Application implements Serializable{ + public static void main(String[] args) throws IOException { + File fichier = new File("essai.ser"); + // ouverture d'un flux sur un fichier + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier)); - //private static final long serialVersionUID = -5923230541404119541L; - - private static final long serialVersionUID = 1L; - - public static void main(String[] args) throws IOException { - - File fichier = new File("essai.ser") ; - - // ouverture d'un flux sur un fichier - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier)) ; - - Grid grid = new Grid(); + Grid grid = new Grid(); // Création System.out.println("Création de quelques cases..."); - try { grid.createCell("A", 1, 10.); grid.createCell("B", 1, 0.); @@ -52,14 +43,9 @@ public class Application implements Serializable{ averageList.add(grid.getCell("B", 1)); grid.createCell("B", 2, new Average(averageList)); - } catch (CellNotFoundException exception) { + } catch (CellNotFoundException | CreateCycleException | InvalidIntervalLineColumnEception exception) { System.out.println(exception.getMessage()); - } catch (CreateCycleException exception) { - System.out.println(exception.toString()); } - catch (InvalidIntervalLineColumnEception exception) { - System.out.println(exception.toString()); - } // Affichage List cells = grid.getCells(); @@ -86,23 +72,9 @@ public class Application implements Serializable{ System.out.println("Affichage des valeurs après modification :"); for (Cell cell : cells) System.out.println(cell.getId() + ": " + cell.getValue()); - // sérialization de l'objet - oos.writeObject(grid); oos.close(); } - - - - - - - - - - - - } diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index 53e168c..3221591 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -9,98 +9,94 @@ import java.util.List; public class Cell implements Serializable { - private static final long serialVersionUID = 1L; - - final int MAX_LIGNES = 20; - - private String column; - private int line; - private double value; - private Formula formula; - private List usedIn = new ArrayList<>(); - - public boolean IntervallCondition(String column, int line){ - - return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0; - - } - - public Cell(String column,int line, double value) throws InvalidIntervalLineColumnEception { - column = column.toUpperCase(); - if (IntervallCondition(column,line)) { - this.column = column; - this.line = line; - this.setValue(value); - } else - throw new InvalidIntervalLineColumnEception(); - } - - public Cell(String column, int line, Formula formula) - throws CreateCycleException, InvalidIntervalLineColumnEception { - column = column.toUpperCase(); - if (IntervallCondition(column,line)) { - this.column = column; - this.line = line; - this.setFormula(formula); - } else - throw new InvalidIntervalLineColumnEception(); - - } - - public double getValue() { - return this.value; - } - - public Formula getFormula() { - return this.formula; - } - - public String getDevelopedFormula() { - return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId(); - } - - public String getId() { - return this.column + this.line; - } - - public List getUsedIn() { - return this.usedIn; - } - - public String toString() { - return this.containFormula() ? this.formula.toString() : this.getId(); - } - - public void updateValue() { - if (this.containFormula()) - this.value = this.formula.eval(); - } - - public Boolean containFormula() { - return this.formula != null; - } - - public void setFormula(Formula formula) throws CreateCycleException { - if (formula.createCycle(this)) - throw new CreateCycleException(); - - 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.formula = null; - this.spreadValue(); - } - - private void spreadValue() { - for (Cell cell : this.usedIn) { - cell.updateValue(); - cell.spreadValue(); - } - } + private static final long serialVersionUID = 1L; + private static final int MAX_LIGNES = 20; + + private String column; + private int line; + private double value; + private Formula formula; + private List usedIn = new ArrayList<>(); + + public Cell(String column, int line, double value) throws InvalidIntervalLineColumnEception { + column = column.toUpperCase(); + if (!validateInterval(column, line)) + throw new InvalidIntervalLineColumnEception(); + + this.column = column; + this.line = line; + this.setValue(value); + } + + public Cell(String column, int line, Formula formula) + throws CreateCycleException, InvalidIntervalLineColumnEception { + column = column.toUpperCase(); + if (!validateInterval(column, line)) + throw new InvalidIntervalLineColumnEception(); + + this.column = column; + this.line = line; + this.setFormula(formula); + } + + public double getValue() { + return this.value; + } + + public Formula getFormula() { + return this.formula; + } + + public String getDevelopedFormula() { + return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId(); + } + + public String getId() { + return this.column + this.line; + } + + public List getUsedIn() { + return this.usedIn; + } + + public String toString() { + return this.containFormula() ? this.formula.toString() : this.getId(); + } + + public void updateValue() { + if (this.containFormula()) + this.value = this.formula.eval(); + } + + public boolean containFormula() { + return this.formula != null; + } + + public void setFormula(Formula formula) throws CreateCycleException { + if (formula.createCycle(this)) + throw new CreateCycleException(); + + 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.formula = null; + this.spreadValue(); + } + + private void spreadValue() { + for (Cell cell : this.usedIn) { + cell.updateValue(); + cell.spreadValue(); + } + } + + private boolean validateInterval(String column, int line) { + return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0; + } } diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index 95a08dd..b54903a 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -12,9 +12,9 @@ import java.util.Map; public class Grid implements Serializable { - private static final long serialVersionUID = 1L; - - private Map cells = new HashMap<>(); + private static final long serialVersionUID = 1L; + + private Map cells = new HashMap<>(); public static LanguageEnum language = LanguageEnum.FR; public String createCell(String column, int line, double value) throws InvalidIntervalLineColumnEception { @@ -57,7 +57,7 @@ public class Grid implements Serializable { return new ArrayList<>(this.cells.values()); } - public Double getValue(String column, int line) throws CellNotFoundException { + public double getValue(String column, int line) throws CellNotFoundException { return this.getCell(column, line).getValue(); } diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java index 74776af..3ebd89d 100644 --- a/src/kernel/function/Average.java +++ b/src/kernel/function/Average.java @@ -8,16 +8,15 @@ import java.util.OptionalDouble; public class Average extends Function { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public Average(List listCells) { + public Average(List listCells) { super(listCells); this.names.put(LanguageEnum.FR, "MOYENNE"); this.names.put(LanguageEnum.EN, "AVERAGE"); } public double eval() { - OptionalDouble average = this.listCells.stream() .mapToDouble(Cell::getValue) .average(); diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java index 972584f..2aeb671 100644 --- a/src/kernel/function/Function.java +++ b/src/kernel/function/Function.java @@ -11,11 +11,10 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -abstract public class Function implements Formula,Serializable { +abstract public class Function implements Formula, Serializable { - - private static final long serialVersionUID = 1L; - public Map names = new HashMap<>(); + private static final long serialVersionUID = 1L; + public Map names = new HashMap<>(); public List listCells; public Function(List listCells) { @@ -39,32 +38,15 @@ abstract public class Function implements Formula,Serializable { } public boolean createCycle(Cell cell) { - - if (!this.listCells.contains(cell)) - for(Cell currentCell:this.listCells) - return currentCell.containFormula() && currentCell.getFormula().createCycle(cell); + if (!this.listCells.contains(cell)) + for (Cell currentCell : this.listCells) + if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) + return true; - return true; + return true; } - /* - public Boolean createCycle(Cell cell) { - 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; - } + return this.listCells; + } } diff --git a/src/kernel/function/Sum.java b/src/kernel/function/Sum.java index c9c9756..a0c9451 100644 --- a/src/kernel/function/Sum.java +++ b/src/kernel/function/Sum.java @@ -7,10 +7,9 @@ import java.util.List; public class Sum extends Function { + private static final long serialVersionUID = 1L; - private static final long serialVersionUID = 1L; - - public Sum(List listCells) { + public Sum(List listCells) { super(listCells); this.names.put(LanguageEnum.FR, "SOMME"); this.names.put(LanguageEnum.EN, "SUM"); diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java index 641a65f..8a71503 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -4,18 +4,16 @@ import kernel.Cell; public class Addition extends BinaryOperation { + private static final long serialVersionUID = 1L; - private static final long serialVersionUID = 1L; - - public Addition(Cell leftCell, Cell rightCell) { + public Addition(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - } - - public String getOperator(){ - return "+"; - } - + + public String getOperator() { + return "+"; + } + public double eval() { return this.leftCell.getValue() + this.rightCell.getValue(); } diff --git a/src/kernel/operation/BinaryOperation.java b/src/kernel/operation/BinaryOperation.java index 2b300f6..1a5f0ab 100644 --- a/src/kernel/operation/BinaryOperation.java +++ b/src/kernel/operation/BinaryOperation.java @@ -7,12 +7,11 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; -abstract public class BinaryOperation implements Formula,Serializable { +abstract public class BinaryOperation implements Formula, Serializable { - private static final long serialVersionUID = 1L; - protected Cell leftCell; + private static final long serialVersionUID = 1L; + protected Cell leftCell; protected Cell rightCell; - public BinaryOperation(Cell leftCell, Cell rightCell) { this.leftCell = leftCell; @@ -20,33 +19,30 @@ abstract public class BinaryOperation implements Formula,Serializable { } abstract public double eval(); + abstract public String getOperator(); + public String getDevelopedFormula() { - return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula()+")"; + return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")"; } public String toString() { return this.leftCell.getId() + this.getOperator() + this.rightCell.getId(); } - public boolean createCycle(Cell cell) { if (this.leftCell.containFormula() && !this.rightCell.containFormula()) return this.leftCell.getFormula().createCycle(cell); - + if (!this.leftCell.containFormula() && this.rightCell.containFormula()) return this.rightCell.getFormula().createCycle(cell); - + if (this.leftCell.containFormula() && this.rightCell.containFormula()) return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell); - - return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); + return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); } - - - public List getUtilisedCells() { List cells = new ArrayList<>(); cells.add(this.leftCell); diff --git a/src/kernel/operation/Division.java b/src/kernel/operation/Division.java index d7c6137..51b4734 100644 --- a/src/kernel/operation/Division.java +++ b/src/kernel/operation/Division.java @@ -4,21 +4,16 @@ import kernel.Cell; public class Division extends BinaryOperation { + private static final long serialVersionUID = 1L; - private static final long serialVersionUID = 1L; - - public Division(Cell leftCell, Cell rightCell) { + public Division(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - } - - - public String getOperator(){ - return "/"; - } - - + public String getOperator() { + return "/"; + } + public double eval() { return this.leftCell.getValue() / this.rightCell.getValue(); } diff --git a/src/kernel/operation/Multiplication.java b/src/kernel/operation/Multiplication.java index c283872..a626697 100644 --- a/src/kernel/operation/Multiplication.java +++ b/src/kernel/operation/Multiplication.java @@ -4,18 +4,16 @@ import kernel.Cell; public class Multiplication extends BinaryOperation { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public Multiplication(Cell leftCell, Cell rightCell) { + public Multiplication(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); + } + public String getOperator() { + return "*"; } - - public String getOperator(){ - return "*"; - } - - + public double eval() { return this.leftCell.getValue() * this.rightCell.getValue(); } diff --git a/src/kernel/operation/Subtraction.java b/src/kernel/operation/Subtraction.java index 4768133..02d68ec 100644 --- a/src/kernel/operation/Subtraction.java +++ b/src/kernel/operation/Subtraction.java @@ -4,19 +4,17 @@ import kernel.Cell; public class Subtraction extends BinaryOperation { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public Subtraction(Cell leftCell, Cell rightCell) { + public Subtraction(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); + } + + public String getOperator() { + return "-"; } - - public String getOperator(){ - return "-"; - } - - public double eval() { return this.leftCell.getValue() - this.rightCell.getValue(); } -- libgit2 0.21.2