Tohir5258

OOP_11

Nov 10th, 2025 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace OOP_11
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             AquariumSimulation simulator = new AquariumSimulation(5);
  12.             simulator.Run();
  13.         }
  14.     }
  15.  
  16.     class Fish
  17.     {
  18.         public Fish(string name, int maxAge)
  19.         {
  20.             Name = name;
  21.             MaxAge = maxAge;
  22.             Age = 0;
  23.         }
  24.  
  25.         public string Name { get; }
  26.         public int Age { get; private set; }
  27.         public int MaxAge { get; }
  28.         public bool IsAlive => Age < MaxAge;
  29.  
  30.         public void GrowOlder()
  31.         {
  32.             Age++;
  33.         }
  34.  
  35.         public override string ToString()
  36.         {
  37.             return $"{Name} (Возраст: {Age}/{MaxAge}) {(IsAlive ? "" : "✖ Погибла")}";
  38.         }
  39.     }
  40.  
  41.     class Aquarium
  42.     {
  43.         private List<Fish> _fishes;
  44.         private int _capacity;
  45.  
  46.         public Aquarium(int capacity)
  47.         {
  48.             _capacity = capacity;
  49.             _fishes = new List<Fish>();
  50.         }
  51.  
  52.         public void AddFish(Fish fish)
  53.         {
  54.             if (_fishes.Count >= _capacity)
  55.             {
  56.                 Console.WriteLine("Аквариум переполнен! Нельзя добавить больше рыб.");
  57.                 return;
  58.             }
  59.  
  60.             _fishes.Add(fish);
  61.             Console.WriteLine($"Добавлена рыба: {fish.Name}");
  62.         }
  63.  
  64.         public void RemoveDeadFish()
  65.         {
  66.             for (int i = _fishes.Count - 1; i >= 0; i--)
  67.             {
  68.                 if (_fishes[i].IsAlive == false)
  69.                 {
  70.                     Console.WriteLine($"Рыба {_fishes[i].Name} умерла и удалена из аквариума.");
  71.                     _fishes.RemoveAt(i);
  72.                 }
  73.             }
  74.         }
  75.  
  76.         public void IncreaseAgeForAll()
  77.         {
  78.             foreach (var fish in _fishes)
  79.             {
  80.                 fish.GrowOlder();
  81.             }
  82.         }
  83.  
  84.         public void ShowStatus()
  85.         {
  86.             Console.WriteLine("\nТекущее состояние аквариума:");
  87.  
  88.             if (_fishes.Count == 0)
  89.             {
  90.                 Console.WriteLine("Аквариум пуст.");
  91.             }
  92.             else
  93.             {
  94.                 foreach (var fish in _fishes)
  95.                     Console.WriteLine(fish);
  96.             }
  97.         }
  98.     }
  99.  
  100.     class AquariumSimulation
  101.     {
  102.         private const int AddFishCommand = 1;
  103.         private const int NextCycleCommand = 2;
  104.         private const int ExitCommand = 0;
  105.  
  106.         private Aquarium _aquarium;
  107.         private bool _isRunning;
  108.         private Random _random;
  109.  
  110.         public AquariumSimulation(int capacity)
  111.         {
  112.             _aquarium = new Aquarium(capacity);
  113.             _isRunning = true;
  114.             _random = new Random();
  115.         }
  116.  
  117.         public void Run()
  118.         {
  119.             while (_isRunning)
  120.             {
  121.                 Console.Clear();
  122.                 _aquarium.ShowStatus();
  123.                 Console.WriteLine();
  124.  
  125.                 Console.WriteLine($"{AddFishCommand} - Добавить рыбу");
  126.                 Console.WriteLine($"{NextCycleCommand} - Прожить один цикл");
  127.                 Console.WriteLine($"{ExitCommand} - Выход");
  128.                 Console.Write("Выбор: ");
  129.  
  130.                 string input = Console.ReadLine();
  131.                 Console.WriteLine();
  132.  
  133.                 if (int.TryParse(input, out int choice) == false)
  134.                 {
  135.                     continue;
  136.                 }
  137.  
  138.                 switch (choice)
  139.                 {
  140.                     case AddFishCommand:
  141.                         AddFish();
  142.                         break;
  143.  
  144.                     case NextCycleCommand:
  145.                         NextCycle();
  146.                         break;
  147.  
  148.                     case ExitCommand:
  149.                         _isRunning = false;
  150.                         break;
  151.  
  152.                     default:
  153.                         Console.WriteLine("Неверный выбор. Повторите попытку.");
  154.                         break;
  155.                 }
  156.  
  157.                 Thread.Sleep(1000);
  158.             }
  159.         }
  160.  
  161.         private void AddFish()
  162.         {
  163.             int startAge = 1;
  164.             int endAge = 10;
  165.  
  166.             Console.Write("Введите имя рыбы: ");
  167.             string name = Console.ReadLine();
  168.             int maxAge = _random.Next(startAge, endAge);
  169.             _aquarium.AddFish(new Fish(name, maxAge));
  170.         }
  171.  
  172.         private void NextCycle()
  173.         {
  174.             _aquarium.IncreaseAgeForAll();
  175.             _aquarium.RemoveDeadFish();
  176.         }
  177.     }
  178. }
  179.  
Tags: OOP_11
Advertisement
Add Comment
Please, Sign In to add comment