Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace OOP_11
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- AquariumSimulation simulator = new AquariumSimulation(5);
- simulator.Run();
- }
- }
- class Fish
- {
- public Fish(string name, int maxAge)
- {
- Name = name;
- MaxAge = maxAge;
- Age = 0;
- }
- public string Name { get; }
- public int Age { get; private set; }
- public int MaxAge { get; }
- public bool IsAlive => Age < MaxAge;
- public void GrowOlder()
- {
- Age++;
- }
- public override string ToString()
- {
- return $"{Name} (Возраст: {Age}/{MaxAge}) {(IsAlive ? "" : "✖ Погибла")}";
- }
- }
- class Aquarium
- {
- private List<Fish> _fishes;
- private int _capacity;
- public Aquarium(int capacity)
- {
- _capacity = capacity;
- _fishes = new List<Fish>();
- }
- public void AddFish(Fish fish)
- {
- if (_fishes.Count >= _capacity)
- {
- Console.WriteLine("Аквариум переполнен! Нельзя добавить больше рыб.");
- return;
- }
- _fishes.Add(fish);
- Console.WriteLine($"Добавлена рыба: {fish.Name}");
- }
- public void RemoveDeadFish()
- {
- for (int i = _fishes.Count - 1; i >= 0; i--)
- {
- if (_fishes[i].IsAlive == false)
- {
- Console.WriteLine($"Рыба {_fishes[i].Name} умерла и удалена из аквариума.");
- _fishes.RemoveAt(i);
- }
- }
- }
- public void IncreaseAgeForAll()
- {
- foreach (var fish in _fishes)
- {
- fish.GrowOlder();
- }
- }
- public void ShowStatus()
- {
- Console.WriteLine("\nТекущее состояние аквариума:");
- if (_fishes.Count == 0)
- {
- Console.WriteLine("Аквариум пуст.");
- }
- else
- {
- foreach (var fish in _fishes)
- Console.WriteLine(fish);
- }
- }
- }
- class AquariumSimulation
- {
- private const int AddFishCommand = 1;
- private const int NextCycleCommand = 2;
- private const int ExitCommand = 0;
- private Aquarium _aquarium;
- private bool _isRunning;
- private Random _random;
- public AquariumSimulation(int capacity)
- {
- _aquarium = new Aquarium(capacity);
- _isRunning = true;
- _random = new Random();
- }
- public void Run()
- {
- while (_isRunning)
- {
- Console.Clear();
- _aquarium.ShowStatus();
- Console.WriteLine();
- Console.WriteLine($"{AddFishCommand} - Добавить рыбу");
- Console.WriteLine($"{NextCycleCommand} - Прожить один цикл");
- Console.WriteLine($"{ExitCommand} - Выход");
- Console.Write("Выбор: ");
- string input = Console.ReadLine();
- Console.WriteLine();
- if (int.TryParse(input, out int choice) == false)
- {
- continue;
- }
- switch (choice)
- {
- case AddFishCommand:
- AddFish();
- break;
- case NextCycleCommand:
- NextCycle();
- break;
- case ExitCommand:
- _isRunning = false;
- break;
- default:
- Console.WriteLine("Неверный выбор. Повторите попытку.");
- break;
- }
- Thread.Sleep(1000);
- }
- }
- private void AddFish()
- {
- int startAge = 1;
- int endAge = 10;
- Console.Write("Введите имя рыбы: ");
- string name = Console.ReadLine();
- int maxAge = _random.Next(startAge, endAge);
- _aquarium.AddFish(new Fish(name, maxAge));
- }
- private void NextCycle()
- {
- _aquarium.IncreaseAgeForAll();
- _aquarium.RemoveDeadFish();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment