Advertisement
Torgach

Zoo

May 3rd, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.37 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 zoo
  8. {
  9.     class main
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Zoo zoo = new Zoo();
  14.             zoo.RunMenu();
  15.         }
  16.     }
  17.  
  18.     class Zoo
  19.     {
  20.         private List<Aviary> _aviaries;
  21.         private List<Animal> _animals;
  22.  
  23.         public Zoo()
  24.         {
  25.             _aviaries = new List<Aviary>();
  26.  
  27.             Animal bear = new Animal("Медведь", 'М', "Рык");
  28.             Animal wolf = new Animal("Волк", 'М', "Лай");
  29.             Animal tiger = new Animal("Тигр", 'Ж', "Мяу");
  30.  
  31.             _animals = new List<Animal>()
  32.             {
  33.                 bear,
  34.                 wolf,
  35.                 tiger
  36.             };
  37.         }
  38.        
  39.         public void RunMenu()
  40.         {
  41.             bool isRun = true;
  42.             while(isRun)
  43.             {
  44.                 Console.WriteLine("[1] - список вальеров с животными\n" +
  45.                     "[2] - список животных Зоопарка\n" +
  46.                     "[3] - создать вальер\n" +
  47.                     "[4] - добавить животное в вальер\n" +
  48.                     "[5] - убрать животное из вальера\n" +
  49.                     "[6] - добавить животного в зоопарк\n" +
  50.                     "[7] - удалить животного из зоопарка\n" +
  51.                     "[8] - выход");
  52.                 Console.Write("Ввод: ");
  53.  
  54.                 switch (Console.ReadLine())
  55.                 {
  56.                     case "1":
  57.                         ShowAviaries();
  58.                         break;
  59.                     case "2":
  60.                         ShowAnimals();
  61.                         break;
  62.                     case "3":
  63.                         AddAviary();
  64.                         break;
  65.                     case "4":
  66.                         AddAviaryAnimal();
  67.                         break;
  68.                     case "5":
  69.                         RemoveAviaryAnimal();
  70.                         break;
  71.                     case "6":
  72.                         AddAnimal();
  73.                         break;
  74.                     case "7":
  75.                         RemoveAnimal();
  76.                         break;
  77.                     case "8":
  78.                         isRun = false;
  79.                         break;
  80.                     default:
  81.                         Console.WriteLine("Ошибка!");
  82.                         break;
  83.                 }
  84.                 Console.ReadKey();
  85.                 Console.Clear();
  86.             }
  87.         }
  88.  
  89.         private void ShowAviaries()
  90.         {
  91.             if(CheckAviaryCount() == false)
  92.             {
  93.                 return;
  94.             }
  95.  
  96.             foreach (var aviary in _aviaries)
  97.             {
  98.                 Console.WriteLine($"{aviary.Number}:");
  99.                 aviary.ShowAnimals();
  100.             }
  101.         }
  102.  
  103.         private void ShowAnimals()
  104.         {
  105.             if (CheckAnimalsCount() == false)
  106.             {
  107.                 return;
  108.             }
  109.  
  110.             foreach (var animal in _animals)
  111.             {
  112.                 Console.WriteLine($"{animal.Name}; Пол - {animal.Sex}; Звук - {animal.Sound}");
  113.             }
  114.         }
  115.  
  116.         private void AddAviary()
  117.         {
  118.             Aviary aviary = new Aviary(_aviaries.Count + 1);
  119.             _aviaries.Add(aviary);
  120.         }
  121.  
  122.         private void AddAviaryAnimal()
  123.         {
  124.             Console.WriteLine("Укажите номер вальера: ");
  125.             if (IsInputVerified(out int avaryNumber, _aviaries.Count()))
  126.             {
  127.                 Console.WriteLine("Укажите номер животного: ");
  128.                 if (IsInputVerified(out int animalNumber, _animals.Count()))
  129.                 {
  130.                     _aviaries[avaryNumber].AddAnimal(_animals[animalNumber]);
  131.                 }
  132.             }
  133.         }
  134.  
  135.         private void RemoveAviaryAnimal()
  136.         {
  137.             if (CheckAnimalsCount() == false)
  138.             {
  139.                 return;
  140.             }
  141.  
  142.             Console.WriteLine("Укажите номер вальера: ");
  143.             if (IsInputVerified(out int avaryNumber, _aviaries.Count()))
  144.             {
  145.                 if (_aviaries[avaryNumber].DoAnimalsExist() == false)
  146.                 {
  147.                     Console.WriteLine("В вальере нет животных!");
  148.                     return;
  149.                 }
  150.                 Console.WriteLine("Укажите номер животного: ");
  151.                 if (IsInputVerified(out int animalNumber, _animals.Count()))
  152.                 {
  153.                     _aviaries[avaryNumber].RemoveAnimal(_animals[animalNumber]);
  154.                 }
  155.             }
  156.         }
  157.  
  158.         private void AddAnimal()
  159.         {
  160.             Console.Write("Введите имя: ");
  161.             string name = Console.ReadLine();
  162.  
  163.             Console.Write("Введите пол. [М] - Самец, [Ж] - Самка: ");
  164.             char sex = Convert.ToChar(Console.ReadLine());
  165.  
  166.             Console.Write("Какой звук издает животное: ");
  167.             string sound = Console.ReadLine();
  168.  
  169.             Animal animal = new Animal(name, sex, sound);
  170.             _animals.Add(animal);
  171.         }
  172.  
  173.         private void RemoveAnimal()
  174.         {
  175.             if(CheckAnimalsCount() == false)
  176.             {
  177.                 return;
  178.             }
  179.  
  180.             Console.WriteLine("Номер животного, которого нужно вычеркнуть из зоопарка: ");
  181.             if (IsInputVerified(out int number, _animals.Count))
  182.             {
  183.                 _animals.RemoveAt(number);
  184.             }
  185.         }
  186.  
  187.         private bool IsInputVerified(out int userInput, int count)
  188.         {
  189.             if (int.TryParse(Console.ReadLine(), out userInput) && userInput <= count && userInput > 0)
  190.             {
  191.                 --userInput;
  192.                 return true;
  193.             }
  194.             Console.WriteLine("Ошибка, такого номера нет!");
  195.             return false;
  196.         }
  197.  
  198.         private bool CheckAnimalsCount()
  199.         {
  200.             if(_animals.Count == 0)
  201.             {
  202.                 Console.WriteLine("В зоопарке нет животных!");
  203.                 return false;
  204.             }
  205.             return true;
  206.         }
  207.  
  208.         private bool CheckAviaryCount()
  209.         {
  210.             if (_aviaries.Count == 0)
  211.             {
  212.                 Console.WriteLine("Вальеры не созданы!");
  213.                 return false;
  214.             }
  215.             return true;
  216.         }
  217.     }
  218.  
  219.     class Aviary
  220.     {
  221.         private List<Animal> _animals;
  222.  
  223.         public int Number { get; private set; }
  224.  
  225.         public Aviary(int aviaryNumber)
  226.         {
  227.             _animals = new List<Animal>();
  228.             Number = aviaryNumber;
  229.         }
  230.  
  231.         public void ShowAnimals()
  232.         {
  233.             if(_animals.Count == 0)
  234.             {
  235.                 Console.WriteLine("В вальере нет животных!");
  236.                 return;
  237.             }
  238.  
  239.             Console.WriteLine("Список животных: ");
  240.  
  241.             foreach (var animal in _animals)
  242.             {
  243.                 Console.WriteLine($"{animal.Name}; Пол - {animal.Sex}; Звук - {animal.Sound}");
  244.             }
  245.         }
  246.  
  247.         public void AddAnimal(Animal animal)
  248.         {
  249.             _animals.Add(animal);
  250.         }
  251.  
  252.         public void RemoveAnimal(Animal animal)
  253.         {
  254.             _animals.Remove(animal);
  255.         }
  256.  
  257.         public bool DoAnimalsExist()
  258.         {
  259.             return _animals.Count > 0;
  260.         }
  261.     }
  262.  
  263.     class Animal
  264.     {
  265.         public string Male = "Самец";
  266.         public string Female = "Самка";
  267.  
  268.         public string Name { get; private set; }
  269.         public string Sound { get; private set; }
  270.         public string Sex { get; private set; }
  271.  
  272.         public Animal(string name, char sex, string sound)
  273.         {
  274.             Name = name;
  275.             Sound = sound;
  276.  
  277.             if(sex == 'Ж')
  278.             {
  279.                 Sex = Female;
  280.             }
  281.             else
  282.             {
  283.                 Sex = Male;
  284.             }
  285.         }
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement