Advertisement
Rodunskiy

Untitled

Aug 16th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Aquarium aquarium = new Aquarium();
  9.  
  10.         aquarium.Work();
  11.     }
  12. }
  13.  
  14. class Aquarium
  15. {
  16.     private static Random _random = new Random();
  17.  
  18.     private List<Fish> _fishTypes;
  19.     private List<Fish> _fishInAquarium;
  20.     private int _maxPlace = 6;
  21.  
  22.     public Aquarium()
  23.     {
  24.         _fishTypes = new List<Fish>() { new Fish("Гуппи", 0, 5), new Fish("Акара", 0, 10), new Fish("Золотая", 0, 15), new Fish("Немо", 0, 20) };
  25.         _fishInAquarium = new List<Fish>();
  26.     }
  27.  
  28.     public void Work()
  29.     {
  30.         const string AddFishCommand = "1";
  31.         const string RemoveFishCommand = "2";
  32.         const string ExitCommand = "3";
  33.  
  34.         string userInput;
  35.         bool isWorking = true;
  36.  
  37.         while (isWorking)
  38.         {
  39.             ShowAllFish();
  40.  
  41.             Console.SetCursorPosition(0, 10);
  42.             Console.WriteLine($"{AddFishCommand})Добавить рыбку.\n{RemoveFishCommand})Убрать рыбку.\n{ExitCommand})Выйти из программы.");
  43.             userInput = Console.ReadLine();
  44.  
  45.             Console.Clear();
  46.  
  47.             switch (userInput)
  48.             {
  49.                 case AddFishCommand:
  50.                     AddFish();
  51.                     break;
  52.  
  53.                 case RemoveFishCommand:
  54.                     RemoveFish();
  55.                     break;
  56.  
  57.                 case ExitCommand:
  58.                     isWorking = false;
  59.                     break;
  60.             }
  61.  
  62.             RaisingAgeFish();
  63.         }
  64.     }
  65.  
  66.     public void AddFish()
  67.     {
  68.         if (IsFull())
  69.         {
  70.             Fish fish = _fishTypes[_random.Next(_fishTypes.Count - 1)].Clone();
  71.             _fishInAquarium.Add(fish);
  72.         }
  73.     }
  74.  
  75.     public bool IsFull()
  76.     {
  77.         if (_fishInAquarium.Count >= _maxPlace)
  78.         {
  79.             Console.WriteLine("Аквариум переполнен.");
  80.             return false;
  81.         }
  82.         else
  83.         {
  84.             Console.WriteLine($"Еще есть место для {(_maxPlace - 1) - _fishInAquarium.Count} рыбок.");
  85.             return true;
  86.         }
  87.     }
  88.  
  89.     public void RemoveFish()
  90.     {
  91.         if (TrySearchFish(out Fish fish))
  92.         {
  93.             _fishInAquarium.Remove(fish);
  94.         }
  95.  
  96.         Console.Clear();
  97.     }
  98.  
  99.     private bool TrySearchFish(out Fish fish)
  100.     {
  101.         ShowAllFish();
  102.  
  103.         Console.WriteLine("Какую рыбку вы хотите убрать?");
  104.  
  105.         int userInput = ConvertToInt();
  106.         int summand = 1;
  107.  
  108.         for (int i = 0; i < _fishInAquarium.Count; i++)
  109.         {
  110.             if (i + summand == userInput)
  111.             {
  112.                 fish = _fishInAquarium[i];
  113.                 return true;
  114.             }
  115.         }
  116.  
  117.         fish = null;
  118.  
  119.         Console.WriteLine("Рыбка с таким номером не найдена не найдена. Введите любую клавишу.");
  120.         Console.ReadKey();
  121.  
  122.         return false;
  123.     }
  124.  
  125.     public void RaisingAgeFish()
  126.     {
  127.         Fish fishRemove = null;
  128.  
  129.         foreach (Fish fish in _fishInAquarium)
  130.         {
  131.             if (fish.Age >= fish.YearsOfLife)
  132.             {
  133.                 fishRemove = fish;
  134.                 Console.WriteLine($"К сожалению погибла рыбка {fishRemove.Name}");
  135.             }
  136.             else
  137.             {
  138.                 fish.AddAge();
  139.             }
  140.         }
  141.  
  142.  
  143.         _fishInAquarium.Remove(fishRemove);
  144.     }
  145.  
  146.     public void ShowAllFish()
  147.     {
  148.         for (int i = 0; i < _fishInAquarium.Count; i++)
  149.         {
  150.             Console.WriteLine($"{i + 1}){_fishInAquarium[i].Name}|Осталось жить {_fishInAquarium[i].YearsOfLife - _fishInAquarium[i].Age} лет.");
  151.         }
  152.     }
  153.  
  154.     private int ConvertToInt()
  155.     {
  156.         int templateNumber;
  157.         string userInput = string.Empty;
  158.  
  159.         while (int.TryParse(userInput, out templateNumber) == false)
  160.         {
  161.             userInput = Console.ReadLine();
  162.         }
  163.  
  164.         return templateNumber;
  165.     }
  166. }
  167.  
  168. class Fish
  169. {
  170.     public Fish(string name, int age, int yearsOfLife)
  171.     {
  172.         Name = name;
  173.         Age = age;
  174.         YearsOfLife = yearsOfLife;
  175.     }
  176.  
  177.     public string Name { get; private set; }
  178.     public int Age { get; private set; }
  179.     public int YearsOfLife { get; private set; }
  180.  
  181.     public void AddAge()
  182.     {
  183.         Age++;
  184.     }
  185.  
  186.     public Fish Clone()
  187.     {
  188.         return new Fish(Name, Age, YearsOfLife);
  189.     }
  190. }
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement