Advertisement
Torgach

Aquarium

Apr 27th, 2021
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace aquarium
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Aquarium aquarium = new Aquarium();
  14.             aquarium.RunMenu();
  15.         }
  16.     }
  17.  
  18.     class Aquarium
  19.     {
  20.         private List<Fish> _fishes;
  21.         private int _maxSize = 0;
  22.         public Aquarium()
  23.         {
  24.             _fishes = new List<Fish>();
  25.         }
  26.  
  27.         public void RunMenu()
  28.         {
  29.             bool isRun = true;
  30.  
  31.             while(isRun)
  32.             {
  33.                 Console.WriteLine("[1] - добавить рыбу\n" +
  34.                     "[2] - убрать рыбу\n" +
  35.                     "[3] - увеличить размер аквариума\n" +
  36.                     "[4] - выход\n");
  37.  
  38.                 ShowFishes();
  39.  
  40.                 Console.Write("\nВвод: ");
  41.  
  42.                 switch (Console.ReadLine())
  43.                 {
  44.                     case "1":
  45.                         AddFish();
  46.                         break;
  47.                     case "2":
  48.                         DeleteFish();
  49.                         break;
  50.                     case "3":
  51.                         IncreaseMaxSize();
  52.                         break;
  53.                     case "4":
  54.                         isRun = false;
  55.                         break;
  56.                     default:
  57.                         Console.WriteLine("Ожидаем...");
  58.                         break;
  59.                 }
  60.  
  61.                 Console.ReadKey(true);
  62.                 Console.Clear();
  63.                 IncreaseAgeFishes();
  64.             }
  65.         }
  66.  
  67.         private void IncreaseMaxSize()
  68.         {
  69.             Console.Write("Задайте размер аквариума: ");
  70.             if (int.TryParse(Console.ReadLine(), out int number) && number > _fishes.Count)
  71.             {
  72.                 _maxSize = number;
  73.             }
  74.             else
  75.             {
  76.                 Console.WriteLine("Ошибка!");
  77.             }
  78.         }
  79.  
  80.         private void ShowFishes()
  81.         {
  82.             Console.WriteLine("Количество рыб в аквариуме:" + _fishes.Count());
  83.  
  84.             foreach (var fish in _fishes)
  85.             {
  86.                 Console.Write("Рыба возрастом " + fish.Age);
  87.  
  88.                 if (fish.Age == 1)
  89.                 {
  90.                     Console.WriteLine(" год");
  91.                 }
  92.                 else if (fish.Age > 1 && fish.Age < 5)
  93.                 {
  94.                     Console.WriteLine(" года");
  95.                 }
  96.                 else if(fish.Age >= 5)
  97.                 {
  98.                     Console.WriteLine(" лет");
  99.                 }
  100.  
  101.             }
  102.         }
  103.  
  104.         private void AddFish()
  105.         {
  106.             if(_fishes.Count() <= _maxSize)
  107.             {
  108.                 Fish fish = new Fish();
  109.                 _fishes.Add(fish);
  110.             }
  111.             else
  112.             {
  113.                 Console.WriteLine("Размер аквариума не позволяет добавить рыбу");
  114.             }
  115.         }
  116.  
  117.         private void DeleteFish()
  118.         {
  119.             if(_fishes.Count == 0)
  120.             {
  121.                 Console.WriteLine("Аквариум пуст!");
  122.                 return;
  123.             }    
  124.  
  125.             if(_fishes.Count == 1)
  126.             {
  127.                 _fishes.RemoveAt(0);
  128.                 return;
  129.             }
  130.  
  131.             Console.Write("Какую рыбу вы хотите удалить?\nВвод: ");
  132.  
  133.             if(int.TryParse(Console.ReadLine(), out int number) && number <= _fishes.Count())
  134.             {
  135.                 Console.WriteLine("Удаляем...");
  136.                 _fishes.RemoveAt(--number);
  137.             }
  138.             else
  139.             {
  140.                 Console.WriteLine("Номер такой рыбы не существует!");
  141.             }
  142.         }
  143.  
  144.         private void IncreaseAgeFishes()
  145.         {
  146.             foreach (var fish in _fishes)
  147.             {
  148.                 fish.IncreaseAge();
  149.             }
  150.         }
  151.     }
  152.  
  153.     class Fish
  154.     {
  155.         public int Age { get; private set; }
  156.  
  157.         public Fish()
  158.         {
  159.             Age = 0;
  160.         }
  161.  
  162.         public void IncreaseAge()
  163.         {
  164.             ++Age;
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement