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");
        }
        /// <summary>
        /// Menu for city statistics.
        /// </summary>
        /// <param name="cityStats">A populated statistics object</param>
        static void CityMenu(Statistics cityStats)
        {
            string selection;
            string titleText   = "Cities Menu";
            bool   displayMenu = true;
            string dash        = new string('-', titleText.Length);

            do
            {
                //Display the menu
                if (displayMenu)
                {
                    Console.Clear();
                    Console.WriteLine($"{dash}\n{titleText}\n{dash}\n");
                    Console.WriteLine("Available selections:\n");
                    Console.WriteLine("\t1) Display city's info.");
                    Console.WriteLine("\t2) Display the city with the largest population within a given province.");
                    Console.WriteLine("\t3) Display the city with the smallest population within a given province.");
                    Console.WriteLine("\t4) Compare the population of two cities.");
                    Console.WriteLine("\t5) Display the city on a map.");
                    Console.WriteLine("\t6) Calculate the distance between two cities.");
                    Console.WriteLine("\t7) Update the population of a specific city.");
                    Console.WriteLine("\treturn) Return to the main menu");
                    Console.Write("\nPlease make a selection (ex. 1, return): ");
                }
                displayMenu = true;

                //Handle response
                selection = Console.ReadLine();
                string response;
                switch (selection.ToLower())
                {
                case "1":
                    response = GetValidCityName(cityStats);
                    cityStats.DisplayCityInformation(response);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "2":
                    Console.WriteLine("\nWhich province do you wish to see the largest population city of?");
                    response = GetValidProvince(cityStats);
                    cityStats.DisplayLargestPopulationCity(response);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "3":
                    Console.WriteLine("\nWhich province do you wish to see the smallest population city of?");
                    response = GetValidProvince(cityStats);
                    cityStats.DisplaySmallestPopulationCity(response);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "4":
                    Console.WriteLine("\nWhich city do you want to pick first?");
                    CityInfo city1 = GetCityChoiceObject(cityStats);
                    Console.WriteLine("\nWhich city do you want to compare it to?");
                    CityInfo city2 = GetCityChoiceObject(cityStats);
                    cityStats.CompareCitiesPopulation(city1, city2);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "5":
                    Console.WriteLine("\nWhich city do you want to see on the map?");
                    CityInfo city = GetCityChoiceObject(cityStats);
                    cityStats.ShowCityOnMap($"{city.GetCityName().ToLower()}|{city.GetProvince().ToLower()}");
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "6":
                    Console.WriteLine("\nWhich city do you want to start at?");
                    CityInfo startingCity = GetCityChoiceObject(cityStats);
                    Console.WriteLine("\nWhich city do you want to calculate the distance to?");
                    CityInfo endingCity = GetCityChoiceObject(cityStats);
                    cityStats.CalculateDistanceBetweenCities(startingCity, endingCity).Wait();
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "7":
                    UpdatePopulation(cityStats);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "return":
                    return;

                default:
                    Console.Write("\nInvalid selection, please enter a valid selection: ");
                    displayMenu = false;
                    break;
                }
            } while (true);
        }