using CarCareTracker.Models; namespace CarCareTracker.Helper { public interface IGasHelper { List GetGasRecordViewModels(List result, bool useMPG, bool useUKMPG); string GetAverageGasMileage(List results); } public class GasHelper : IGasHelper { public string GetAverageGasMileage(List results) { var recordWithCalculatedMPG = results.Where(x => x.MilesPerGallon > 0); var minMileage = results.Min(x => x.Mileage); if (recordWithCalculatedMPG.Any()) { var maxMileage = recordWithCalculatedMPG.Max(x => x.Mileage); var totalGallonsConsumed = recordWithCalculatedMPG.Sum(x => x.Gallons); var deltaMileage = maxMileage - minMileage; var averageGasMileage = (maxMileage - minMileage) / totalGallonsConsumed; return averageGasMileage.ToString("F"); } return "0"; } 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 = convertedConsumption > 0.00M ? currentObject.Cost / convertedConsumption : 0, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp, Notes = currentObject.Notes }; 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. if (convertedConsumption > 0.00M) { 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 = convertedConsumption > 0.00M ? currentObject.Cost / convertedConsumption : 0, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp, Notes = currentObject.Notes }); } previousMileage = currentObject.Mileage; } return computedResults; } } }