Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
using System; using System.Collections.Generic; namespace OOP_12 { public enum Sex { Male, Female } internal class Program { static void Main(string[] args) { Zoo zoo = new Zoo(); 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 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 { private readonly List
_animals = new List
(); public Enclosure(string name, string description) { Name = name; Description = description; } public IReadOnlyList
Animals => _animals.AsReadOnly(); public string Name { get; } public string Description { get; } public void AddAnimal(Animal animal) { _animals.Add(animal); } public int TotalAnimals() { return _animals.Count; } public int CountBySex(Sex sex) { int count = 0; foreach (Animal animal in _animals) { if (animal.Sex == sex) { count++; } } return count; } public List
DistinctSounds() { List
sounds = new List
(); foreach (Animal animal in _animals) { string sound = animal.MakeSound(); if (sounds.Contains(sound) == false) { sounds.Add(sound); } } return sounds; } public void ShowInfo() { int lines = 50; Console.WriteLine(new string('-', lines)); Console.WriteLine($"Вольер: {Name}"); Console.WriteLine($"Описание: {Description}"); Console.WriteLine($"Животных всего: {TotalAnimals()} (♂ {CountBySex(Sex.Male)}, ♀ {CountBySex(Sex.Female)})"); List
sounds = DistinctSounds(); if (sounds.Count > 0) { Console.WriteLine("Звуки в вольере: " + string.Join(", ", sounds)); } else { Console.WriteLine("Звуков нет."); } if (_animals.Count > 0) { Console.WriteLine(); Console.WriteLine("Список животных:"); foreach (Animal animal in _animals) { Console.WriteLine($" - {animal} — звук: {animal.MakeSound()}"); } } else { Console.WriteLine("В вольере пока нет животных."); } Console.WriteLine(new string('-', lines)); } } public class Zoo { private readonly List
_enclosures = new List
(); public IReadOnlyList
Enclosures => _enclosures.AsReadOnly(); public void AddEnclosure(Enclosure enclosure) { _enclosures.Add(enclosure); } public void Run() { const int StartEnclosureCommand = 1; const int ExitCommand = 0; BuildSampleZoo(); Console.Clear(); Console.WriteLine("Добро пожаловать в городской зоопарк!"); Console.WriteLine(); while (true) { Console.WriteLine("=== Меню зоопарка ==="); for (int i = 0; i < _enclosures.Count; i++) { Enclosure enclousure = _enclosures[i]; Console.WriteLine($"{i+1}. {enclousure.Name} ({enclousure.TotalAnimals()} животных)"); } Console.WriteLine($"{ExitCommand}. Выход"); Console.WriteLine(); Console.Write("Выберите номер вольера, к которому хотите подойти: "); string input = Console.ReadLine(); if (int.TryParse(input, out int choice) == false) { Console.WriteLine("Неверный ввод. Нажмите любую клавишу для продолжения..."); Console.ReadKey(); Console.Clear(); continue; } if (choice == ExitCommand) { Console.WriteLine("Спасибо за посещение зоопарка! До свидания!"); break; } if (choice < StartEnclosureCommand || choice > _enclosures.Count) { Console.WriteLine("Такого вольера нет. Нажмите любую клавишу для продолжения..."); Console.ReadKey(); Console.Clear(); continue; } Console.Clear(); var selected = _enclosures[choice - 1]; Console.WriteLine($"Вы подошли к вольеру: {selected.Name}"); selected.ShowInfo(); Console.WriteLine(); Console.WriteLine("Нажмите любую клавишу, чтобы вернуться в меню..."); Console.ReadKey(); Console.Clear(); } } private void BuildSampleZoo() { Enclosure lionEnclosure = new Enclosure("Площадь львов", "Просторный вольер с камнями и теневой зоной."); lionEnclosure.AddAnimal(new Lion("Simba", Sex.Male)); lionEnclosure.AddAnimal(new Lion("Nala", Sex.Female)); AddEnclosure(lionEnclosure); Enclosure aviary = new Enclosure("Птичник", "Купол с тропическими птицами."); aviary.AddAnimal(new Parrot("Kiki", Sex.Female)); aviary.AddAnimal(new Parrot("Rio", Sex.Male)); AddEnclosure(aviary); Enclosure elephantHouse = new Enclosure("Дом слонов", "Бассейн и грязевые ванны для слонов."); elephantHouse.AddAnimal(new Elephant("Dumbo", Sex.Male)); elephantHouse.AddAnimal(new Elephant("Ella", Sex.Female)); AddEnclosure(elephantHouse); Enclosure northern = new Enclosure("Северная экспозиция", "Прохладная зона для северных животных."); northern.AddAnimal(new Wolf("Akela", Sex.Female)); northern.AddAnimal(new Penguin("Pingu", Sex.Male)); AddEnclosure(northern); } } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)