Ejemplo n.º 1
0
        /// <summary>
        /// Updates the population of a valid city if one is found and the new population is
        /// different than the current population. Updates both the related CityInfo object
        /// as well as the file containing the city information specified by the user.
        /// </summary>
        /// <param name="cityName">The city to be found</param>
        /// <param name="population">The population to change it to</param>
        /// <param name="fileName">The file to alter</param>
        public void UpdatePopulation(string cityName, ulong population, string fileName)
        {
            try
            {
                CityInfo cityToUpdate = GetSpecificCity(cityName);
                if (cityToUpdate != null)
                {
                    if (cityToUpdate.GetPopulation() == population)
                    {
                        Console.WriteLine($"{cityToUpdate.GetCityName()}, {cityToUpdate.GetProvince()} already has a population of {string.Format("{0:n0}", population)}.");
                        return;
                    }
                    switch (fileName)
                    {
                    case "Canadacities-XML.xml":
                        UpdateXMLFile(cityName, cityToUpdate.GetProvince(), population, fileName);
                        break;

                    case "Canadacities-JSON.json":
                        UpdateJSONFile(cityName, cityToUpdate.GetProvince(), population, fileName);
                        break;

                    case "Canadacities.csv":
                        UpdateCSVFile(cityName, cityToUpdate.GetProvince(), population, fileName);
                        break;

                    default:
                        throw new Exception($"Invalid file name: {fileName}");
                    }
                    ulong oldPopulation = cityToUpdate.GetPopulation();
                    cityToUpdate.SetPopulation(population);
                    PopulationChangeEvent.OnPopulationChange(new PopulationChangeEventArgs(
                                                                 cityToUpdate.GetCityName(),
                                                                 cityToUpdate.GetProvince(),
                                                                 oldPopulation,
                                                                 population
                                                                 ));
                }
                else
                {
                    Console.WriteLine($"\nNo city exists in the collection called {CapitalizeString(cityName)}\n");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Population Update Failed: {ex.Message}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Displays the city information.
        /// </summary>
        /// <param name="cityName">Name of the city.</param>
        public void DisplayCityInformation(string cityName)
        {
            CityInfo chosenCity = GetSpecificCity(cityName);

            if (chosenCity != null)
            {
                PrintCityDetails(chosenCity.GetCityName(), chosenCity.GetProvince(), chosenCity.GetPopulation());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reports which city between two given cities has a higher population
        /// </summary>
        /// <param name="city1">First city object to compare</param>
        /// <param name="city2">Second city object to compare</param>
        public void CompareCitiesPopulation(CityInfo city1, CityInfo city2)
        {
            if (city1 == city2)
            {
                Console.WriteLine("\nThe two cities provided are the same\n");
                return;
            }

            CityInfo smallerCity = city1.GetPopulation() < city2.GetPopulation() ? city1 : city2;
            CityInfo largerCity  = smallerCity == city1 ? city2 : city1;

            Console.WriteLine($"\nLarger City:\t{CapitalizeString(largerCity.GetCityName())}, {CapitalizeString(largerCity.GetProvince())} ({string.Format("{0:n0}", largerCity.GetPopulation())})");
            Console.WriteLine($"Smaller City:\t{CapitalizeString(smallerCity.GetCityName())}, {CapitalizeString(smallerCity.GetProvince())} ({string.Format("{0:n0}", smallerCity.GetPopulation())})\n");
        }