Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOP_12
- {
- public enum Sex
- {
- Male,
- Female
- }
- internal class Program
- {
- static void Main(string[] args)
- {
- ZooManager manager = new ZooManager();
- Zoo zoo = manager.CreateZoo();
- zoo.Run();
- }
- }
- public abstract class Animal
- {
- protected Animal(string name, Sex sex, string species)
- {
- Name = name;
- Sex = sex;
- Species = species;
- }
- public string Name { get; }
- public Sex Sex { get; }
- public string Species { get; }
- public abstract string MakeSound();
- public override string ToString() => $"{Species} \"{Name}\" ({Sex})";
- }
- public interface IEnclosure
- {
- string Name { get; }
- int TotalAnimals { get; }
- void ShowInfo();
- }
- public class Lion : Animal
- {
- public Lion(string name, Sex sex) : base(name, sex, "Lion") { }
- public override string MakeSound() => "Roar";
- }
- public class Parrot : Animal
- {
- public Parrot(string name, Sex sex) : base(name, sex, "Parrot") { }
- public override string MakeSound() => "Squawk";
- }
- public class Elephant : Animal
- {
- public Elephant(string name, Sex sex) : base(name, sex, "Elephant") { }
- public override string MakeSound() => "Trumpet";
- }
- public class Wolf : Animal
- {
- public Wolf(string name, Sex sex) : base(name, sex, "Wolf") { }
- public override string MakeSound() => "Howl";
- }
- public class Penguin : Animal
- {
- public Penguin(string name, Sex sex) : base(name, sex, "Penguin") { }
- public override string MakeSound() => "Honk";
- }
- public class Enclosure<T> : IEnclosure where T : Animal
- {
- private readonly List<T> _animals = new List<T>();
- public Enclosure(string name, string description)
- {
- Name = name;
- Description = description;
- }
- public string Name { get; }
- public string Description { get; }
- public IReadOnlyList<T> Animals => _animals.AsReadOnly();
- public int TotalAnimals => _animals.Count;
- public void AddAnimal(T animal)
- {
- _animals.Add(animal);
- }
- public int CountBySex(Sex sex)
- {
- int count = 0;
- foreach (Animal animal in _animals)
- {
- if (animal.Sex == sex)
- {
- count++;
- }
- }
- return count;
- }
- public List<string> DistinctSounds()
- {
- List<string> sounds = new List<string>();
- foreach (Animal animal in _animals)
- {
- String sound = animal.MakeSound();
- if (sounds.Contains(sound)== false)
- {
- sounds.Add(sound);
- }
- }
- return sounds;
- }
- public void ShowInfo()
- {
- const int LineWidth = 50;
- Console.WriteLine(new string('-', LineWidth));
- Console.WriteLine($"Вольер: {Name}");
- Console.WriteLine($"Описание: {Description}");
- Console.WriteLine($"Животных: {TotalAnimals} (♂ {CountBySex(Sex.Male)}, ♀ {CountBySex(Sex.Female)})");
- List<string> sounds = DistinctSounds();
- Console.WriteLine(sounds.Count > 0 ?
- "Звуки: " + string.Join(", ", sounds) :
- "Звуков нет.");
- Console.WriteLine("\nЖивотные:");
- foreach (Animal animal in _animals)
- {
- Console.WriteLine($"- {animal} — звук: {animal.MakeSound()}");
- }
- Console.WriteLine(new string('-', LineWidth));
- }
- }
- public class Zoo
- {
- private readonly List<IEnclosure> _enclosures;
- public Zoo(List<IEnclosure> enclosures)
- {
- _enclosures = enclosures;
- }
- public void Run()
- {
- const int ExitCommand = 0;
- const int StartEnclosureCommand = 1;
- bool isRunning = true;
- Console.Clear();
- Console.WriteLine("Добро пожаловать в городской зоопарк!\n");
- while (isRunning)
- {
- Console.WriteLine("=== Меню зоопарка ===");
- for (int i = 0; i < _enclosures.Count; i++)
- {
- Console.WriteLine($"{i + 1}. {_enclosures[i].Name} ({_enclosures[i].TotalAnimals} животных)");
- }
- Console.WriteLine($"{ExitCommand}. Выход\n");
- Console.Write("Выберите номер вольера: ");
- string input = Console.ReadLine();
- if (int.TryParse(input, out int choice) == false)
- {
- Console.WriteLine("Ошибка ввода!");
- WaitAndClear();
- continue;
- }
- if (choice == ExitCommand)
- {
- Console.WriteLine("Спасибо за посещение!");
- isRunning = false;
- continue;
- }
- if (choice < StartEnclosureCommand || choice > _enclosures.Count)
- {
- Console.WriteLine("Такого вольера нет!");
- WaitAndClear();
- continue;
- }
- Console.Clear();
- var selected = _enclosures[choice - 1];
- selected.ShowInfo();
- Console.WriteLine("\nНажмите любую клавишу, чтобы вернуться...");
- WaitAndClear();
- }
- }
- private void WaitAndClear()
- {
- Console.ReadKey(true);
- Console.Clear();
- }
- }
- public class ZooManager
- {
- public Zoo CreateZoo()
- {
- List<IEnclosure> enclosures = new List<IEnclosure>
- {
- CreateLionEnclosure(),
- CreateParrotEnclosure(),
- CreateElephantEnclosure(),
- CreateNorthernEnclosure()
- };
- return new Zoo(enclosures);
- }
- private IEnclosure CreateLionEnclosure()
- {
- var enclosure = new Enclosure<Lion>("Площадь львов", "Вольер с камнями.");
- enclosure.AddAnimal(new Lion("Simba", Sex.Male));
- enclosure.AddAnimal(new Lion("Nala", Sex.Female));
- return enclosure;
- }
- private IEnclosure CreateParrotEnclosure()
- {
- var enclosure = new Enclosure<Parrot>("Птичник", "Тропические птицы.");
- enclosure.AddAnimal(new Parrot("Kiki", Sex.Female));
- enclosure.AddAnimal(new Parrot("Rio", Sex.Male));
- return enclosure;
- }
- private IEnclosure CreateElephantEnclosure()
- {
- var enclosure = new Enclosure<Elephant>("Дом слонов", "Грязевые ванны.");
- enclosure.AddAnimal(new Elephant("Dumbo", Sex.Male));
- enclosure.AddAnimal(new Elephant("Ella", Sex.Female));
- return enclosure;
- }
- private IEnclosure CreateNorthernEnclosure()
- {
- var enclosure = new Enclosure<Animal>("Северная экспозиция", "Холодный климат.");
- enclosure.AddAnimal(new Wolf("Akela", Sex.Female));
- enclosure.AddAnimal(new Penguin("Pingu", Sex.Male));
- return enclosure;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment