-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoad.java
More file actions
125 lines (113 loc) · 4.02 KB
/
Load.java
File metadata and controls
125 lines (113 loc) · 4.02 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author asraf
*/
public class Load{
static String postID = null;
static String content = null;
static String postTime = null;
static String parentID = null;
ReplyNode<ConfessionPost> tree = read();
public static void main(String[] args) {
}
public ReplyNode<ConfessionPost> read(){
try {
File fileName = new File("Data.txt");
Scanner Reader = new Scanner(fileName);
while (Reader.hasNextLine()) {
String data = Reader.nextLine();
getDataOnly(data);
if(postID!=null && content!=null && postTime!=null && parentID!=null){
ConfessionPost post = new ConfessionPost(postID,content,postTime);
if(postID.equals("UM00")){
tree = new ReplyNode<>(post);
}else{
insertData(parentID, post);
}
postID = null;
content = null;
postTime = null;
parentID = null;
}
}
Reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return tree;
}
public static void getDataOnly(String data){
if(data.startsWith("postID=")){
StringBuilder buf = new StringBuilder(data);
buf.replace(0, 7, "");
postID = buf.toString();
}else if(data.startsWith("content=")){
StringBuilder buf = new StringBuilder(data);
buf.replace(0, 8, "");
content = buf.toString();
}else if(data.startsWith("postTime=")){
StringBuilder buf = new StringBuilder(data);
buf.replace(0, 9, "");
postTime = buf.toString();
}else if(data.startsWith("Parent ID=")){
StringBuilder buf = new StringBuilder(data);
buf.replace(0, 10, "");
parentID = buf.toString();
}
}
public void insertData(String postID, ConfessionPost newPost){
ReplyNode<ConfessionPost> node = searchData(postID);
if(node != null){
node.addChild(newPost);
}
}
/**
* Search for Confession Post ID that matches the user input.
* This method will search for Confession Post that matches the post ID entered by the user.
* @param input Confession Post ID that entered by the user
* @return a node if found, else return null
*/
public ReplyNode<ConfessionPost> searchData(String input){
for (ReplyNode<ConfessionPost> node : tree){
if(node.data.toString().contains(input)){
return node;
}
}
return null;
}
public GenericQueue<ReplyNode> searchDataByDate(String input){
GenericQueue<ReplyNode> queue = new GenericQueue<>();
for (ReplyNode<ConfessionPost> node : tree){
if(node.data.getTime().contains(input)){
queue.enqueue(node);
}
}
return queue;
}
public ReplyNode searchDataByPostID(String input){
for (ReplyNode<ConfessionPost> node : tree){
if(node.data.getID().contains(input)){
return node;
}
}
return null;
}
public GenericQueue<ReplyNode> searchDataByKeyword(String input){
GenericQueue<ReplyNode> queue = new GenericQueue<>();
for (ReplyNode<ConfessionPost> node : tree){
if(node.data.getContent().contains(input)){
queue.enqueue(node);
}
}
return queue;
}
}