Commit eb07e5e3e665a2ae0ddf7dd1d1633051a0382218
1 parent
5731029f
avec serialisation
Showing
10 changed files
with
195 additions
and
39 deletions
Show diff stats
No preview for this file type
src/app/Application.java
@@ -4,24 +4,38 @@ import kernel.Cell; | @@ -4,24 +4,38 @@ import kernel.Cell; | ||
4 | import kernel.Grid; | 4 | import kernel.Grid; |
5 | import kernel.exception.CellNotFoundException; | 5 | import kernel.exception.CellNotFoundException; |
6 | import kernel.exception.CreateCycleException; | 6 | import kernel.exception.CreateCycleException; |
7 | +import kernel.exception.InvalidIntervalLineColumnEception; | ||
7 | import kernel.function.Average; | 8 | import kernel.function.Average; |
8 | import kernel.function.Sum; | 9 | import kernel.function.Sum; |
9 | 10 | ||
11 | +import java.io.File; | ||
12 | +import java.io.FileOutputStream; | ||
13 | +import java.io.IOException; | ||
14 | +import java.io.ObjectOutputStream; | ||
15 | +import java.io.Serializable; | ||
10 | import java.util.ArrayList; | 16 | import java.util.ArrayList; |
11 | import java.util.List; | 17 | import java.util.List; |
12 | 18 | ||
13 | -public class Application { | ||
14 | 19 | ||
15 | - public static void main(String[] args) { | ||
16 | - Grid grid = new Grid(); | 20 | +public class Application implements Serializable{ |
21 | + | ||
22 | + public static void main(String[] args) throws IOException { | ||
23 | + | ||
24 | + File fichier = new File("essai.ser") ; | ||
25 | + | ||
26 | + // ouverture d'un flux sur un fichier | ||
27 | + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier)) ; | ||
28 | + | ||
29 | + Grid grid = new Grid(); | ||
17 | 30 | ||
18 | // Création | 31 | // Création |
19 | System.out.println("Création de quelques cases..."); | 32 | System.out.println("Création de quelques cases..."); |
20 | - grid.createCell("A", 1, 10.); | ||
21 | - grid.createCell("B", 1, 0.); | ||
22 | - grid.createCell("A", 2, 5.); | 33 | + |
23 | 34 | ||
24 | try { | 35 | try { |
36 | + grid.createCell("A", 1, 10.); | ||
37 | + grid.createCell("B", 1, 0.); | ||
38 | + grid.createCell("A", 2, 5.); | ||
25 | List<Cell> sumList = new ArrayList<>(); | 39 | List<Cell> sumList = new ArrayList<>(); |
26 | sumList.add(grid.getCell("A", 1)); | 40 | sumList.add(grid.getCell("A", 1)); |
27 | sumList.add(grid.getCell("A", 2)); | 41 | sumList.add(grid.getCell("A", 2)); |
@@ -38,6 +52,9 @@ public class Application { | @@ -38,6 +52,9 @@ public class Application { | ||
38 | } catch (CreateCycleException exception) { | 52 | } catch (CreateCycleException exception) { |
39 | System.out.println(exception.toString()); | 53 | System.out.println(exception.toString()); |
40 | } | 54 | } |
55 | + catch (InvalidIntervalLineColumnEception exception) { | ||
56 | + System.out.println(exception.toString()); | ||
57 | + } | ||
41 | 58 | ||
42 | // Affichage | 59 | // Affichage |
43 | List<Cell> cells = grid.getCells(); | 60 | List<Cell> cells = grid.getCells(); |
@@ -64,5 +81,23 @@ public class Application { | @@ -64,5 +81,23 @@ public class Application { | ||
64 | System.out.println("Affichage des valeurs après modification :"); | 81 | System.out.println("Affichage des valeurs après modification :"); |
65 | for (Cell cell : cells) | 82 | for (Cell cell : cells) |
66 | System.out.println(cell.getId() + ": " + cell.getValue()); | 83 | System.out.println(cell.getId() + ": " + cell.getValue()); |
84 | + | ||
85 | + | ||
86 | + // sérialization de l'objet | ||
87 | + | ||
88 | + oos.writeObject(grid); | ||
89 | + oos.close(); | ||
67 | } | 90 | } |
91 | + | ||
92 | + | ||
93 | + | ||
94 | + | ||
95 | + | ||
96 | + | ||
97 | + | ||
98 | + | ||
99 | + | ||
100 | + | ||
101 | + | ||
102 | + | ||
68 | } | 103 | } |
@@ -0,0 +1,38 @@ | @@ -0,0 +1,38 @@ | ||
1 | +package app; | ||
2 | + | ||
3 | +import java.io.File; | ||
4 | +import java.io.FileInputStream; | ||
5 | +import java.io.IOException; | ||
6 | +import java.io.ObjectInputStream; | ||
7 | +import java.util.List; | ||
8 | + | ||
9 | +import kernel.Cell; | ||
10 | +import kernel.Grid; | ||
11 | + | ||
12 | +public class ApplicationDeserialization { | ||
13 | + public static void main(String[] args) throws IOException, ClassNotFoundException { | ||
14 | + File fichier = new File("essai.ser") ; | ||
15 | + | ||
16 | + // ouverture d'un flux sur un fichier | ||
17 | + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichier)) ; | ||
18 | + | ||
19 | + // désérialization de l'objet | ||
20 | + Grid grid = (Grid)ois.readObject() ; | ||
21 | + // Affichage | ||
22 | + List<Cell> cells = grid.getCells(); | ||
23 | + | ||
24 | + System.out.println("Affichage des valeurs :"); | ||
25 | + for (Cell cell : cells) | ||
26 | + System.out.println(cell.getId() + ": " + cell.getValue()); | ||
27 | + | ||
28 | + System.out.println("Affichage des formules :"); | ||
29 | + for (Cell cell : cells) | ||
30 | + System.out.println(cell.getId() + ": " + cell.toString()); | ||
31 | + | ||
32 | + System.out.println("Affichage des formules développées :"); | ||
33 | + for (Cell cell : cells) | ||
34 | + System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); | ||
35 | + ois.close(); | ||
36 | + } | ||
37 | + | ||
38 | +} |
src/kernel/Cell.java
1 | package kernel; | 1 | package kernel; |
2 | 2 | ||
3 | import kernel.exception.CreateCycleException; | 3 | import kernel.exception.CreateCycleException; |
4 | +import kernel.exception.InvalidIntervalLineColumnEception; | ||
4 | 5 | ||
6 | +import java.io.Serializable; | ||
5 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
6 | import java.util.List; | 8 | import java.util.List; |
7 | 9 | ||
8 | -public class Cell { | ||
9 | - | 10 | +public class Cell implements Serializable{ |
11 | + final int MAX_LIGNES=20; | ||
12 | + | ||
10 | private String column; | 13 | private String column; |
11 | private Integer line; | 14 | private Integer line; |
12 | private Double value; | 15 | private Double value; |
13 | private Formula formula; | 16 | private Formula formula; |
14 | private List<Cell> usedIn = new ArrayList<>(); | 17 | private List<Cell> usedIn = new ArrayList<>(); |
15 | 18 | ||
16 | - public Cell(String column, Integer line, Double value) { | ||
17 | - this.column = column; | ||
18 | - this.line = line; | ||
19 | - this.setValue(value); | 19 | + public Cell(String column, Integer line, Double value) throws InvalidIntervalLineColumnEception { |
20 | + column=column.toUpperCase(); | ||
21 | + if ((line>= 1 && line<=MAX_LIGNES) || (column.compareTo("A")>=0 && column.compareTo("Z")<=0)){ | ||
22 | + this.column = column; | ||
23 | + this.line = line; | ||
24 | + this.setValue(value); | ||
25 | + } | ||
26 | + else | ||
27 | + throw new InvalidIntervalLineColumnEception(); | ||
20 | } | 28 | } |
21 | 29 | ||
22 | - public Cell(String column, Integer line, Formula formula) throws CreateCycleException { | ||
23 | - this.column = column; | ||
24 | - this.line = line; | ||
25 | - this.setFormula(formula); | 30 | + public Cell(String column, Integer line, Formula formula) throws CreateCycleException,InvalidIntervalLineColumnEception { |
31 | + column=column.toUpperCase(); | ||
32 | + if ((line>= 1 && line<=MAX_LIGNES) || (column.compareTo("A")>=0 && column.compareTo("Z")<=0)){ | ||
33 | + this.column = column; | ||
34 | + this.line = line; | ||
35 | + this.setFormula(formula); | ||
36 | + } | ||
37 | + else | ||
38 | + throw new InvalidIntervalLineColumnEception(); | ||
39 | + | ||
26 | } | 40 | } |
27 | 41 | ||
28 | public Double getValue() { | 42 | public Double getValue() { |
src/kernel/Grid.java
@@ -2,18 +2,20 @@ package kernel; | @@ -2,18 +2,20 @@ package kernel; | ||
2 | 2 | ||
3 | import kernel.exception.CellNotFoundException; | 3 | import kernel.exception.CellNotFoundException; |
4 | import kernel.exception.CreateCycleException; | 4 | import kernel.exception.CreateCycleException; |
5 | +import kernel.exception.InvalidIntervalLineColumnEception; | ||
5 | 6 | ||
7 | +import java.io.Serializable; | ||
6 | import java.util.ArrayList; | 8 | import java.util.ArrayList; |
7 | import java.util.HashMap; | 9 | import java.util.HashMap; |
8 | import java.util.List; | 10 | import java.util.List; |
9 | import java.util.Map; | 11 | import java.util.Map; |
10 | 12 | ||
11 | -public class Grid { | 13 | +public class Grid implements Serializable { |
12 | 14 | ||
13 | private Map<String, Cell> cells = new HashMap<>(); | 15 | private Map<String, Cell> cells = new HashMap<>(); |
14 | public static LanguageEnum language = LanguageEnum.FR; | 16 | public static LanguageEnum language = LanguageEnum.FR; |
15 | 17 | ||
16 | - public String createCell(String column, Integer line, Double value) { | 18 | + public String createCell(String column, Integer line, Double value) throws InvalidIntervalLineColumnEception { |
17 | String id = this.getCellId(column, line); | 19 | String id = this.getCellId(column, line); |
18 | Cell cell = new Cell(column, line, value); | 20 | Cell cell = new Cell(column, line, value); |
19 | 21 | ||
@@ -22,7 +24,7 @@ public class Grid { | @@ -22,7 +24,7 @@ public class Grid { | ||
22 | return id; | 24 | return id; |
23 | } | 25 | } |
24 | 26 | ||
25 | - public String createCell(String column, Integer line, Formula formula) throws CreateCycleException { | 27 | + public String createCell(String column, Integer line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { |
26 | String id = this.getCellId(column, line); | 28 | String id = this.getCellId(column, line); |
27 | Cell cell = new Cell(column, line, formula); | 29 | Cell cell = new Cell(column, line, formula); |
28 | 30 |
src/kernel/exception/CycleFoundedException.java deleted
src/kernel/exception/InvalidIntervalLineColumnEception.java
0 → 100644
src/kernel/function/Function.java
@@ -5,13 +5,14 @@ import kernel.Formula; | @@ -5,13 +5,14 @@ import kernel.Formula; | ||
5 | import kernel.Grid; | 5 | import kernel.Grid; |
6 | import kernel.LanguageEnum; | 6 | import kernel.LanguageEnum; |
7 | 7 | ||
8 | +import java.io.Serializable; | ||
8 | import java.util.HashMap; | 9 | import java.util.HashMap; |
9 | import java.util.Iterator; | 10 | import java.util.Iterator; |
10 | import java.util.List; | 11 | import java.util.List; |
11 | import java.util.Map; | 12 | import java.util.Map; |
12 | import java.util.stream.Collectors; | 13 | import java.util.stream.Collectors; |
13 | 14 | ||
14 | -abstract public class Function implements Formula { | 15 | +abstract public class Function implements Formula,Serializable { |
15 | 16 | ||
16 | public Map<LanguageEnum, String> names = new HashMap<>(); | 17 | public Map<LanguageEnum, String> names = new HashMap<>(); |
17 | public List<Cell> listCells; | 18 | public List<Cell> listCells; |
src/kernel/operation/BinaryOperation.java
@@ -20,7 +20,7 @@ abstract public class BinaryOperation implements Formula { | @@ -20,7 +20,7 @@ abstract public class BinaryOperation implements Formula { | ||
20 | abstract public Double eval(); | 20 | abstract public Double eval(); |
21 | 21 | ||
22 | public String getDevelopedFormula() { | 22 | public String getDevelopedFormula() { |
23 | - return this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula(); | 23 | + return "(" + this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula()+")"; |
24 | } | 24 | } |
25 | 25 | ||
26 | public String toString() { | 26 | public String toString() { |
src/kernel/test/Test.java renamed to src/kernel/test/CellTest.java
@@ -6,11 +6,13 @@ import java.util.ArrayList; | @@ -6,11 +6,13 @@ import java.util.ArrayList; | ||
6 | import java.util.List; | 6 | import java.util.List; |
7 | 7 | ||
8 | import kernel.Cell; | 8 | import kernel.Cell; |
9 | +import kernel.exception.CreateCycleException; | ||
10 | +import kernel.function.Average; | ||
9 | import kernel.function.Sum; | 11 | import kernel.function.Sum; |
10 | import kernel.operation.Addition; | 12 | import kernel.operation.Addition; |
11 | 13 | ||
12 | 14 | ||
13 | -public class Test { | 15 | +public class CellTest { |
14 | 16 | ||
15 | @org.junit.Test | 17 | @org.junit.Test |
16 | public void CreateCellValueTest() { | 18 | public void CreateCellValueTest() { |
@@ -23,7 +25,7 @@ public class Test { | @@ -23,7 +25,7 @@ public class Test { | ||
23 | 25 | ||
24 | 26 | ||
25 | @org.junit.Test | 27 | @org.junit.Test |
26 | - public void CreateCellBinaryOperationTest() { | 28 | + public void CreateCellBinaryOperationTest() throws CreateCycleException { |
27 | Cell A1= new Cell("A",1,25.); | 29 | Cell A1= new Cell("A",1,25.); |
28 | Cell A2= new Cell("A",2,35.); | 30 | Cell A2= new Cell("A",2,35.); |
29 | 31 | ||
@@ -35,7 +37,7 @@ public class Test { | @@ -35,7 +37,7 @@ public class Test { | ||
35 | assertEquals(A3.getUsedIn().size(),0); | 37 | assertEquals(A3.getUsedIn().size(),0); |
36 | assertEquals(A3.getId(),"A3"); | 38 | assertEquals(A3.getId(),"A3"); |
37 | 39 | ||
38 | - assertEquals(A3.toString(),"A1 + A2"); | 40 | + assertEquals(A3.toString(),"A1+A2"); |
39 | assertEquals(A1.getUsedIn().size(),1); | 41 | assertEquals(A1.getUsedIn().size(),1); |
40 | assertEquals(A2.getUsedIn().size(),1); | 42 | assertEquals(A2.getUsedIn().size(),1); |
41 | 43 | ||
@@ -44,7 +46,7 @@ public class Test { | @@ -44,7 +46,7 @@ public class Test { | ||
44 | 46 | ||
45 | 47 | ||
46 | @org.junit.Test | 48 | @org.junit.Test |
47 | - public void CreateCellFunctionTest() { | 49 | + public void CreateCellFunctionTest() throws CreateCycleException { |
48 | Cell A1= new Cell("A",1,25.); | 50 | Cell A1= new Cell("A",1,25.); |
49 | Cell A2= new Cell("A",2,35.); | 51 | Cell A2= new Cell("A",2,35.); |
50 | Cell A3= new Cell("A",3,new Addition(A1,A2)); | 52 | Cell A3= new Cell("A",3,new Addition(A1,A2)); |
@@ -62,7 +64,7 @@ public class Test { | @@ -62,7 +64,7 @@ public class Test { | ||
62 | 64 | ||
63 | 65 | ||
64 | assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); | 66 | assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); |
65 | - assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,A1 + A2,A5)"); | 67 | + assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,(A1+A2),A5)"); |
66 | assertEquals(A1.getUsedIn().size(),2); | 68 | assertEquals(A1.getUsedIn().size(),2); |
67 | assertEquals(A2.getUsedIn().size(),2); | 69 | assertEquals(A2.getUsedIn().size(),2); |
68 | assertEquals(A3.getUsedIn().size(),1); | 70 | assertEquals(A3.getUsedIn().size(),1); |
@@ -73,7 +75,7 @@ public class Test { | @@ -73,7 +75,7 @@ public class Test { | ||
73 | 75 | ||
74 | 76 | ||
75 | @org.junit.Test | 77 | @org.junit.Test |
76 | - public void ModifyCellValueTest() { | 78 | + public void ModifyCellValueTest() throws CreateCycleException { |
77 | Cell A1= new Cell("A",1,25.); | 79 | Cell A1= new Cell("A",1,25.); |
78 | Cell A2= new Cell("A",2,35.); | 80 | Cell A2= new Cell("A",2,35.); |
79 | Cell A3= new Cell("A",3,new Addition(A1,A2)); | 81 | Cell A3= new Cell("A",3,new Addition(A1,A2)); |
@@ -87,7 +89,7 @@ public class Test { | @@ -87,7 +89,7 @@ public class Test { | ||
87 | } | 89 | } |
88 | 90 | ||
89 | @org.junit.Test | 91 | @org.junit.Test |
90 | - public void ModifyCellFormulaTest() { | 92 | + public void ModifyCellFormulaTest() throws CreateCycleException { |
91 | Cell A1= new Cell("A",1,25.); | 93 | Cell A1= new Cell("A",1,25.); |
92 | Cell A2= new Cell("A",2,35.); | 94 | Cell A2= new Cell("A",2,35.); |
93 | Cell A3= new Cell("A",3,new Addition(A1,A2)); | 95 | Cell A3= new Cell("A",3,new Addition(A1,A2)); |
@@ -103,6 +105,11 @@ public class Test { | @@ -103,6 +105,11 @@ public class Test { | ||
103 | assertEquals(A4.containFormula(),true); | 105 | assertEquals(A4.containFormula(),true); |
104 | assertEquals(A4.getUsedIn().size(),0); | 106 | assertEquals(A4.getUsedIn().size(),0); |
105 | 107 | ||
108 | + assertEquals(A1.getUsedIn().size(),2); | ||
109 | + assertEquals(A2.getUsedIn().size(),2); | ||
110 | + assertEquals(A3.getUsedIn().size(),1); | ||
111 | + assertEquals(A5.getUsedIn().size(),1); | ||
112 | + | ||
106 | assertEquals(A1.containFormula(),false); | 113 | assertEquals(A1.containFormula(),false); |
107 | A1.setFormula(new Addition(A2,A5)); | 114 | A1.setFormula(new Addition(A2,A5)); |
108 | assertEquals(A1.containFormula(),true); | 115 | assertEquals(A1.containFormula(),true); |
@@ -111,14 +118,13 @@ public class Test { | @@ -111,14 +118,13 @@ public class Test { | ||
111 | assertEquals(A3.getValue(),115.,0); | 118 | assertEquals(A3.getValue(),115.,0); |
112 | assertEquals(A4.getValue(),275.,0); | 119 | assertEquals(A4.getValue(),275.,0); |
113 | 120 | ||
114 | - assertEquals(A1.getUsedIn().size(),2); | ||
115 | - assertEquals(A2.getUsedIn().size(),2); | ||
116 | - assertEquals(A3.getUsedIn().size(),1); | ||
117 | - assertEquals(A5.getUsedIn().size(),1); | 121 | + |
118 | 122 | ||
119 | 123 | ||
120 | - assertEquals(A1.toString(),"A2 + A5"); | ||
121 | - assertEquals(A4.getDevelopedFormula(),"SOMME(A2 + A5,A2,A1 + A2,A5)"); | 124 | + assertEquals(A1.toString(),"A2+A5"); |
125 | + assertEquals(A1.getDevelopedFormula(),"(A2+A5)"); | ||
126 | + assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); | ||
127 | + assertEquals(A4.getDevelopedFormula(),"SOMME((A2+A5),A2,((A2+A5)+A2),A5)"); | ||
122 | 128 | ||
123 | 129 | ||
124 | assertEquals(A1.getUsedIn().size(),2); | 130 | assertEquals(A1.getUsedIn().size(),2); |
@@ -129,5 +135,65 @@ public class Test { | @@ -129,5 +135,65 @@ public class Test { | ||
129 | 135 | ||
130 | } | 136 | } |
131 | 137 | ||
138 | + @org.junit.Test(expected=CreateCycleException.class) | ||
139 | + public void DirectCycleBynaryOperation() throws CreateCycleException { | ||
140 | + Cell A1= new Cell("A",1,25.); | ||
141 | + Cell A2= new Cell("A",2,35.); | ||
142 | + try{ | ||
143 | + A1.setFormula(new Addition(A1,A2)); | ||
144 | + } | ||
145 | + catch(CreateCycleException ex){ | ||
146 | + throw ex; | ||
147 | + } | ||
148 | + } | ||
149 | + | ||
150 | + | ||
151 | + @org.junit.Test(expected=CreateCycleException.class) | ||
152 | + public void DirectCycleFunction() throws CreateCycleException { | ||
153 | + Cell A2= new Cell("A",2,25.); | ||
154 | + Cell B2= new Cell("B",2,35.); | ||
155 | + Cell A3= new Cell("A",3,0.5); | ||
156 | + List<Cell> sumList = new ArrayList<>(); | ||
157 | + sumList.add(A2); | ||
158 | + sumList.add(B2); | ||
159 | + sumList.add(A3); | ||
160 | + | ||
161 | + try{ | ||
162 | + B2.setFormula(new Sum(sumList)); | ||
163 | + } | ||
164 | + catch(CreateCycleException ex){ | ||
165 | + throw ex; | ||
166 | + } | ||
167 | + } | ||
168 | + | ||
169 | + @org.junit.Test(expected=CreateCycleException.class) | ||
170 | + public void IndirectCycle() throws CreateCycleException { | ||
171 | + Cell A1= new Cell("A",1,25.); | ||
172 | + Cell A2= new Cell("A",2,5.); | ||
173 | + Cell B4= new Cell("B",4,new Addition(A1,A2)); | ||
174 | + Cell A4= new Cell("A",4,0.); | ||
175 | + Cell A3= new Cell("A",3,0.5); | ||
176 | + Cell B2= new Cell("B",2,12.); | ||
177 | + | ||
178 | + List<Cell> sumList = new ArrayList<>(); | ||
179 | + sumList.add(A2); | ||
180 | + sumList.add(B2); | ||
181 | + sumList.add(A3); | ||
182 | + Cell B6= new Cell("B",6,new Sum(sumList)); | ||
183 | + | ||
184 | + List<Cell> averageList = new ArrayList<>(); | ||
185 | + averageList.add(B6); | ||
186 | + averageList.add(B4); | ||
187 | + averageList.add(A4); | ||
188 | + Cell C6= new Cell("C",6,new Average(averageList)); | ||
189 | + | ||
190 | + try{ | ||
191 | + B2.setFormula(new Addition(A1,C6)); | ||
192 | + } | ||
193 | + catch(CreateCycleException ex){ | ||
194 | + throw ex; | ||
195 | + } | ||
196 | + } | ||
197 | + | ||
132 | 198 | ||
133 | } | 199 | } |