Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ООП
- {
- class Program
- {
- static void Main(string[] args)
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Work();
- }
- }
- class Aquarium
- {
- private List<Fish> _fishes = new List<Fish>();
- public Aquarium()
- {
- _fishes = new List<Fish>();
- }
- public void Work()
- {
- bool isOpen = true;
- while (isOpen)
- {
- GrowUp();
- ShowFishes();
- SelectAction();
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void SelectAction()
- {
- Console.Write("1) Добавить рыбку (Максимум 10)\n2) Достать рыбку\nВведите номер действия: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- AddFish();
- break;
- case "2":
- RemoveFish();
- break;
- default:
- Console.WriteLine("Такого действия нет");
- break;
- }
- }
- private void RemoveFish()
- {
- Console.Write("Введите номер рыбки, которую необходимо достать: ");
- string userInput = Console.ReadLine();
- bool correctIndex = int.TryParse(userInput, out int correctNumber);
- --correctNumber;
- if (correctIndex && correctNumber < _fishes.Count && correctNumber >= 0)
- _fishes.RemoveAt(correctNumber);
- else
- Console.WriteLine("Такой рыбки нет!");
- }
- private void AddFish()
- {
- int maxCount = 10;
- if (_fishes.Count < maxCount)
- {
- _fishes.Add(new Fish());
- }
- else
- {
- Console.WriteLine("Вы не можете добавить рыбок! Аквариум заполнен!");
- }
- }
- private void ShowFishes()
- {
- foreach(Fish fish in _fishes)
- {
- int fishNumber = _fishes.IndexOf(fish) + 1;
- Console.WriteLine($"Рыбка №{fishNumber} возраста {fish.Age}");
- }
- }
- private void GrowUp()
- {
- int youngAge = 15;
- foreach (Fish fish in _fishes.ToList())
- {
- fish.GrowUp();
- if (ChanceDie(fish.Age - youngAge))
- {
- Console.WriteLine($"Рыбка №{_fishes.IndexOf(fish) + 1} возраста {fish.Age} умерла\n");
- _fishes.Remove(fish);
- }
- }
- }
- private bool ChanceDie(int probability)
- {
- Random random = new Random();
- int percent = 101;
- int chance = random.Next(percent);
- return (chance < probability);
- }
- }
- class Fish
- {
- public int Age { get; private set; }
- public Fish()
- {
- Random random = new Random();
- int minAge = 1;
- int maxAge = 10;
- Age = random.Next(minAge, maxAge);
- }
- public void GrowUp()
- {
- Age++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement