Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CSLightFirst
- {
- class Program
- {
- private static void Main()
- {
- Zoo zoo = new Zoo(GenerateAviaries());
- while (true)
- {
- Console.Clear();
- Console.WriteLine("Выберите, к какому вольеру вы хотели бы подойти: ");
- for (int i = 0; i < zoo.AviariesCount; i++)
- Console.WriteLine($" {i + 1} - {zoo.GetAviary(i).Name}");
- Console.Write("\nВаш ответ: ");
- if (int.TryParse(Console.ReadLine(), out int aviaryIndex) && aviaryIndex > 0 &&
- aviaryIndex <= zoo.AviariesCount)
- {
- Console.Clear();
- zoo.GetAviary(aviaryIndex - 1).ShowInfo();
- Console.WriteLine("Нажмите любую клавишу, чтобы завершить просмотр вольера...");
- }
- else
- Console.WriteLine("Введен неверный номер вольера, нажмите любую клавишу, чтобы попробовать ещё раз...");
- Console.ReadKey();
- }
- }
- private static List<Aviary> GenerateAviaries()
- {
- List<Aviary> aviaries = new List<Aviary>(6);
- aviaries.Add(new Aviary("Вольер с тиграми", GenerateAnimals("Тигр")));
- aviaries.Add(new Aviary("Вольер со львами", GenerateAnimals("Лев")));
- aviaries.Add(new Aviary("Вольер с пингвинами", GenerateAnimals("Пингвин")));
- aviaries.Add(new Aviary("Клетка с совами", GenerateAnimals("Сова")));
- aviaries.Add(new Aviary("Клетка со змеями", GenerateAnimals("Змея")));
- aviaries.Add(new Aviary("Вольер с лисами", GenerateAnimals("Лиса")));
- return aviaries;
- }
- private static List<Animal> GenerateAnimals(string animalName)
- {
- Random rand = new Random();
- int animalCount = rand.Next(2, 6);
- List<Animal> animals = new List<Animal>(animalCount);
- switch (animalName)
- {
- case "Тигр":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Tiger((AnimalGender)rand.Next(0, 2)));
- break;
- case "Лев":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Lion((AnimalGender)rand.Next(0, 2)));
- break;
- case "Пингвин":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Penguin((AnimalGender)rand.Next(0, 2)));
- break;
- case "Сова":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Owl((AnimalGender)rand.Next(0, 2)));
- break;
- case "Змея":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Snake((AnimalGender)rand.Next(0, 2)));
- break;
- case "Лиса":
- for (int i = 0; i < animalCount; i++)
- animals.Add(new Fox((AnimalGender)rand.Next(0, 2)));
- break;
- default:
- return null;
- }
- return animals;
- }
- }
- class Zoo
- {
- public int AviariesCount => _aviaries.Count;
- private List<Aviary> _aviaries;
- public Zoo(List<Aviary> aviaries)
- {
- _aviaries = aviaries;
- }
- public Aviary GetAviary(int index)
- {
- if (index < 0 || index >= AviariesCount)
- throw new ArgumentException();
- return _aviaries[index];
- }
- }
- class Aviary
- {
- public string Name { get; }
- public int AnimalsCount => _animals.Count;
- private List<Animal> _animals;
- public Aviary(string name, List<Animal> animals)
- {
- Name = name;
- _animals = animals;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Перед вами {Name.ToLower()}\nЖивотных в вольере: {AnimalsCount}\n");
- foreach (Animal animal in _animals)
- animal.ShowInfo();
- }
- }
- abstract class Animal
- {
- public string Name { get; protected set; }
- public AnimalSound Sound { get; protected set; }
- public AnimalGender Gender { get; }
- protected Animal(AnimalGender gender)
- {
- Gender = gender;
- }
- public void ShowInfo()
- {
- Console.WriteLine($" Животное: {Name}\n" +
- $" Издаваемый звук: {Sound}\n" +
- $" Пол: {Gender}\n");
- }
- }
- class Tiger : Animal
- {
- public Tiger(AnimalGender gender) : base(gender)
- {
- Name = "Тигр";
- Sound = AnimalSound.Рёв;
- }
- }
- class Lion : Animal
- {
- public Lion(AnimalGender gender) : base(gender)
- {
- Name = "Лев";
- Sound = AnimalSound.Рёв;
- }
- }
- class Penguin : Animal
- {
- public Penguin(AnimalGender gender) : base(gender)
- {
- Name = "Пингвин";
- Sound = AnimalSound.Писк;
- }
- }
- class Owl : Animal
- {
- public Owl(AnimalGender gender) : base(gender)
- {
- Name = "Сова";
- Sound = AnimalSound.Уханье;
- }
- }
- class Snake : Animal
- {
- public Snake(AnimalGender gender) : base(gender)
- {
- Name = "Змея";
- Sound = AnimalSound.Шипение;
- }
- }
- class Fox : Animal
- {
- public Fox(AnimalGender gender) : base(gender)
- {
- Name = "Лиса";
- Sound = AnimalSound.Крик;
- }
- }
- enum AnimalSound
- {
- Рёв,
- Писк,
- Уханье,
- Шипение,
- Крик
- }
- enum AnimalGender
- {
- М,
- Ж
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement