Tohir5258

OOP_12

Nov 12th, 2025 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace OOP_12
  5. {
  6.     public enum Sex
  7.     {
  8.         Male,
  9.         Female
  10.     }
  11.  
  12.     internal class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             ZooManager manager = new ZooManager();
  17.             Zoo zoo = manager.CreateZoo();
  18.             zoo.Run();
  19.         }
  20.     }
  21.  
  22.     public abstract class Animal
  23.     {
  24.         protected Animal(string name, Sex sex, string species)
  25.         {
  26.             Name = name;
  27.             Sex = sex;
  28.             Species = species;
  29.         }
  30.  
  31.         public string Name { get; }
  32.         public Sex Sex { get; }
  33.         public string Species { get; }
  34.        
  35.         public abstract string MakeSound();
  36.  
  37.         public override string ToString() => $"{Species} \"{Name}\" ({Sex})";
  38.     }
  39.  
  40.     public interface IEnclosure
  41.     {
  42.         string Name { get; }
  43.         int TotalAnimals { get; }
  44.         void ShowInfo();
  45.     }
  46.  
  47.     public class Lion : Animal
  48.     {
  49.         public Lion(string name, Sex sex) : base(name, sex, "Lion") { }
  50.         public override string MakeSound() => "Roar";
  51.     }
  52.  
  53.     public class Parrot : Animal
  54.     {
  55.         public Parrot(string name, Sex sex) : base(name, sex, "Parrot") { }
  56.         public override string MakeSound() => "Squawk";
  57.     }
  58.  
  59.     public class Elephant : Animal
  60.     {
  61.         public Elephant(string name, Sex sex) : base(name, sex, "Elephant") { }
  62.         public override string MakeSound() => "Trumpet";
  63.     }
  64.  
  65.     public class Wolf : Animal
  66.     {
  67.         public Wolf(string name, Sex sex) : base(name, sex, "Wolf") { }
  68.         public override string MakeSound() => "Howl";
  69.     }
  70.  
  71.     public class Penguin : Animal
  72.     {
  73.         public Penguin(string name, Sex sex) : base(name, sex, "Penguin") { }
  74.         public override string MakeSound() => "Honk";
  75.     }
  76.  
  77.     public class Enclosure<T> : IEnclosure where T : Animal
  78.     {
  79.         private readonly List<T> _animals = new List<T>();
  80.  
  81.         public Enclosure(string name, string description)
  82.         {
  83.             Name = name;
  84.             Description = description;
  85.         }
  86.  
  87.         public string Name { get; }
  88.         public string Description { get; }
  89.         public IReadOnlyList<T> Animals => _animals.AsReadOnly();
  90.         public int TotalAnimals => _animals.Count;
  91.  
  92.         public void AddAnimal(T animal)
  93.         {
  94.             _animals.Add(animal);
  95.         }
  96.  
  97.         public int CountBySex(Sex sex)
  98.         {
  99.             int count = 0;
  100.  
  101.             foreach (Animal animal in _animals)
  102.             {
  103.                 if (animal.Sex == sex)
  104.                 {
  105.                     count++;
  106.                 }
  107.             }
  108.  
  109.             return count;
  110.         }
  111.  
  112.         public List<string> DistinctSounds()
  113.         {
  114.             List<string> sounds = new List<string>();
  115.  
  116.             foreach (Animal animal in _animals)
  117.             {
  118.                 String sound = animal.MakeSound();
  119.  
  120.                 if (sounds.Contains(sound)== false)
  121.                 {
  122.                     sounds.Add(sound);
  123.                 }
  124.             }
  125.  
  126.             return sounds;
  127.         }
  128.  
  129.         public void ShowInfo()
  130.         {
  131.             const int LineWidth = 50;
  132.  
  133.             Console.WriteLine(new string('-', LineWidth));
  134.             Console.WriteLine($"Вольер: {Name}");
  135.             Console.WriteLine($"Описание: {Description}");
  136.             Console.WriteLine($"Животных: {TotalAnimals} (♂ {CountBySex(Sex.Male)}, ♀ {CountBySex(Sex.Female)})");
  137.  
  138.             List<string> sounds = DistinctSounds();
  139.             Console.WriteLine(sounds.Count > 0 ?
  140.                 "Звуки: " + string.Join(", ", sounds) :
  141.                 "Звуков нет.");
  142.  
  143.             Console.WriteLine("\nЖивотные:");
  144.  
  145.             foreach (Animal animal in _animals)
  146.             {
  147.                 Console.WriteLine($"- {animal} — звук: {animal.MakeSound()}");
  148.             }
  149.  
  150.             Console.WriteLine(new string('-', LineWidth));
  151.         }
  152.     }
  153.  
  154.     public class Zoo
  155.     {
  156.         private readonly List<IEnclosure> _enclosures;
  157.  
  158.         public Zoo(List<IEnclosure> enclosures)
  159.         {
  160.             _enclosures = enclosures;
  161.         }
  162.  
  163.         public void Run()
  164.         {
  165.             const int ExitCommand = 0;
  166.             const int StartEnclosureCommand = 1;
  167.  
  168.             bool isRunning = true;
  169.  
  170.             Console.Clear();
  171.             Console.WriteLine("Добро пожаловать в городской зоопарк!\n");
  172.  
  173.             while (isRunning)
  174.             {
  175.                 Console.WriteLine("=== Меню зоопарка ===");
  176.  
  177.                 for (int i = 0; i < _enclosures.Count; i++)
  178.                 {
  179.                     Console.WriteLine($"{i + 1}. {_enclosures[i].Name} ({_enclosures[i].TotalAnimals} животных)");
  180.                 }
  181.  
  182.                 Console.WriteLine($"{ExitCommand}. Выход\n");
  183.                 Console.Write("Выберите номер вольера: ");
  184.  
  185.                 string input = Console.ReadLine();
  186.  
  187.                 if (int.TryParse(input, out int choice) == false)
  188.                 {
  189.                     Console.WriteLine("Ошибка ввода!");
  190.                     WaitAndClear();
  191.                     continue;
  192.                 }
  193.  
  194.                 if (choice == ExitCommand)
  195.                 {
  196.                     Console.WriteLine("Спасибо за посещение!");
  197.                     isRunning = false;
  198.                     continue;
  199.                 }
  200.  
  201.                 if (choice < StartEnclosureCommand || choice > _enclosures.Count)
  202.                 {
  203.                     Console.WriteLine("Такого вольера нет!");
  204.                     WaitAndClear();
  205.                     continue;
  206.                 }
  207.  
  208.                 Console.Clear();
  209.                 var selected = _enclosures[choice - 1];
  210.                 selected.ShowInfo();
  211.  
  212.                 Console.WriteLine("\nНажмите любую клавишу, чтобы вернуться...");
  213.                 WaitAndClear();
  214.             }
  215.         }
  216.  
  217.         private void WaitAndClear()
  218.         {
  219.             Console.ReadKey(true);
  220.             Console.Clear();
  221.         }
  222.     }
  223.  
  224.     public class ZooManager
  225.     {
  226.         public Zoo CreateZoo()
  227.         {
  228.             List<IEnclosure> enclosures = new List<IEnclosure>
  229.             {
  230.                 CreateLionEnclosure(),
  231.                 CreateParrotEnclosure(),
  232.                 CreateElephantEnclosure(),
  233.                 CreateNorthernEnclosure()
  234.             };
  235.  
  236.             return new Zoo(enclosures);
  237.         }
  238.  
  239.         private IEnclosure CreateLionEnclosure()
  240.         {
  241.             var enclosure = new Enclosure<Lion>("Площадь львов", "Вольер с камнями.");
  242.             enclosure.AddAnimal(new Lion("Simba", Sex.Male));
  243.             enclosure.AddAnimal(new Lion("Nala", Sex.Female));
  244.             return enclosure;
  245.         }
  246.  
  247.         private IEnclosure CreateParrotEnclosure()
  248.         {
  249.             var enclosure = new Enclosure<Parrot>("Птичник", "Тропические птицы.");
  250.             enclosure.AddAnimal(new Parrot("Kiki", Sex.Female));
  251.             enclosure.AddAnimal(new Parrot("Rio", Sex.Male));
  252.             return enclosure;
  253.         }
  254.  
  255.         private IEnclosure CreateElephantEnclosure()
  256.         {
  257.             var enclosure = new Enclosure<Elephant>("Дом слонов", "Грязевые ванны.");
  258.             enclosure.AddAnimal(new Elephant("Dumbo", Sex.Male));
  259.             enclosure.AddAnimal(new Elephant("Ella", Sex.Female));
  260.             return enclosure;
  261.         }
  262.  
  263.         private IEnclosure CreateNorthernEnclosure()
  264.         {
  265.             var enclosure = new Enclosure<Animal>("Северная экспозиция", "Холодный климат.");
  266.             enclosure.AddAnimal(new Wolf("Akela", Sex.Female));
  267.             enclosure.AddAnimal(new Penguin("Pingu", Sex.Male));
  268.             return enclosure;
  269.         }
  270.     }
  271. }
  272.  
Tags: OOP_12
Advertisement
Add Comment
Please, Sign In to add comment