Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Zoo zoo = new Zoo();
- zoo.ShowAnimals();
- }
- }
- class Zoo
- {
- private AviaryCreator _aviaryCreator;
- private List<Aviary> _aviaries;
- public Zoo()
- {
- _aviaryCreator = new AviaryCreator();
- _aviaries = new List<Aviary>();
- InitAviaries();
- }
- public void ShowAnimals()
- {
- const string CommandExit = "exit";
- bool isRunnig = true;
- while (isRunnig)
- {
- Console.WriteLine($"В зоопарке {_aviaries.Count} вольера. К какому подойти?");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int result))
- {
- if (result >= 0 && result < _aviaries.Count)
- {
- _aviaries[result].ShowInfo();
- }
- else
- {
- Console.WriteLine("Такого вольера нет");
- }
- }
- else if (userInput == CommandExit)
- {
- isRunnig = false;
- }
- Console.ReadKey();
- }
- }
- private void InitAviaries()
- {
- int animalTypesCount = 4;
- for (int i = 0; i < animalTypesCount; i++)
- {
- Aviary aviary = _aviaryCreator.Create(i);
- _aviaries.Add(aviary);
- }
- }
- }
- class AviaryCreator
- {
- private AnimalCreator _animalCreator;
- public AviaryCreator()
- {
- _animalCreator = new AnimalCreator();
- }
- public Aviary Create(int id)
- {
- Aviary aviary = new Aviary(GenerateName(id));
- for (int i = 0; i < aviary.Capacity; i++)
- {
- aviary.AddAnimal(_animalCreator.Create(id));
- }
- return aviary;
- }
- private string GenerateName(int value)
- {
- string name = "Вольер " + value;
- return name;
- }
- }
- class AnimalCreator
- {
- public Animal Create(int value)
- {
- Dictionary<string, string> animals = InitAnimalTypes();
- string name = animals.ElementAt(value).Value;
- string sound = animals.ElementAt(value).Key;
- Animal animal = new Animal(name, sound);
- return animal;
- }
- private Dictionary<string, string> InitAnimalTypes()
- {
- Dictionary<string, string> animals = new Dictionary<string, string>();
- animals.Add("Собака", "Гав");
- animals.Add("Птица", "Чирик");
- animals.Add("Кошка", "Мяу");
- animals.Add("Корова", "Му");
- return animals;
- }
- }
- class Aviary
- {
- private List<Animal> _animals;
- public Aviary(string description)
- {
- _animals = new List<Animal>();
- Capacity = 10;
- Description = description;
- }
- public int Capacity { get; private set; }
- public string Description { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine(Description);
- foreach (Animal animal in _animals)
- {
- Console.WriteLine($"{animal.Type} - {animal.Gender} - {animal.Sound}");
- }
- }
- public void AddAnimal(Animal animal)
- {
- _animals.Add(animal);
- }
- }
- class Animal
- {
- public Animal(string type, string sound)
- {
- SelectRandomGender();
- Type = type;
- Sound = sound;
- }
- public string Type { get; protected set; }
- public Genders Gender { get; protected set; }
- public string Sound { get; protected set; }
- private void SelectRandomGender()
- {
- int gendersCount = Enum.GetNames(typeof(Genders)).Length;
- int genderId = UserUtils.GenerateRandomNumber(gendersCount);
- Gender = (Genders)genderId;
- }
- }
- enum Genders
- {
- Male,
- Female
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int maxValue)
- {
- return s_random.Next(maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement