-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListTest.java
More file actions
80 lines (67 loc) · 2.59 KB
/
ListTest.java
File metadata and controls
80 lines (67 loc) · 2.59 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
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.list.ArrayList;
import datastructure.list.DoubleEndedList;
public class ListTest {
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("APPEND") && !cmd[0].equals("SEARCH") &&
!cmd[0].equals("DELETE") && !cmd[0].equals("SET") && !cmd[0].equals("GET"))
throw new IOException("Invalid command in line: " + line);
if(cmd.length != 2)
throw new IOException("Invalid file format: " + line);
try {
Integer.parseInt(cmd[1]);
} catch(Exception e) {
throw new IOException("Invalid key format: " + line);
}
}
return lines;
}
public static void main(String args[]) {
List<String> lines = null;
if(args.length != 1) {
System.err.println("Usage: ListTest <filename>\n");
System.exit(0);
}
try {
lines = readCommands(args[0]);
} catch(Exception e) {
System.err.println(e);
System.exit(0);
}
//ArrayList<Integer> L = new ArrayList<>();
DoubleEndedList<Integer> L = new DoubleEndedList<>();
for(String l: lines) {
String[] cmd = l.split(" ");
Integer data = Integer.parseInt(cmd[1]);
switch(cmd[0]) {
case "INSERT":
L.insert(data);
System.out.println("Inserting " + data + "\n");
break;
case "APPEND":
L.append(data);
System.out.println("Appending " + data + "\n");
break;
case "SEARCH":
boolean bool = L.search(data);
System.out.println("Searching " + data + " -> " + bool + "\n");
break;
case "DELETE":
L.delete(data);
System.out.println("Deleting " + data + "\n");
break;
}
System.out.println("Number of elements: " + L.size());
System.out.println(L);
System.out.println("****************\n");
}
}
}