Advertisement
VodVas

Зоопарк

Dec 11th, 2023 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | Software | 0 0
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Data;
  5.  
  6. namespace Зоопарк
  7. {
  8.     internal class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Zoo zoo = new Zoo();
  13.  
  14.             zoo.Work();
  15.         }
  16.     }
  17.  
  18.     class Zoo
  19.     {
  20.         private List<Aviary> _aviaries = new List<Aviary>();
  21.  
  22.         public Zoo()
  23.         {
  24.             _aviaries.Add(new Aviary("Лев", "Рык"));
  25.             _aviaries.Add(new Aviary("Волк", "Вой"));
  26.             _aviaries.Add(new Aviary("Обезьяна", "Крик"));
  27.             _aviaries.Add(new Aviary("Слон", "Рёв"));
  28.         }
  29.  
  30.         public void Work()
  31.         {
  32.             bool isOpen = true;
  33.  
  34.             while (isOpen)
  35.             {
  36.                 Console.ForegroundColor = ConsoleColor.Green;
  37.                 Console.WriteLine("Зоопарк «Лимпопо»\n\n");
  38.                 Console.ForegroundColor = ConsoleColor.White;
  39.  
  40.                 Console.WriteLine("Доступные вольеры с животными:");
  41.  
  42.                 for (int i = 0; i < _aviaries.Count; i++)
  43.                 {
  44.                     Console.WriteLine($"{i + 1} - {_aviaries[i].Name}");
  45.                 }
  46.  
  47.                 Console.WriteLine("\nВыбери вольер:");
  48.  
  49.                 int userInput = (Utility.ReturnInputNumber() - 1);
  50.  
  51.                 if (userInput < 0 || userInput >= _aviaries.Count)
  52.                 {
  53.                     Console.ForegroundColor = ConsoleColor.Red;
  54.                     Console.WriteLine("\nНет такой команды\n");
  55.                     Console.ForegroundColor = ConsoleColor.White;
  56.                     Console.WriteLine("Нажми любую клавишу...");
  57.                     Console.ReadKey();
  58.                     Console.Clear();
  59.                 }
  60.                 else
  61.                 {
  62.                     Console.ForegroundColor = ConsoleColor.DarkYellow;
  63.                     Console.WriteLine("\nЖивотные в вольере:\n");
  64.                     Console.ForegroundColor = ConsoleColor.White;
  65.  
  66.                     _aviaries[userInput].ShowAnimal();
  67.  
  68.                     Console.ForegroundColor = ConsoleColor.DarkGreen;
  69.                     Console.WriteLine("\nНажми любую клавишу, что бы выбрать вольер");
  70.                     Console.ForegroundColor = ConsoleColor.White;
  71.                     Console.ReadKey();
  72.                     Console.Clear();
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     class Aviary
  79.     {
  80.         private List<Animal> _animals = new List<Animal>();
  81.  
  82.         public Aviary(string genus, string sound)
  83.         {
  84.             FillAviary(genus, sound);
  85.         }
  86.  
  87.         public string Name => _animals.First().Genus;
  88.  
  89.         public void ShowAnimal()
  90.         {
  91.             foreach (var animal in _animals)
  92.             {
  93.                 Console.WriteLine($"Животное - {animal.Genus} || Издает звук - {animal.Sound} || Пол - {animal.Gender}");
  94.             }
  95.         }
  96.  
  97.         private void FillAviary(string genus, string sound)
  98.         {
  99.             CreateAnimals(genus, sound);
  100.         }
  101.  
  102.         private void CreateAnimals(string genus, string sound)
  103.         {
  104.             int minAnimalsInAviary = 1;
  105.             int maxAnimalsInAviary = 4;
  106.  
  107.             int quantityAnimals = Utility.GenerateRandomNumber(minAnimalsInAviary, maxAnimalsInAviary + 1);
  108.  
  109.             for (int i = 0; i < quantityAnimals; i++)
  110.             {
  111.                 _animals.Add(new Animal(genus, sound));
  112.             }
  113.         }
  114.     }
  115.  
  116.     class Animal
  117.     {
  118.         public Animal(string genus, string sound)
  119.         {
  120.             Genus = genus;
  121.             Gender = GenerateGender();
  122.             Sound = sound;
  123.         }
  124.  
  125.         public string Genus { get; private set; }
  126.         public string Gender { get; private set; }
  127.         public string Sound { get; private set; }
  128.  
  129.         private string GenerateGender()
  130.         {
  131.             string[] genders = { "male", "female" };
  132.  
  133.             int randomNumber = Utility.GenerateRandomNumber(genders.Length);
  134.  
  135.             return genders[randomNumber];
  136.         }
  137.     }
  138.  
  139.     class Utility
  140.     {
  141.         private static Random s_random = new Random();
  142.  
  143.         public static int GenerateRandomNumber(int min, int max)
  144.         {
  145.             return s_random.Next(min, max);
  146.         }
  147.  
  148.         public static int GenerateRandomNumber(int number)
  149.         {
  150.             return s_random.Next(number);
  151.         }
  152.  
  153.         public static int ReturnInputNumber()
  154.         {
  155.             int number;
  156.  
  157.             while (int.TryParse(Console.ReadLine(), out number) == false)
  158.             {
  159.                 Console.WriteLine("Введено не число, попробуйте еще раз: ");
  160.             }
  161.  
  162.             return number;
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement