01: package com.db4o.f1.chapter5;
02:
03: import java.util.*;
04:
05: public class Car {
06: private String model;
07: private Pilot pilot;
08: private SensorReadout history;
09:
10: public Car(String model) {
11: this .model = model;
12: this .pilot = null;
13: this .history = null;
14: }
15:
16: public Pilot getPilot() {
17: return pilot;
18: }
19:
20: public void setPilot(Pilot pilot) {
21: this .pilot = pilot;
22: }
23:
24: public String getModel() {
25: return model;
26: }
27:
28: public SensorReadout getHistory() {
29: return history;
30: }
31:
32: public void snapshot() {
33: appendToHistory(new TemperatureSensorReadout(new Date(), this ,
34: "oil", pollOilTemperature()));
35: appendToHistory(new TemperatureSensorReadout(new Date(), this ,
36: "water", pollWaterTemperature()));
37: appendToHistory(new PressureSensorReadout(new Date(), this ,
38: "oil", pollOilPressure()));
39: }
40:
41: protected double pollOilTemperature() {
42: return 0.1 * countHistoryElements();
43: }
44:
45: protected double pollWaterTemperature() {
46: return 0.2 * countHistoryElements();
47: }
48:
49: protected double pollOilPressure() {
50: return 0.3 * countHistoryElements();
51: }
52:
53: public String toString() {
54: return model + "[" + pilot + "]/" + countHistoryElements();
55: }
56:
57: private int countHistoryElements() {
58: return (history == null ? 0 : history.countElements());
59: }
60:
61: private void appendToHistory(SensorReadout readout) {
62: if (history == null) {
63: history = readout;
64: } else {
65: history.append(readout);
66: }
67: }
68: }
|