-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
107 lines (94 loc) · 2.8 KB
/
Main.java
File metadata and controls
107 lines (94 loc) · 2.8 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
import java.awt.print.Printable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import javax.naming.directory.InvalidSearchControlsException;
public class Main {
public static void WriteToFile(ArrayList<Node> nodes) {
try {
FileOutputStream fileOut = new FileOutputStream("booktree.txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
for (Node node : nodes) {
objectOut.writeObject(node);
}
objectOut.close();
System.out.println("Successfully written to file");
} catch (Exception e) {
// TODO: handle exception
}
}
public static ArrayList<Node> ReadFromFile() {
ArrayList<Node> nodes = new ArrayList<Node>();
Boolean cont = true;
try {
FileInputStream fi = new FileInputStream(new File("bookTree.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
while(cont) {
Node n = (Node)oi.readObject();
if(n != null) {
nodes.add(n);
}else {
cont = false;
oi.close();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return nodes;
}
public static void main(String[] args) {
Parser p = new Parser();
p.FillShelves();
ArrayList<Node> nodes = new ArrayList<Node>();
while(p.ids.size() >0) {
String name = p.ids.get(0);
p.ids.remove(0);
nodes.add(new Node(name, p.ids, p.shelves));
System.out.println("Finished node: "+p.ids.size() );
}
WriteToFile(nodes);
//ArrayList<Node> nodes = ReadFromFile();
System.out.print("Read from file");
Scanner bookScanner = new Scanner(System.in);
System.out.println("Enter up to 2 books that you have liked to get a book reccomendation (space seperated): ");
String books = bookScanner.nextLine();
String[] booksArray = books.split(" ");
//get the relevant rules
ArrayList<String> possiblities = new ArrayList<String>();
for (int i = 0; i < booksArray.length; i++) {
for (Node node : nodes) {
for(String rule: node.Rules) {
String[] ruleBooks = rule.split(",");
for(int j = 0; j < ruleBooks.length; j++) {
if(booksArray[i].equals(ruleBooks[j])) {
possiblities.add(rule);
}
}
}
}
}
//sort through the relevant rules to find the most helpful ones
for (String rule : possiblities) {
int count = 0;
String[] ruleBooks = rule.split(",");
for (int i = 0; i < ruleBooks.length; i++) {
for (int j = 0; j < booksArray.length; j++) {
if(booksArray[j].equals(ruleBooks[i])) {
count++;
j = booksArray.length;
}
}
}
if (count == booksArray.length&& count < ruleBooks.length ) {
System.out.println(rule.toString());
}
}
}
}