2e8fbd04
Remi
global refactor
|
1
|
package kernel;
|
18e7a394
Remi
add package
|
2
|
|
02c44758
Remi
finish ihm
|
3
|
import kernel.exception.CannotDeleteCellException;
|
2e8fbd04
Remi
global refactor
|
4
|
import kernel.exception.CellNotFoundException;
|
5731029f
Remi
merge
|
5
|
import kernel.exception.CreateCycleException;
|
4186cd92
Remi
fix tests
|
6
|
import kernel.exception.InvalidIntervalException;
|
8112a2dd
Remi
merge
|
7
|
|
02c44758
Remi
finish ihm
|
8
|
import java.io.*;
|
94ba86ff
Remi
Auto stash before...
|
9
|
import java.util.ArrayList;
|
8112a2dd
Remi
merge
|
10
|
import java.util.HashMap;
|
94ba86ff
Remi
Auto stash before...
|
11
|
import java.util.List;
|
8112a2dd
Remi
merge
|
12
13
|
import java.util.Map;
|
eb07e5e3
[mandjemb]
avec serialisation
|
14
|
public class Grid implements Serializable {
|
4186cd92
Remi
fix tests
|
15
16
|
private static final long serialVersionUID = 1L;
|
02c44758
Remi
finish ihm
|
17
18
|
private static final int MAX_ROWS = 20;
private static final String MAX_COLUMNS = "Z";
|
4186cd92
Remi
fix tests
|
19
20
|
private Map<String, Cell> cells = new HashMap<>();
public static LanguageEnum language = LanguageEnum.FR;
|
02c44758
Remi
finish ihm
|
21
|
|
a7f554a1
Remi
delete return val...
|
22
|
public void createCell(String column, int line, double value) throws InvalidIntervalException {
|
23982881
[mandjemb]
update ihm
|
23
24
|
if (!validateInterval(column, line))
throw new InvalidIntervalException();
|
080a0e68
Remi
change return
|
25
|
|
4186cd92
Remi
fix tests
|
26
27
|
String id = this.getCellId(column, line);
Cell cell = new Cell(column, line, value);
|
02c44758
Remi
finish ihm
|
28
|
|
080a0e68
Remi
change return
|
29
|
this.cells.put(id, cell);
|
4186cd92
Remi
fix tests
|
30
31
|
}
|
a7f554a1
Remi
delete return val...
|
32
|
public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException {
|
23982881
[mandjemb]
update ihm
|
33
34
|
if (!validateInterval(column, line))
throw new InvalidIntervalException();
|
080a0e68
Remi
change return
|
35
|
|
4186cd92
Remi
fix tests
|
36
37
|
String id = this.getCellId(column, line);
Cell cell = new Cell(column, line, formula);
|
02c44758
Remi
finish ihm
|
38
|
|
080a0e68
Remi
change return
|
39
|
this.cells.put(id, cell);
|
4186cd92
Remi
fix tests
|
40
41
42
|
}
public void setValue(String column, int line, double value) throws CellNotFoundException {
|
4186cd92
Remi
fix tests
|
43
44
45
46
47
|
this.getCell(column, line).setValue(value);
}
public void setFormula(String column, int line, Formula formula) throws CellNotFoundException,
CreateCycleException {
|
4186cd92
Remi
fix tests
|
48
49
50
51
|
this.getCell(column, line).setFormula(formula);
}
public Cell getCell(String column, int line) throws CellNotFoundException {
|
080a0e68
Remi
change return
|
52
53
|
String id = this.getCellId(column, line);
Cell cell = this.cells.get(id);
|
4186cd92
Remi
fix tests
|
54
55
56
57
|
if (cell != null)
return cell;
else
|
080a0e68
Remi
change return
|
58
|
throw new CellNotFoundException("La cellule " + id + " n'existe pas.");
|
4186cd92
Remi
fix tests
|
59
60
|
}
|
178cc4eb
Remi
fix(conflict)
|
61
62
63
64
|
public Cell getCell(String id) {
return this.cells.get(id);
}
|
4186cd92
Remi
fix tests
|
65
66
67
68
|
public List<Cell> getCells() {
return new ArrayList<>(this.cells.values());
}
|
4186cd92
Remi
fix tests
|
69
|
public double getValue(String column, int line) throws CellNotFoundException {
|
080a0e68
Remi
change return
|
70
|
return this.getCell(column.toUpperCase(), line).getValue();
|
4186cd92
Remi
fix tests
|
71
72
73
|
}
public String getFormulaAsString(String column, int line) throws CellNotFoundException {
|
080a0e68
Remi
change return
|
74
|
return this.getCell(column.toUpperCase(), line).toString();
|
4186cd92
Remi
fix tests
|
75
76
77
|
}
public String getDevelopedFormula(String column, int line) throws CellNotFoundException {
|
080a0e68
Remi
change return
|
78
|
return this.getCell(column.toUpperCase(), line).getDevelopedFormula();
|
4186cd92
Remi
fix tests
|
79
80
81
|
}
private String getCellId(String column, int line) {
|
080a0e68
Remi
change return
|
82
|
return column.toUpperCase() + line;
|
4186cd92
Remi
fix tests
|
83
84
|
}
|
23982881
[mandjemb]
update ihm
|
85
|
public int getTotalColumn() {
|
02c44758
Remi
finish ihm
|
86
|
return MAX_COLUMNS.charAt(0) - (int) 'A' + 1;
|
23982881
[mandjemb]
update ihm
|
87
88
|
}
|
02c44758
Remi
finish ihm
|
89
90
|
public int getTotalRow() {
return MAX_ROWS;
|
a7f554a1
Remi
delete return val...
|
91
92
|
}
|
23982881
[mandjemb]
update ihm
|
93
|
private boolean validateInterval(String column, int line) {
|
02c44758
Remi
finish ihm
|
94
95
96
97
98
|
return line >= 1 && line <= MAX_ROWS && convertStringToInt(column) >= convertStringToInt("A") && convertStringToInt(column) <= convertStringToInt(MAX_COLUMNS);
}
public boolean cellExist(String key) {
return this.cells.containsKey(key);
|
4186cd92
Remi
fix tests
|
99
100
|
}
|
02c44758
Remi
finish ihm
|
101
102
103
104
105
106
107
|
public void deleteCell(String column, int line) throws CannotDeleteCellException {
String id = this.getCellId(column, line);
if (this.cellExist(id)) {
Cell cell = this.getCell(id);
if (!cell.getUsedIn().isEmpty())
|
080a0e68
Remi
change return
|
108
|
throw new CannotDeleteCellException("La cellule " + id + " est utilisée dans une autre case.");
|
02c44758
Remi
finish ihm
|
109
110
|
cell.updateUsedIn();
|
080a0e68
Remi
change return
|
111
|
this.cells.remove(id);
|
02c44758
Remi
finish ihm
|
112
113
114
115
116
117
|
}
}
private int convertStringToInt(String str) {
int ascii = 0;
for (int i = 0; i < str.length(); i++) { // while counting characters if less than the length add one
|
23982881
[mandjemb]
update ihm
|
118
|
char character = str.charAt(i); // start on the first character
|
02c44758
Remi
finish ihm
|
119
120
|
ascii = ascii + (int) character;
|
23982881
[mandjemb]
update ihm
|
121
122
|
}
return ascii;
|
4186cd92
Remi
fix tests
|
123
|
}
|
23982881
[mandjemb]
update ihm
|
124
|
|
080a0e68
Remi
change return
|
125
126
|
public void save(String fileName) throws IOException {
File file = new File(fileName);
|
02c44758
Remi
finish ihm
|
127
128
129
130
131
132
|
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
stream.writeObject(this);
stream.close();
}
|
080a0e68
Remi
change return
|
133
134
|
public static Grid load(String fileName) throws IOException, ClassNotFoundException {
File file = new File(fileName);
|
02c44758
Remi
finish ihm
|
135
136
|
ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
|
080a0e68
Remi
change return
|
137
138
|
Grid grid = (Grid) stream.readObject();
stream.close();
|
02c44758
Remi
finish ihm
|
139
|
|
080a0e68
Remi
change return
|
140
|
return grid;
|
02c44758
Remi
finish ihm
|
141
|
}
|
18e7a394
Remi
add package
|
142
|
}
|