-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.java
93 lines (84 loc) · 3.32 KB
/
Table.java
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
///////////////////
// Table: The struct class that holds all table data read through from a file (used for processing in SQLEngine)
///////////////////
public class Table extends SQL {
String data[][] = new String[100][100]; //holds all data
int numColumns = 0; //the number of columns in the table
int numRows = 0; //the number of rows in the table
WriteFile writeFile = new WriteFile(); //the file writer, used to write files
ReadFile readFile = new ReadFile(); //the file reader, used to read files
String fileLocation; //the file location of the table's file format
Lock lock; //the table mutex lock used for atomic verification
//Table - default constructor that initializes the fileLocation and parses the data
Table(String inputLocation) {
fileLocation = inputLocation;
lock = new Lock(fileLocation);
parse();
}
//parse - parses the file at fileLocation and loads the data into the data array
public void parse() {
String fileText[] = readFile.read(fileLocation).replaceAll("(?m)^[ \t]*\r?\n", "").split("\n");
for(int i = 0; i < fileText.length; i++) {
numRows++;
String valueArray[] = fileText[i].split(" \\| ");
for(int j = 0; j < valueArray.length; j++) {
numColumns++;
data[i][j] = valueArray[j];
}
}
//recompute column value
numColumns = numColumns / numRows;
}
//export - exports the data in the data array to the file at fileLocation
public void export() {
String exportString = "";
for(int i = 0; i < numRows; i++) {
String valueString = "";
for(int j = 0; j < numColumns; j++) {
valueString += data[i][j] + " | ";
}
valueString = valueString.substring(0, valueString.length() - 3);
if(!valueString.replace("|","").trim().equals("")) {
exportString += valueString + "\n";
}
}
writeFile.write(fileLocation, false, exportString);
}
//delete - removes a row at rowValue
public void delete(int rowValue) {
for(int i = 0; i < numColumns; i++) {
data[rowValue][i] = "";
}
}
//getColumnValue - returns a column value using an input test string
public int getColumnValue(String inputValue) {
for(int i = 0; i < numColumns; i++) {
if(data[0][i].contains(inputValue)) {
return i;
}
}
return -1;
}
//getRowValue - returns a row value based on an input test string and column to search on
public int getRowValue(String inputValue, int columnValue) {
for(int i = 0; i < numRows; i++) {
if(inputValue.equals(data[i][columnValue])) {
return i;
}
}
return -1;
}
//print - prints out the entire data payload
public void print() {
for(int i = 0; i < numRows; i++) {
String valueString = "";
for(int j = 0; j < numColumns; j++) {
valueString += data[i][j] + " | ";
}
valueString = valueString.substring(0, valueString.length() - 3);
if(!valueString.replace("|","").trim().equals("")) {
console.data(valueString);
}
}
}
}