/** * Set odometer in string format, so that patterns can be parsed out (such as the '+' prefix * notation. Note that if you are using this feature, the caller needs to set the vehicle ID * before calling this in order to get the previous odometer value! Also, remember that this * method is not free, due to the additional database lookup(s)! * * @param odometer The string representing the odometer value */ public void setOdometer(String odometer) throws NumberFormatException { if (m_vehicleId < 0) { throw new IllegalStateException( "Need to set vehicle ID before calling setOdometer(String odometer)!"); } if (odometer.startsWith("+")) { // m_odometer must be maxed out for getPrevious() to work. m_odometer = Double.MAX_VALUE; FillUp previous = getPrevious(); if (previous == null) { setOdometer(odometer.substring(1)); return; } m_odometer = previous.getOdometer() + Double.parseDouble(odometer.substring(1)); } else { m_odometer = Double.parseDouble(odometer); } }
/** * Calculates the distance since the previous fill-up. * * @return the distance since the previous fill-up */ public double calcDistance() { if (m_distance == 0D) { m_previous = getPrevious(); if (m_previous == null) { // we're at the first fill-up, so there's nothing we can do return -1D; } m_distance = m_odometer - m_previous.getOdometer(); } return m_distance; }
/** * Calculates the fuel economy (based on the user's preferences) since the previous fill-up. * * @return the fuel economy since the previous fill-up. */ public double calcEconomy() { if (m_partial) { return -1D; } if (m_economy == 0D) { FillUp previous = getPrevious(); if (previous == null) { return -1D; } double distance = calcDistance(); double fuel = getAmount(); while (previous != null) { if (previous.isPartial() == false) { break; } // partial; we need to keep iterating distance += previous.calcDistance(); fuel += previous.getAmount(); previous = previous.getPrevious(); } m_economy = m_calculator.calculateEconomy(distance, fuel); } return m_economy; }