Advertisement
RedFlys

Home work - aquarium

Jan 18th, 2022 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.51 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Aquarium aquarium = new Aquarium();
  14.             aquarium.Observe();
  15.         }
  16.     }
  17.  
  18.     class Aquarium
  19.     {
  20.         private List<Fish> _fishes;
  21.         private int _maxCountFish;
  22.  
  23.         public Aquarium()
  24.         {
  25.             _fishes = new List<Fish>();
  26.             _maxCountFish = 10;
  27.         }
  28.  
  29.         public void Observe()
  30.         {
  31.             bool isObserving = true;
  32.  
  33.             while (isObserving == true)
  34.             {            
  35.                 ShowAllFish();
  36.                 ShowMenu();
  37.  
  38.                 switch (Console.ReadLine())
  39.                 {
  40.                 case "1":
  41.                         AddFish();
  42.                     break;
  43.  
  44.                 case "2":
  45.                         RemoveFish();
  46.                     break;
  47.  
  48.                 case "3":
  49.                         RemoveAllDeadFish();
  50.                     break;
  51.  
  52.                 case "4":
  53.                 default:
  54.                         SkipTime();
  55.                     break;
  56.  
  57.                 case "5":
  58.                         isObserving = false;
  59.                     break;
  60.                 }
  61.  
  62.                 Console.Clear();
  63.             }
  64.         }
  65.  
  66.         private void ShowAllFish()
  67.         {
  68.             if (_fishes.Count > 0)
  69.             {
  70.                 foreach(Fish fish in _fishes)
  71.                 {
  72.                     fish.ShowInfo();
  73.                 }
  74.             }
  75.             else
  76.             {
  77.                 Console.WriteLine("Аквариум пуст.");
  78.             }
  79.  
  80.             Console.WriteLine();
  81.         }
  82.  
  83.         private void ShowMenu()
  84.         {
  85.             Console.WriteLine("\n1. Добавить рыбку.\n" +
  86.                 "2. Убрать рыбку.\n" +
  87.                 "3. Убрать мертвых рыбок.\n" +
  88.                 "4. Пропустить время.\n" +
  89.                 "5. Выйти.\n\n");
  90.         }
  91.  
  92.         private void AddFish()
  93.         {
  94.             if (_fishes.Count < _maxCountFish)
  95.             {
  96.                 Fish newFish;
  97.                 string name;
  98.  
  99.                 Console.Write("Введите имя для новой рыбки: ");
  100.                 name = Console.ReadLine();
  101.  
  102.                 FishCreator fishCreator = new FishCreator(name);
  103.                 newFish = fishCreator.ChooseFish();
  104.  
  105.                 _fishes.Add(newFish);
  106.             }
  107.             else
  108.             {
  109.                 Console.WriteLine("Аквариум заполнен!");
  110.             }
  111.         }
  112.  
  113.         private void RemoveFish()
  114.         {
  115.             string userInput;
  116.  
  117.             Console.Write("Введите индекс рыбки, которую хотите достать: ");
  118.             userInput = Console.ReadLine();
  119.  
  120.             if (Int32.TryParse(userInput, out int index) && index > 0 && index <= _fishes.Count)
  121.             {
  122.                 Console.WriteLine("Рыбка удалена.");
  123.  
  124.                 _fishes.RemoveAt(index - 1);
  125.             }
  126.             else
  127.             {
  128.                 Console.WriteLine("Рыбки с таким индексом нет.");
  129.             }
  130.         }
  131.  
  132.         private void RemoveAllDeadFish()
  133.         {
  134.             for(int i = 0; i < _fishes.Count; i++)
  135.             {
  136.                 if(_fishes[i].IsAlive == false)
  137.                 {
  138.                     _fishes.RemoveAt(i);
  139.                     i--;
  140.                 }
  141.             }
  142.         }
  143.  
  144.         private void SkipTime()
  145.         {
  146.             foreach(Fish fish in _fishes)
  147.             {
  148.                 fish.GrowUp();
  149.             }
  150.         }
  151.     }
  152.  
  153.     class Fish
  154.     {
  155.         private int _maxAge;
  156.         private int _age;
  157.         private string _typeName;
  158.         private string _name;
  159.  
  160.         public Fish(string name, string typeName = "Необычная рыбка", int maxAge = 15)
  161.         {
  162.             _name = name;
  163.             _typeName = typeName;
  164.             _age = 0;
  165.             _maxAge = maxAge;
  166.         }
  167.  
  168.         public bool IsAlive => _age < _maxAge;
  169.         public string TypeName => _typeName;
  170.  
  171.         public void ShowInfo()
  172.         {
  173.             string statusLive;
  174.  
  175.             if (IsAlive == true)
  176.             {
  177.                 statusLive = "жива";
  178.             }
  179.             else
  180.             {
  181.                 statusLive = "мертва";
  182.             }
  183.  
  184.             Console.WriteLine($"{_typeName} {_name} живёт у вас уже {_age} лет. Рыбка {statusLive}.");
  185.         }
  186.  
  187.         public void GrowUp()
  188.         {
  189.             _age++;
  190.         }
  191.     }
  192.  
  193.     class Сockerel : Fish
  194.     {
  195.         public Сockerel(string name, string typeName = "Петушок", int maxAge = 5) : base(name, typeName, maxAge) { }
  196.     }
  197.  
  198.     class Scalar : Fish
  199.     {
  200.         public Scalar(string name, string typeName = "Скалярия", int maxAge = 10) : base(name, typeName, maxAge) { }
  201.     }
  202.  
  203.     class Barb : Fish
  204.     {
  205.         public Barb(string name, string typeName = "Барбус", int maxAge = 4) : base(name, typeName, maxAge) { }
  206.     }
  207.  
  208.     class Danio : Fish
  209.     {
  210.         public Danio(string name, string typeName = "Данио", int maxAge = 4) : base(name, typeName, maxAge) { }
  211.     }
  212.  
  213.     class Ancistrus : Fish
  214.     {
  215.         public Ancistrus(string name, string typeName = "Анциструс", int maxAge = 7) : base(name, typeName, maxAge) { }
  216.     }
  217.  
  218.     class FishCreator
  219.     {
  220.         private List<Fish> _fishes;
  221.  
  222.         public FishCreator(string name)
  223.         {
  224.             _fishes= new List<Fish>();
  225.  
  226.             _fishes.Add(new Сockerel(name));
  227.             _fishes.Add(new Scalar(name));
  228.             _fishes.Add(new Barb(name));
  229.             _fishes.Add(new Danio(name));
  230.             _fishes.Add(new Ancistrus(name));
  231.         }
  232.  
  233.         public Fish ChooseFish()
  234.         {
  235.             Fish selectedFish = null;
  236.             int index;
  237.  
  238.             ShowAllFish();
  239.             index = GetIndexFish();
  240.  
  241.             selectedFish = _fishes[index];
  242.  
  243.             return selectedFish;
  244.         }
  245.  
  246.         private void ShowAllFish()
  247.         {
  248.             int index = 1;
  249.                        
  250.             foreach (Fish fish in _fishes)
  251.             {
  252.                 Console.WriteLine($"{index}. {fish.TypeName}.");
  253.  
  254.                 index++;
  255.             }
  256.  
  257.             Console.WriteLine();
  258.         }
  259.  
  260.         private int GetIndexFish()
  261.         {
  262.             int number = 0;
  263.             bool isGetIndex = false;
  264.             string userInput;
  265.  
  266.             while (isGetIndex == false)
  267.             {
  268.                 Console.Write("Введите индекс вида рыбки, которую вы хотите добавить: ");
  269.                 userInput = Console.ReadLine();
  270.  
  271.                 if (Int32.TryParse(userInput, out number))
  272.                 {
  273.                     if (number > 0 && number <= _fishes.Count)
  274.                     {
  275.                         isGetIndex = true;
  276.                     }
  277.                     else
  278.                     {
  279.                         Console.WriteLine("Введите корректный индекс.");
  280.                     }
  281.                 }
  282.                 else
  283.                 {
  284.                     Console.WriteLine("Введите целое число.");
  285.                 }
  286.             }
  287.  
  288.             return number - 1;
  289.         }
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement