Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework9
- {
- class Program
- {
- static void Main(string[] args)
- {
- Aquarium aquarium = new Aquarium(4);
- aquarium.ChooseAction();
- }
- }
- class Aquarium
- {
- private int _capacity;
- private List<Fish> _fishes = new List<Fish>();
- public Aquarium(int capacity)
- {
- _capacity = capacity;
- }
- public void ChooseAction()
- {
- string userInput;
- bool isPlaying = true;
- while (isPlaying == true)
- {
- if (_fishes.Count > 0)
- {
- FilterFishes();
- ShowStats();
- Console.SetCursorPosition(0, 0);
- Console.WriteLine($"{_fishes.Count}/{_capacity}");
- Console.SetCursorPosition(0, _fishes.Count + 1);
- Console.WriteLine("Добавить рыбку/Убрать рыбку/Любая клавиша, чтобы пропустить");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case "Добавить":
- AddFish();
- break;
- case "Убрать":
- GetOutOfAquarium();
- break;
- default:
- break;
- }
- Console.Clear();
- }
- else if (_fishes.Count <= 0)
- AddFish();
- }
- }
- private void AddFish()
- {
- if (_fishes.Count < _capacity)
- {
- _fishes.Add(new Fish());
- Console.WriteLine($"Рыбка добавлена. У нее {_fishes[_fishes.Count - 1].Health} жизней");
- }
- else
- {
- Console.WriteLine("В аквариуме больше нет мест!");
- }
- Console.ReadKey();
- }
- private void GetOutOfAquarium()
- {
- Console.Write("Введите номер рыбки, которую хотите достать:");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int result) == true)
- {
- if (result >= 0 && result < _capacity)
- {
- _fishes.RemoveAt(result);
- }
- else
- {
- Console.WriteLine("Неверное число!");
- }
- }
- else
- {
- Console.WriteLine("Это не число!");
- }
- }
- private void RemoveFish(int i)
- {
- _fishes.RemoveAt(i);
- Console.Write("О нет! Рыбка умерла!");
- Console.ReadKey();
- Console.Clear();
- }
- private void ShowStats()
- {
- Console.WriteLine();
- for (int i = 0; i < _fishes.Count; i++)
- {
- if (_fishes.Count > 0 && i >= 0)
- {
- Console.Write(i + "| ");
- _fishes[i].ShowInfo();
- }
- }
- }
- private void FilterFishes()
- {
- for (int i = 0; i < _fishes.Count; i++)
- {
- _fishes[i].Grow();
- if (_fishes[i].Health <= 0)
- RemoveFish(i);
- }
- }
- }
- class Fish
- {
- public int Health { get; private set; }
- public Fish()
- {
- Random random = new Random();
- Health = random.Next(5, 10);
- }
- public void Grow()
- {
- Health -= 1;
- }
- public void ShowInfo()
- {
- Console.WriteLine(Health + " Жизней");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment