Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- internal class Program
- {
- static void Main(string[] args)
- {
- ZooManager zooManager = new ZooManager();
- zooManager.Run();
- }
- }
- public static class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max + 1);
- }
- public static int ReadInt()
- {
- int number = 0;
- bool isParsed = false;
- while (isParsed == false)
- {
- Console.Write("Ввод: ");
- string input = Console.ReadLine();
- isParsed = int.TryParse(input, out number);
- if (isParsed == false)
- {
- Console.WriteLine("Ошибка! Нужно ввести число.");
- }
- }
- return number;
- }
- }
- public enum Gender
- {
- Male,
- Female
- }
- public class AnimalType
- {
- public AnimalType(string species, string sound)
- {
- Species = species;
- Sound = sound;
- }
- public string Species { get; private set; }
- public string Sound { get; private set; }
- }
- public class Animal
- {
- public Animal(AnimalType type, Gender gender)
- {
- Type = type;
- Gender = gender;
- }
- public AnimalType Type { get; private set; }
- public Gender Gender { get; private set; }
- }
- public class Aviary
- {
- private List<Animal> _animals;
- public Aviary(AnimalType animalType, int animalCount)
- {
- AnimalType = animalType;
- _animals = new List<Animal>();
- FillAnimals(animalCount);
- }
- public AnimalType AnimalType { get; private set; }
- public int AnimalCount
- {
- get
- {
- return _animals.Count;
- }
- }
- public void ShowInfo()
- {
- Console.WriteLine(
- $"В вольере живут {AnimalType.Species}. " +
- $"Самцов - {GetMaleCount()}, самок - {GetFemaleCount()}. " +
- $"Они {AnimalType.Sound}.");
- }
- private void FillAnimals(int animalCount)
- {
- for (int i = 0; i < animalCount; i++)
- {
- _animals.Add(CreateRandomAnimal());
- }
- }
- private Animal CreateRandomAnimal()
- {
- Gender gender;
- if (UserUtils.GenerateRandomNumber(0, 1) == 0)
- {
- gender = Gender.Male;
- }
- else
- {
- gender = Gender.Female;
- }
- return new Animal(AnimalType, gender);
- }
- private int GetMaleCount()
- {
- int count = 0;
- foreach (Animal animal in _animals)
- {
- if (animal.Gender == Gender.Male)
- {
- count++;
- }
- }
- return count;
- }
- private int GetFemaleCount()
- {
- int count = 0;
- foreach (Animal animal in _animals)
- {
- if (animal.Gender == Gender.Female)
- {
- count++;
- }
- }
- return count;
- }
- }
- public class Zoo
- {
- private List<Aviary> _aviaries;
- public Zoo()
- {
- _aviaries = new List<Aviary>();
- FillAviaries();
- }
- public int AviaryCount
- {
- get
- {
- return _aviaries.Count;
- }
- }
- public void ShowAviaryInfo(int index)
- {
- _aviaries[index].ShowInfo();
- }
- private void FillAviaries()
- {
- AnimalType parrots = new AnimalType("Попугаи", "чирикают");
- AnimalType elephants = new AnimalType("Слоны", "трубят");
- AnimalType tigers = new AnimalType("Тигры", "рычат");
- AnimalType horses = new AnimalType("Лошади", "фыркают");
- AnimalType ducks = new AnimalType("Утки", "крякают");
- _aviaries.Add(new Aviary(parrots, 7));
- _aviaries.Add(new Aviary(elephants, 10));
- _aviaries.Add(new Aviary(tigers, 5));
- _aviaries.Add(new Aviary(horses, 4));
- _aviaries.Add(new Aviary(ducks, 15));
- }
- }
- public class ZooManager
- {
- private const int CommandExit = 0;
- private Zoo _zoo;
- public ZooManager()
- {
- _zoo = new Zoo();
- }
- public void Run()
- {
- bool isInZoo = true;
- while (isInZoo)
- {
- ShowMenu();
- int userInput = UserUtils.ReadInt();
- if (userInput == CommandExit)
- {
- isInZoo = false;
- }
- else if (userInput >= 1 && userInput <= _zoo.AviaryCount)
- {
- OpenAviary(userInput - 1);
- }
- else
- {
- Console.WriteLine("Неверный номер вольера.");
- Console.ReadKey();
- }
- }
- }
- private void ShowMenu()
- {
- Console.Clear();
- Console.WriteLine("****** ЗООПАРК ******");
- Console.WriteLine($"Перед тобой {_zoo.AviaryCount} вольеров.");
- Console.WriteLine("Введи номер вольера, который хочешь посмотреть.");
- Console.WriteLine($"{CommandExit} - выйти из зоопарка.");
- }
- private void OpenAviary(int index)
- {
- Console.Clear();
- _zoo.ShowAviaryInfo(index);
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment