mardi 15 mai 2018

Determining MPG over a range of time with a Dictionary

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) 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.

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

Storing the csv 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 that 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?

Aucun commentaire:

Enregistrer un commentaire