Last active
June 19, 2025 13:16
-
-
Save willywannacry/ddac12cb1126fc7e9acfd7bdf1b85e22 to your computer and use it in GitHub Desktop.
Зоопарк
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace YaJunior; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Aviary beastsAviary = new("Зверинец"); | |
beastsAviary.AddAnimal(new Animal("Лев", GetRandomSex(), "рычит")); | |
beastsAviary.AddAnimal(new Animal("Слон", GetRandomSex(), "трубит")); | |
Aviary reptileAviary = new("Террариум"); | |
reptileAviary.AddAnimal(new Animal("Змея", GetRandomSex(), "шипит")); | |
reptileAviary.AddAnimal(new Animal("Жаба", GetRandomSex(), "квакает")); | |
Aviary birdAviary = new("Авиарий"); | |
birdAviary.AddAnimal(new Animal("Попугай", GetRandomSex(), "повторяет за пингвином")); | |
birdAviary.AddAnimal(new Animal("Пингвин", GetRandomSex(), "пикает")); | |
Aviary seaAviary = new("Океанариум"); | |
seaAviary.AddAnimal(new Animal("Морж", GetRandomSex(), "кричит")); | |
seaAviary.AddAnimal(new Animal("Тюлень", GetRandomSex(), "хрюкает")); | |
List<Aviary> aviaries = new() { beastsAviary, reptileAviary, birdAviary, seaAviary }; | |
Zoo zoo = new Zoo(aviaries); | |
zoo.Work(); | |
} | |
static char GetRandomSex() | |
{ | |
return new Random().Next(2) == 0 ? 'м' : 'ж'; | |
} | |
} | |
static class Utilities | |
{ | |
public static int ReadInt() | |
{ | |
int value; | |
string input; | |
do | |
{ | |
Console.Write("Введите число: "); | |
input = Console.ReadLine(); | |
} while (!int.TryParse(input, out value)); | |
return value; | |
} | |
} | |
class Zoo | |
{ | |
private List<Aviary> _aviaries = new(); | |
public Zoo(List<Aviary> aviaries) | |
{ | |
_aviaries = aviaries; | |
} | |
public void Work() | |
{ | |
Console.WriteLine("Добро пожаловать в Зоопарк! " + | |
"К какому вольеру вы хотите подойти?"); | |
ShowAviaries(); | |
int index = Utilities.ReadInt(); | |
if (index < 1 || index > _aviaries.Count) | |
{ | |
Console.WriteLine("Такого вольера нет."); | |
return; | |
} | |
Aviary aviary = GetAviary(index); | |
aviary.ShowAnimals(); | |
} | |
private void ShowAviaries() | |
{ | |
int index = 1; | |
foreach (var aviary in _aviaries) | |
{ | |
Console.WriteLine($"{index}. {aviary.Name}"); | |
index++; | |
} | |
} | |
private Aviary GetAviary(int index) | |
{ | |
return _aviaries[index -1]; | |
} | |
} | |
class Aviary | |
{ | |
private List<Animal> _animals = new(); | |
public Aviary(string name) | |
{ | |
Name = name; | |
} | |
public string Name { get; private set; } | |
public void AddAnimal(Animal animal) | |
{ | |
_animals.Add(animal); | |
} | |
public void ShowAnimals() | |
{ | |
foreach (var animal in _animals) | |
{ | |
Console.WriteLine($"Животное: {animal.Name}, пол: {animal.Sex}. " + | |
$"Подойдя поближе, вы услышали, " + | |
$"как {animal.Name} {animal.Sound}"); | |
} | |
} | |
} | |
class Animal | |
{ | |
public Animal(string name, char sex, string sound) | |
{ | |
Name = name; | |
Sound = sound; | |
Sex = sex; | |
} | |
public string Sound { get; private set; } | |
public string Name { get; private set; } | |
public char Sex { get; private set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment