From 080a0e68dfee9e542a95e2dd8463c8aa522afffc Mon Sep 17 00:00:00 2001 From: Remi Date: Thu, 20 Jun 2019 10:37:38 +0200 Subject: [PATCH] change return --- .gitignore | 5 ++--- grid.data | Bin 1764 -> 0 bytes src/ihm/TablooProto.java | 4 ++-- src/kernel/Cell.java | 20 +++++++++++++++++--- src/kernel/Formula.java | 1 + src/kernel/Grid.java | 48 ++++++++++++++++++++---------------------------- src/kernel/exception/BadSyntaxException.java | 14 +++++++++++++- src/kernel/exception/CannotDeleteCellException.java | 14 +++++++++++++- src/kernel/exception/CellNotFoundException.java | 14 +++++++++++++- src/kernel/exception/CreateCycleException.java | 15 ++++++++++++++- src/kernel/exception/InvalidIntervalException.java | 16 ++++++++++++++++ src/kernel/function/Average.java | 7 ++++--- src/kernel/function/Function.java | 29 +++++++++++++++++++---------- src/kernel/function/Sum.java | 7 ++++--- src/kernel/operation/Addition.java | 4 +++- src/kernel/operation/BinaryOperation.java | 16 ++++++++++++++-- src/kernel/operation/Division.java | 4 +++- src/kernel/operation/Multiplication.java | 4 +++- src/kernel/operation/Subtraction.java | 4 +++- src/kernel/test/GridTest.java | 4 ++-- 20 files changed, 166 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index c53d513..1b1b725 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,8 @@ -essai.ser -grid.data +*.data /.idea *.iml /out /bin /.metadata .project -.classpath \ No newline at end of file +.classpath diff --git a/grid.data b/grid.data index 123c347..abdeb3e 100644 Binary files a/grid.data and b/grid.data differ diff --git a/src/ihm/TablooProto.java b/src/ihm/TablooProto.java index 76ecfaf..acd26d7 100644 --- a/src/ihm/TablooProto.java +++ b/src/ihm/TablooProto.java @@ -277,7 +277,7 @@ public class TablooProto extends JPanel { // Exécution de l'interface graphique a partir d'un terminal. public static void main(String[] args) throws ClassNotFoundException, IOException { try { - grid = Grid.load(); + grid = Grid.load("grid.data"); } catch (IOException | ClassNotFoundException e) { grid = new Grid(); } @@ -298,7 +298,7 @@ public class TablooProto extends JPanel { @Override public void windowClosing(WindowEvent we) { try { - grid.save(); + grid.save("grid.data"); } catch (IOException e) { System.out.println(e.getMessage()); } diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index b01f5c7..f3c5995 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -5,12 +5,11 @@ import kernel.exception.CreateCycleException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class Cell implements Serializable { private static final long serialVersionUID = 1L; - - private String column; private int line; private double value; @@ -72,7 +71,7 @@ public class Cell implements Serializable { public void setFormula(Formula formula) throws CreateCycleException { if (formula.createCycle(this)) - throw new CreateCycleException(); + throw new CreateCycleException("L'assignation de la formule " + formula.toString() + " créée un cycle."); this.formula = formula; for (Cell cell : this.formula.getUtilisedCells()) @@ -94,5 +93,20 @@ public class Cell implements Serializable { } } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof Cell)) + return false; + + Cell cell = (Cell) o; + + return line == cell.line && column.equals(cell.column); + } + @Override + public int hashCode() { + return Objects.hash(column, line); + } } diff --git a/src/kernel/Formula.java b/src/kernel/Formula.java index a6dc8dd..8286224 100644 --- a/src/kernel/Formula.java +++ b/src/kernel/Formula.java @@ -6,6 +6,7 @@ public interface Formula { String getDevelopedFormula(); + @Override String toString(); double eval(); diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index bd43524..aebe96a 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -20,46 +20,42 @@ public class Grid implements Serializable { public static LanguageEnum language = LanguageEnum.FR; public void createCell(String column, int line, double value) throws InvalidIntervalException { - column = column.toUpperCase(); - if (!validateInterval(column, line)) throw new InvalidIntervalException(); + String id = this.getCellId(column, line); Cell cell = new Cell(column, line, value); - this.cells.put(id, cell); + this.cells.put(id, cell); } public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException { - column = column.toUpperCase(); - if (!validateInterval(column, line)) throw new InvalidIntervalException(); + String id = this.getCellId(column, line); Cell cell = new Cell(column, line, formula); - this.cells.put(id, cell); + this.cells.put(id, cell); } public void setValue(String column, int line, double value) throws CellNotFoundException { - column = column.toUpperCase(); this.getCell(column, line).setValue(value); } public void setFormula(String column, int line, Formula formula) throws CellNotFoundException, CreateCycleException { - column = column.toUpperCase(); this.getCell(column, line).setFormula(formula); } public Cell getCell(String column, int line) throws CellNotFoundException { - column = column.toUpperCase(); - Cell cell = this.cells.get(this.getCellId(column, line)); + String id = this.getCellId(column, line); + Cell cell = this.cells.get(id); if (cell != null) return cell; else - throw new CellNotFoundException(); + throw new CellNotFoundException("La cellule " + id + " n'existe pas."); } public Cell getCell(String id) { @@ -71,26 +67,21 @@ public class Grid implements Serializable { } public double getValue(String column, int line) throws CellNotFoundException { - column = column.toUpperCase(); - return this.getCell(column, line).getValue(); + return this.getCell(column.toUpperCase(), line).getValue(); } public String getFormulaAsString(String column, int line) throws CellNotFoundException { - column = column.toUpperCase(); - return this.getCell(column, line).toString(); + return this.getCell(column.toUpperCase(), line).toString(); } public String getDevelopedFormula(String column, int line) throws CellNotFoundException { - column = column.toUpperCase(); - return this.getCell(column, line).getDevelopedFormula(); + return this.getCell(column.toUpperCase(), line).getDevelopedFormula(); } private String getCellId(String column, int line) { - column = column.toUpperCase(); - return column + line; + return column.toUpperCase() + line; } - public int getTotalColumn() { return MAX_COLUMNS.charAt(0) - (int) 'A' + 1; } @@ -114,10 +105,10 @@ public class Grid implements Serializable { Cell cell = this.getCell(id); if (!cell.getUsedIn().isEmpty()) - throw new CannotDeleteCellException(); + throw new CannotDeleteCellException("La cellule " + id + " est utilisée dans une autre case."); cell.updateUsedIn(); - this.cells.remove(this.getCellId(column, line)); + this.cells.remove(id); } } @@ -131,20 +122,21 @@ public class Grid implements Serializable { return ascii; } - public void save() throws IOException { - File file = new File("grid.data"); + public void save(String fileName) throws IOException { + File file = new File(fileName); ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file)); stream.writeObject(this); stream.close(); } - public static Grid load() throws IOException, ClassNotFoundException { - File file = new File("grid.data"); + public static Grid load(String fileName) throws IOException, ClassNotFoundException { + File file = new File(fileName); ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file)); + Grid grid = (Grid) stream.readObject(); + stream.close(); - return (Grid) stream.readObject(); + return grid; } - } diff --git a/src/kernel/exception/BadSyntaxException.java b/src/kernel/exception/BadSyntaxException.java index 1d4ae1d..ca610b4 100644 --- a/src/kernel/exception/BadSyntaxException.java +++ b/src/kernel/exception/BadSyntaxException.java @@ -3,6 +3,18 @@ package kernel.exception; public class BadSyntaxException extends Exception { public BadSyntaxException() { - super("Erreur de syntaxe."); + super(); + } + + public BadSyntaxException(String message) { + super(message); + } + + public BadSyntaxException(Throwable throwable) { + super(throwable); + } + + public BadSyntaxException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/src/kernel/exception/CannotDeleteCellException.java b/src/kernel/exception/CannotDeleteCellException.java index 7899a53..38016e6 100644 --- a/src/kernel/exception/CannotDeleteCellException.java +++ b/src/kernel/exception/CannotDeleteCellException.java @@ -3,6 +3,18 @@ package kernel.exception; public class CannotDeleteCellException extends Exception { public CannotDeleteCellException() { - super("Cette cellule est utilisée dans une autre cellule."); + super(); + } + + public CannotDeleteCellException(String message) { + super(message); + } + + public CannotDeleteCellException(Throwable throwable) { + super(throwable); + } + + public CannotDeleteCellException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/src/kernel/exception/CellNotFoundException.java b/src/kernel/exception/CellNotFoundException.java index 44fe037..a0b2a6a 100644 --- a/src/kernel/exception/CellNotFoundException.java +++ b/src/kernel/exception/CellNotFoundException.java @@ -3,6 +3,18 @@ package kernel.exception; public class CellNotFoundException extends Exception { public CellNotFoundException() { - super("Vous voulez utiliser une cellule qui n'est pas encore créée."); + super(); + } + + public CellNotFoundException(String message) { + super(message); + } + + public CellNotFoundException(Throwable throwable) { + super(throwable); + } + + public CellNotFoundException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/src/kernel/exception/CreateCycleException.java b/src/kernel/exception/CreateCycleException.java index e12a981..b5e0754 100644 --- a/src/kernel/exception/CreateCycleException.java +++ b/src/kernel/exception/CreateCycleException.java @@ -1,7 +1,20 @@ package kernel.exception; public class CreateCycleException extends Exception { + public CreateCycleException() { - super("L'aasignation de cette formule créée un cycle."); + super(); + } + + public CreateCycleException(String message) { + super(message); + } + + public CreateCycleException(Throwable throwable) { + super(throwable); + } + + public CreateCycleException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/src/kernel/exception/InvalidIntervalException.java b/src/kernel/exception/InvalidIntervalException.java index 65e9bc6..bb1c0d0 100644 --- a/src/kernel/exception/InvalidIntervalException.java +++ b/src/kernel/exception/InvalidIntervalException.java @@ -1,4 +1,20 @@ package kernel.exception; public class InvalidIntervalException extends Exception { + + public InvalidIntervalException() { + super(); + } + + public InvalidIntervalException(String message) { + super(message); + } + + public InvalidIntervalException(Throwable throwable) { + super(throwable); + } + + public InvalidIntervalException(String message, Throwable throwable) { + super(message, throwable); + } } diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java index 466f874..791d504 100644 --- a/src/kernel/function/Average.java +++ b/src/kernel/function/Average.java @@ -10,12 +10,13 @@ public class Average extends Function { public Average(List listCells) { super(listCells); - this.names.put(LanguageEnum.FR, "MOYENNE"); - this.names.put(LanguageEnum.EN, "AVERAGE"); + this.getNames().put(LanguageEnum.FR, "MOYENNE"); + this.getNames().put(LanguageEnum.EN, "AVERAGE"); } + @Override public double eval() { - OptionalDouble average = this.listCells.stream().mapToDouble(Cell::getValue).average(); + OptionalDouble average = this.getUtilisedCells().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 index b42d8ff..0c37b50 100644 --- a/src/kernel/function/Function.java +++ b/src/kernel/function/Function.java @@ -14,8 +14,8 @@ import java.util.stream.Collectors; abstract public class Function implements Formula, Serializable { private static final long serialVersionUID = 1L; - public Map names = new HashMap<>(); - public List listCells; + private Map names = new HashMap<>(); + private List listCells; public Function(List listCells) { this.listCells = listCells; @@ -23,6 +23,11 @@ abstract public class Function implements Formula, Serializable { abstract public double eval(); + public Map getNames() { + return this.names; + } + + @Override public String getDevelopedFormula() { return names.get(Grid.language) + "(" + this.listCells.stream() @@ -30,6 +35,7 @@ abstract public class Function implements Formula, Serializable { .collect(Collectors.joining(",")) + ")"; } + @Override public String toString() { return names.get(Grid.language) + "(" + this.listCells.stream() @@ -37,18 +43,21 @@ abstract public class Function implements Formula, Serializable { .collect(Collectors.joining(",")) + ")"; } + @Override public boolean createCycle(Cell cell) { - if (!this.listCells.contains(cell)) { - for (Cell currentCell : this.listCells) - if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) - return true; - } else - return true; - - return false; + return this.listCells.contains(cell) || this.generateIndirectCycle(cell); } + @Override public List getUtilisedCells() { return this.listCells; } + + private boolean generateIndirectCycle(Cell cell) { + for (Cell currentCell : this.listCells) + if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) + return true; + + return false; + } } diff --git a/src/kernel/function/Sum.java b/src/kernel/function/Sum.java index 02667d3..0429594 100644 --- a/src/kernel/function/Sum.java +++ b/src/kernel/function/Sum.java @@ -9,11 +9,12 @@ public class Sum extends Function { public Sum(List listCells) { super(listCells); - this.names.put(LanguageEnum.FR, "SOMME"); - this.names.put(LanguageEnum.EN, "SUM"); + this.getNames().put(LanguageEnum.FR, "SOMME"); + this.getNames().put(LanguageEnum.EN, "SUM"); } + @Override public double eval() { - return this.listCells.stream().mapToDouble(Cell::getValue).sum(); + return this.getUtilisedCells().stream().mapToDouble(Cell::getValue).sum(); } } diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java index f239c07..c578131 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -8,11 +8,13 @@ public class Addition extends BinaryOperation { super(leftCell, rightCell); } + @Override public String getOperator() { return "+"; } + @Override public double eval() { - return this.leftCell.getValue() + this.rightCell.getValue(); + return this.getLeftCell().getValue() + this.getRightCell().getValue(); } } diff --git a/src/kernel/operation/BinaryOperation.java b/src/kernel/operation/BinaryOperation.java index 8e0003d..6181112 100644 --- a/src/kernel/operation/BinaryOperation.java +++ b/src/kernel/operation/BinaryOperation.java @@ -10,8 +10,8 @@ import java.util.List; abstract public class BinaryOperation implements Formula, Serializable { private static final long serialVersionUID = 1L; - protected Cell leftCell; - protected Cell rightCell; + private Cell leftCell; + private Cell rightCell; public BinaryOperation(Cell leftCell, Cell rightCell) { this.leftCell = leftCell; @@ -22,14 +22,25 @@ abstract public class BinaryOperation implements Formula, Serializable { abstract public String getOperator(); + public Cell getLeftCell() { + return this.leftCell; + } + + public Cell getRightCell() { + return this.rightCell; + } + + @Override public String getDevelopedFormula() { return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")"; } + @Override public String toString() { return "(" + this.leftCell.getId() + this.getOperator() + this.rightCell.getId() + ")"; } + @Override public boolean createCycle(Cell cell) { if (this.leftCell.containFormula() && !this.rightCell.containFormula()) return this.leftCell.getFormula().createCycle(cell); @@ -43,6 +54,7 @@ abstract public class BinaryOperation implements Formula, Serializable { return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); } + @Override 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 9db6dfa..2067987 100644 --- a/src/kernel/operation/Division.java +++ b/src/kernel/operation/Division.java @@ -8,11 +8,13 @@ public class Division extends BinaryOperation { super(leftCell, rightCell); } + @Override public String getOperator() { return "/"; } + @Override public double eval() { - return this.leftCell.getValue() / this.rightCell.getValue(); + return this.getLeftCell().getValue() / this.getRightCell().getValue(); } } diff --git a/src/kernel/operation/Multiplication.java b/src/kernel/operation/Multiplication.java index fc718c4..a201fad 100644 --- a/src/kernel/operation/Multiplication.java +++ b/src/kernel/operation/Multiplication.java @@ -8,11 +8,13 @@ public class Multiplication extends BinaryOperation { super(leftCell, rightCell); } + @Override public String getOperator() { return "*"; } + @Override public double eval() { - return this.leftCell.getValue() * this.rightCell.getValue(); + return this.getLeftCell().getValue() * this.getRightCell().getValue(); } } diff --git a/src/kernel/operation/Subtraction.java b/src/kernel/operation/Subtraction.java index 1ab7598..33d5a71 100644 --- a/src/kernel/operation/Subtraction.java +++ b/src/kernel/operation/Subtraction.java @@ -8,11 +8,13 @@ public class Subtraction extends BinaryOperation { super(leftCell, rightCell); } + @Override public String getOperator() { return "-"; } + @Override public double eval() { - return this.leftCell.getValue() - this.rightCell.getValue(); + return this.getLeftCell().getValue() - this.getRightCell().getValue(); } } diff --git a/src/kernel/test/GridTest.java b/src/kernel/test/GridTest.java index c8c0739..79e59b7 100644 --- a/src/kernel/test/GridTest.java +++ b/src/kernel/test/GridTest.java @@ -213,9 +213,9 @@ public class GridTest { CreateCycleException { this.createCellsWithFormula(); - this.grid.save(); + this.grid.save("test.data"); - Grid g = Grid.load(); + Grid g = Grid.load("test.data"); Cell cell = g.getCell("A", 1); -- libgit2 0.21.2