1 package com.db4o.f1.chapter5; 2 3 import java.util.*; 4 5 public class Car { 6 private String model; 7 private Pilot pilot; 8 private SensorReadout history; 9 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( 34 new Date(),this,"oil",pollOilTemperature())); 35 appendToHistory(new TemperatureSensorReadout( 36 new Date(),this,"water",pollWaterTemperature())); 37 appendToHistory(new PressureSensorReadout( 38 new Date(),this,"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 } 65 else { 66 history.append(readout); 67 } 68 } 69 } | Popular Tags |