Advertisement
TeT91

ДЗ Зоопарк

Jun 10th, 2024 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CSLight
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Zoo zoo = new Zoo();
  12.             zoo.ShowAnimals();
  13.         }
  14.     }
  15.  
  16.     class Zoo
  17.     {
  18.         private AviaryCreator _aviaryCreator;
  19.         private List<Aviary> _aviaries;
  20.  
  21.         public Zoo()
  22.         {
  23.             _aviaryCreator = new AviaryCreator();
  24.             _aviaries = new List<Aviary>();
  25.  
  26.             InitAviaries();
  27.         }
  28.  
  29.         public void ShowAnimals()
  30.         {
  31.             const string CommandExit = "exit";
  32.  
  33.             bool isRunnig = true;
  34.  
  35.             while (isRunnig)
  36.             {
  37.                 Console.WriteLine($"В зоопарке {_aviaries.Count} вольера. К какому подойти?");
  38.  
  39.                 string userInput = Console.ReadLine();
  40.  
  41.                 if (int.TryParse(userInput, out int result))
  42.                 {
  43.                     if (result >= 0 && result < _aviaries.Count)
  44.                     {
  45.                         _aviaries[result].ShowInfo();
  46.                     }
  47.                     else
  48.                     {
  49.                         Console.WriteLine("Такого вольера нет");
  50.                     }
  51.                 }
  52.                 else if (userInput == CommandExit)
  53.                 {
  54.                     isRunnig = false;
  55.                 }
  56.  
  57.                 Console.ReadKey();
  58.             }
  59.         }
  60.  
  61.         private void InitAviaries()
  62.         {
  63.             int animalTypesCount = 4;
  64.  
  65.             for (int i = 0; i < animalTypesCount; i++)
  66.             {
  67.                 Aviary aviary = _aviaryCreator.Create(i);
  68.  
  69.                 _aviaries.Add(aviary);
  70.             }
  71.         }
  72.     }
  73.  
  74.     class AviaryCreator
  75.     {
  76.         private AnimalCreator _animalCreator;
  77.  
  78.         public AviaryCreator()
  79.         {
  80.             _animalCreator = new AnimalCreator();
  81.         }
  82.  
  83.         public Aviary Create(int id)
  84.         {
  85.             Aviary aviary = new Aviary(GenerateName(id));
  86.  
  87.             for (int i = 0; i < aviary.Capacity; i++)
  88.             {
  89.                 aviary.AddAnimal(_animalCreator.Create(id));
  90.             }
  91.  
  92.             return aviary;
  93.         }
  94.  
  95.         private string GenerateName(int value)
  96.         {
  97.             string name = "Вольер " + value;
  98.             return name;
  99.         }
  100.     }
  101.  
  102.     class AnimalCreator
  103.     {
  104.         public Animal Create(int value)
  105.         {
  106.             Dictionary<string, string> animals = InitAnimalTypes();
  107.  
  108.             string name = animals.ElementAt(value).Value;
  109.             string sound = animals.ElementAt(value).Key;
  110.  
  111.             Animal animal = new Animal(name, sound);
  112.  
  113.             return animal;
  114.         }
  115.  
  116.         private Dictionary<string, string> InitAnimalTypes()
  117.         {
  118.             Dictionary<string, string> animals = new Dictionary<string, string>();
  119.  
  120.             animals.Add("Собака", "Гав");
  121.             animals.Add("Птица", "Чирик");
  122.             animals.Add("Кошка", "Мяу");
  123.             animals.Add("Корова", "Му");
  124.  
  125.             return animals;
  126.         }
  127.     }
  128.  
  129.     class Aviary
  130.     {
  131.         private List<Animal> _animals;
  132.  
  133.         public Aviary(string description)
  134.         {
  135.             _animals = new List<Animal>();
  136.             Capacity = 10;
  137.             Description = description;
  138.         }
  139.  
  140.         public int Capacity { get; private set; }
  141.  
  142.         public string Description { get; private set; }
  143.  
  144.         public void ShowInfo()
  145.         {
  146.             Console.WriteLine(Description);
  147.  
  148.             foreach (Animal animal in _animals)
  149.             {
  150.                 Console.WriteLine($"{animal.Type} - {animal.Gender} - {animal.Sound}");
  151.             }
  152.         }
  153.  
  154.         public void AddAnimal(Animal animal)
  155.         {
  156.             _animals.Add(animal);
  157.         }
  158.     }
  159.  
  160.     class Animal
  161.     {
  162.         public Animal(string type, string sound)
  163.         {
  164.             SelectRandomGender();
  165.             Type = type;
  166.             Sound = sound;
  167.         }
  168.  
  169.         public string Type { get; protected set; }
  170.  
  171.         public Genders Gender { get; protected set; }
  172.  
  173.         public string Sound { get; protected set; }
  174.  
  175.         private void SelectRandomGender()
  176.         {
  177.             int gendersCount = Enum.GetNames(typeof(Genders)).Length;
  178.             int genderId = UserUtils.GenerateRandomNumber(gendersCount);
  179.  
  180.             Gender = (Genders)genderId;
  181.         }
  182.     }
  183.  
  184.     enum Genders
  185.     {
  186.         Male,
  187.         Female
  188.     }
  189.  
  190.     class UserUtils
  191.     {
  192.         private static Random s_random = new Random();
  193.  
  194.         public static int GenerateRandomNumber(int maxValue)
  195.         {
  196.             return s_random.Next(maxValue);
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement