diff --git a/essai.ser b/essai.ser index b4d3973..3cbd397 100644 Binary files a/essai.ser and b/essai.ser differ diff --git a/src/app/Application.java b/src/app/Application.java index 15a203f..d117c45 100644 --- a/src/app/Application.java +++ b/src/app/Application.java @@ -19,7 +19,10 @@ import java.util.List; public class Application implements Serializable{ - //private static final long serialVersionUID = 1L; + + //private static final long serialVersionUID = -5923230541404119541L; + + private static final long serialVersionUID = 1L; public static void main(String[] args) throws IOException { @@ -35,7 +38,7 @@ public class Application implements Serializable{ try { - grid.createCell("AA", 1, 10.); + grid.createCell("A", 1, 10.); grid.createCell("B", 1, 0.); grid.createCell("A", 2, 5.); List sumList = new ArrayList<>(); diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index 4a4e3d0..53e168c 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -8,17 +8,26 @@ import java.util.ArrayList; import java.util.List; public class Cell implements Serializable { + + private static final long serialVersionUID = 1L; + final int MAX_LIGNES = 20; private String column; - private Integer line; - private Double value; + 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, Integer line, Double value) throws InvalidIntervalLineColumnEception { + } + + public Cell(String column,int line, double value) throws InvalidIntervalLineColumnEception { column = column.toUpperCase(); - if ((line >= 1 && line <= MAX_LIGNES) || (column.compareTo("A") >= 0 && column.compareTo("Z") <= 0)) { + if (IntervallCondition(column,line)) { this.column = column; this.line = line; this.setValue(value); @@ -26,10 +35,10 @@ public class Cell implements Serializable { throw new InvalidIntervalLineColumnEception(); } - public Cell(String column, Integer line, Formula formula) + public Cell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { column = column.toUpperCase(); - if ((line >= 1 && line <= MAX_LIGNES) || (column.compareTo("A") >= 0 && column.compareTo("Z") <= 0)) { + if (IntervallCondition(column,line)) { this.column = column; this.line = line; this.setFormula(formula); @@ -38,7 +47,7 @@ public class Cell implements Serializable { } - public Double getValue() { + public double getValue() { return this.value; } @@ -51,7 +60,7 @@ public class Cell implements Serializable { } public String getId() { - return this.column + this.line.toString(); + return this.column + this.line; } public List getUsedIn() { diff --git a/src/kernel/Formula.java b/src/kernel/Formula.java index 121a136..5f2bd89 100644 --- a/src/kernel/Formula.java +++ b/src/kernel/Formula.java @@ -8,9 +8,9 @@ public interface Formula { String toString(); - Double eval(); + double eval(); - Boolean createCycle(Cell cell); + boolean createCycle(Cell cell); List getUtilisedCells(); } diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index 22c73f9..95a08dd 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -12,10 +12,12 @@ import java.util.Map; public class Grid implements Serializable { - 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, Integer line, Double value) throws InvalidIntervalLineColumnEception { + public String createCell(String column, int line, double value) throws InvalidIntervalLineColumnEception { String id = this.getCellId(column, line); Cell cell = new Cell(column, line, value); @@ -24,7 +26,7 @@ public class Grid implements Serializable { return id; } - public String createCell(String column, Integer line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { + public String createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { String id = this.getCellId(column, line); Cell cell = new Cell(column, line, formula); @@ -33,16 +35,16 @@ public class Grid implements Serializable { return id; } - public void setValue(String column, Integer line, Double value) throws CellNotFoundException { + public void setValue(String column, int line, double value) throws CellNotFoundException { this.getCell(column, line).setValue(value); } - public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException, + public void setFormula(String column, int line, Formula formula) throws CellNotFoundException, CreateCycleException { this.getCell(column, line).setFormula(formula); } - public Cell getCell(String column, Integer line) throws CellNotFoundException { + public Cell getCell(String column, int line) throws CellNotFoundException { Cell cell = this.cells.get(this.getCellId(column, line)); if (cell != null) @@ -55,19 +57,19 @@ public class Grid implements Serializable { return new ArrayList<>(this.cells.values()); } - public Double getValue(String column, Integer line) throws CellNotFoundException { + public Double getValue(String column, int line) throws CellNotFoundException { return this.getCell(column, line).getValue(); } - public String getFormulaAsString(String column, Integer line) throws CellNotFoundException { + public String getFormulaAsString(String column, int line) throws CellNotFoundException { return this.getCell(column, line).toString(); } - public String getDevelopedFormula(String column, Integer line) throws CellNotFoundException { + public String getDevelopedFormula(String column, int line) throws CellNotFoundException { return this.getCell(column, line).getDevelopedFormula(); } - private String getCellId(String column, Integer line) { - return column + line.toString(); + private String getCellId(String column, int line) { + return column + line; } } diff --git a/src/kernel/exception/CellNotFoundException.java b/src/kernel/exception/CellNotFoundException.java index 3b059e4..7c9c8ab 100644 --- a/src/kernel/exception/CellNotFoundException.java +++ b/src/kernel/exception/CellNotFoundException.java @@ -1,4 +1,7 @@ package kernel.exception; public class CellNotFoundException extends Exception { + + + private static final long serialVersionUID = 1L; } diff --git a/src/kernel/exception/CreateCycleException.java b/src/kernel/exception/CreateCycleException.java index 8e20268..9340e02 100644 --- a/src/kernel/exception/CreateCycleException.java +++ b/src/kernel/exception/CreateCycleException.java @@ -1,4 +1,6 @@ package kernel.exception; public class CreateCycleException extends Exception { + + private static final long serialVersionUID = 1L; } diff --git a/src/kernel/exception/InvalidIntervalLineColumnEception.java b/src/kernel/exception/InvalidIntervalLineColumnEception.java index a98d39b..e0a361b 100644 --- a/src/kernel/exception/InvalidIntervalLineColumnEception.java +++ b/src/kernel/exception/InvalidIntervalLineColumnEception.java @@ -2,4 +2,7 @@ package kernel.exception; public class InvalidIntervalLineColumnEception extends Exception { + + private static final long serialVersionUID = 1L; + } diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java index f504d58..74776af 100644 --- a/src/kernel/function/Average.java +++ b/src/kernel/function/Average.java @@ -8,13 +8,15 @@ import java.util.OptionalDouble; public class Average extends Function { - public Average(List listCells) { + private static final long serialVersionUID = 1L; + + public Average(List listCells) { super(listCells); this.names.put(LanguageEnum.FR, "MOYENNE"); this.names.put(LanguageEnum.EN, "AVERAGE"); } - public Double eval() { + public double eval() { OptionalDouble average = this.listCells.stream() .mapToDouble(Cell::getValue) diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java index fc53b11..972584f 100644 --- a/src/kernel/function/Function.java +++ b/src/kernel/function/Function.java @@ -7,21 +7,22 @@ import kernel.LanguageEnum; import java.io.Serializable; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; abstract public class Function implements Formula,Serializable { - public Map names = new HashMap<>(); + + private static final long serialVersionUID = 1L; + public Map names = new HashMap<>(); public List listCells; public Function(List listCells) { this.listCells = listCells; } - abstract public Double eval(); + abstract public double eval(); public String getDevelopedFormula() { return names.get(Grid.language) + "(" @@ -37,6 +38,16 @@ abstract public class Function implements Formula,Serializable { .collect(Collectors.joining(",")) + ")"; } + public boolean createCycle(Cell cell) { + + if (!this.listCells.contains(cell)) + for(Cell currentCell:this.listCells) + return currentCell.containFormula() && currentCell.getFormula().createCycle(cell); + + return true; + } + + /* public Boolean createCycle(Cell cell) { boolean cycle = false; if (!this.listCells.contains(cell)) { @@ -51,7 +62,8 @@ abstract public class Function implements Formula,Serializable { } else return true; } - + + */ public List getUtilisedCells() { return this.listCells; } diff --git a/src/kernel/function/Sum.java b/src/kernel/function/Sum.java index 72b4895..c9c9756 100644 --- a/src/kernel/function/Sum.java +++ b/src/kernel/function/Sum.java @@ -7,13 +7,16 @@ import java.util.List; public class Sum extends Function { - public Sum(List listCells) { + + private static final long serialVersionUID = 1L; + + public Sum(List listCells) { super(listCells); this.names.put(LanguageEnum.FR, "SOMME"); this.names.put(LanguageEnum.EN, "SUM"); } - public Double eval() { + 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 index f5b91fb..7ce3f37 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -4,12 +4,19 @@ import kernel.Cell; public class Addition extends BinaryOperation { - public Addition(Cell leftCell, Cell rightCell) { + + private static final long serialVersionUID = 1L; + + public Addition(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - this.operator = "+"; + } - - public Double eval() { + + 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 759a807..2b300f6 100644 --- a/src/kernel/operation/BinaryOperation.java +++ b/src/kernel/operation/BinaryOperation.java @@ -3,47 +3,50 @@ package kernel.operation; import kernel.Cell; import kernel.Formula; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; -abstract public class BinaryOperation implements Formula { +abstract public class BinaryOperation implements Formula,Serializable { - protected Cell leftCell; + private static final long serialVersionUID = 1L; + 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(); - + abstract public double eval(); + abstract public String getOperator(); public String getDevelopedFormula() { - return "(" + this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula()+")"; + return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula()+")"; } public String toString() { - return this.leftCell.getId() + this.operator + this.rightCell.getId(); + return this.leftCell.getId() + this.getOperator() + this.rightCell.getId(); } - public Boolean createCycle(Cell cell) { + + 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())); - } - } - - } + + 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())); + } + + + 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 121e3be..d7c6137 100644 --- a/src/kernel/operation/Division.java +++ b/src/kernel/operation/Division.java @@ -4,12 +4,22 @@ import kernel.Cell; public class Division extends BinaryOperation { - public Division(Cell leftCell, Cell rightCell) { + + private static final long serialVersionUID = 1L; + + public Division(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - this.operator = "/"; + } - public Double eval() { + + + 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 24327db..c283872 100644 --- a/src/kernel/operation/Multiplication.java +++ b/src/kernel/operation/Multiplication.java @@ -4,12 +4,19 @@ import kernel.Cell; public class Multiplication extends BinaryOperation { - public Multiplication(Cell leftCell, Cell rightCell) { + private static final long serialVersionUID = 1L; + + public Multiplication(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - this.operator = "*"; - } - public Double eval() { + } + + 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 23d5d9b..4768133 100644 --- a/src/kernel/operation/Subtraction.java +++ b/src/kernel/operation/Subtraction.java @@ -4,12 +4,20 @@ import kernel.Cell; public class Subtraction extends BinaryOperation { - public Subtraction(Cell leftCell, Cell rightCell) { + private static final long serialVersionUID = 1L; + + public Subtraction(Cell leftCell, Cell rightCell) { super(leftCell, rightCell); - this.operator = "-"; + } - public Double eval() { + + public String getOperator(){ + return "-"; + } + + + public double eval() { return this.leftCell.getValue() - this.rightCell.getValue(); } } diff --git a/src/kernel/test/CellTest.java b/src/kernel/test/CellTest.java index 88e5ce4..d2d32b8 100644 --- a/src/kernel/test/CellTest.java +++ b/src/kernel/test/CellTest.java @@ -7,6 +7,7 @@ import java.util.List; import kernel.Cell; import kernel.exception.CreateCycleException; +import kernel.exception.InvalidIntervalLineColumnEception; import kernel.function.Average; import kernel.function.Sum; import kernel.operation.Addition; @@ -15,7 +16,7 @@ import kernel.operation.Addition; public class CellTest { @org.junit.Test - public void CreateCellValueTest() { + public void CreateCellValueTest() throws InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); assertEquals(A1.getValue(),25.,0); assertEquals(A1.containFormula(),false); @@ -25,7 +26,7 @@ public class CellTest { @org.junit.Test - public void CreateCellBinaryOperationTest() throws CreateCycleException { + public void CreateCellBinaryOperationTest() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); @@ -46,7 +47,7 @@ public class CellTest { @org.junit.Test - public void CreateCellFunctionTest() throws CreateCycleException { + public void CreateCellFunctionTest() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -75,7 +76,7 @@ public class CellTest { @org.junit.Test - public void ModifyCellValueTest() throws CreateCycleException { + public void ModifyCellValueTest() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -89,7 +90,7 @@ public class CellTest { } @org.junit.Test - public void ModifyCellFormulaTest() throws CreateCycleException { + public void ModifyCellFormulaTest() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -136,7 +137,7 @@ public class CellTest { } @org.junit.Test(expected=CreateCycleException.class) - public void DirectCycleBynaryOperation() throws CreateCycleException { + public void DirectCycleBynaryOperation() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); try{ @@ -149,7 +150,7 @@ public class CellTest { @org.junit.Test(expected=CreateCycleException.class) - public void DirectCycleFunction() throws CreateCycleException { + public void DirectCycleFunction() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A2= new Cell("A",2,25.); Cell B2= new Cell("B",2,35.); Cell A3= new Cell("A",3,0.5); @@ -167,7 +168,7 @@ public class CellTest { } @org.junit.Test(expected=CreateCycleException.class) - public void IndirectCycle() throws CreateCycleException { + public void IndirectCycle() throws CreateCycleException, InvalidIntervalLineColumnEception { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,5.); Cell B4= new Cell("B",4,new Addition(A1,A2)); -- libgit2 0.21.2