W1thr

ООП-10

Jun 12th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework10
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<Aviary> aviaries = new List<Aviary>
  11.             {
  12.                 new Aviary(new List<Inhabitant>
  13.                 {
  14.                     new Inhabitant("lion", "male", "meow"),
  15.                     new Inhabitant("lion", "female" , "meow"),
  16.                 }),
  17.  
  18.                 new Aviary(new List<Inhabitant>
  19.                 {
  20.                     new Inhabitant("elephant", "male", "trumpet"),
  21.                     new Inhabitant("elephant", "female" , "trumpet"),
  22.                 }),
  23.  
  24.                 new Aviary(new List<Inhabitant>
  25.                 {
  26.                     new Inhabitant("bird", "male", "chirp"),
  27.                     new Inhabitant("bird", "female"),
  28.                 }),
  29.  
  30.                 new Aviary(new List<Inhabitant>
  31.                 {
  32.                     new Inhabitant("wolf", "male", "bark"),
  33.                     new Inhabitant("wolf", "female" , "bark"),
  34.                 }),
  35.  
  36.                 new Aviary(),
  37.                 new Aviary(),
  38.                 new Aviary(),
  39.                 new Aviary(),
  40.             };
  41.  
  42.             bool isActive = true;
  43.  
  44.             while (isActive == true)
  45.             {
  46.                 ChooseAviary(aviaries);
  47.                 Console.ReadKey();
  48.                 Console.Clear();
  49.             }
  50.         }
  51.  
  52.         static void ChooseAviary(List<Aviary> aviaries)
  53.         {
  54.             for (int i = 0; i < aviaries.Count; i++)
  55.             {
  56.                 Console.Write(i + " ");
  57.                 aviaries[i].Glance();
  58.             }
  59.             Console.Write("Choose:");
  60.  
  61.             if (int.TryParse(Console.ReadLine(), out int aviaryNumber) == true)
  62.             {
  63.                 if (aviaryNumber >= 0 && aviaryNumber < aviaries.Count)
  64.                 {
  65.                     Console.Clear();
  66.                     aviaries[aviaryNumber].ShowInfo();
  67.                 }
  68.                 else
  69.                 {
  70.                     Console.WriteLine("Неверное число!");
  71.                 }
  72.             }
  73.             else
  74.             {
  75.                 Console.WriteLine("Это не число!");
  76.             }
  77.         }
  78.     }
  79.  
  80.     class Aviary
  81.     {
  82.         private List<Inhabitant> _inhabitants;
  83.         private static Random _random;
  84.  
  85.         public int Capacity { get; private set; }
  86.  
  87.         static Aviary()
  88.         {
  89.             _random = new Random();
  90.         }
  91.  
  92.         public Aviary(List<Inhabitant> inhabitants)
  93.         {
  94.             _inhabitants = inhabitants;
  95.             Capacity = inhabitants.Count;
  96.         }
  97.  
  98.         public Aviary()
  99.         {
  100.             Capacity = _random.Next(5, 11);
  101.  
  102.             _inhabitants = new List<Inhabitant> { };
  103.  
  104.             for (int i = 0; i < Capacity; i++)
  105.             {
  106.                 _inhabitants.Add(new Inhabitant());
  107.             }
  108.         }
  109.  
  110.         public void ShowInfo()
  111.         {
  112.             Console.WriteLine($"--{Capacity} inhabitant(s)--");
  113.             foreach (var inhabitant in _inhabitants)
  114.             {
  115.                 inhabitant.Introduce();
  116.             }
  117.         }
  118.  
  119.         public void Glance()
  120.         {
  121.             Console.WriteLine($"Вольер с {Capacity} животными");
  122.         }
  123.     }
  124.  
  125.     class Inhabitant
  126.     {
  127.         private List<string> _types = new List<string> { "wolf", "bird", "elephant", "lion" };
  128.         private List<string> _genders = new List<string> { "male", "female" };
  129.         private List<string> _sounds = new List<string> { "bark", "chirp", "trumpet", "meow"};
  130.         private static Random _random;
  131.  
  132.         public string Type { get; private set; }
  133.         public string Gender { get; private set; }
  134.         public string Sound { get; private set; }
  135.  
  136.         static Inhabitant()
  137.         {
  138.             _random = new Random();
  139.         }
  140.  
  141.         public Inhabitant(string type, string gender, string sound = null)
  142.         {
  143.             Type = type;
  144.             Gender = gender;
  145.             Sound = sound;
  146.         }
  147.  
  148.         public Inhabitant()
  149.         {
  150.             int inhabitantNumber = _random.Next(0, _types.Count);
  151.  
  152.             Type = _types[inhabitantNumber];
  153.             Gender = _genders[_random.Next(0, 2)];
  154.             Sound = _sounds[inhabitantNumber];
  155.         }
  156.  
  157.         public void Introduce()
  158.         {
  159.             if (Sound == null)
  160.                 Sound = "no sound";
  161.  
  162.             Console.WriteLine($"Type: {Type}\nGender: {Gender}\nSound: {Sound}");
  163.             Console.WriteLine();
  164.         }
  165.     }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment