Example #1
0
        public Race(string id, List <Pilot> pilots, List <LapRace> lapRaces)
        {
            // this property is not handle automatically because we dont have a real database to handle
            // this for us
            Id = id;

            _lapRaces = lapRaces;

            _positionPilots = GetPilotsPositions(pilots);
            _pilots         = pilots;

            // calculate the BestLap [which we are considering the Lap in which some pilot finished in less time]
            BestLap = GetBestLap();
        }
Example #2
0
        /// <summary>
        /// Fetch RaceEvent from source data file. The RaceEvent will be used to create all repositories
        /// </summary>
        /// <returns></returns>
        private ICollection <RaceEvent> getRaceEvents()
        {
            // create the list race Events
            var raceEvents = new List <RaceEvent>();

            // set lines
            IEnumerable <string> lines = null;

            try
            {
                var path = Path.Combine(Directory.GetCurrentDirectory(), "racing_data.txt");

                // get all lines from .txt data source
                lines = File.ReadLines(path);
            }
            catch (Exception ex)
            {
                throw new Exception("There was a problem in read the data source file", ex);
            }

            // if the lines is still null, then return null
            if (lines == null)
            {
                return(null);
            }

            try
            {
                // iterate through every line
                var lineNumber = 1;
                foreach (var line in lines)
                {
                    // skip the first line
                    if (lineNumber == 1)
                    {
                        lineNumber++;
                        continue;
                    }

                    // Split string separated by multiple spaces, ignoring single spaces
                    var fields = System.Text.RegularExpressions.Regex.Split(line, @"\s{2,}");

                    // create the pilot
                    var pilot = new Pilot
                    {
                        Id   = fields[1].Split(" ").First(),
                        Name = fields[1].Split(" ").Last()
                    };

                    // create lap race
                    var lapRace = new LapRace
                    {
                        TimeEvent    = TimeSpan.Parse(fields[0]),
                        PilotId      = fields[1].Split(" ").First(),
                        Number       = int.Parse(fields[2]),
                        TimeDuration = TimeSpan.ParseExact(fields[3], @"%m\:ss\.fff", CultureInfo.InvariantCulture),
                        MeanVelocity = double.Parse(fields[4], new CultureInfo("pt-BR"))
                    };


                    // create the race event
                    var raceEvent = new RaceEvent
                    {
                        Sequence = lineNumber - 1,
                        Pilot    = pilot,
                        LapRace  = lapRace
                    };

                    // add event Race to the collection
                    raceEvents.Add(raceEvent);

                    // increment lineNumber
                    lineNumber++;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("There was a problem on parse values from the data source", ex);
            }

            return(raceEvents);
        }