NikaBang

ДЗ: Зоопарк

Apr 1st, 2026 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.47 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. internal class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         ZooManager zooManager = new ZooManager();
  9.         zooManager.Run();
  10.     }
  11. }
  12.  
  13. public static class UserUtils
  14. {
  15.     private static Random s_random = new Random();
  16.  
  17.     public static int GenerateRandomNumber(int min, int max)
  18.     {
  19.         return s_random.Next(min, max + 1);
  20.     }
  21.  
  22.     public static int ReadInt()
  23.     {
  24.         int number = 0;
  25.         bool isParsed = false;
  26.  
  27.         while (isParsed == false)
  28.         {
  29.             Console.Write("Ввод: ");
  30.             string input = Console.ReadLine();
  31.             isParsed = int.TryParse(input, out number);
  32.  
  33.             if (isParsed == false)
  34.             {
  35.                 Console.WriteLine("Ошибка! Нужно ввести число.");
  36.             }
  37.         }
  38.  
  39.         return number;
  40.     }
  41. }
  42.  
  43. public enum Gender
  44. {
  45.     Male,
  46.     Female
  47. }
  48.  
  49. public class AnimalType
  50. {
  51.     public AnimalType(string species, string sound)
  52.     {
  53.         Species = species;
  54.         Sound = sound;
  55.     }
  56.  
  57.     public string Species { get; private set; }
  58.     public string Sound { get; private set; }
  59. }
  60.  
  61. public class Animal
  62. {
  63.     public Animal(AnimalType type, Gender gender)
  64.     {
  65.         Type = type;
  66.         Gender = gender;
  67.     }
  68.  
  69.     public AnimalType Type { get; private set; }
  70.     public Gender Gender { get; private set; }
  71. }
  72.  
  73. public class Aviary
  74. {
  75.     private List<Animal> _animals;
  76.  
  77.     public Aviary(AnimalType animalType, int animalCount)
  78.     {
  79.         AnimalType = animalType;
  80.         _animals = new List<Animal>();
  81.  
  82.         FillAnimals(animalCount);
  83.     }
  84.  
  85.     public AnimalType AnimalType { get; private set; }
  86.  
  87.     public int AnimalCount
  88.     {
  89.         get
  90.         {
  91.             return _animals.Count;
  92.         }
  93.     }
  94.  
  95.     public void ShowInfo()
  96.     {
  97.         Console.WriteLine(
  98.             $"В вольере живут {AnimalType.Species}. " +
  99.             $"Самцов - {GetMaleCount()}, самок - {GetFemaleCount()}. " +
  100.             $"Они {AnimalType.Sound}.");
  101.     }
  102.  
  103.     private void FillAnimals(int animalCount)
  104.     {
  105.         for (int i = 0; i < animalCount; i++)
  106.         {
  107.             _animals.Add(CreateRandomAnimal());
  108.         }
  109.     }
  110.  
  111.     private Animal CreateRandomAnimal()
  112.     {
  113.         Gender gender;
  114.  
  115.         if (UserUtils.GenerateRandomNumber(0, 1) == 0)
  116.         {
  117.             gender = Gender.Male;
  118.         }
  119.         else
  120.         {
  121.             gender = Gender.Female;
  122.         }
  123.  
  124.         return new Animal(AnimalType, gender);
  125.     }
  126.  
  127.     private int GetMaleCount()
  128.     {
  129.         int count = 0;
  130.  
  131.         foreach (Animal animal in _animals)
  132.         {
  133.             if (animal.Gender == Gender.Male)
  134.             {
  135.                 count++;
  136.             }
  137.         }
  138.  
  139.         return count;
  140.     }
  141.  
  142.     private int GetFemaleCount()
  143.     {
  144.         int count = 0;
  145.  
  146.         foreach (Animal animal in _animals)
  147.         {
  148.             if (animal.Gender == Gender.Female)
  149.             {
  150.                 count++;
  151.             }
  152.         }
  153.  
  154.         return count;
  155.     }
  156. }
  157.  
  158. public class Zoo
  159. {
  160.     private List<Aviary> _aviaries;
  161.  
  162.     public Zoo()
  163.     {
  164.         _aviaries = new List<Aviary>();
  165.         FillAviaries();
  166.     }
  167.  
  168.     public int AviaryCount
  169.     {
  170.         get
  171.         {
  172.             return _aviaries.Count;
  173.         }
  174.     }
  175.  
  176.     public void ShowAviaryInfo(int index)
  177.     {
  178.         _aviaries[index].ShowInfo();
  179.     }
  180.  
  181.     private void FillAviaries()
  182.     {
  183.         AnimalType parrots = new AnimalType("Попугаи", "чирикают");
  184.         AnimalType elephants = new AnimalType("Слоны", "трубят");
  185.         AnimalType tigers = new AnimalType("Тигры", "рычат");
  186.         AnimalType horses = new AnimalType("Лошади", "фыркают");
  187.         AnimalType ducks = new AnimalType("Утки", "крякают");
  188.  
  189.         _aviaries.Add(new Aviary(parrots, 7));
  190.         _aviaries.Add(new Aviary(elephants, 10));
  191.         _aviaries.Add(new Aviary(tigers, 5));
  192.         _aviaries.Add(new Aviary(horses, 4));
  193.         _aviaries.Add(new Aviary(ducks, 15));
  194.     }
  195. }
  196.  
  197. public class ZooManager
  198. {
  199.     private const int CommandExit = 0;
  200.     private Zoo _zoo;
  201.  
  202.     public ZooManager()
  203.     {
  204.         _zoo = new Zoo();
  205.     }
  206.  
  207.     public void Run()
  208.     {
  209.         bool isInZoo = true;
  210.  
  211.         while (isInZoo)
  212.         {
  213.             ShowMenu();
  214.             int userInput = UserUtils.ReadInt();
  215.  
  216.             if (userInput == CommandExit)
  217.             {
  218.                 isInZoo = false;
  219.             }
  220.             else if (userInput >= 1 && userInput <= _zoo.AviaryCount)
  221.             {
  222.                 OpenAviary(userInput - 1);
  223.             }
  224.             else
  225.             {
  226.                 Console.WriteLine("Неверный номер вольера.");
  227.                 Console.ReadKey();
  228.             }
  229.         }
  230.     }
  231.  
  232.     private void ShowMenu()
  233.     {
  234.         Console.Clear();
  235.         Console.WriteLine("****** ЗООПАРК ******");
  236.         Console.WriteLine($"Перед тобой {_zoo.AviaryCount} вольеров.");
  237.         Console.WriteLine("Введи номер вольера, который хочешь посмотреть.");
  238.         Console.WriteLine($"{CommandExit} - выйти из зоопарка.");
  239.     }
  240.  
  241.     private void OpenAviary(int index)
  242.     {
  243.         Console.Clear();
  244.         _zoo.ShowAviaryInfo(index);
  245.         Console.ReadKey();
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment