Esempio n. 1
0
        public static CovidRow FromCsv(string line)
        {
            string[] cols = line.Split(",");

            try
            {
                if (cols.Length == 6)
                {
                    string fips = cols[3];

                    string deaths = string.IsNullOrWhiteSpace(cols[5]) ? "0" : cols[5];

                    // county
                    var row = new CovidRow();
                    row.Date   = DateTime.Parse(cols[0]);
                    row.County = cols[1];
                    row.State  = cols[2];
                    row.Fips   = string.IsNullOrWhiteSpace(fips) ? 0 : Convert.ToInt32(fips);
                    row.Cases  = Convert.ToInt32(cols[4]);
                    row.Deaths = Convert.ToInt32(deaths);

                    return(row);
                }
                else
                {
                    string fips = cols[3];

                    var row = new CovidRow();
                    row.Date   = DateTime.Parse(cols[0]);
                    row.State  = cols[1];
                    row.Fips   = string.IsNullOrWhiteSpace(fips) ? 0 : Convert.ToInt32(fips);
                    row.Cases  = Convert.ToInt32(cols[3]);
                    row.Deaths = Convert.ToInt32(cols[4]);

                    return(row);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("CovidRow.FromCSV: EXCEPTION");
                Console.WriteLine(line);
                Console.WriteLine($"Exception message={e.Message}");

                throw;
            }
        }
Esempio n. 2
0
        public Dictionary <string, State> Read(DateTime?onOrAfter = null)
        {
            string filename = _dataSetRetriever.GetFilename();

            try
            {
                var results = new List <CovidRow>();

                using var file = new StreamReader(filename);
                string line = file.ReadLine();

                while ((line = file.ReadLine()) != null)
                {
                    var row = CovidRow.FromCsv(line);

                    if (onOrAfter.HasValue)
                    {
                        bool check = row.Date.Date < onOrAfter.Value.Date;
                        if (check)
                        {
                            continue;
                        }
                    }

                    results.Add(row);
                }

                var states = new Dictionary <string, State>();

                foreach (var record in results)
                {
                    bool exists = states.TryGetValue(record.State, out var counties);
                    if (exists)
                    {
                        State state = states[record.State];

                        bool cExists = state.Counties.Exists(x => x.Fips == record.Fips);
                        if (cExists)
                        {
                            var c = state.Counties.Single(x => x.Fips == record.Fips);
                            c.Records.Add(new CovidDay {
                                Date = record.Date, Cases = record.Cases, Deaths = record.Deaths
                            });
                        }
                        else
                        {
                            var county = new County {
                                CountyName = record.County, Fips = record.Fips
                            };
                            county.Records.Add(new CovidDay {
                                Date = record.Date, Cases = record.Cases, Deaths = record.Deaths
                            });

                            state.Counties.Add(county);
                        }
                    }
                    else
                    {
                        var county = new County {
                            CountyName = record.County, Fips = record.Fips
                        };
                        county.Records.Add(new CovidDay {
                            Date = record.Date, Cases = record.Cases, Deaths = record.Deaths
                        });

                        var state = new State {
                            StateName = record.State
                        };
                        state.Counties.Add(county);

                        states.Add(record.State, state);
                    }
                }

                return(states);
            }
            finally
            {
                try
                {
                    Console.WriteLine("Deleting file...");
                    File.Delete(filename);
                    Console.WriteLine("Deleted file.");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error deleting file. Message={e.Message}");
                }
            }
        }