-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.java
74 lines (54 loc) · 2.42 KB
/
Runner.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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Runner {
//Arguments you program should expect:
//1. t (the value for BTree).
//2. m (the size of expected hash table with chaining).
//Note that the files used (friends.txt,messages.txt,spam_words.txt)
//should be located in the same location as your src folder.
public static void main(String[] args) {
//Create the btree using the t value and the path to the friends file.
BTree btree = createTree(args[0]);
//Get the BFS representation of the btree
String treeLayout = btree.toString();
// Create a Messages object based on the messages file.
Messages messages = createArrayOfMessages();
//For each message, create a hash table based on the words in the message.
messages.createHashTables(args[1]);
//Find the spam messages- Use the path of the spam_words file.
// (The Messages object should use the file path to create the Spam array.
// Then, it will determine for each message if it is spam, using the Spam array,
// the btree and the hash table of the message).
String spamMessages = messages.findSpams(System.getProperty("user.dir")+"/spam_words.txt", btree);
//create a file with the program's expected output
createOutputFile(treeLayout, spamMessages, System.getProperty("user.dir")+"/output.txt");
}
// Create a Messages object based on the messages file.
// Creates a Messages object and updates its' Message array using the messages file.
private static Messages createArrayOfMessages() {
Messages messages = new Messages();
messages.generateMessages(System.getProperty("user.dir")+"/messages.txt");
return messages;
}
// Creates BTree using the t value, and the friends file.
// Inserts the friends into the tree.
private static BTree createTree(String tVal) {
BTree btree = new BTree(tVal);
btree.createFullTree(System.getProperty("user.dir")+"/friends.txt");
return btree;
}
//Creates a file with the program's expected output
private static void createOutputFile(String treeLayout, String spamMessages, String pathToOutput) {
Path path = Paths.get(pathToOutput);
StringBuilder sb = new StringBuilder();
sb.append(treeLayout).append(System.lineSeparator()).append(spamMessages);
byte[] strToBytes = sb.toString().getBytes();
try {
Files.write(path, strToBytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}