-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataManager.java
128 lines (114 loc) · 3.67 KB
/
DataManager.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
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
126
127
128
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* Created by dakle on 21/3/17.
*/
public class DataManager {
private String filePath = "";
private BufferedReader reader = null;
public Map<Short, Integer> movieMap;
public Map<Integer, Integer> userMap;
public List<User> userList;
public List<Movie> movieList;
public TreeSet<Integer> userIdSet;
public TreeSet<Short> movieIdSet;
public DataManager(String filePath) {
this.filePath = filePath;
this.movieMap = new HashMap<>();
this.userMap = new HashMap<>();
this.userList = new ArrayList<>();
this.movieList = new ArrayList<>();
this.userIdSet = new TreeSet<>();
this.movieIdSet = new TreeSet<>();
}
public boolean openFile() {
try {
File f = new File(this.filePath);
reader = new BufferedReader(new FileReader(f));
if(reader != null) {
return true;
}
return false;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
public String getLine() {
try {
if(reader != null) {
return reader.readLine();
}
return null;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
public boolean closeFile() {
try {
if(reader != null) {
reader.close();
return true;
}
return false;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean generateData() {
String line;
String[] splitLine;
int userCounter = 0, movieCounter = 0;
int userId;
short movieId;
Double dRating;
byte rating;
if(reader == null) {
if(this.openFile()) {
while ((line = getLine()) != null) {
splitLine = line.split(",");
movieId = Short.parseShort(splitLine[0]);
userId = Integer.parseInt(splitLine[1]);
dRating = Double.parseDouble(splitLine[2]);
rating = dRating.byteValue();
if(!userMap.containsKey(userId)) {
userIdSet.add(userId);
userMap.put(userId, userCounter);
userList.add(new User());
userCounter++;
}
userList.get(userMap.get(userId)).movieIdsList.add(movieId);
userList.get(userMap.get(userId)).ratingsList.add(rating);
if(!movieMap.containsKey(movieId)) {
movieIdSet.add(movieId);
movieMap.put(movieId, movieCounter);
movieList.add(new Movie());
movieCounter++;
}
movieList.get(movieMap.get(movieId)).userIdList.add(userId);
movieList.get(movieMap.get(movieId)).ratingsList.add(rating);
}
for (Integer id :
userIdSet) {
userList.get(userMap.get(id)).generateArrays();
}
for (Short id :
movieIdSet) {
movieList.get(movieMap.get(id)).generateArrays();
}
this.closeFile();
return true;
}
return false;
}
return false;
}
}