Advertisement
NikaBang

Аквариум

Aug 21st, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. internal class Program
  5. {
  6.     //      Есть аквариум, в котором плавают рыбы.В этом аквариуме может быть максимум определенное кол-во рыб.
  7.     //      Рыб можно добавить в аквариум или рыб можно достать из аквариума. (программу делать в цикле для того, чтобы рыбы могли “жить”)
  8.     //      Все рыбы отображаются списком, у рыб также есть возраст.За 1 итерацию рыбы стареют на определенное кол-во жизней и могут умереть.
  9.     static void Main(string[] args)
  10.     {
  11.         bool nearAquarium = true;
  12.         Aquarium aquarium = new Aquarium();
  13.  
  14.         while (nearAquarium)
  15.         {
  16.             aquarium.ShowInfo();
  17.  
  18.             Console.WriteLine("\nТы подошел к аквариуму.\n1 - Добавить рыбку.\n2 - Убрать рыбку." +
  19.                 "\n3 - Уйти.\nМожешь просто наблюдать (любая клавиша).");
  20.             string userInput = Console.ReadLine();
  21.  
  22.             switch (userInput)
  23.             {
  24.                 case "1":
  25.                     aquarium.AddFish();
  26.                     break;
  27.                 case "2":
  28.                     aquarium.RemoveFish();
  29.                     break;
  30.                 case "3":
  31.                     nearAquarium = false;
  32.                     break;
  33.                 default:
  34.                     aquarium.CycleTimes();
  35.                     break;
  36.             }
  37.  
  38.             Console.Clear();
  39.         }
  40.  
  41.         Console.WriteLine("Рыбки будут скучать..");
  42.     }
  43.  
  44.     class Fish
  45.     {
  46.         public string Name { get; private set; }
  47.         public int Lifetime { get; private set; }
  48.  
  49.         public Fish(string name)
  50.         {
  51.             Name = name;
  52.             int minRandom = 5;
  53.             int maxRandom = 11;
  54.             Random random = new Random();
  55.  
  56.             Lifetime = random.Next(minRandom, maxRandom);
  57.         }
  58.  
  59.         public void ShowInfo()
  60.         {
  61.             Console.Write("Рыбка ");
  62.  
  63.             if (Lifetime > 0)
  64.             {
  65.                 Console.ForegroundColor = ConsoleColor.Green;
  66.             }
  67.             else
  68.             {
  69.                 Console.ForegroundColor = ConsoleColor.Red;
  70.             }
  71.  
  72.             Console.Write(Name + " ");
  73.             Console.ForegroundColor = ConsoleColor.Gray;
  74.  
  75.             if (Lifetime > 0)
  76.             {
  77.                 Console.WriteLine($"осталось жить {Lifetime} цикла беспощадного кода.");
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine(" всплыла вверх пузом. Кажется ее нужно убрать.");
  82.             }
  83.         }
  84.  
  85.         public void DecreaseLifetime()
  86.         {
  87.             if (Lifetime > 0)
  88.             {
  89.                 Lifetime--;
  90.             }
  91.         }
  92.     }
  93.  
  94.     class Aquarium
  95.     {
  96.         private List<Fish> _fishes = new List<Fish>();
  97.         public int Capacity { get; }
  98.         public int CountFish { get; private set; }
  99.  
  100.         public Aquarium()
  101.         {
  102.             Capacity = 3;
  103.             CountFish = 0;
  104.         }
  105.  
  106.         public void AddFish()
  107.         {
  108.             if (CountFish < Capacity)
  109.             {
  110.                 Console.Write("Дай имя рыбке: ");
  111.                 string userInput = Console.ReadLine();
  112.                 Fish fish = new Fish(userInput);
  113.                 CountFish++;
  114.  
  115.                 _fishes.Add(fish);
  116.             }
  117.             else
  118.             {
  119.                 Console.WriteLine("В аквариуме нет свободного места.");
  120.                 Console.ReadKey();
  121.             }
  122.  
  123.             CycleTimes();
  124.         }
  125.  
  126.         public void RemoveFish()
  127.         {
  128.             if (_fishes.Count > 0)
  129.             {
  130.                 Console.Write("Введите имя рыбки: ");
  131.                 string userInput = Console.ReadLine();
  132.  
  133.                 foreach (var fish in _fishes)
  134.                 {
  135.                     if (fish.Name == userInput)
  136.                     {
  137.                         _fishes.Remove(fish);
  138.                         CountFish--;
  139.                         break;
  140.                     }
  141.                 }
  142.             }
  143.             else
  144.             {
  145.                 Console.WriteLine("Сейчас нет рыбок в аквариуме.");
  146.                 Console.ReadKey();
  147.             }
  148.  
  149.             CycleTimes();
  150.         }
  151.  
  152.         public void ShowInfo()
  153.         {
  154.             if (_fishes.Count == 0)
  155.             {
  156.                 Console.WriteLine("В аквариуме нет рыбок!");
  157.             }
  158.             else
  159.             {
  160.                 foreach (var fish in _fishes)
  161.                 {
  162.                     fish.ShowInfo();
  163.                 }
  164.             }
  165.  
  166.             if (CountFish < Capacity)
  167.             {
  168.                 Console.WriteLine($"Можно добавить {Capacity - CountFish} рыб(ок).");
  169.             }
  170.         }
  171.  
  172.         public void CycleTimes()
  173.         {
  174.             if (_fishes.Count > 0)
  175.             {
  176.                 foreach (var fish in _fishes)
  177.                 {
  178.                     fish.DecreaseLifetime();
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement