From eb07e5e3e665a2ae0ddf7dd1d1633051a0382218 Mon Sep 17 00:00:00 2001 From: [mandjemb] Date: Tue, 11 Jun 2019 12:11:06 +0200 Subject: [PATCH] avec serialisation --- essai.ser | Bin 0 -> 1120 bytes src/app/Application.java | 47 +++++++++++++++++++++++++++++++++++++++++------ src/app/ApplicationDeserialization.java | 38 ++++++++++++++++++++++++++++++++++++++ src/kernel/Cell.java | 34 ++++++++++++++++++++++++---------- src/kernel/Grid.java | 8 +++++--- src/kernel/exception/CycleFoundedException.java | 5 ----- src/kernel/exception/InvalidIntervalLineColumnEception.java | 5 +++++ src/kernel/function/Function.java | 3 ++- src/kernel/operation/BinaryOperation.java | 2 +- src/kernel/test/CellTest.java | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/test/Test.java | 133 ------------------------------------------------------------------------------------------------------------------------------------- 11 files changed, 315 insertions(+), 159 deletions(-) create mode 100644 essai.ser create mode 100644 src/app/ApplicationDeserialization.java delete mode 100644 src/kernel/exception/CycleFoundedException.java create mode 100644 src/kernel/exception/InvalidIntervalLineColumnEception.java create mode 100644 src/kernel/test/CellTest.java delete mode 100644 src/kernel/test/Test.java diff --git a/essai.ser b/essai.ser new file mode 100644 index 0000000..6fce183 Binary files /dev/null and b/essai.ser differ diff --git a/src/app/Application.java b/src/app/Application.java index cd5caff..0c968df 100644 --- a/src/app/Application.java +++ b/src/app/Application.java @@ -4,24 +4,38 @@ import kernel.Cell; import kernel.Grid; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; +import kernel.exception.InvalidIntervalLineColumnEception; import kernel.function.Average; import kernel.function.Sum; +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 static void main(String[] args) { - Grid grid = new Grid(); +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)) ; + + Grid grid = new Grid(); // Création System.out.println("Création de quelques cases..."); - grid.createCell("A", 1, 10.); - grid.createCell("B", 1, 0.); - grid.createCell("A", 2, 5.); + try { + grid.createCell("A", 1, 10.); + grid.createCell("B", 1, 0.); + grid.createCell("A", 2, 5.); List sumList = new ArrayList<>(); sumList.add(grid.getCell("A", 1)); sumList.add(grid.getCell("A", 2)); @@ -38,6 +52,9 @@ public class Application { } catch (CreateCycleException exception) { System.out.println(exception.toString()); } + catch (InvalidIntervalLineColumnEception exception) { + System.out.println(exception.toString()); + } // Affichage List cells = grid.getCells(); @@ -64,5 +81,23 @@ public class Application { 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/app/ApplicationDeserialization.java b/src/app/ApplicationDeserialization.java new file mode 100644 index 0000000..f8455ba --- /dev/null +++ b/src/app/ApplicationDeserialization.java @@ -0,0 +1,38 @@ +package app; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.List; + +import kernel.Cell; +import kernel.Grid; + +public class ApplicationDeserialization { + public static void main(String[] args) throws IOException, ClassNotFoundException { + File fichier = new File("essai.ser") ; + + // ouverture d'un flux sur un fichier + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichier)) ; + + // désérialization de l'objet + Grid grid = (Grid)ois.readObject() ; + // Affichage + List cells = grid.getCells(); + + System.out.println("Affichage des valeurs :"); + for (Cell cell : cells) + System.out.println(cell.getId() + ": " + cell.getValue()); + + System.out.println("Affichage des formules :"); + for (Cell cell : cells) + System.out.println(cell.getId() + ": " + cell.toString()); + + System.out.println("Affichage des formules développées :"); + for (Cell cell : cells) + System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); + ois.close(); + } + +} diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index c2f9a46..105c403 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -1,28 +1,42 @@ package kernel; import kernel.exception.CreateCycleException; +import kernel.exception.InvalidIntervalLineColumnEception; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class Cell { - +public class Cell implements Serializable{ + final int MAX_LIGNES=20; + 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.setValue(value); + public Cell(String column, Integer line, Double value) throws InvalidIntervalLineColumnEception { + column=column.toUpperCase(); + if ((line>= 1 && line<=MAX_LIGNES) || (column.compareTo("A")>=0 && column.compareTo("Z")<=0)){ + this.column = column; + this.line = line; + this.setValue(value); + } + else + throw new InvalidIntervalLineColumnEception(); } - public Cell(String column, Integer line, Formula formula) throws CreateCycleException { - this.column = column; - this.line = line; - this.setFormula(formula); + public Cell(String column, Integer line, Formula formula) throws CreateCycleException,InvalidIntervalLineColumnEception { + column=column.toUpperCase(); + if ((line>= 1 && line<=MAX_LIGNES) || (column.compareTo("A")>=0 && column.compareTo("Z")<=0)){ + this.column = column; + this.line = line; + this.setFormula(formula); + } + else + throw new InvalidIntervalLineColumnEception(); + } public Double getValue() { diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index 1116a7e..22c73f9 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -2,18 +2,20 @@ package kernel; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; +import kernel.exception.InvalidIntervalLineColumnEception; +import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -public class Grid { +public class Grid implements Serializable { private Map cells = new HashMap<>(); public static LanguageEnum language = LanguageEnum.FR; - public String createCell(String column, Integer line, Double value) { + public String createCell(String column, Integer line, Double value) throws InvalidIntervalLineColumnEception { String id = this.getCellId(column, line); Cell cell = new Cell(column, line, value); @@ -22,7 +24,7 @@ public class Grid { return id; } - public String createCell(String column, Integer line, Formula formula) throws CreateCycleException { + public String createCell(String column, Integer line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { String id = this.getCellId(column, line); Cell cell = new Cell(column, line, formula); diff --git a/src/kernel/exception/CycleFoundedException.java b/src/kernel/exception/CycleFoundedException.java deleted file mode 100644 index bd1918c..0000000 --- a/src/kernel/exception/CycleFoundedException.java +++ /dev/null @@ -1,5 +0,0 @@ -package kernel.exception; - -public class CycleFoundedException extends Exception { - -} diff --git a/src/kernel/exception/InvalidIntervalLineColumnEception.java b/src/kernel/exception/InvalidIntervalLineColumnEception.java new file mode 100644 index 0000000..a98d39b --- /dev/null +++ b/src/kernel/exception/InvalidIntervalLineColumnEception.java @@ -0,0 +1,5 @@ +package kernel.exception; + +public class InvalidIntervalLineColumnEception extends Exception { + +} diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java index 5455f71..43ce5c2 100644 --- a/src/kernel/function/Function.java +++ b/src/kernel/function/Function.java @@ -5,13 +5,14 @@ import kernel.Formula; import kernel.Grid; 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 { +abstract public class Function implements Formula,Serializable { public Map names = new HashMap<>(); public List listCells; diff --git a/src/kernel/operation/BinaryOperation.java b/src/kernel/operation/BinaryOperation.java index f30b41f..759a807 100644 --- a/src/kernel/operation/BinaryOperation.java +++ b/src/kernel/operation/BinaryOperation.java @@ -20,7 +20,7 @@ abstract public class BinaryOperation implements Formula { abstract public Double eval(); public String getDevelopedFormula() { - return this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula(); + return "(" + this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula()+")"; } public String toString() { diff --git a/src/kernel/test/CellTest.java b/src/kernel/test/CellTest.java new file mode 100644 index 0000000..88e5ce4 --- /dev/null +++ b/src/kernel/test/CellTest.java @@ -0,0 +1,199 @@ +package kernel.test; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import kernel.Cell; +import kernel.exception.CreateCycleException; +import kernel.function.Average; +import kernel.function.Sum; +import kernel.operation.Addition; + + +public class CellTest { + + @org.junit.Test + public void CreateCellValueTest() { + Cell A1= new Cell("A",1,25.); + assertEquals(A1.getValue(),25.,0); + assertEquals(A1.containFormula(),false); + assertEquals(A1.getUsedIn().size(),0); + assertEquals(A1.getId(),"A1"); + } + + + @org.junit.Test + public void CreateCellBinaryOperationTest() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + + + Cell A3= new Cell("A",3,new Addition(A1,A2)); + + assertEquals(A3.getValue(),60.,0); + assertEquals(A3.containFormula(),true); + assertEquals(A3.getUsedIn().size(),0); + assertEquals(A3.getId(),"A3"); + + assertEquals(A3.toString(),"A1+A2"); + assertEquals(A1.getUsedIn().size(),1); + assertEquals(A2.getUsedIn().size(),1); + + + } + + + @org.junit.Test + public void CreateCellFunctionTest() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + Cell A5= new Cell("A",5,45.); + List sumList = new ArrayList<>(); + sumList.add(A1); + sumList.add(A2); + sumList.add(A3); + sumList.add(A5); + Cell A4= new Cell("A",4,new Sum(sumList)); + + assertEquals(A4.getValue(),165.,0); + assertEquals(A4.containFormula(),true); + assertEquals(A4.getUsedIn().size(),0); + + + assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); + assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,(A1+A2),A5)"); + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),2); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),1); + + + } + + + @org.junit.Test + public void ModifyCellValueTest() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + assertEquals(A1.getValue(),25.,0); + assertEquals(A2.getValue(),35.,0); + assertEquals(A3.getValue(),60.,0); + A1.setValue(45.); + assertEquals(A1.getValue(),45.,0); + assertEquals(A3.getValue(),80.,0); + + } + + @org.junit.Test + public void ModifyCellFormulaTest() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + Cell A5= new Cell("A",5,45.); + List sumList = new ArrayList<>(); + sumList.add(A1); + sumList.add(A2); + sumList.add(A3); + sumList.add(A5); + Cell A4= new Cell("A",4,new Sum(sumList)); + + assertEquals(A4.getValue(),165.,0); + assertEquals(A4.containFormula(),true); + assertEquals(A4.getUsedIn().size(),0); + + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),2); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),1); + + assertEquals(A1.containFormula(),false); + A1.setFormula(new Addition(A2,A5)); + assertEquals(A1.containFormula(),true); + + assertEquals(A1.getValue(),80.,0); + assertEquals(A3.getValue(),115.,0); + assertEquals(A4.getValue(),275.,0); + + + + + assertEquals(A1.toString(),"A2+A5"); + assertEquals(A1.getDevelopedFormula(),"(A2+A5)"); + assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); + assertEquals(A4.getDevelopedFormula(),"SOMME((A2+A5),A2,((A2+A5)+A2),A5)"); + + + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),3); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),2); + + + } + + @org.junit.Test(expected=CreateCycleException.class) + public void DirectCycleBynaryOperation() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + try{ + A1.setFormula(new Addition(A1,A2)); + } + catch(CreateCycleException ex){ + throw ex; + } + } + + + @org.junit.Test(expected=CreateCycleException.class) + public void DirectCycleFunction() throws CreateCycleException { + Cell A2= new Cell("A",2,25.); + Cell B2= new Cell("B",2,35.); + Cell A3= new Cell("A",3,0.5); + List sumList = new ArrayList<>(); + sumList.add(A2); + sumList.add(B2); + sumList.add(A3); + + try{ + B2.setFormula(new Sum(sumList)); + } + catch(CreateCycleException ex){ + throw ex; + } + } + + @org.junit.Test(expected=CreateCycleException.class) + public void IndirectCycle() throws CreateCycleException { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,5.); + Cell B4= new Cell("B",4,new Addition(A1,A2)); + Cell A4= new Cell("A",4,0.); + Cell A3= new Cell("A",3,0.5); + Cell B2= new Cell("B",2,12.); + + List sumList = new ArrayList<>(); + sumList.add(A2); + sumList.add(B2); + sumList.add(A3); + Cell B6= new Cell("B",6,new Sum(sumList)); + + List averageList = new ArrayList<>(); + averageList.add(B6); + averageList.add(B4); + averageList.add(A4); + Cell C6= new Cell("C",6,new Average(averageList)); + + try{ + B2.setFormula(new Addition(A1,C6)); + } + catch(CreateCycleException ex){ + throw ex; + } + } + + +} diff --git a/src/kernel/test/Test.java b/src/kernel/test/Test.java deleted file mode 100644 index 5a4f572..0000000 --- a/src/kernel/test/Test.java +++ /dev/null @@ -1,133 +0,0 @@ -package kernel.test; - -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - -import kernel.Cell; -import kernel.function.Sum; -import kernel.operation.Addition; - - -public class Test { - - @org.junit.Test - public void CreateCellValueTest() { - Cell A1= new Cell("A",1,25.); - assertEquals(A1.getValue(),25.,0); - assertEquals(A1.containFormula(),false); - assertEquals(A1.getUsedIn().size(),0); - assertEquals(A1.getId(),"A1"); - } - - - @org.junit.Test - public void CreateCellBinaryOperationTest() { - Cell A1= new Cell("A",1,25.); - Cell A2= new Cell("A",2,35.); - - - Cell A3= new Cell("A",3,new Addition(A1,A2)); - - assertEquals(A3.getValue(),60.,0); - assertEquals(A3.containFormula(),true); - assertEquals(A3.getUsedIn().size(),0); - assertEquals(A3.getId(),"A3"); - - assertEquals(A3.toString(),"A1 + A2"); - assertEquals(A1.getUsedIn().size(),1); - assertEquals(A2.getUsedIn().size(),1); - - - } - - - @org.junit.Test - public void CreateCellFunctionTest() { - Cell A1= new Cell("A",1,25.); - Cell A2= new Cell("A",2,35.); - Cell A3= new Cell("A",3,new Addition(A1,A2)); - Cell A5= new Cell("A",5,45.); - List sumList = new ArrayList<>(); - sumList.add(A1); - sumList.add(A2); - sumList.add(A3); - sumList.add(A5); - Cell A4= new Cell("A",4,new Sum(sumList)); - - assertEquals(A4.getValue(),165.,0); - assertEquals(A4.containFormula(),true); - assertEquals(A4.getUsedIn().size(),0); - - - assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); - assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,A1 + A2,A5)"); - assertEquals(A1.getUsedIn().size(),2); - assertEquals(A2.getUsedIn().size(),2); - assertEquals(A3.getUsedIn().size(),1); - assertEquals(A5.getUsedIn().size(),1); - - - } - - - @org.junit.Test - public void ModifyCellValueTest() { - Cell A1= new Cell("A",1,25.); - Cell A2= new Cell("A",2,35.); - Cell A3= new Cell("A",3,new Addition(A1,A2)); - assertEquals(A1.getValue(),25.,0); - assertEquals(A2.getValue(),35.,0); - assertEquals(A3.getValue(),60.,0); - A1.setValue(45.); - assertEquals(A1.getValue(),45.,0); - assertEquals(A3.getValue(),80.,0); - - } - - @org.junit.Test - public void ModifyCellFormulaTest() { - Cell A1= new Cell("A",1,25.); - Cell A2= new Cell("A",2,35.); - Cell A3= new Cell("A",3,new Addition(A1,A2)); - Cell A5= new Cell("A",5,45.); - List sumList = new ArrayList<>(); - sumList.add(A1); - sumList.add(A2); - sumList.add(A3); - sumList.add(A5); - Cell A4= new Cell("A",4,new Sum(sumList)); - - assertEquals(A4.getValue(),165.,0); - assertEquals(A4.containFormula(),true); - assertEquals(A4.getUsedIn().size(),0); - - assertEquals(A1.containFormula(),false); - A1.setFormula(new Addition(A2,A5)); - assertEquals(A1.containFormula(),true); - - assertEquals(A1.getValue(),80.,0); - assertEquals(A3.getValue(),115.,0); - assertEquals(A4.getValue(),275.,0); - - assertEquals(A1.getUsedIn().size(),2); - assertEquals(A2.getUsedIn().size(),2); - assertEquals(A3.getUsedIn().size(),1); - assertEquals(A5.getUsedIn().size(),1); - - - assertEquals(A1.toString(),"A2 + A5"); - assertEquals(A4.getDevelopedFormula(),"SOMME(A2 + A5,A2,A1 + A2,A5)"); - - - assertEquals(A1.getUsedIn().size(),2); - assertEquals(A2.getUsedIn().size(),3); - assertEquals(A3.getUsedIn().size(),1); - assertEquals(A5.getUsedIn().size(),2); - - - } - - -} -- libgit2 0.21.2