How to create 2 classes of country and city and create relationship between the classes

Viewed 32

I need to create menu with options which create list of countries,list cities and display the cities by country. The project should contain 2 classes of Country and City. The fields of Country should contain Name and Code(auto increament) and fields of City-Name, code(auto increament), code of country. I tried to create it by following code but it's not the right way and i really strugelling with this, anyone can show me please what is the right way? comment-all the options should be happen by functions

//main
City c = new City();
Street s = new Street();
c.menu();


//Country
  public string Name { get; set; }
        public int Code = 111;
        public List<Country> countries= new List<Country>();

        public Country(string _name)
        {
            this.Name = _name;
            this.Display++;
            this.Code++;
        }
        public Country()
        {
                
        }
        public void CreateCountry()
        {
            string name;
            Console.WriteLine("Please choose the name of country you want to add");
            name = Console.ReadLine();
            countries.Add(new Country() { Name = name,Code=Code++});
        }

        public void PrintCountry()
        {
            
            foreach (Countries country in Countries)
            {

                Console.WriteLine(country.Name+", The code of country"+ country.Code);
            }

        }

         public void Menu()
         {
             int num;
              Console.WriteLine("Please choose one from the options\n 1 - Create Country\n 2 - Create City\n" +
                     " 3-Display all countries\n 4-Display all cities\n 5-Exit");
                 num = Convert.ToInt32(Console.ReadLine());
                 switch (num)
                 {
                     case 1:
                         c.CreateCountry();
                         break;

                     case 2:
                         s.CreateCities();
                         break;

                     case 3:
                         c.PrintCountry();
                         break;

                     case 4:
                         s.PrintCity();
                         break;

                     case 5:
                         Environment.Exit(0);
                         break;

                 }
             Menu();
         }
//City
//Here i need help how to build the class
1 Answers

If I correct understand, you need a program where you can a country list and city list. When you create a city, it should be linked to a country. In this case, it looks like you need to use the MVP patter to do it. What does it mean? https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

  1. you should have separated code that can display what you need in any format

  2. you need to create a model (the City and Country classes) in this case your Country class should contain only a list of City and City contain a link to Country. That's all for your model.

  3. add a controller, something like LocationController this class will be response to store your data and process your actions. In this code, you will create links between the Country and City. Also, you can print here, but not really well approach if thinking about design pattern

It's the simplest way to do it. But to create better code, you can try to use the Command pattern. See more here https://refactoring.guru/design-patterns/command

Related