import { FuelUnitConversionFactory } from './fuel-unit-conversion' ; import { DistanceUnitConversionFactory } from './distance-unit-conversion' ; import { UserPreferences } from '../../model/user-preferences' ; export class ConsumptionConverter { constructor ( private userPreferences ) { } public calculateConsumption ( fuelAmount , tripDistance ) { const consumptionUnit = this . userPreferences . fuelConsumptionUnit . unit ; const fuelUnit = this . userPreferences . fuelUnit . unit ; const distanceUnit = this . userPreferences . distanceUnit . unit ; const fuelUnitConversion = FuelUnitConversionFactory . get ( fuelUnit , fuelAmount ) ; const distanceUnitConversion = DistanceUnitConversionFactory . get ( distanceUnit , tripDistance ) ; if ( 'mpg' === consumptionUnit ) { const fuelUnitAsGallons = ( 'gal (US)' === fuelUnit ? fuelUnitConversion . toGallonsUS ( ) : fuelUnitConversion . toGallons ( ) ) ; return Math . round ( ( distanceUnitConversion . toMiles ( ) / fuelUnitAsGallons ) * 100 ) / 100 ; } if ( 'l/100km' === consumptionUnit ) { return Math . round ( ( ( fuelUnitConversion . toLitres ( ) / distanceUnitConversion . toKilometers ( ) ) * 100 ) * 100 ) / 100 ; } return 0 ; } }