-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaterDatabase.java
71 lines (59 loc) · 1.87 KB
/
RaterDatabase.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
/**
* Write a description of RaterDatabase here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
public class RaterDatabase {
private static HashMap<String,Rater> ourRaters;
private static void initialize() {
// this method is only called from addRatings
if (ourRaters == null) {
ourRaters = new HashMap<String,Rater>();
}
}
public static void initialize(String filename) {
if (ourRaters == null) {
ourRaters= new HashMap<String,Rater>();
addRatings("data/" + filename);
}
}
public static void addRatings(String filename) {
initialize();
FileResource fr = new FileResource(filename);
CSVParser csvp = fr.getCSVParser();
for(CSVRecord rec : csvp) {
String id = rec.get("rater_id");
String item = rec.get("movie_id");
String rating = rec.get("rating");
addRaterRating(id,item,Double.parseDouble(rating));
}
}
public static void addRaterRating(String raterID, String movieID, double rating) {
initialize();
Rater rater = null;
if (ourRaters.containsKey(raterID)) {
rater = ourRaters.get(raterID);
}
else {
rater = new EfficientRater(raterID);
ourRaters.put(raterID,rater);
}
rater.addRating(movieID,rating);
}
public static Rater getRater(String id) {
initialize();
return ourRaters.get(id);
}
public static ArrayList<Rater> getRaters() {
initialize();
ArrayList<Rater> list = new ArrayList<Rater>(ourRaters.values());
return list;
}
public static int size() {
return ourRaters.size();
}
}