Blame view

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