using CarCareTracker.Models; namespace CarCareTracker.Helper { public interface IGasHelper { List GetGasRecordViewModels(List result, bool useMPG, bool useUKMPG); } public class GasHelper : IGasHelper { public List GetGasRecordViewModels(List result, bool useMPG, bool useUKMPG) { var computedResults = new List(); int previousMileage = 0; decimal unFactoredConsumption = 0.00M; int unFactoredMileage = 0; //perform computation. for (int i = 0; i < result.Count; i++) { var currentObject = result[i]; decimal convertedConsumption; if (useUKMPG && useMPG) { //if we're using UK MPG and the user wants imperial calculation insteace of l/100km //if UK MPG is selected then the gas consumption are stored in liters but need to convert into UK gallons for computation. convertedConsumption = currentObject.Gallons / 4.546M; } else { convertedConsumption = currentObject.Gallons; } if (i > 0) { var deltaMileage = currentObject.Mileage - previousMileage; var gasRecordViewModel = new GasRecordViewModel() { Id = currentObject.Id, VehicleId = currentObject.VehicleId, MonthId = currentObject.Date.Month, Date = currentObject.Date.ToShortDateString(), Mileage = currentObject.Mileage, Gallons = convertedConsumption, Cost = currentObject.Cost, DeltaMileage = deltaMileage, CostPerGallon = currentObject.Cost / convertedConsumption, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp }; if (currentObject.MissedFuelUp) { //if they missed a fuel up, we skip MPG calculation. gasRecordViewModel.MilesPerGallon = 0; //reset unFactored vars for missed fuel up because the numbers wont be reliable. unFactoredConsumption = 0; unFactoredMileage = 0; } else if (currentObject.IsFillToFull) { //if user filled to full. gasRecordViewModel.MilesPerGallon = useMPG ? (unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption)); //reset unFactored vars unFactoredConsumption = 0; unFactoredMileage = 0; } else { unFactoredConsumption += convertedConsumption; unFactoredMileage += deltaMileage; gasRecordViewModel.MilesPerGallon = 0; } computedResults.Add(gasRecordViewModel); } else { computedResults.Add(new GasRecordViewModel() { Id = currentObject.Id, VehicleId = currentObject.VehicleId, MonthId = currentObject.Date.Month, Date = currentObject.Date.ToShortDateString(), Mileage = currentObject.Mileage, Gallons = convertedConsumption, Cost = currentObject.Cost, DeltaMileage = 0, MilesPerGallon = 0, CostPerGallon = currentObject.Cost / convertedConsumption, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp }); } previousMileage = currentObject.Mileage; } return computedResults; } } }