-
Notifications
You must be signed in to change notification settings - Fork 101
/
Stats.java
109 lines (93 loc) · 2.7 KB
/
Stats.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
/**
*
*/
package com.javaaid.ip.wissen_infotech;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Kanahaiya Gupta
*
*/
public class Stats {
public static class StatisticsAggregatorImpl implements StatisticsAggregator {
ConcurrentHashMap<String, ArrayList<Double>> map=new ConcurrentHashMap<String,ArrayList<Double>>();
ArrayList list=null;
volatile int count=0;
@Override
public synchronized void putNewPrice(String symbol, double price) {
// YOUR CODE HERE
if(map.containsKey(symbol)) {
list=map.get(symbol);
}else {
list=new ArrayList<Double>();
}
list.add(price);
map.put(symbol, list);
}
@Override
public synchronized double getAveragePrice(String symbol) {
ArrayList<Double> tmp=map.get(symbol);
count=tmp.size();
Double price=0.0;
for(Double d:map.get(symbol)) {
price+=d;
}
return price/count;
// YOUR CODE HERE
}
@Override
public synchronized int getTickCount(String symbol) {
return count;
// YOUR CODE HERE
}
}
////////////////// DO NOT MODIFY BELOW THIS LINE ///////////////////
public interface StatisticsAggregator {
// This is an input. Make note of this price.
public void putNewPrice(String symbol, double price);
// Get the average price
public double getAveragePrice(String symbol);
// Get the total number of prices recorded
public int getTickCount(String symbol);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
final StatisticsAggregator stats = new StatisticsAggregatorImpl();
final Set<String> symbols = new TreeSet<>();
String line = scanner.nextLine();
String[] inputs = line.split(",");
int threads = Integer.parseInt(inputs[0]);
ExecutorService pool = Executors.newFixedThreadPool(threads);
for (int i = 1; i < inputs.length; ++i) {
String[] tokens = inputs[i].split(" ");
final String symbol = tokens[0];
symbols.add(symbol);
final double price = Double.parseDouble(tokens[1]);
pool.submit(new Runnable() {
@Override
public void run() {
stats.putNewPrice(symbol, price);
}
});
}
pool.shutdown();
try {
pool.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String symbol : symbols) {
System.out.println(
String.format("%s %.4f %d", symbol, stats.getAveragePrice(symbol), stats.getTickCount(symbol)));
}
}
scanner.close();
}
}