Question: C# Create a class called Country. It contains two private variables for the name of a country and the population of a country. Give it

C#

Create a class called Country. It contains two private variables for the name of a country and the population of a country. Give it a constructor that sets those variables. Give it get methods for the name and population variables. In your Main method, write code that requests name and population data from the user. Use that data to instantiate Country objects. Store those objects in an ArrayList that has been configured to only contain Country objects. Your code should allow for an indefinate number of Country objects to be created and loaded that way (i.e. a loop is used). Also in your Main method, include code that allows the user to enter country names. The program should find that country name in the ArrayList and display the associated population. If it cannot find a match, display a message to that effect. This code should also be in a loop that runs until the user tells it to stop.

I have everything to work except the bold part. I don't know how to search through a list with objects in it.

namespace Project { class Program { static void Main(string[] args) { string nameInput; string populationInput;

Console.Write("How many countries would you like to add? "); int count = Convert.ToInt32(Console.ReadLine());

List CountryList = new List();

for (int x = 0; x < count; x++) { Console.WriteLine("Enter a country: "); nameInput = Console.ReadLine(); Console.WriteLine("Enter a population: "); populationInput = Console.ReadLine(); country o = new country(nameInput, populationInput); CountryList.Add(o.ToString()); }

string countrySearch; Console.WriteLine("Please enter the country you'd like to search."); countrySearch = Console.ReadLine();

for (int y = 0; y < CountryList.Count; y++) { if (countrySearch = CountryList[y]) {

} Console.WriteLine(CountryList[y]); }

Console.ReadKey();

} } class country { private string name; private string population;

public country(string n, string p) { Name = n; Population = p; } public string Name { get { return name; } set { name = value; } }

public string Population { get { return population; } set { population = value; } } public override string ToString() { String output;

output = ("Country: " + name + " Population: " + population + " "); return output;

} } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!