Advertisement
Askor

Hw37

Dec 16th, 2021 (edited)
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zoo
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Zoo zoo = new Zoo();
  14.             zoo.Work();
  15.         }
  16.     }
  17.  
  18.     class Zoo
  19.     {
  20.         private List<Cage> _cages;
  21.  
  22.         public Zoo()
  23.         {
  24.             _cages = new List<Cage>();
  25.             Create();
  26.         }
  27.  
  28.         public void Work()
  29.         {
  30.             ConsoleKeyInfo consoleKey;
  31.  
  32.             do
  33.             {
  34.                 Console.Clear();
  35.                 ShowAllCages();
  36.                 ShowCage();
  37.  
  38.                 consoleKey = Console.ReadKey();
  39.             }
  40.             while(consoleKey.Key != ConsoleKey.Escape);
  41.         }
  42.  
  43.         private void ShowCage()
  44.         {
  45.             int cageIndex;
  46.  
  47.             Console.WriteLine("\nChoose cage: ");
  48.             cageIndex = GetNumber() - 1;
  49.  
  50.             if (cageIndex >= 0 && cageIndex < _cages.Count)
  51.             {
  52.                 _cages[cageIndex].ShowInfo();
  53.             }
  54.             else
  55.             {
  56.                 Console.WriteLine("Wrong action");
  57.             }
  58.         }
  59.  
  60.         private void ShowAllCages()
  61.         {
  62.             for (int i = 1; i <= _cages.Count; i++)
  63.             {
  64.                 Console.WriteLine($"{i}.{_cages[i - 1].Name} cage");
  65.             }
  66.         }
  67.  
  68.         private void Create()
  69.         {
  70.             _cages.Add(new Cage(3, new Animal("Wolfs", "A-ooooooo", "male")));
  71.             _cages.Add(new Cage(6, new Animal("Monkey", "Ooo-a-a-a-ooo-a", "male, female")));
  72.             _cages.Add(new Cage(2, new Animal("Bear", "Aghrrr", "male")));
  73.             _cages.Add(new Cage(9, new Animal("Snake", "Sssss-ssss", "male, female")));
  74.         }
  75.  
  76.         private int GetNumber()
  77.         {
  78.             int number;
  79.             string userInput;
  80.  
  81.             do
  82.             {
  83.                 userInput = Console.ReadLine();
  84.             }
  85.             while (int.TryParse(userInput, out number) == false);
  86.  
  87.             return number;
  88.         }
  89.     }
  90.  
  91.     class Cage
  92.     {
  93.         private Animal _animal;
  94.  
  95.         public int AnimalsQuantity { get; private set; }
  96.  
  97.         public string Name => _animal.Name;
  98.  
  99.         public Cage(int quantity, Animal animal)
  100.         {
  101.             AnimalsQuantity = quantity;
  102.             _animal = animal;
  103.         }
  104.  
  105.         public void ShowInfo()
  106.         {
  107.             Console.WriteLine($"Cage of {_animal.Name} | Animals gender: {_animal.Sex} | Animals quantity: {AnimalsQuantity} | Animals sound: {_animal.Sound}");
  108.         }
  109.     }
  110.  
  111.     class Animal
  112.     {
  113.         public string Name { get; private set; }
  114.  
  115.         public string Sound { get; private set; }
  116.  
  117.         public string Sex { get; private set; }
  118.  
  119.         public Animal(string name, string sound, string sex)
  120.         {
  121.             Name = name;
  122.             Sound = sound;
  123.             Sex = sex;
  124.         }
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement