Blame view

src/kernel/Grid.java 4.23 KB
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
  		column = column.toUpperCase();
02c44758   Remi   finish ihm
24
  		
23982881   [mandjemb]   update ihm
25
26
  		if (!validateInterval(column, line))
  			throw new InvalidIntervalException();
4186cd92   Remi   fix tests
27
28
  		String id = this.getCellId(column, line);
  		Cell cell = new Cell(column, line, value);
4186cd92   Remi   fix tests
29
  		this.cells.put(id, cell);
02c44758   Remi   finish ihm
30
  		
4186cd92   Remi   fix tests
31
32
  	}
  	
a7f554a1   Remi   delete return val...
33
  	public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException {
23982881   [mandjemb]   update ihm
34
  		column = column.toUpperCase();
02c44758   Remi   finish ihm
35
  		
23982881   [mandjemb]   update ihm
36
37
  		if (!validateInterval(column, line))
  			throw new InvalidIntervalException();
4186cd92   Remi   fix tests
38
39
  		String id = this.getCellId(column, line);
  		Cell cell = new Cell(column, line, formula);
4186cd92   Remi   fix tests
40
  		this.cells.put(id, cell);
02c44758   Remi   finish ihm
41
  		
4186cd92   Remi   fix tests
42
43
44
  	}
  	
  	public void setValue(String column, int line, double value) throws CellNotFoundException {
e4ef4371   [mandjemb]   mm
45
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
46
47
48
49
50
  		this.getCell(column, line).setValue(value);
  	}
  	
  	public void setFormula(String column, int line, Formula formula) throws CellNotFoundException,
  			CreateCycleException {
e4ef4371   [mandjemb]   mm
51
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
52
53
54
55
  		this.getCell(column, line).setFormula(formula);
  	}
  	
  	public Cell getCell(String column, int line) throws CellNotFoundException {
e4ef4371   [mandjemb]   mm
56
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
57
58
59
60
61
62
63
64
  		Cell cell = this.cells.get(this.getCellId(column, line));
  		
  		if (cell != null)
  			return cell;
  		else
  			throw new CellNotFoundException();
  	}
  	
178cc4eb   Remi   fix(conflict)
65
66
67
68
  	public Cell getCell(String id) {
  		return this.cells.get(id);
  	}
  	
4186cd92   Remi   fix tests
69
70
71
72
  	public List<Cell> getCells() {
  		return new ArrayList<>(this.cells.values());
  	}
  	
4186cd92   Remi   fix tests
73
  	public double getValue(String column, int line) throws CellNotFoundException {
e4ef4371   [mandjemb]   mm
74
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
75
76
77
78
  		return this.getCell(column, line).getValue();
  	}
  	
  	public String getFormulaAsString(String column, int line) throws CellNotFoundException {
e4ef4371   [mandjemb]   mm
79
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
80
81
82
83
  		return this.getCell(column, line).toString();
  	}
  	
  	public String getDevelopedFormula(String column, int line) throws CellNotFoundException {
e4ef4371   [mandjemb]   mm
84
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
85
86
87
88
  		return this.getCell(column, line).getDevelopedFormula();
  	}
  	
  	private String getCellId(String column, int line) {
e4ef4371   [mandjemb]   mm
89
  		column = column.toUpperCase();
4186cd92   Remi   fix tests
90
91
92
  		return column + line;
  	}
  	
23982881   [mandjemb]   update ihm
93
94
  	
  	public int getTotalColumn() {
02c44758   Remi   finish ihm
95
  		return MAX_COLUMNS.charAt(0) - (int) 'A' + 1;
23982881   [mandjemb]   update ihm
96
97
  	}
  	
02c44758   Remi   finish ihm
98
99
  	public int getTotalRow() {
  		return MAX_ROWS;
a7f554a1   Remi   delete return val...
100
101
  	}
  	
23982881   [mandjemb]   update ihm
102
  	private boolean validateInterval(String column, int line) {
02c44758   Remi   finish ihm
103
104
105
106
107
  		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
108
109
  	}
  	
02c44758   Remi   finish ihm
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  	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();
  			
  			cell.updateUsedIn();
  			this.cells.remove(this.getCellId(column, line));
  		}
  	}
  	
  	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
127
  			char character = str.charAt(i); // start on the first character
02c44758   Remi   finish ihm
128
129
  			ascii = ascii + (int) character;
  			
23982881   [mandjemb]   update ihm
130
131
  		}
  		return ascii;
4186cd92   Remi   fix tests
132
  	}
23982881   [mandjemb]   update ihm
133
  	
02c44758   Remi   finish ihm
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  	public void save() throws IOException {
  		File file = new File("grid.data");
  		
  		ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
  		stream.writeObject(this);
  		stream.close();
  	}
  	
  	public static Grid load() throws IOException, ClassNotFoundException {
  		File file = new File("grid.data");
  		
  		ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
  		
  		return (Grid) stream.readObject();
  	}
  	
18e7a394   Remi   add package
150
  }