Blame view

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