Advertisement
RedFlys

Home work - zoo

Jan 18th, 2022
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Zoo zoo = new Zoo();
  14.             zoo.Work();
  15.         }
  16.     }
  17.  
  18.     class Zoo
  19.     {
  20.         private List<Aviary> _aviares;
  21.  
  22.         public Zoo()
  23.         {
  24.             _aviares = new List<Aviary>();
  25.             FillAviares();
  26.         }
  27.  
  28.         public void Work()
  29.         {
  30.             bool isWorking = true;
  31.             string userInput;
  32.  
  33.             while (isWorking == true)
  34.             {
  35.                 Console.WriteLine($"В зоопарке {_aviares.Count} вольеров.");
  36.  
  37.                 for (int i = 0; i < _aviares.Count; i++)
  38.                 {
  39.                     Console.WriteLine($"{i + 1}. {_aviares[i].GetName()}.");
  40.                 }
  41.  
  42.                 Console.WriteLine($"{_aviares.Count + 1}. Выход.");
  43.                 Console.Write("Выберите, к какому хотите подойти: ");
  44.                 userInput = Console.ReadLine();
  45.                 Console.Clear();
  46.  
  47.                 if (Int32.TryParse(userInput, out int index))
  48.                 {
  49.                     if (index > 0 && index <= _aviares.Count)
  50.                     {
  51.                         _aviares[index - 1].ShowInfo();
  52.                     }
  53.                     else if (index == _aviares.Count + 1)
  54.                     {
  55.                         isWorking = false;
  56.  
  57.                         Console.WriteLine("Будем рады увидеть вас снова.");
  58.                     }
  59.                     else
  60.                     {
  61.                         Console.WriteLine("Нет такого вольера.");
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     Console.WriteLine("Вы ввели не число.");
  67.                 }
  68.  
  69.                 Console.WriteLine("Нажмите любую клавишу.");
  70.                 Console.ReadKey();
  71.                 Console.Clear();
  72.             }
  73.         }
  74.  
  75.         private void FillAviares()
  76.         {
  77.             int countAnimal = Enum.GetNames(typeof(TypeAnimal)).Length;
  78.  
  79.             for (int indexAnimal = 1; indexAnimal <= countAnimal; indexAnimal++)
  80.             {
  81.                 _aviares.Add(new Aviary(indexAnimal));
  82.             }
  83.         }
  84.     }
  85.  
  86.     class Aviary
  87.     {
  88.         private List<Animal> _animals;
  89.         private int _maxCountAnimal;
  90.         private int _indexAnimal;
  91.  
  92.         public Aviary(int indexAnimal)
  93.         {
  94.             _maxCountAnimal = 5;
  95.             _indexAnimal = indexAnimal;
  96.             _animals = new List<Animal>();
  97.  
  98.             Fill();
  99.         }
  100.  
  101.         public string GetName()
  102.         {
  103.             return "Тут " + (TypeAnimal)_indexAnimal;
  104.         }
  105.  
  106.         public void ShowInfo()
  107.         {
  108.             int index = 1;
  109.  
  110.             Console.WriteLine($"В вольере {_maxCountAnimal} животных.\n");
  111.  
  112.             foreach(Animal animal in _animals)
  113.             {
  114.                 Console.Write(index + ". ");
  115.                 animal.ShowInfo();
  116.  
  117.                 index++;
  118.             }
  119.         }
  120.  
  121.         private void Fill()
  122.         {
  123.             for (int i = 0; i < _maxCountAnimal; i++)
  124.             {
  125.                 _animals.Add(new Animal(_indexAnimal));
  126.             }
  127.         }
  128.     }
  129.  
  130.     class Animal
  131.     {
  132.         private static readonly Random _random;
  133.         private TypeAnimal _type;
  134.         private SoundAnimal _sound;
  135.         private bool _isMan;
  136.  
  137.         static Animal()
  138.         {
  139.             _random = new Random();
  140.         }
  141.  
  142.         public Animal(int indexAnimal)
  143.         {
  144.             int maxCountSex = 2;
  145.  
  146.             _type = (TypeAnimal)indexAnimal;
  147.             _sound = (SoundAnimal)indexAnimal;
  148.             _isMan = Convert.ToBoolean(_random.Next(maxCountSex));
  149.         }
  150.  
  151.         public string TypeName => Convert.ToString(_type);
  152.         private string _sex => _isMan ? "Мужской" : "Женский";
  153.  
  154.         public void ShowInfo()
  155.         {
  156.             Console.WriteLine($"{_type} говорит \"{_sound}\". Пол: {_sex}.");
  157.         }
  158.     }
  159.  
  160.     enum TypeAnimal
  161.     {
  162.         Лев = 1,
  163.         Гиена,
  164.         Медведь,
  165.         Лиса
  166.     }
  167.  
  168.     enum SoundAnimal
  169.     {
  170.         Мяу = 1,
  171.         Хыхыхы,
  172.         Сгущёночка,
  173.         Фыр
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement