-3

A group of friends are tracking the miles per gallon for each of their cars. Each time one of them fills up their gas tank, they record the following in a file:

His or her name, The type of car they drove, How many miles driven since they last filled up, How many gallons purchased at this fill up, Date of the fill.

Their data is formatted as a comma separate value (csv) file with the following format for each row:(#person,carName,milesDriven,gallonsFilled,fillupDate). For simplicity, I am avoiding csv parsing for now. Miles are recorded as floating-point numbers and gallons as integers.

Task is to create a program that allows members of this group to determine the miles per gallon (MPG) of each of their cars during a specific time range. Note: person may have more than one so a time range query might need to output data for one or more cars.

Query: GetRangeMPG(PersonName, StartDate, EndDate) Returns list of objects containing (CarName, MPG)

MPG is calculated as (total miles traveled during time period)/(total gallons filled during time period.

Also, we'll assume dates input as a query would be the dates on which we filled the car.

Trying to figure out the best way to store this kind of data, this is what I have below:

Storing the data in a dictionary with PersonName as the key and List of Objects of type Car. GetRangeMPG method would search through this dictionary with the PersonName argument provided and try to find the MPG for the cars the person owns.

class Car
{
    public string CarName { get; set; }
    public double MilesDriven { get; set; }
    public int GallonFilled { get; set; }
    public DateTime DateFilled { get; set; }
}

class MPGCalc
{
    Dictionary<string, List<Car>> log = new Dictionary<string, List<Car>>();
    public void LoadData(string inp)
    {
        string[] lines = inp.Split(new[] { Environment.NewLine },StringSplitOptions.None);
        string personName, carName;
        double milesDriven;
        int gallonsFilled;
        DateTime fillupDate;
        List<Car> carList = new List<Car>();
        foreach(var line in lines)
        {
            string[] info = line.Split(',');
            //(#person,carName,milesDriven,gallonsFilled,fillupDate)
            personName = info[0];
            carName = info[1];
            Double.TryParse(info[2], out milesDriven);
            int.TryParse(info[2], out gallonsFilled);
            DateTime.TryParse(info[4], out fillupDate);
            if(log.ContainsKey(info[0]))
            {
                log.TryGetValue(personName, out carList);
                carList.Add(new Car {CarName = personName, MilesDriven = milesDriven, GallonFilled = gallonsFilled, DateFilled = fillupDate});
                log[personName] = carList;
            }
            else
            {
                carList.Add(new Car {CarName = personName, MilesDriven = milesDriven, GallonFilled = gallonsFilled, DateFilled = fillupDate});
                log.Add(personName, carList);
            }
        }
    }

}

How is my progress so far? Is this best approach to tackle problems similar to these?

  • 2
    Do you have a specific question? "How is my progress so far" is not a real question, because it doesn't focus on specifics. "Is this the best approach" isn't a real question, because you haven't stated what you mean by "best." Tautologies like "most effective" and "most popular" don't work either; your question must be specific. – Robert Harvey May 15 '18 at 21:45
  • Basically my question is I am thinking when I would write the GetRangeMPG method it would require a lot of computations to calculate MPG for cars belonging to the person supplied as input. Unable to wrap my head around a data-structure which can be used to make computing MPG easier. – RaksMen May 15 '18 at 22:07
  • Your data is not 3NF. Car can have multiple trips. A trip can calculate millage. If you just test for ContainsKey then why do you need TryGetValue? – paparazzo May 16 '18 at 16:18
0

This does not address the data loading .

I would not use Dictionary. Just a collection of Owners and use LINQ to find the Owner.

This class design may work better:

    public List<Owner> Owners { get; } = new List<Owner>();
}   
public class Owner
{
    public string Name { get; set; }
    public List<Car> Cars { get; } = new List<Car>();
}
public class Car
{
    public string CarName { get; set; }
    public List<Trip> Trips { get; } = new List<Trip>();
}
public class Trip
{
    public float MPG
    {
        get
        {
            return GallonFilled == 0 ? 0 :  MilesDriven / GallonFilled;
        }
    }
    public float MilesDriven { get; set; }
    public float GallonFilled { get; set; }
    public DateTime DateFilled { get; set; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.