Grid.java
4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package kernel;
import kernel.exception.CannotDeleteCellException;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Grid implements Serializable {
private static final long serialVersionUID = 1L;
private static final int MAX_ROWS = 20;
private static final String MAX_COLUMNS = "Z";
private Map<String, Cell> cells = new HashMap<>();
public static LanguageEnum language = LanguageEnum.FR;
public void createCell(String column, int line, double value) throws InvalidIntervalException {
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);
}
public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException {
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);
}
public void setValue(String column, int line, double value) throws CellNotFoundException {
this.getCell(column, line).setValue(value);
}
public void setFormula(String column, int line, Formula formula) throws CellNotFoundException,
CreateCycleException {
this.getCell(column, line).setFormula(formula);
}
public Cell getCell(String column, int line) throws CellNotFoundException {
String id = this.getCellId(column, line);
Cell cell = this.cells.get(id);
if (cell != null)
return cell;
else
throw new CellNotFoundException("La cellule " + id + " n'existe pas.");
}
public Cell getCell(String id) {
return this.cells.get(id);
}
public List<Cell> getCells() {
return new ArrayList<>(this.cells.values());
}
public double getValue(String column, int line) throws CellNotFoundException {
return this.getCell(column.toUpperCase(), line).getValue();
}
public String getFormulaAsString(String column, int line) throws CellNotFoundException {
return this.getCell(column.toUpperCase(), line).toString();
}
public String getDevelopedFormula(String column, int line) throws CellNotFoundException {
return this.getCell(column.toUpperCase(), line).getDevelopedFormula();
}
private String getCellId(String column, int line) {
return column.toUpperCase() + line;
}
public int getTotalColumn() {
return MAX_COLUMNS.charAt(0) - (int) 'A' + 1;
}
public int getTotalRow() {
return MAX_ROWS;
}
private boolean validateInterval(String column, int line) {
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);
}
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())
throw new CannotDeleteCellException("La cellule " + id + " est utilisée dans une autre case.");
cell.updateUsedIn();
this.cells.remove(id);
}
}
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
char character = str.charAt(i); // start on the first character
ascii = ascii + (int) character;
}
return ascii;
}
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(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;
}
}