Rodunskiy

Untitled

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