import java.util.ArrayList;
public class MovingAverage {
private ArrayList<Double> data;
private int period;
public MovingAverage(int period) {
this.period = period;
this.data = new ArrayList<Double>();
}
public void addData(double value) {
this.data.add(value);
if (this.data.size() > this.period) {
this.data.remove(0);
}
}
public double calculate() {
double sum = 0;
for (double value : this.data) {
sum += value;
}
return sum / this.data.size();
}
}