Advertisement
Rodunskiy

Untitled

Aug 15th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 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.             AgeingFish();
  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.  
  90.     public void RemoveFish()
  91.     {
  92.         if (SearchFish(out Fish fish))
  93.         {
  94.             _fishInAquarium.Remove(fish);
  95.         }
  96.  
  97.         Console.Clear();
  98.     }
  99.  
  100.     private bool SearchFish(out Fish fish)
  101.     {
  102.         ShowAllFish();
  103.  
  104.         Console.WriteLine("Какую рыбку вы хотите убрать?");
  105.  
  106.         int userInput = ConvertToInt();
  107.         int summand = 1;
  108.  
  109.         for (int i = 0; i < _fishInAquarium.Count; i++)
  110.         {
  111.             if (i + summand == userInput)
  112.             {
  113.                 fish = _fishInAquarium[i];
  114.                 return true;
  115.             }
  116.         }
  117.  
  118.         fish = null;
  119.  
  120.         Console.WriteLine("Рыбка с таким номером не найдена не найдена. Введите любую клавишу.");
  121.         Console.ReadKey();
  122.  
  123.         return false;
  124.     }
  125.  
  126.     public void AgeingFish()
  127.     {
  128.         Fish fishRemove = null;
  129.  
  130.         foreach (Fish fish in _fishInAquarium)
  131.         {
  132.             if (fish.Age >= fish.YearsOfLife)
  133.             {
  134.                 fishRemove = fish;
  135.                 Console.WriteLine($"К сожалению погибла рыбка {fishRemove.Name}");
  136.             }
  137.             else
  138.             {
  139.                 fish.AddAge();
  140.             }
  141.         }
  142.  
  143.  
  144.         _fishInAquarium.Remove(fishRemove);
  145.     }
  146.  
  147.     public void ShowAllFish()
  148.     {
  149.         for (int i = 0; i < _fishInAquarium.Count; i++)
  150.         {
  151.             Console.WriteLine($"{i + 1}){_fishInAquarium[i].Name}|Осталось жить {_fishInAquarium[i].YearsOfLife - _fishInAquarium[i].Age} лет.");
  152.         }
  153.     }
  154.  
  155.     private int ConvertToInt()
  156.     {
  157.         int templateNumber;
  158.         string userInput = string.Empty;
  159.  
  160.         while (int.TryParse(userInput, out templateNumber) == false)
  161.         {
  162.             userInput = Console.ReadLine();
  163.         }
  164.  
  165.         return templateNumber;
  166.     }
  167. }
  168.  
  169. class Fish
  170. {
  171.     public Fish(string name, int age, int yearsOfLife)
  172.     {
  173.         Name = name;
  174.         Age = age;
  175.         YearsOfLife = yearsOfLife;
  176.     }
  177.  
  178.     public string Name { get; private set; }
  179.     public int Age { get; private set; }
  180.     public int YearsOfLife { get; private set; }
  181.  
  182.     public void AddAge()
  183.     {
  184.         Age++;
  185.     }
  186.  
  187.     public Fish Clone()
  188.     {
  189.         return new Fish(Name, Age, YearsOfLife);
  190.     }
  191. }
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement