Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

What skill does a network manager need?

Answered: 1 week ago