Advertisement
Rodunskiy

Untitled

Aug 15th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Zoo zoo = new Zoo();
  9.  
  10.         bool isWorking = true;
  11.  
  12.         while (isWorking)
  13.         {
  14.             zoo.Work();
  15.         }
  16.     }
  17. }
  18.  
  19. class Zoo
  20. {
  21.     private List<Aviary> _aviarys = new List<Aviary>();
  22.  
  23.     public Zoo()
  24.     {
  25.         Fill();
  26.     }
  27.  
  28.     public void Fill()
  29.     {
  30.         _aviarys.Add(new Aviary(new Monkey(), "Обезьяны", 1));
  31.         _aviarys.Add(new Aviary(new Elephant(), "Слоны", 2));
  32.         _aviarys.Add(new Aviary(new Giraffe(), "Жирафы", 3));
  33.         _aviarys.Add(new Aviary(new Bird(), "Птицы", 4));
  34.     }
  35.  
  36.     public void Work()
  37.     {
  38.         Console.WriteLine("Добро пожаловать в зоопарк! К какому вальеру вы хотели бы подойти?");
  39.  
  40.         ShowInfo();
  41.  
  42.         int userInput = Utils.ConvertToInt();
  43.  
  44.         Console.Clear();
  45.  
  46.         for (int i = 0; i < _aviarys.Count; i++)
  47.         {
  48.             _aviarys[i].ShowAviary();
  49.         }
  50.  
  51.         foreach (var aviary in _aviarys)
  52.         {
  53.             aviary.ShowAviary();
  54.         }
  55.  
  56.         Console.ReadKey();
  57.     }
  58.  
  59.     public void ShowInfo()
  60.     {
  61.         foreach (var valliere in _aviarys)
  62.         {
  63.             Console.WriteLine($"{valliere.IdentificationNumber}){valliere.Name}");
  64.         }
  65.     }
  66. }
  67.  
  68. class Aviary
  69. {
  70.     private List<Animal> _animals = new List<Animal>();
  71.  
  72.     public Aviary(Animal animal, string name, int identificationNumber)
  73.     {
  74.         IdentificationNumber = identificationNumber;
  75.         Name = name;
  76.         AddToAviary(animal);
  77.     }
  78.  
  79.     public int IdentificationNumber { get; private set; }
  80.     public string Name { get; private set; }
  81.  
  82.     public void ShowAviary()
  83.     {
  84.         for (int i = 0; i < _animals.Count; i++)
  85.         {
  86.             Console.WriteLine($"{_animals[i].Name}|{_animals[i].Gender}|{_animals[i].Sound}");
  87.         }
  88.  
  89.         Console.ReadKey();
  90.     }
  91.  
  92.     public void AddToAviary(Animal animal)
  93.     {
  94.         int maxPlaces = 5;
  95.         for (int i = 0; i < maxPlaces; i++)
  96.         {
  97.             _animals.Add(animal.Clone());
  98.         }
  99.     }
  100. }
  101.  
  102. class Animal
  103. {
  104.     private static Random _random = new Random();
  105.  
  106.     public Animal(string name, string sound)
  107.     {
  108.         Name = name;
  109.         Sound = sound;
  110.         Gender = GetRandomGender();
  111.     }
  112.  
  113.     public string Name { get; private set; }
  114.     public string Gender { get; private set; }
  115.     public string Sound { get; private set; }
  116.  
  117.     public virtual Animal Clone()
  118.     {
  119.         return new Animal(Name, Sound);
  120.     }
  121.  
  122.     private string GetRandomGender()
  123.     {
  124.         string[] genders = { "Женский", "Мужской" };
  125.  
  126.         int randomIndex = _random.Next(genders.Length);
  127.         return genders[randomIndex];
  128.     }
  129. }
  130.  
  131. class Monkey : Animal
  132. {
  133.     public override Animal Clone()
  134.     {
  135.         return new Monkey();
  136.     }
  137.  
  138.     public Monkey() : base("Обезьяна", "Ухахахах уа") { }
  139. }
  140.  
  141. class Elephant : Animal
  142. {
  143.     public override Animal Clone()
  144.     {
  145.         return new Elephant();
  146.     }
  147.  
  148.     public Elephant() : base("Слон", "Трубит") { }
  149. }
  150.  
  151. class Giraffe : Animal
  152. {
  153.     public override Animal Clone()
  154.     {
  155.         return new Giraffe();
  156.     }
  157.  
  158.     public Giraffe() : base("Жираф", "Молчит") { }
  159. }
  160.  
  161. class Bird : Animal
  162. {
  163.     public override Animal Clone()
  164.     {
  165.         return new Bird();
  166.     }
  167.  
  168.     public Bird() : base("Птица", "Курлык") { }
  169. }
  170.  
  171. class Utils
  172. {
  173.     public static int ConvertToInt()
  174.     {
  175.         int templateNumber;
  176.         string userInput = string.Empty;
  177.  
  178.         while (int.TryParse(userInput, out templateNumber) == false)
  179.         {
  180.             userInput = Console.ReadLine();
  181.         }
  182.  
  183.         return templateNumber;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement