Advertisement
TeT91

ДЗ Аквариум

Jun 9th, 2024 (edited)
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Aquarium aquarium = new Aquarium();
  11.             aquarium.LiveLifeCycle();
  12.         }
  13.     }
  14.  
  15.     class Aquarium
  16.     {
  17.         private int _capacity;
  18.         private List<Fish> _fishes;
  19.  
  20.         public Aquarium()
  21.         {
  22.             _capacity = 10;
  23.             InitFishes();
  24.         }
  25.  
  26.         public void LiveLifeCycle()
  27.         {
  28.             const string CommandAddFish = "1";
  29.             const string CommandRemoveFish = "2";
  30.             const string CommandExit = "exit";
  31.  
  32.             bool isRunning = true;
  33.  
  34.             while (isRunning)
  35.             {
  36.                 RemoveDeadFishes();
  37.                 ShowFishesInfo();
  38.                 CompleteLifeCycleIteration();
  39.  
  40.                 string userInput = Console.ReadLine();
  41.  
  42.                 switch (userInput)
  43.                 {
  44.                     case CommandAddFish:
  45.                         TryAddFish();
  46.                         break;
  47.  
  48.                     case CommandRemoveFish:
  49.                         RemoveRandomFish();
  50.                         break;
  51.  
  52.                     case CommandExit:
  53.                         isRunning = false;
  54.                         break;
  55.  
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private void CompleteLifeCycleIteration()
  61.         {
  62.             foreach (Fish fish in _fishes)
  63.             {
  64.                 fish.IncreaseAge();
  65.             }
  66.         }
  67.  
  68.         private void RemoveRandomFish()
  69.         {
  70.             int minValue = 0;
  71.             int id = UserUtils.GenerateRandomNumber(minValue, _fishes.Count);
  72.  
  73.             _fishes.RemoveAt(id);
  74.         }
  75.  
  76.         private void RemoveDeadFishes()
  77.         {
  78.             for (int i = _fishes.Count - 1; i >= 0; i--)
  79.             {
  80.                 if (_fishes[i].HasDied)
  81.                 {
  82.                     _fishes.Remove(_fishes[i]);
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private void ShowFishesInfo()
  88.         {
  89.             foreach (Fish fish in _fishes)
  90.             {
  91.                 Console.WriteLine($"{fish.Name} - возраст {fish.Age}/{fish.LifeTime}");
  92.             }
  93.         }
  94.  
  95.         private void TryAddFish()
  96.         {
  97.             int id = GenerateId();
  98.             string name = "Fish " + id;
  99.  
  100.             if (_fishes.Count < _capacity)
  101.             {
  102.                 _fishes.Add(new Fish(name, id));
  103.             }
  104.         }
  105.  
  106.         private int GenerateId()
  107.         {
  108.             List<int> ids = GetIds();
  109.             int freeId = 0;
  110.  
  111.             bool isGenerating = true;
  112.  
  113.             while (isGenerating)
  114.             {
  115.                 if (ids.Contains(freeId))
  116.                 {
  117.                     freeId++;
  118.                 }
  119.                 else
  120.                 {
  121.                     isGenerating = false;
  122.                 }
  123.             }
  124.  
  125.             return freeId;
  126.         }
  127.  
  128.         private List<int> GetIds()
  129.         {
  130.             List<int> ids = new List<int>();
  131.  
  132.             foreach (Fish fish in _fishes)
  133.             {
  134.                 ids.Add(fish.Id);
  135.             }
  136.  
  137.             return ids;
  138.         }
  139.  
  140.         private void InitFishes()
  141.         {
  142.             _fishes = new List<Fish>();
  143.  
  144.             for (int i = 0; i < _capacity; i++)
  145.             {
  146.                 TryAddFish();
  147.             }
  148.         }
  149.     }
  150.  
  151.     class Fish
  152.     {
  153.         private int _maxAge;
  154.         private int _minAge;
  155.  
  156.         public Fish(string name, int id)
  157.         {
  158.             Name = name;
  159.             Age = 0;
  160.             _minAge = 10;
  161.             _maxAge = 20;
  162.             LifeTime = UserUtils.GenerateRandomNumber(_minAge, _maxAge);
  163.             Id = id;
  164.         }
  165.  
  166.         public string Name { get; private set; }
  167.  
  168.         public int Age { get; private set; }
  169.  
  170.         public int LifeTime { get; private set; }
  171.  
  172.         public int Id { get; private set; }
  173.  
  174.         public bool HasDied
  175.         {
  176.             get
  177.             {
  178.                 return Age > LifeTime;
  179.             }
  180.         }
  181.  
  182.         public void IncreaseAge()
  183.         {
  184.             Age++;
  185.         }
  186.     }
  187.  
  188.     class UserUtils
  189.     {
  190.         private static Random s_random = new Random();
  191.  
  192.         public static int GenerateRandomNumber(int minValue, int maxValue)
  193.         {
  194.             return s_random.Next(minValue, maxValue);
  195.         }
  196.     }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement