-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageSensor.java
54 lines (46 loc) · 1.19 KB
/
AverageSensor.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
package application;
import java.util.*;
public class AverageSensor implements Sensor {
private List<Sensor> sensors;
private List<Integer> readings;
public AverageSensor(){
this.sensors = new ArrayList<>();
this.readings = new ArrayList<>();
}
public void addSensor(Sensor toAdd){
this.sensors.add(toAdd);
}
public List<Integer> readings(){
return this.readings;
}
@Override
public boolean isOn(){
for(Sensor each : this.sensors){
if(!each.isOn()){
return false;
}
}
return true;
}
@Override
public void setOn(){
for(Sensor each : this.sensors){
each.setOn();
}
}
@Override
public void setOff(){
this.sensors.get(0).setOff();
}
@Override
public int read(){
if(!this.isOn() || this.sensors.isEmpty()){
throw new IllegalStateException("");
}
int sum = this.sensors.stream()
.mapToInt(s -> s.read())
.sum();
this.readings.add(sum/this.sensors.size());
return sum/this.sensors.size();
}
}