Advertisement
OldBeliver

Aquarium_ver01

Jun 11th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.23 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 Aquarium
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int maxFishQuantity = 10;
  14.             Aquarium aquarium = new Aquarium(maxFishQuantity);
  15.             aquarium.ToStream();
  16.         }
  17.     }
  18.  
  19.     class Aquarium
  20.     {
  21.         private List<Fish> _fishes;
  22.         public int Size { get; private set; }
  23.  
  24.         public Aquarium(int size)
  25.         {
  26.             _fishes = new List<Fish>();
  27.             Size = size;
  28.         }
  29.  
  30.         public void ToStream()
  31.         {
  32.             bool isStream = true;
  33.  
  34.             while (isStream)
  35.             {
  36.                 ShowFishes();
  37.                 ShowMenu();
  38.  
  39.                 string userInput = Console.ReadLine();
  40.  
  41.                 switch (userInput)
  42.                 {
  43.                     case "1":
  44.                         AddFish();
  45.                         break;
  46.                     case "2":
  47.                         RemoveFish();
  48.                         break;
  49.                     case "9":
  50.                         isStream = false;
  51.                         break;
  52.                     case "0":
  53.                         SkipWeek();
  54.                         FixOverPopulation();
  55.                         MeetDeath();
  56.                         break;
  57.                     default:
  58.                         Console.WriteLine($"Ошибка ввода команды.");
  59.                         break;
  60.                 }
  61.                 Console.Write($"нажмите любую для продолжения ...");
  62.                 Console.ReadKey();
  63.                 Console.Clear();
  64.             }
  65.         }
  66.  
  67.         private void ShowMenu()
  68.         {
  69.             Console.WriteLine($"-------------------");
  70.             Console.WriteLine($"1. Добавить рыбку\n2. Отсадить рыбку\n0. Завершить ход\n9. Выйти");
  71.             Console.Write($"\nВведите команду: ");
  72.         }
  73.  
  74.         private void DrawLine(string text)
  75.         {
  76.             string line = "";
  77.  
  78.             for (int i = 0; i < text.Length; i++)
  79.             {
  80.                 line += "-";
  81.             }
  82.  
  83.             Console.WriteLine($"{line}");
  84.         }
  85.  
  86.         private void ShowFishes()
  87.         {
  88.             string title = " рыбки в аквариуме ";
  89.  
  90.             if (_fishes.Count == 0)
  91.             {
  92.                 title = " аквариум пустой ";
  93.             }
  94.             else if (_fishes.Count > Size)
  95.             {
  96.                 title = "аквариум перенаселен";
  97.             }
  98.  
  99.             DrawLine(title);
  100.             Console.WriteLine($"{title} {_fishes.Count}/{Size}");
  101.             DrawLine(title);
  102.  
  103.             for (int i = 0; i < _fishes.Count; i++)
  104.             {
  105.                 Console.Write($"{i + 1:d2}. ");
  106.                 _fishes[i].ShowStats();
  107.             }
  108.         }
  109.  
  110.         private void AddFish()
  111.         {
  112.             Console.WriteLine($"Добавить в аквариум рыбку под номером:");
  113.             Console.Write($"1. Склярия 2.Барбус 3.Гуппи : ");
  114.  
  115.             string userInput = Console.ReadLine();
  116.  
  117.             switch (userInput)
  118.             {
  119.                 case "1":
  120.                     _fishes.Add(new AngelFish());
  121.                     Console.WriteLine($"в аквариум запустили рыбку склярия");
  122.                     break;
  123.                 case "2":
  124.                     _fishes.Add(new Barbus());
  125.                     Console.WriteLine($"в аквариум запустили рыбку барбус");
  126.                     break;
  127.                 case "3":
  128.                     _fishes.Add(new Guppy());
  129.                     Console.WriteLine($"в аквариум запустили рыбку гуппи");
  130.                     break;
  131.             }
  132.         }
  133.  
  134.         private void RemoveFish()
  135.         {
  136.             int number;
  137.             Console.Write($"Введите номер рыбки, которую нужно отсадить: ");
  138.  
  139.             if (TryGetNumber(out number))
  140.             {
  141.                 if(number >= 1 && number <= _fishes.Count)
  142.                 {
  143.                     _fishes.RemoveAt(number-1);
  144.                     Console.WriteLine($"рыбку отсадили");
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 Console.WriteLine($"ошибка выбора номера рыбки");
  150.             }
  151.         }
  152.  
  153.         private bool TryGetNumber(out int number)
  154.         {
  155.             bool isNumber = int.TryParse(Console.ReadLine(), out number);
  156.             return isNumber;
  157.         }
  158.  
  159.         private void SkipWeek()
  160.         {
  161.             for (int i = 0; i < _fishes.Count; i++)
  162.             {
  163.                 _fishes[i].SkipWeek();
  164.             }
  165.         }
  166.  
  167.         private void MeetDeath()
  168.         {
  169.             for (int i = _fishes.Count; i > 0; i--)
  170.             {
  171.                 if (_fishes[i - 1].isDead())
  172.                 {
  173.                     Console.WriteLine($"{_fishes[i - 1].Name} не будет больше радовать Вас");
  174.                     _fishes.RemoveAt(i - 1);
  175.                 }
  176.             }
  177.         }
  178.  
  179.         private void FixOverPopulation()
  180.         {
  181.             Random rand = new Random();
  182.  
  183.             while (_fishes.Count > Size)
  184.             {
  185.                 _fishes.RemoveAt(rand.Next(_fishes.Count));
  186.                 Console.WriteLine($"перенаселение в аквариуме привело к гибели одной из рыбок");
  187.             }
  188.         }
  189.     }
  190.  
  191.     abstract class Fish
  192.     {
  193.         private static Random rand = new Random();
  194.  
  195.         protected string Sort;
  196.  
  197.         public string Name { get; protected set; }
  198.         public int LifeTime { get; protected set; }
  199.         public int Age { get; private set; }
  200.  
  201.         public Fish()
  202.         {
  203.             LifeTime = CreateLifeTime();
  204.             Age = 0;
  205.         }
  206.  
  207.         public void ShowStats()
  208.         {
  209.             Console.WriteLine($"{Name} {Sort}, возраст {Age} недель");
  210.         }
  211.  
  212.         public string TakeOutSort(string[] sort)
  213.         {
  214.             string choosenSort = sort[rand.Next(sort.Length)];
  215.  
  216.             return choosenSort;
  217.         }
  218.  
  219.         virtual public int CreateLifeTime()
  220.         {
  221.             int minLifeSpan = 5;
  222.             int maxLifeSpan = 10;
  223.  
  224.             return rand.Next(minLifeSpan, maxLifeSpan + 1);
  225.         }
  226.  
  227.         public void SkipWeek()
  228.         {
  229.             Age++;
  230.         }
  231.  
  232.         public bool isDead()
  233.         {
  234.             return Age > LifeTime;
  235.         }
  236.     }
  237.  
  238.     class AngelFish : Fish
  239.     {
  240.         public AngelFish() : base()
  241.         {
  242.             Name = "скалярия";
  243.  
  244.             string[] sort =
  245.                 {
  246.                     "дымчато-вуалевая",
  247.                     "золотисто-перламутровая",
  248.                     "золотая",
  249.                     "мраморная",
  250.                     "шлейфовая",
  251.                     "черная",
  252.                     "Кои"
  253.                 };
  254.  
  255.             Sort = TakeOutSort(sort);
  256.         }
  257.     }
  258.  
  259.     class Barbus : Fish
  260.     {
  261.         public Barbus() : base()
  262.         {
  263.             Name = "барбус";
  264.             Sort = TakeOutSort(LoadSort());
  265.         }
  266.  
  267.         private string[] LoadSort()
  268.         {
  269.             string[] sort =
  270.             {
  271.                 "суматранский",
  272.                 "мшистый",
  273.                 "огненный",
  274.                 "вишневый",
  275.                 "флуоресцентный",
  276.             };
  277.  
  278.             return sort;
  279.         }
  280.     }
  281.  
  282.     class Guppy : Fish
  283.     {
  284.         public Guppy() : base()
  285.         {
  286.             Name = "гуппи";
  287.  
  288.             string[] sort =
  289.             {
  290.                 "вуалевая",
  291.                 "ковровая",
  292.                 "ленточная",
  293.                 "сетчатая",
  294.                 "смарагдовая"
  295.             };
  296.  
  297.             Sort = TakeOutSort(sort);
  298.         }
  299.     }
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement