-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
275 lines (231 loc) · 6.18 KB
/
Table.java
File metadata and controls
275 lines (231 loc) · 6.18 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
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class Table {
private String name;
private String[] header;
private String[] type;
private int[] keys;
Records records;
// Create a table with new name
Table(String name) {
this.name = name;
}
// Set the table header
void setHeader(String... header) {
this.header = header;
records = new Records(header.length);
}
boolean setType(String... type) {
if(header.length != type.length) return false;
this.type = type;
if(records.addRecord(type));
return true;
}
void setKey(int... keys) {
this.keys = keys;
records.setKeys(keys);
}
int[] getKey() {
return keys;
}
String getName(){
return name;
}
String[] getHeader() {
return header;
}
String[] getType() {
return type;
}
Records getRecords() {
return records;
}
boolean addTableData(String... toadd) {
if (checkDataType(toadd)) {
if (records.addRecord(toadd)) return true;
// else System.out.println("Duplicate records!");
}
return false;
}
boolean checkDataType(String... toadd) {
for (int i = 0; i < header.length; i++) {
if (!checkCertainDataType(toadd[i], i))
return false;
}
return true;
}
boolean checkCertainDataType(String toupdate, int i) {
if ((type[i].equals("number") && !checkNum(toupdate)) ||
(type[i].equals("date") && !checkDate(toupdate)) ||
(type[i].equals("currency") && !checkCurrency(toupdate)))
return false;
return true;
}
List<String[]> getTableRow(int... number) {
String[] row;
List<String[]> list = new ArrayList<String[]>();
for (int n : number ) {
if (!checkRowIndexValid(n)) return null;
row = records.getCertainRecord(n);
list.add(row);
}
return list;
}
boolean deleteRow(int... number) {
for (int n : number) {
if (!checkRowIndexValid(n)) return false;
records.removeCertainRecord(n);
}
return true;
}
boolean updateRow(int rownumber, int colnumber, String toupdate) {
if (!checkRowIndexValid(rownumber) || !checkColIndexValid(colnumber) ||
!checkCertainDataType(toupdate,colnumber-1)){
return false;
}
String origin = records.getCertainRecord(rownumber)[colnumber-1];
records.getCertainRecord(rownumber)[colnumber-1] = toupdate;
if (records.checkDuplicateUpdate(records.getCertainRecord(rownumber))){
records.updateCertainRecord(rownumber, colnumber-1, origin);
return false;
}
return true;
}
boolean checkRowIndexValid(int number) {
if (number >= records.getRecordsNumber() || number < 1) {
return false;
}
return true;
}
boolean checkColIndexValid(int number) {
if (number > header.length || number <= 0) {
return false;
}
return true;
}
boolean checkNum(String string) {
return string.matches("-?\\d+(\\.\\d+)?");
}
boolean checkDate(String string) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setLenient(false);
try {
dateFormat.parse(string.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
boolean checkCurrency(String string) {
Currency currency = Currency.getInstance(Locale.UK);
String symbol = currency.getSymbol();
if(string.startsWith(symbol) || string.endsWith(symbol)){
return true;
}
return false;
}
// ===============TESTING===============
void printTable() {
printStringArray(header);
for (int i = 0; i < records.getRecordsNumber(); i++) {
printStringArray(records.getCertainRecord(i));
}
}
void printStringArray(String[] toprint) {
System.out.print("|\t");
for (String string : toprint) {
System.out.print(string + "\t|\t");
}
System.out.println();
}
public static void main(String[] args) {
Table program = new Table("new table");
program.run();
}
private void run() {
boolean testing = false;
assert(testing = true);
if (!testing) throw new Error("Use java -ea Triangle");
test(); //test everything
System.out.println("All tests pass");
}
private void test() {
testName();
testHeader();
testAddTableData();
testGetTableRow();
testDeleteRow();
testUpdateRow();
testStringType();
testCheckType();
}
private void testName() {
assert(name == "new table");
}
private void testHeader() {
setHeader("name", "age");
records.setKeys(0);
assert(header[0] == "name");
assert(header[1] == "age");
}
private void testAddTableData() {
assert(setType("text", "number"));
assert(addTableData(new String[]{"John", "10"}));
assert(addTableData(new String[]{"Mary", "13"}));
assert(addTableData(new String[]{"Frank", "25"}));
assert(!addTableData(new String[]{"John", "10"}));
assert(!addTableData(new String[]{"John", "f1"}));
assert(!addTableData(new String[]{"Mary", "13"}));
}
private void testGetTableRow() {
assert(getTableRow(0)==null);
// assert(!getTableRow(-3));
// assert(!getTableRow(4));
}
private void testDeleteRow() {
assert(!deleteRow(4));
assert(!deleteRow(0));
assert(getTableRow(3) != null);
assert(deleteRow(1));
assert(getTableRow(4) == null);
}
private void testUpdateRow() {
assert(!updateRow(0,1,"Bob"));
assert(!updateRow(4,1,"Bob"));
assert(!updateRow(2,0,"22"));
assert(!updateRow(2,3,"22"));
assert(updateRow(2,2,"22"));
assert(updateRow(2,1,"Bob"));
assert(getTableRow(2) != null);
}
private void testStringType() {
assert(checkNum("2.2"));
assert(checkNum("0"));
assert(checkNum("-1"));
assert(checkNum("-505"));
assert(checkNum("2.02"));
assert(!checkNum("2.2.2"));
assert(!checkNum("-.43"));
assert(checkDate("14-03-2019"));
assert(!checkDate("29-02-2019"));
assert(!checkDate("44-03-2019"));
assert(!checkDate("14032019"));
assert(checkCurrency("£20.0"));
assert(!checkCurrency("20.0"));
assert(!checkCurrency("-£20.0"));
}
private void testCheckType() {
assert(!setType("number"));
assert(checkDataType("Bob","40.5"));
assert(!checkDataType("Bob","Ann"));
assert(!checkDataType("Bob","fr3"));
assert(!checkDataType("Bob","£20"));
assert(!addTableData("Bob", "A1"));
assert(!addTableData("Bob", "2f"));
assert(setType("date", "currency"));
assert(checkDataType("12-04-2019","£10"));
assert(!checkDataType("Bob","22"));
assert(!checkDataType("12-04-2019","22"));
}
}