Advertisement
Rodunskiy

Untitled

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