예제 #1
0
 public void AddOrUpdateData(float Population, float Gdp, string nameOfContinent)
 {
     if (AggregatedData.ContainsKey(nameOfContinent))
     {
         AggregatedData[nameOfContinent].GDP_2012        += Gdp;
         AggregatedData[nameOfContinent].POPULATION_2012 += Population;
     }
     else
     {
         GDPPopulation Object = new GDPPopulation()
         {
             GDP_2012 = Gdp, POPULATION_2012 = Population
         };
         AggregatedData.Add(nameOfContinent, Object);
     }
 }
예제 #2
0
        public static async Task Aggregate() {
            string datafilePath = "../../../../AggregateGDPPopulation/data/datafile.csv";
            string mapfilePath = "../../../../AggregateGDPPopulation/data/country-continent-mapper.json";
            string outputPath = "../../../../AggregateGDPPopulation/output/output.json";
            Task<string> datatask = ReadFileAsync(datafilePath);
            Task<string> mapfiletask = ReadFileAsync(mapfilePath);
            string datafile = await datatask;
            string mapfile = await mapfiletask;

            Regex reg = new Regex("[*'\"_&#^@]");
            datafile = reg.Replace(datafile, string.Empty);
            var mapfileDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(mapfile);
            string[] datafileArr = datafile.Split('\n');
            string[] header = datafileArr[0].Split(',');
            int indexOfCountryName = Array.IndexOf(header, "Country Name");
            int indexofGDP2012 = Array.IndexOf(header, "GDP Billions (USD) 2012");
            int indexofPopulation2012 = Array.IndexOf(header, "Population (Millions) 2012");
            Dictionary<string, GDPPopulation> result = new Dictionary<string, GDPPopulation>();
            
            for(int i=1; i<(datafileArr.Length - 1); i++)
            {
                try
                {
                    string[] rows = datafileArr[i].Split(',');
                    var continent = mapfileDict[rows[indexOfCountryName]];
                    try
                    {
                        result[continent].GDP_2012 += float.Parse(rows[indexofGDP2012]);
                        result[continent].POPULATION_2012 += float.Parse(rows[indexofPopulation2012]);
                    }
                    catch
                    {
                        GDPPopulation gDPPopulation = new GDPPopulation()
                        {
                            GDP_2012 = float.Parse(rows[indexofGDP2012]),
                            POPULATION_2012 = float.Parse(rows[indexofPopulation2012])
                        };
                        result.Add(continent, gDPPopulation);
                    }
                }

                catch(Exception e) { }
            }
            string json = JsonConvert.SerializeObject(result, Formatting.Indented);
            WriteFileAsync(outputPath, json);
        }
        public async Task AggregateGDP2012Population(string filePath)
        {
            var csvfile  = ReadAsync(filePath);
            var jsonfile = ReadAsync(countryJsonPath);
            await Task.WhenAll(csvfile, jsonfile);

            var Mapper = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonfile.Result);

            string[] CsvProcessed    = csvfile.Result.Replace("\"", String.Empty).Trim().Split('\n');
            string[] headers         = CsvProcessed[0].Split(',');
            int      populationIndex = Array.IndexOf(headers, "Population (Millions) 2012");
            int      gdpIndex        = Array.IndexOf(headers, "GDP Billions (USD) 2012");
            int      countriesIndex  = Array.IndexOf(headers, "Country Name");
            Dictionary <string, GDPPopulation> dictonaryObject = new Dictionary <string, GDPPopulation>();

            for (int i = 1; i < csvfile.Result.Length; i++)
            {
                try
                {
                    string[] row             = CsvProcessed[i].Split(',');
                    string   countryName     = row[countriesIndex];
                    string   nameOfContinent = Mapper[countryName];
                    float    Population      = float.Parse(row[populationIndex]);
                    float    Gdp             = float.Parse(row[gdpIndex]);
                    try
                    {
                        dictonaryObject[nameOfContinent].GDP_2012        += Gdp;
                        dictonaryObject[nameOfContinent].POPULATION_2012 += Population;
                    }
                    catch (Exception e) {
                    }
                    finally
                    {
                        GDPPopulation Object = new GDPPopulation()
                        {
                            GDP_2012 = Gdp, POPULATION_2012 = Population
                        };
                        dictonaryObject.Add(nameOfContinent, Object);
                    }
                }
                catch (Exception e) {
                }
            }
            await WriteFile(dictonaryObject);
        }