Exemple #1
0
        public void FindPopulationByCodeDctnr()
        {
            Dictionary <string, Country> countries = new Dictionary <string, Country>();
            StreamReader sr = new StreamReader(_csvFilePath);

            // read header line
            sr.ReadLine();

            string csvLine;

            while ((csvLine = sr.ReadLine()) != null)
            {
                Country countryToFill = ReadCountryFromCsvLine(csvLine);
                countries.Add(countryToFill.Code, countryToFill);
            }
            sr.Close();

            Console.Write("Which country code do you want to look up? ");
            string  countryCode = Console.ReadLine();
            Country countryToSearch;
            bool    existsCountry = countries.TryGetValue(countryCode, out countryToSearch);

            while (!existsCountry)
            {
                Console.WriteLine($"Sorry, there is no country with code {countryCode}");
                Console.Write("Try again: ");
                countryCode   = Console.ReadLine();
                existsCountry = countries.TryGetValue(countryCode, out countryToSearch);
            }
            Console.WriteLine(PopulationFormatter.PrintCountries(countryToSearch.Name, countryToSearch.Population, 1));
        }
Exemple #2
0
        public void ReadFirstNCountriesArr()
        {
            Console.Write("\nHow many top countries do you want to list? ");
            int count;

            while (!int.TryParse(Console.ReadLine(), out count))
            {
                Console.Write("Not a number, try again: ");
            }

            Country[]    countries = new Country[count];
            StreamReader sr        = new StreamReader(_csvFilePath);

            // read header line
            sr.ReadLine();

            for (int i = 0; i < count; i++)
            {
                string csvLine = sr.ReadLine();
                if (string.IsNullOrEmpty(csvLine))
                {
                    break;
                }
                countries[i] = ReadCountryFromCsvLine(csvLine);
            }
            sr.Close();

            // remove all empty items
            countries = countries.Where(x => !Country.IsNull(x)).ToArray();

            Console.WriteLine(PopulationFormatter.PrintCountries(countries));
        }
Exemple #3
0
        public void ReadAllCountriesLst()
        {
            List <Country> countries = new List <Country>();

            StreamReader sr = new StreamReader(_csvFilePath);

            // read header line
            sr.ReadLine();

            string csvLine;

            while ((csvLine = sr.ReadLine()) != null)
            {
                countries.Add(ReadCountryFromCsvLine(csvLine));
            }
            sr.Close();

            Console.WriteLine(PopulationFormatter.PrintCountries(countries));
        }