Advertisement
Vapio

task34

Jun 1st, 2021
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         Zoo zoo = new Zoo();
  9.  
  10.         bool isInZoo = true;
  11.  
  12.         while (isInZoo)
  13.             isInZoo = zoo.Update();
  14.     }
  15. }
  16.  
  17. public class Zoo
  18. {
  19.     private List<Aviary> _aviaries;
  20.     private Random _random;
  21.     private bool _isShowAviary;
  22.     private int _aviarySelected;
  23.  
  24.     private int _aviariesAmountMinimum = 4;
  25.     private int _aviariesAmountMaximum = 10;
  26.  
  27.     public Zoo()
  28.     {
  29.         _random = new Random();
  30.         _aviaries = new List<Aviary>();
  31.         _aviarySelected = -1;
  32.         _isShowAviary = false;
  33.         CreateAviaries();
  34.     }
  35.  
  36.     public bool Update()
  37.     {
  38.         bool isExit = true;
  39.        
  40.         if (_isShowAviary)
  41.             ShowAviary();
  42.         else
  43.             isExit = ShowMenu();
  44.  
  45.         return isExit;
  46.     }
  47.  
  48.     private void CreateAviaries()
  49.     {
  50.         int amountAviaries = _random.Next(_aviariesAmountMinimum, _aviariesAmountMaximum);
  51.         Aviary aviary;
  52.        
  53.         for (int i = 0; i < amountAviaries; ++i)
  54.         {
  55.             aviary = new Aviary(i, _random);
  56.             _aviaries.Add(aviary);
  57.         }
  58.     }
  59.  
  60.     private bool ShowMenu()
  61.     {
  62.         string keyExit = "q";
  63.  
  64.         bool isInput = true;
  65.         bool isParse = false;
  66.         bool isInZoo = true;
  67.  
  68.         int inputNumber = 0;
  69.  
  70.         string input;
  71.        
  72.         do
  73.         {
  74.             Console.Clear();
  75.             Console.WriteLine("Menu : ");
  76.             Console.WriteLine($"Please input number between 0 and {_aviaries.Count - 1} or input {keyExit} for exit.");
  77.             input = Console.ReadLine();
  78.             isParse = int.TryParse(input, out inputNumber);
  79.  
  80.             if (isParse)
  81.             {
  82.                 if (inputNumber >= 0 && inputNumber < _aviaries.Count)
  83.                 {
  84.                     isInput = false;
  85.                     _isShowAviary = true;
  86.                     _aviarySelected = inputNumber;
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 if (input.Equals(keyExit))
  92.                 {
  93.                     isInput = false;
  94.                     isInZoo = false;
  95.                     Console.WriteLine("Exit from zoo ...");
  96.                 }
  97.             }
  98.         }
  99.         while (isInput);
  100.  
  101.         return isInZoo;
  102.     }
  103.  
  104.     private void ShowAviary()
  105.     {
  106.         string keyExit = "q";
  107.         bool isInput = true;
  108.  
  109.         string input;
  110.  
  111.         do
  112.         {
  113.             Console.Clear();
  114.             Console.WriteLine("Aviary : ");
  115.             Console.WriteLine(_aviaries[_aviarySelected]);
  116.             Console.WriteLine($"Exit - {keyExit}");
  117.  
  118.             input = Console.ReadLine();
  119.  
  120.             if (input.Equals(keyExit))
  121.             {
  122.                 _aviarySelected = -1;
  123.                 _isShowAviary = false;
  124.                 isInput = false;
  125.             }
  126.         }
  127.         while (isInput);
  128.     }
  129. }
  130.  
  131. public class Aviary
  132. {
  133.     private List<Animal> _animals;
  134.     private Random _random;
  135.     private int _aviaryNumber;
  136.  
  137.     private string[] _animalTypes =
  138.     {
  139.         "Tiger",
  140.         "Monkey",
  141.         "Wolf",
  142.         "Parrot"
  143.     };
  144.  
  145.     private string[] _animalSounds =
  146.     {
  147.         "*Roar*",
  148.         "*Scream*",
  149.         "*Woof*",
  150.         "*Squawk*"
  151.     };
  152.  
  153.     private int _animalsAmountMinimum = 4;
  154.     private int _animalsAmountMaximum = 20;
  155.     private int _malePercent = 50;
  156.     private int _percentRangeRandom = 100;
  157.  
  158.     public Aviary(int aviaryNumber, Random random)
  159.     {
  160.         _random = random;
  161.         _animals = new List<Animal>();
  162.         _aviaryNumber = aviaryNumber;
  163.         CreateAnimals();
  164.     }
  165.  
  166.     public override string ToString()
  167.     {
  168.         string result = "";
  169.  
  170.         result += "Aviary number: " + _aviaryNumber + "\n";
  171.         result += "Amount animals in aviary : " + _animals.Count + "\n";
  172.         result += "Animals : \n";
  173.  
  174.         for (int i = 0; i < _animals.Count; ++i)
  175.             result += i + ".\n" + _animals[i].ToString() + "\n";
  176.  
  177.         return result;
  178.     }
  179.  
  180.     private void CreateAnimals()
  181.     {
  182.         int animalsAmount = _random.Next(_animalsAmountMinimum, _animalsAmountMaximum);
  183.  
  184.         Animal animal = null;
  185.         bool isMale = false;
  186.         int animalType = 0;
  187.  
  188.         for (int i = 0; i < animalsAmount; ++i)
  189.         {
  190.             animalType = _random.Next(_animalTypes.Length);
  191.             isMale = (_random.Next(_percentRangeRandom) / _malePercent) == 1;
  192.             animal = new Animal(_animalTypes[animalType], _animalSounds[animalType], isMale);
  193.             _animals.Add(animal);
  194.         }
  195.     }
  196. }
  197.  
  198. public class Animal
  199. {
  200.     private bool _isMale;
  201.     private string _sound;
  202.     private string _type;
  203.  
  204.     public Animal(string type, string sound, bool isMale)
  205.     {
  206.         _type = type;
  207.         _sound = sound;
  208.         _isMale = isMale;
  209.     }
  210.  
  211.     public override string ToString()
  212.     {
  213.         string result = "";
  214.  
  215.         result += "Type of animal : " + _type + ".\n";
  216.  
  217.         if (_isMale)
  218.             result += "Sex : male\n";
  219.         else
  220.             result += "Sex : female\n";
  221.  
  222.         result += "Sound of animal : " + _sound + ".\n";
  223.         return result;
  224.     }
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement