Blame view

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