Advertisement
Dr_Max_Experience

Task 11

May 12th, 2022 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 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 ООП
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Aquarium aquarium = new Aquarium();
  14.             aquarium.Work();
  15.         }
  16.     }
  17.  
  18.     class Aquarium
  19.     {
  20.         private List<Fish> _fishes = new List<Fish>();
  21.  
  22.         public Aquarium()
  23.         {
  24.             _fishes = new List<Fish>();
  25.         }
  26.  
  27.         public void Work()
  28.         {
  29.             bool isOpen = true;
  30.  
  31.             while (isOpen)
  32.             {
  33.                 GrowUp();
  34.                 ShowFishes();
  35.                 SelectAction();
  36.                 Console.ReadKey();
  37.                 Console.Clear();
  38.             }
  39.         }
  40.  
  41.         private void SelectAction()
  42.         {
  43.             Console.Write("1) Добавить рыбку (Максимум 10)\n2) Достать рыбку\nВведите номер действия: ");
  44.             string userInput = Console.ReadLine();
  45.  
  46.             switch (userInput)
  47.             {
  48.                 case "1":
  49.                     AddFish();
  50.                     break;
  51.  
  52.                 case "2":
  53.                     RemoveFish();
  54.                     break;
  55.  
  56.                 default:
  57.                     Console.WriteLine("Такого действия нет");
  58.                     break;
  59.             }
  60.         }
  61.  
  62.         private void RemoveFish()
  63.         {
  64.             Console.Write("Введите номер рыбки, которую необходимо достать: ");
  65.             string userInput = Console.ReadLine();
  66.  
  67.             bool correctIndex = int.TryParse(userInput, out int correctNumber);
  68.  
  69.             --correctNumber;
  70.  
  71.             if (correctIndex && correctNumber < _fishes.Count && correctNumber >= 0)
  72.                 _fishes.RemoveAt(correctNumber);
  73.             else
  74.                 Console.WriteLine("Такой рыбки нет!");
  75.         }
  76.  
  77.         private void AddFish()
  78.         {
  79.             int maxCount = 10;
  80.  
  81.             if (_fishes.Count < maxCount)
  82.             {
  83.                 _fishes.Add(new Fish());
  84.             }
  85.             else
  86.             {
  87.                 Console.WriteLine("Вы не можете добавить рыбок! Аквариум заполнен!");
  88.             }
  89.         }
  90.  
  91.         private void ShowFishes()
  92.         {
  93.             foreach(Fish fish in _fishes)
  94.             {
  95.                 int fishNumber = _fishes.IndexOf(fish) + 1;
  96.                 Console.WriteLine($"Рыбка №{fishNumber} возраста {fish.Age}");
  97.             }
  98.         }
  99.  
  100.         private void GrowUp()
  101.         {
  102.             int youngAge = 15;
  103.  
  104.             foreach (Fish fish in _fishes.ToList())
  105.             {
  106.                 fish.GrowUp();
  107.  
  108.                 if (ChanceDie(fish.Age - youngAge))
  109.                 {
  110.                     Console.WriteLine($"Рыбка №{_fishes.IndexOf(fish) + 1} возраста {fish.Age} умерла\n");
  111.                     _fishes.Remove(fish);
  112.                 }
  113.             }
  114.         }
  115.  
  116.         private bool ChanceDie(int probability)
  117.         {
  118.             Random random = new Random();
  119.             int percent = 101;
  120.             int chance = random.Next(percent);
  121.  
  122.             return (chance < probability);
  123.         }
  124.     }
  125.  
  126.     class Fish
  127.     {
  128.         public int Age { get; private set; }
  129.  
  130.         public Fish()
  131.         {
  132.             Random random = new Random();
  133.             int minAge = 1;
  134.             int maxAge = 10;
  135.             Age = random.Next(minAge, maxAge);
  136.         }
  137.  
  138.         public void GrowUp()  
  139.         {
  140.             Age++;
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement