-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadSheet.java
More file actions
469 lines (459 loc) · 20.8 KB
/
SpreadSheet.java
File metadata and controls
469 lines (459 loc) · 20.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* SpreadSheet class compiles all the Cell values in one spreadsheet for the user to use and keeps track of the Cell values as they apply to the location.
*/
import java.lang.Character;
import java.lang.String;
public class SpreadSheet
{
private Cell[][] spreadsheet;
private char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private int rows; //numbers
private int columns; //alphabet
public SpreadSheet(int rows, int columns) //constructor makes a 10 by 7 2D array to hold all the Cells in. Creates new cells for each.
{
this.rows = rows;
this.columns = columns;
spreadsheet = new Cell[rows][columns];
for (int r = 0; r < spreadsheet.length; r++)
{
for (int c = 0; c < spreadsheet[r].length; c++)
{
spreadsheet[r][c] = new Cell(r, c);
}
}
}
public char[] getAlphabet() //alphabet is used for labeling the columns of the spreadhseet when printing.
{
return alphabet;
}
public void print() //print runs through the 2D array and prints, in user friendly format, all the cell values in their location.
{
System.out.print(" |");
for (int r = 0; r < columns; r++) //prints out the alphabet labels
{
System.out.print(" " + alphabet[r]+" |");
}
System.out.println();
for (int r = 0; r < rows; r++)
{
System.out.println(printLine());//"------------+------------+------------+------------+------------+------------+------------+------------+");
if (r >= 9)
System.out.print(" " + (r+1)+ " |");
else
System.out.print(" " + (r+1) + " |");
for (int c = 0; c < spreadsheet[r].length; c++)
{
Cell cell = spreadsheet[r][c];
String value = cell.getValue();
System.out.print(printCell(cell.getValue()));
}
System.out.println();
}
System.out.println(printLine());//"------------+------------+------------+------------+------------+------------+------------+------------+");
System.out.println();
}
private String printCell(String value) //method is used to print the write value for each cell depending on different cases. Handles all of those cases.
{
if (value.length() > 12)
return value.substring(0, 11) + ">|";
if (value.equals("<empty>"))
return " |";
String toReturn = value;
for (int i = 0; i < (12-value.length()); i++)
{
toReturn += " ";
}
return toReturn + "|";
}
public void setCell(String input)
{
int index = input.indexOf("=");
char[] location = input.substring(0, index-1).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
if (letterLocation == -1 || input.substring(input.indexOf('=')+1).length() == 0)// if not a valid letter
{
System.out.println("Invalid Input");
}
else
{
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
{
int column = (location[1]-48)*10 + location[2]-48;
Cell toChange = spreadsheet[column-1][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual(input.substring(index+2));//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(input.substring(0, index-1));
}
else
{
Cell toChange = spreadsheet[location[1]-49][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual(input.substring(index+2));//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(input.substring(0, index-1));
}
}
}
public void setFormulaCell(String input)
{
int index = input.indexOf("=");
char[] location = input.substring(0, index-1).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
String actualInput = input;
if (letterLocation == -1) // if not a valid letter
{
System.out.println("Invalid Input: invalid letter input");
}
else
{
if (replace(input.substring(input.indexOf("(")+2, input.indexOf(")")-1)).contains("Invalid Input"))
{
System.out.println("Invalid Input");
return;
}
input = input.substring(0, input.indexOf("(")+1) + replace(input.substring(input.indexOf("(")+2, input.indexOf(")")-1)) + ")";//gets to actual formula and includes
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
{
int column = (location[1]-48)*10 + location[2]-48;//offset char by the ascii 49 because 1 char is 49 number
Cell toSet = spreadsheet[column-1][letterLocation];
CellFormula toChange = new CellFormula(column-1, letterLocation);
toSet.setCellIndividual(toChange.setCellIndividualFormula(input.substring(index+2)));
toSet.setPrintLocation(input.substring(0, index-1));
toSet.setInput(actualInput.substring(index+2));
}
else
{
Cell toSet = spreadsheet[location[1]-49][letterLocation];
CellFormula toChange = new CellFormula(location[1]-49, letterLocation);
toSet.setCellIndividual(toChange.setCellIndividualFormula(input.substring(index+2)));
toSet.setPrintLocation(input.substring(0, index-1));
toSet.setInput(actualInput.substring(index+2));
}
}
}
public int search(char letter)
{
letter = Character.toUpperCase(letter);
for (int i = 0; i < columns; i++)
{
if (alphabet[i] == letter)
{
return i;
}
}
return -1;
}
private String replace(String input) throws NumberFormatException //replaces all cell locations with cell values
{
String toReturn = " ";
String[] inputArray = input.split(" ");
for (int i = 0; i < inputArray.length; i++)
{
double finalValue = 0;
String nextString = inputArray[i];
try{
if (canParseDouble(nextString) || isOperation(nextString))//valid number
{
toReturn += nextString + " ";
}
else if (!getCellValue(nextString).contains("Invalid Input") && !getCellValue(nextString).contains(" = <empty>") && canParseDouble(getDoubleValue(nextString).substring(getDoubleValue(nextString).indexOf("=")+1))) //if letter location is valid and if double
{
finalValue = Double.parseDouble(getDoubleValue(nextString).substring(getDoubleValue(nextString).indexOf("=")+1));
toReturn += finalValue + " ";
}
else //not valid at all
throw new NumberFormatException();
}
catch(NumberFormatException err)
{
return "Invalid Input";
}
}
return toReturn;
}
public void setSumCell(String input)
{
String actualInput = input;
int index = input.indexOf("sum")+3; //index of space after sum
int letterLocationOne = 0;
int rowLocationOne = 0;
int letterLocationTwo = 0;
int rowLocationTwo = 0;
//first location and second location
for (int i = 0; i < 2; i++)
{
input = input.substring(index+1);
char[] location = input.substring(0, input.indexOf(" ")).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
if (letterLocation == -1 || !canParseDouble("" + location[1]) || location[1]-48 > rows || (location.length > 2 && !canParseDouble("" + location[1] + location[2]) && Double.parseDouble("" + location[1] + location[2]) > rows)) // if not a valid letter
{
System.out.println("Invalid Input: Not valid location");
actualInput = "<empty>";
return;
}
else if (i == 0)
{
letterLocationOne = letterLocation;
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
rowLocationOne = ((location[1]-48)*10 + location[2]-48)-1;
else
rowLocationOne = location[1]-48-1;
}
else
{
letterLocationTwo = letterLocation;
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
rowLocationTwo = ((location[1]-48)*10 + location[2]-48)-1;
else
rowLocationTwo = location[1]-48-1;
}
index = input.indexOf("-")+1;
}
if (rowLocationOne > rowLocationTwo || letterLocationOne > letterLocationTwo || (rowLocationOne == rowLocationTwo && letterLocationOne == letterLocationTwo))
{
System.out.println("Invalid Input: Invalid range");
return;
}
double toReturn = 0;
for (int r = rowLocationOne; r <= rowLocationTwo; r++) {
for (int c = letterLocationOne; c <= letterLocationTwo; c++)
{
Cell toChange = spreadsheet[r][c];
String value = getCellValue("" + alphabet[c] + (r+1));
value = value.substring(value.indexOf("=") + 1);
if(canParseDouble(value))
toReturn += Double.parseDouble(value);
else
{
System.out.println("Invalid Input: Not double input");
actualInput = "<empty>";
return;
}
}
}
index = actualInput.indexOf("=");
char[] location = actualInput.substring(0, index-1).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
if (letterLocation == -1 || !canParseDouble("" + location[1]) || location[1]-48 > rows || (location.length > 2 && (!canParseDouble("" + actualInput.substring(0, index-1)) || Double.parseDouble("" + actualInput.substring(0, index-1)) > rows)))// if not a valid letter
{
System.out.println("Invalid Input: Not valid location");
actualInput = "<empty>";
return;
}
else
{
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
{
int row = (location[1]-48)*10 + location[2]-48;
Cell toChange = spreadsheet[row-1][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual("" + toReturn);//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(actualInput.substring(0, index-1));
toChange.setInput(actualInput.substring(actualInput.indexOf("=")+2));
}
else
{
Cell toChange = spreadsheet[location[1]-49][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual("" + toReturn);//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(actualInput.substring(0, index-1));
toChange.setInput(actualInput.substring(actualInput.indexOf("=")+2));
}
}
}
public void setAvgCell(String input)
{
String actualInput = input;
int index = input.indexOf("avg")+3; //index of space after avg
int letterLocationOne = 0;
int rowLocationOne = 0;
int letterLocationTwo = 0;
int rowLocationTwo = 0;
//first location and second location
for (int i = 0; i < 2; i++)
{
input = input.substring(index+1);
char[] location = input.substring(0, input.indexOf(" ")).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
if (letterLocation == -1 || !canParseDouble("" + location[1]) || location[1]-48 > rows || (location.length > 2 && !canParseDouble("" + location[1] + location[2]) && Double.parseDouble("" + location[1] + location[2]) > rows)) // if not a valid letter
{
System.out.println("Invalid Input: Not valid location");
actualInput = "<empty>";
return;
}
else if (i == 0)
{
letterLocationOne = letterLocation;
rowLocationOne = location[1]-48-1;
}
else
{
letterLocationTwo = letterLocation;
rowLocationTwo = location[1]-48-1;
}
index = input.indexOf("-")+1;
}
if (rowLocationOne > rowLocationTwo || letterLocationOne > letterLocationTwo || (rowLocationOne == rowLocationTwo && letterLocationOne == letterLocationTwo))
{
System.out.println("Invalid Input: Invalid range");
return;
}
double toReturn = 0;
int count = 0;
for (int r = rowLocationOne; r <= rowLocationTwo; r++) {
for (int c = letterLocationOne; c <= letterLocationTwo; c++)
{
Cell toChange = spreadsheet[r][c];
String value = getCellValue("" + alphabet[c] + (r+1));
value = value.substring(value.indexOf("=") + 1);
if(canParseDouble(value))
{
toReturn += Double.parseDouble(value);
count++;
}
else
{
System.out.println("Invalid Input: Not double value");
actualInput = "<empty>";
return;
}
}
}
toReturn /= count;
index = actualInput.indexOf("=");
char[] location = actualInput.substring(0, index-1).toCharArray(); //gets a letter and a number
int letterLocation = search(location[0]); //makes sure within dimensions
if (letterLocation == -1 || !canParseDouble("" + location[1]) || location[1]-48 > rows || (location.length > 2 && (!canParseDouble("" + actualInput.substring(0, index-1)) || Double.parseDouble("" + actualInput.substring(0, index-1)) > rows)))// if not a valid letter
{
System.out.println("Invalid Input: Not valid location");
actualInput = "<empty>";
return;
}
else
{
if (location.length > 2 && location[2] != ' ') //if the number is 10 or greater
{
int row = (location[1]-48)*10 + location[2]-48;
Cell toChange = spreadsheet[row-1][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual("" + toReturn);//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(actualInput.substring(0, index-1));
toChange.setInput(actualInput.substring(actualInput.indexOf("=")+2));
}
else
{
Cell toChange = spreadsheet[location[1]-49][letterLocation]; //offset char by the ascii 49 because 1 char is 49 number
toChange.setCellIndividual("" + toReturn);//returns a cellLocation and sets cell using that location in the spreadsheet
toChange.setPrintLocation(actualInput.substring(0, index-1));
toChange.setInput(actualInput.substring(actualInput.indexOf("=")+2));
}
}
}
private boolean isOperation(String input)
{
if (input.length() < 3 && input.contains("+") || input.contains("-") || input.contains("*") || input.contains("/"))
{
return true;
}
return false;
}
private boolean canParseDouble(String input)
{
try
{
Double.parseDouble(input);
}
catch(NumberFormatException err)
{
return false;
}
return true;
}
public String getCellValue(String input) throws NumberFormatException
{
try{ //checks if not a cell location available
int row = Integer.parseInt(input.substring(1));
if (search(input.charAt(0)) == -1)
return "Invalid Input";
Cell toPrint = spreadsheet[row-1][search(input.charAt(0))];
return toPrint.getLocationString() + " = " + toPrint.getInputValue();
}
catch(NumberFormatException err)
{
return "Invalid input";
}
}
public String getDoubleValue(String input)
{
try{ //checks if not a cell location available
int row = Integer.parseInt(input.substring(1));
if (search(input.charAt(0)) == -1)
return "Invalid Input";
Cell toPrint = spreadsheet[row-1][search(input.charAt(0))];
return toPrint.getLocationString() + " = " + toPrint.getDoubleValue();
}
catch(NumberFormatException err)
{
return "Invalid Input";
}
}
public void clearIndividual(String input)
{
int column = input.charAt(1)-48; //char to integer, ascii
char letter = input.charAt(0); //get letter
if (column == 1 && input.length() > 2 && input.charAt(2)-48 == 0) //ten
spreadsheet[9][search(letter)].clear();
else if (input.length() == 2)
spreadsheet[column-1][search(letter)].clear();
else
System.out.println("Invalid Input");
}
public void clear()
{
for (int r = 0; r < spreadsheet.length; r++)
{
for (int c = 0; c < spreadsheet[r].length; c++)
{
clearIndividual("" + alphabet[c] + (r+1));
}
}
}
public String printLine()
{
String toReturn = "";
for(int i = 0; i < columns+1; i++)
{
toReturn += "------------+";
}
return toReturn;
}
public Cell findCell(String location)
{
int row = location.charAt(1)-48; //gets number
int column = search(location.charAt(0)); //gets number value of letter
if (location.length() > 2 && location.charAt(2) != ' ') //if the number is 10 or greater
{
row = (location.charAt(1)-48)*10 + location.charAt(2)-48;
return spreadsheet[row-1][column]; //offset char by the ascii 49 because 1 char is 49 number
}
return spreadsheet[row-1][column]; //offset char by the ascii 49 because 1 char is 49 number
}
public boolean validFormula(String input) throws NumberFormatException //include parenthesis and = and location
{
String[] inputArray = input.split(" ");
if (getCellValue(inputArray[0]).equals("Invalid Input") || !inputArray[1].equals("=") || !inputArray[2].equals("(") || !inputArray[inputArray.length-1].equals(")"))
return false;
String[] formulaArray = input.substring(input.indexOf("(")).split(" ");
for (int i = 1; i < formulaArray.length; i++)
{
if(i%2 == 0 && input.charAt(i) != '+' && input.charAt(i) != '/' && input.charAt(i) != '*' && (input.indexOf('-') == -1 || input.charAt(input.indexOf('-') + 1) != ' '))//gets rid of posibility of negatives
return false;
if (i%2 == 1)
{
try
{
Double.parseDouble(formulaArray[i]);
}
catch (NumberFormatException err)
{
return false;
}
}
}
return true;
}
}