lubelog/Helper/GasHelper.cs
2024-01-20 14:30:07 -07:00

116 lines
5.4 KiB
C#

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