NikaBang

ДЗ: Аквариум

Mar 25th, 2026 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. internal class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         ManagerAquarium managerAquarium = new ManagerAquarium();
  9.         managerAquarium.Run();
  10.     }
  11. }
  12.  
  13. public static class UserUtils
  14. {
  15.     private static Random s_random = new Random();
  16.  
  17.     public static int GenerateRandomNumber(int minRandom, int maxRandom)
  18.     {
  19.         return s_random.Next(minRandom, maxRandom + 1);
  20.     }
  21. }
  22.  
  23. public class Fish
  24. {
  25.     private int _lifeTime;
  26.  
  27.     public Fish(string name)
  28.     {
  29.         Name = name;
  30.         int minLifeTime = 6;
  31.         int maxLifeTime = 15;
  32.  
  33.         _lifeTime = UserUtils.GenerateRandomNumber(minLifeTime, maxLifeTime);
  34.     }
  35.  
  36.     public string Name { get; private set; }
  37.  
  38.     public void DecreaseLifetime()
  39.     {
  40.         if (_lifeTime > 0)
  41.         {
  42.             _lifeTime--;
  43.         }
  44.     }
  45.  
  46.     public void ShowInfo()
  47.     {
  48.         Console.Write("Рыбка ");
  49.  
  50.         if (_lifeTime > 0)
  51.         {
  52.             Console.ForegroundColor = ConsoleColor.Green;
  53.         }
  54.         else
  55.         {
  56.             Console.ForegroundColor = ConsoleColor.Red;
  57.         }
  58.  
  59.         Console.Write(Name + " ");
  60.         Console.ForegroundColor = ConsoleColor.Gray;
  61.  
  62.         if (_lifeTime > 0)
  63.         {
  64.             Console.WriteLine($"осталось жить {_lifeTime} цикла беспощадного кода.");
  65.         }
  66.         else
  67.         {
  68.             Console.WriteLine(" всплыла вверх пузом. Пора ее убрать.");
  69.         }
  70.     }
  71. }
  72.  
  73. public class Aquarium
  74. {
  75.     private List<Fish> _fishes = new List<Fish>();
  76.     private int _capacity;
  77.  
  78.     public Aquarium()
  79.     {
  80.         _capacity = 5;
  81.     }
  82.  
  83.     public void ShowInfo()
  84.     {
  85.         Console.WriteLine($"В аквариуме {_fishes.Count}/{_capacity} рыб.");
  86.  
  87.         foreach (Fish fish in _fishes)
  88.         {
  89.             fish.ShowInfo();
  90.         }
  91.     }
  92.  
  93.     public void AddFish()
  94.     {
  95.         if (_fishes.Count < _capacity)
  96.         {
  97.             Console.Write("Придумай имя рыбке: ");
  98.             _fishes.Add(new Fish(Console.ReadLine()));
  99.             Console.WriteLine("Рыбка добавлена.");
  100.         }
  101.         else
  102.         {
  103.             Console.WriteLine("В аквариуме нет места.");
  104.             Console.ReadKey();
  105.         }
  106.     }
  107.  
  108.     public void RemoveFish()
  109.     {
  110.         if (_fishes.Count == 0)
  111.         {
  112.             Console.WriteLine("Аквариум и так пуст.");
  113.             Console.ReadKey();
  114.             return;
  115.         }
  116.  
  117.         Console.Write("Имя рыбки: ");
  118.         string name = Console.ReadLine();
  119.  
  120.         bool fishFound = false;
  121.  
  122.         for (int i = 0; i < _fishes.Count; i++)
  123.         {
  124.             if (_fishes[i].Name == name)
  125.             {
  126.                 _fishes.RemoveAt(i);
  127.                 fishFound = true;
  128.                 Console.WriteLine("Рыбка убрана.");
  129.                 break;
  130.             }
  131.         }
  132.  
  133.         if (fishFound == false)
  134.         {
  135.             Console.WriteLine("Рыбки с таким именем нет.");
  136.             Console.ReadKey();
  137.         }
  138.     }
  139.  
  140.  
  141.     public void CycleTimes()
  142.     {
  143.         foreach (Fish fish in _fishes)
  144.         {
  145.             fish.DecreaseLifetime();
  146.         }
  147.     }
  148. }
  149.  
  150. public class ManagerAquarium
  151. {
  152.     private Aquarium _aquarium = new Aquarium();
  153.  
  154.     public void Run()
  155.     {
  156.         const string CommandAddFish = "1";
  157.         const string CommandRemoveFish = "2";
  158.         const string CommandExit = "3";
  159.  
  160.         bool nearAquarium = true;
  161.  
  162.         while (nearAquarium)
  163.         {
  164.             _aquarium.ShowInfo();
  165.  
  166.             Console.WriteLine($"\nТы подошел к аквариуму.\n" +
  167.                 $"{CommandAddFish} - Добавить рыбку.\n" +
  168.                 $"{CommandRemoveFish} - Убрать рыбку.\n" +
  169.                 $"{CommandExit} - Уйти.\n" +
  170.                 "Можешь просто наблюдать (любая клавиша).");
  171.  
  172.             switch (Console.ReadLine())
  173.             {
  174.                 case CommandAddFish:
  175.                     _aquarium.AddFish();
  176.                     break;
  177.  
  178.                 case CommandRemoveFish:
  179.                     _aquarium.RemoveFish();
  180.                     break;
  181.  
  182.                 case CommandExit:
  183.                     nearAquarium = false;
  184.                     break;
  185.             }
  186.  
  187.             _aquarium.CycleTimes();
  188.             Console.ReadKey();
  189.             Console.Clear();
  190.         }
  191.  
  192.         Console.WriteLine("Приходи еще!");
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment