-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryTest.java
More file actions
83 lines (69 loc) · 2.82 KB
/
DictionaryTest.java
File metadata and controls
83 lines (69 loc) · 2.82 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
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import java.io.IOException;
import datastructure.dictionary.AVLDictionary;
import datastructure.dictionary.HashTable;
import datastructure.dictionary.Dictionary;
public class DictionaryTest {
private static List<String> readCommands(String filename) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(filename), Charset.defaultCharset());
// Parsing file formant
for(String line: lines) {
String[] cmd = line.split(" ");
if(!cmd[0].equals("INSERT") && !cmd[0].equals("DELETE") && !cmd[0].equals("SEARCH"))
throw new IOException("Invalid command in line: " + line);
if((cmd.length != 2 && cmd.length != 3) || (!cmd[0].equals("INSERT") && cmd.length == 3))
throw new IOException("Invalid file format: " + line);
try {
Integer.parseInt(cmd[1]);
} catch(Exception e) {
throw new IOException("Invalid key format: " + line);
}
}
return lines;
}
private static void printkeys(datastructure.list.List<Integer> L) {
for(Integer key: L)
System.out.print(key.toString() + " ");
System.out.println();
}
public static void main(String args[]) {
List<String> lines = null;
if(args.length != 1) {
System.err.println("Usage: DictionaryTest <filename>\n");
System.exit(0);
}
try {
lines = readCommands(args[0]);
} catch(Exception e) {
System.err.println(e);
System.exit(0);
}
Dictionary<Integer,String> D = new HashTable<>();
//Dictionary<Integer,String> D = new AVLDictionary<>();
for(String l: lines) {
String[] cmd = l.split(" ");
Integer key = Integer.parseInt(cmd[1]);
String data;
switch(cmd[0]) {
case "INSERT":
data = cmd.length == 3 ? cmd[2] : null;
D.insert(key,data);
System.out.println("\nInserting (" + key + "," + (data == null ? "null" : data) + ")\n");
break;
case "SEARCH":
data = D.search(key);
System.out.println("\nSearching " + key + " -> " + (data == null ? "null" : data) + "\n");
break;
case "DELETE":
data = D.delete(key);
System.out.println("\nDeleting " + key + " -> " + (data == null ? "null" : data) + "\n");
break;
}
System.out.println("Number of elements: " + D.size() + "\n");
System.out.print(D);
}
}
}