Anonim_999

Aquarium

Oct 9th, 2021 (edited)
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace ConsoleApp2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Aquarium aquarium = new Aquarium();
  11.             aquarium.StartWork();
  12.         }
  13.     }
  14.  
  15.     class Aquarium
  16.     {
  17.         private List<Fish> _fishes = new List<Fish>();
  18.         private int _maxCountFishes;
  19.  
  20.         public Aquarium()
  21.         {
  22.             Random random = new Random();
  23.             _maxCountFishes = random.Next(6, 20);
  24.         }
  25.         public void StartWork()
  26.         {
  27.             bool isWorking = true;
  28.  
  29.             while (isWorking)
  30.             {
  31.                 Console.Clear();
  32.                 ShowInfo();
  33.                 Console.WriteLine("\n\nМеню:\n1.Добавить рыбку\n2.Убрать рыбку\n3.Выход");
  34.                 string userInput = Console.ReadLine();
  35.  
  36.                 switch (userInput)
  37.                 {
  38.                     case "1":
  39.                         AddFish();
  40.                         break;
  41.                     case "2":
  42.                         RemoveFish();
  43.                         break;
  44.                     case "3":
  45.                         isWorking = false;
  46.                         break;
  47.                     default:
  48.                         Console.WriteLine("Нет такой команды");
  49.                         break;
  50.                 }
  51.  
  52.                 foreach (Fish fish in _fishes)
  53.                 {
  54.                     if (fish.Health > 0)
  55.                         fish.ReduceHealth();
  56.                 }
  57.                 Console.ReadKey();
  58.             }
  59.         }
  60.  
  61.         private void ShowInfo()
  62.         {
  63.             Console.WriteLine($"Кол-во рыб в аквариуме: {_fishes.Count} || Максимальное вместмиость аквариума: {_maxCountFishes}");
  64.  
  65.             for (int i = 0; i < _fishes.Count; i++)
  66.             {
  67.                 Console.Write($"{i + 1}: ");
  68.                 _fishes[i].ShowInfo();
  69.             }
  70.         }
  71.  
  72.         private void AddFish()
  73.         {
  74.             if (_fishes.Count < _maxCountFishes)
  75.             {
  76.                 _fishes.Add(new Fish());
  77.                 Console.WriteLine("Рыбка добавлена!");
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine("Аквариум полн");
  82.             }
  83.         }
  84.  
  85.         private void RemoveFish()
  86.         {
  87.             Console.Write("Укажите номер рыбы которую нужно убрать: ");
  88.             string userInput = Console.ReadLine();
  89.             int number;
  90.  
  91.             if (int.TryParse(userInput, out number))
  92.             {
  93.                 if (Convert.ToInt32(userInput) <= _fishes.Count && Convert.ToInt32(userInput) > 0)
  94.                 {
  95.                     _fishes.RemoveAt(Convert.ToInt32(userInput) - 1);
  96.                     Console.WriteLine("Рыбка убрана");
  97.                 }
  98.                 else
  99.                 {
  100.                     Console.WriteLine("Нет такой рыбки");
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     class Fish
  107.     {
  108.         public float Health { get; private set; }
  109.         private bool IsAlive => Health > 0;
  110.  
  111.         public Fish()
  112.         {
  113.             Random random = new Random();
  114.             Health = random.Next(2, 5);
  115.         }
  116.  
  117.         public void ShowInfo()
  118.         {
  119.             if (IsAlive)
  120.             {
  121.                 Console.WriteLine($"Рыбке осталось жить: {Health} год(а)(лет)");
  122.             }
  123.             else
  124.             {
  125.                 Console.WriteLine("Рыбка умерла((");
  126.             }
  127.         }
  128.  
  129.         public void ReduceHealth()
  130.         {
  131.             Health -= 0.125f;
  132.         }
  133.     }
  134. }
Add Comment
Please, Sign In to add comment