Advertisement
Vlad_Savitskiy

Zoo

Jul 3rd, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLightFirst
  5. {
  6.     class Program
  7.     {
  8.         private static void Main()
  9.         {
  10.             Zoo zoo = new Zoo(GenerateAviaries());
  11.  
  12.             while (true)
  13.             {
  14.                 Console.Clear();
  15.                 Console.WriteLine("Выберите, к какому вольеру вы хотели бы подойти: ");
  16.  
  17.                 for (int i = 0; i < zoo.AviariesCount; i++)
  18.                     Console.WriteLine($"  {i + 1} - {zoo.GetAviary(i).Name}");
  19.  
  20.                 Console.Write("\nВаш ответ: ");
  21.  
  22.                 if (int.TryParse(Console.ReadLine(), out int aviaryIndex) && aviaryIndex > 0 &&
  23.                     aviaryIndex <= zoo.AviariesCount)
  24.                 {
  25.                     Console.Clear();
  26.                     zoo.GetAviary(aviaryIndex - 1).ShowInfo();
  27.                     Console.WriteLine("Нажмите любую клавишу, чтобы завершить просмотр вольера...");
  28.                 }
  29.                 else
  30.                     Console.WriteLine("Введен неверный номер вольера, нажмите любую клавишу, чтобы попробовать ещё раз...");
  31.  
  32.                 Console.ReadKey();
  33.             }
  34.         }
  35.  
  36.         private static List<Aviary> GenerateAviaries()
  37.         {
  38.             List<Aviary> aviaries = new List<Aviary>(6);
  39.  
  40.             aviaries.Add(new Aviary("Вольер с тиграми", GenerateAnimals("Тигр")));
  41.             aviaries.Add(new Aviary("Вольер со львами", GenerateAnimals("Лев")));
  42.             aviaries.Add(new Aviary("Вольер с пингвинами", GenerateAnimals("Пингвин")));
  43.             aviaries.Add(new Aviary("Клетка с совами", GenerateAnimals("Сова")));
  44.             aviaries.Add(new Aviary("Клетка со змеями", GenerateAnimals("Змея")));
  45.             aviaries.Add(new Aviary("Вольер с лисами", GenerateAnimals("Лиса")));
  46.  
  47.             return aviaries;
  48.         }
  49.  
  50.         private static List<Animal> GenerateAnimals(string animalName)
  51.         {
  52.             Random rand = new Random();
  53.             int animalCount = rand.Next(2, 6);
  54.             List<Animal> animals = new List<Animal>(animalCount);
  55.  
  56.             switch (animalName)
  57.             {
  58.                 case "Тигр":
  59.                     for (int i = 0; i < animalCount; i++)
  60.                         animals.Add(new Tiger((AnimalGender)rand.Next(0, 2)));
  61.                     break;
  62.                 case "Лев":
  63.                     for (int i = 0; i < animalCount; i++)
  64.                         animals.Add(new Lion((AnimalGender)rand.Next(0, 2)));
  65.                     break;
  66.                 case "Пингвин":
  67.                     for (int i = 0; i < animalCount; i++)
  68.                         animals.Add(new Penguin((AnimalGender)rand.Next(0, 2)));
  69.                     break;
  70.                 case "Сова":
  71.                     for (int i = 0; i < animalCount; i++)
  72.                         animals.Add(new Owl((AnimalGender)rand.Next(0, 2)));
  73.                     break;
  74.                 case "Змея":
  75.                     for (int i = 0; i < animalCount; i++)
  76.                         animals.Add(new Snake((AnimalGender)rand.Next(0, 2)));
  77.                     break;
  78.                 case "Лиса":
  79.                     for (int i = 0; i < animalCount; i++)
  80.                         animals.Add(new Fox((AnimalGender)rand.Next(0, 2)));
  81.                     break;
  82.                 default:
  83.                     return null;
  84.             }
  85.  
  86.             return animals;
  87.         }
  88.     }
  89.  
  90.     class Zoo
  91.     {
  92.         public int AviariesCount => _aviaries.Count;
  93.  
  94.         private List<Aviary> _aviaries;
  95.  
  96.         public Zoo(List<Aviary> aviaries)
  97.         {
  98.             _aviaries = aviaries;
  99.         }
  100.  
  101.         public Aviary GetAviary(int index)
  102.         {
  103.             if (index < 0 || index >= AviariesCount)
  104.                 throw new ArgumentException();
  105.  
  106.             return _aviaries[index];
  107.         }
  108.     }
  109.  
  110.     class Aviary
  111.     {
  112.         public string Name { get; }
  113.         public int AnimalsCount => _animals.Count;
  114.         private List<Animal> _animals;
  115.  
  116.         public Aviary(string name, List<Animal> animals)
  117.         {
  118.             Name = name;
  119.             _animals = animals;
  120.         }
  121.  
  122.         public void ShowInfo()
  123.         {
  124.             Console.WriteLine($"Перед вами {Name.ToLower()}\nЖивотных в вольере: {AnimalsCount}\n");
  125.  
  126.             foreach (Animal animal in _animals)
  127.                 animal.ShowInfo();
  128.         }
  129.     }
  130.  
  131.     abstract class Animal
  132.     {
  133.         public string Name { get; protected set; }
  134.         public AnimalSound Sound { get; protected set; }
  135.         public AnimalGender Gender { get; }
  136.  
  137.         protected Animal(AnimalGender gender)
  138.         {
  139.             Gender = gender;
  140.         }
  141.  
  142.         public void ShowInfo()
  143.         {
  144.             Console.WriteLine($"  Животное: {Name}\n" +
  145.                               $"  Издаваемый звук: {Sound}\n" +
  146.                               $"  Пол: {Gender}\n");
  147.         }
  148.     }
  149.  
  150.     class Tiger : Animal
  151.     {
  152.         public Tiger(AnimalGender gender) : base(gender)
  153.         {
  154.             Name = "Тигр";
  155.             Sound = AnimalSound.Рёв;
  156.         }
  157.     }
  158.  
  159.     class Lion : Animal
  160.     {
  161.         public Lion(AnimalGender gender) : base(gender)
  162.         {
  163.             Name = "Лев";
  164.             Sound = AnimalSound.Рёв;
  165.         }
  166.     }
  167.  
  168.     class Penguin : Animal
  169.     {
  170.         public Penguin(AnimalGender gender) : base(gender)
  171.         {
  172.             Name = "Пингвин";
  173.             Sound = AnimalSound.Писк;
  174.         }
  175.     }
  176.  
  177.     class Owl : Animal
  178.     {
  179.         public Owl(AnimalGender gender) : base(gender)
  180.         {
  181.             Name = "Сова";
  182.             Sound = AnimalSound.Уханье;
  183.         }
  184.     }
  185.  
  186.     class Snake : Animal
  187.     {
  188.         public Snake(AnimalGender gender) : base(gender)
  189.         {
  190.             Name = "Змея";
  191.             Sound = AnimalSound.Шипение;
  192.         }
  193.     }
  194.  
  195.     class Fox : Animal
  196.     {
  197.         public Fox(AnimalGender gender) : base(gender)
  198.         {
  199.             Name = "Лиса";
  200.             Sound = AnimalSound.Крик;
  201.         }
  202.     }
  203.  
  204.     enum AnimalSound
  205.     {
  206.         Рёв,
  207.         Писк,
  208.         Уханье,
  209.         Шипение,
  210.         Крик
  211.     }
  212.  
  213.     enum AnimalGender
  214.     {
  215.         М,
  216.         Ж
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement