-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Week9/SensorAndTemperatureMeasurement/application/AverageSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
package application; | ||
|
||
/** | ||
* | ||
* @author giuseppedesantis | ||
*/ | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
public class AverageSensor implements Sensor{ | ||
private List<Sensor> sensors; | ||
private List<Integer> readings; | ||
|
||
public AverageSensor(){ | ||
this.sensors = new ArrayList<Sensor>(); | ||
this.readings = new ArrayList<Integer>(); | ||
} | ||
|
||
public void addSensor(Sensor additional){ | ||
this.sensors.add(additional); | ||
} | ||
|
||
@Override | ||
public boolean isOn(){ | ||
for(Sensor s : this.sensors){ | ||
if(s.isOn()){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void on(){ | ||
for(Sensor s : this.sensors){ | ||
s.on(); | ||
} | ||
} | ||
|
||
@Override | ||
public void off(){ | ||
for(Sensor s: this.sensors){ | ||
s.off(); | ||
} | ||
} | ||
|
||
@Override | ||
public int measure(){ | ||
if(this.sensors.isEmpty() || this.isOn() == false){ | ||
throw new IllegalStateException("No sensors or average sensor is off"); | ||
}else{ | ||
int sum = 0; | ||
for(Sensor s : this.sensors){ | ||
sum += s.measure(); | ||
} | ||
int average = sum/sensors.size(); | ||
this.readings.add(average); | ||
return average; | ||
} | ||
} | ||
|
||
public List<Integer> readings(){ | ||
return readings; | ||
} | ||
} | ||
|