W1thr

ООП-9

Mar 29th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework9
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Aquarium aquarium = new Aquarium(4);
  11.             aquarium.ChooseAction();
  12.         }
  13.     }
  14.  
  15.     class Aquarium
  16.     {
  17.         private int _capacity;
  18.         private List<Fish> _fishes = new List<Fish>();
  19.  
  20.         public Aquarium(int capacity)
  21.         {
  22.             _capacity = capacity;
  23.         }
  24.  
  25.         public void ChooseAction()
  26.         {
  27.             string userInput;
  28.             bool isPlaying = true;
  29.  
  30.             while (isPlaying == true)
  31.             {
  32.                 if (_fishes.Count > 0)
  33.                 {
  34.                     FilterFishes();
  35.                     ShowStats();
  36.  
  37.                     Console.SetCursorPosition(0, 0);
  38.                     Console.WriteLine($"{_fishes.Count}/{_capacity}");
  39.                     Console.SetCursorPosition(0, _fishes.Count + 1);
  40.  
  41.                     Console.WriteLine("Добавить рыбку/Убрать рыбку/Любая клавиша, чтобы пропустить");
  42.                     userInput = Console.ReadLine();
  43.  
  44.                     switch (userInput)
  45.                     {
  46.                         case "Добавить":
  47.                             AddFish();
  48.                             break;
  49.                         case "Убрать":
  50.                             GetOutOfAquarium();
  51.                             break;
  52.                         default:
  53.                             break;
  54.                     }
  55.  
  56.                     Console.Clear();
  57.                 }
  58.                 else if (_fishes.Count <= 0)
  59.                     AddFish();
  60.             }
  61.         }
  62.  
  63.         private void AddFish()
  64.         {
  65.             if (_fishes.Count < _capacity)
  66.             {
  67.                 _fishes.Add(new Fish());
  68.                 Console.WriteLine($"Рыбка добавлена. У нее {_fishes[_fishes.Count - 1].Health} жизней");
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine("В аквариуме больше нет мест!");
  73.             }
  74.             Console.ReadKey();
  75.         }
  76.  
  77.         private void GetOutOfAquarium()
  78.         {
  79.             Console.Write("Введите номер рыбки, которую хотите достать:");
  80.             string userInput = Console.ReadLine();
  81.  
  82.             if (int.TryParse(userInput, out int result) == true)
  83.             {
  84.                 if (result >= 0 && result < _capacity)
  85.                 {
  86.                     _fishes.RemoveAt(result);
  87.                 }
  88.                 else
  89.                 {
  90.                     Console.WriteLine("Неверное число!");
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 Console.WriteLine("Это не число!");
  96.             }
  97.         }
  98.  
  99.         private void RemoveFish(int i)
  100.         {
  101.             _fishes.RemoveAt(i);
  102.             Console.Write("О нет! Рыбка умерла!");
  103.             Console.ReadKey();
  104.             Console.Clear();
  105.         }
  106.  
  107.         private void ShowStats()
  108.         {
  109.             Console.WriteLine();
  110.  
  111.             for (int i = 0; i < _fishes.Count; i++)
  112.             {
  113.                 if (_fishes.Count > 0 && i >= 0)
  114.                 {
  115.                     Console.Write(i + "| ");
  116.                     _fishes[i].ShowInfo();
  117.                 }
  118.             }
  119.         }
  120.  
  121.         private void FilterFishes()
  122.         {
  123.             for (int i = 0; i < _fishes.Count; i++)
  124.             {
  125.                 _fishes[i].Grow();
  126.  
  127.                 if (_fishes[i].Health <= 0)
  128.                     RemoveFish(i);
  129.             }
  130.         }
  131.     }
  132.  
  133.     class Fish
  134.     {
  135.         public int Health { get; private set; }
  136.  
  137.         public Fish()
  138.         {
  139.             Random random = new Random();
  140.             Health = random.Next(5, 10);
  141.         }
  142.  
  143.         public void Grow()
  144.         {
  145.             Health -= 1;
  146.         }
  147.  
  148.         public void ShowInfo()
  149.         {
  150.             Console.WriteLine(Health + " Жизней");
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment