-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerMeans.java
59 lines (58 loc) · 2.02 KB
/
CustomerMeans.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
import java.util.*;
import java.io.*;
public class CustomerMeans
{
public static Map<String, List<Double>> makeRatingDictionary(String dir) {
Map<String, List<Double>> ratings = new HashMap<String, List<Double>>();
File dirName = new File(dir);
File[] mvFiles = dirName.listFiles();
AllMean mean = new AllMean(dir);
for (File f : mvFiles) {
try {
FileInputStream fin = new FileInputStream(f);
BufferedReader inFile = new BufferedReader(new InputStreamReader(fin));
inFile.readLine();
String nLine = inFile.readLine();
while (nLine != null) {
String[] lineArray = nLine.split(",");
if (ratings.containsKey(lineArray[0])) {
List<Double> temp = ratings.get(lineArray[0]);
Double tot = temp.get(0) +
(Double.parseDouble(lineArray[1]) - mean.getMean());
Double num = temp.get(1) + 1;
temp.set(0, tot);
temp.set(1, num);
ratings.replace(lineArray[0], temp);
}
else {
List<Double> temp = new ArrayList<Double>();
temp.add(Double.parseDouble(lineArray[1]) - mean.getMean());
temp.add(new Double(1.0));
ratings.put(lineArray[0], temp);
}
nLine = inFile.readLine();
}
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ratings;
}
public static void main(String[] args) {
try {
FileWriter fout = new FileWriter("customerMeans.txt");
BufferedWriter outFile = new BufferedWriter(fout);
Map<String, List<Double>> ratings = makeRatingDictionary(args[0]);
for (Map.Entry<String, List<Double>> entry : ratings.entrySet()) {
List<Double> values = entry.getValue();
Double average = values.get(0) / values.get(1);
outFile.write(entry.getKey() + ":" + average);
outFile.newLine();
}
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}