Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace СLightt
- {
- class Program
- {
- public static void Main()
- {
- new Aquarium(10).Work();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- }
- public static class Messager
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- if (delay)
- Console.ReadKey();
- }
- }
- class Aquarium
- {
- private List<Fish> _fishs = new List<Fish>();
- private int _maxFish;
- public Aquarium(int maxFish)
- {
- _maxFish = maxFish;
- if (_maxFish <= 0)
- _maxFish = 1;
- }
- public void Work()
- {
- bool isOpen = true;
- while (isOpen)
- {
- bool isMenu = true;
- while (isMenu)
- {
- Console.Clear();
- ShowInfo();
- Console.WriteLine("\n1 - Добавить рыбу \n2 - Убрать рыбу \nЛюбая клавиша - Прожить время");
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.D1:
- AddFish();
- break;
- case ConsoleKey.D2:
- RemoveFish();
- break;
- default:
- isMenu = false;
- break;
- }
- }
- LiveFishs();
- }
- }
- private void ShowInfo()
- {
- Messager.ShowMessageWithColor($"Аквариум содержит {_fishs.Count} рыб из {_maxFish} возможных", ConsoleColor.White, false);
- int count = 0;
- foreach (Fish fish in _fishs)
- {
- ConsoleColor color;
- Messager.ShowMessageWithColor($"{count}: {fish.GetInfo(out color)}", color, false);
- count++;
- }
- Console.WriteLine();
- }
- private void AddFish()
- {
- if (_fishs.Count < _maxFish)
- {
- _fishs.Add(new Fish());
- Messager.ShowMessageWithColor("Новая рыба добавлена", ConsoleColor.Green, true);
- }
- else
- {
- Messager.ShowMessageWithColor("ОШИБКА: Аквариум полностью заполнен", ConsoleColor.Red, true);
- }
- }
- private void RemoveFish()
- {
- if (_fishs.Count > 0)
- {
- Console.Write("Введите индекс рыбы для удаления - ");
- int indexForRemove = Convert.ToInt32(Console.ReadLine());
- _fishs.RemoveAt(indexForRemove);
- }
- else
- {
- Messager.ShowMessageWithColor("ОШИБКА: Рыб нет", ConsoleColor.Red, true);
- }
- }
- private void LiveFishs()
- {
- Messager.ShowMessageWithColor("Рыбы прожили год в аквариуме", ConsoleColor.White, true);
- foreach (Fish fish in _fishs)
- {
- fish.Live();
- }
- }
- }
- class Fish
- {
- public int Age { get; private set; }
- private bool _isAlive;
- private int _maxAge;
- public Fish()
- {
- Age = 0;
- _isAlive = true;
- _maxAge = RandomStatic.GetNext(3, 20);
- }
- public void Live()
- {
- CheckedAlive();
- if (_isAlive)
- Age++;
- }
- public string GetInfo(out ConsoleColor color)
- {
- if (_isAlive)
- {
- color = ConsoleColor.Blue;
- return $"Рыбе - {Age} лет. Она Жива.";
- }
- else
- {
- color = ConsoleColor.Magenta;
- return $"Рыбе - {Age} лет. Она Мертва.";
- }
- }
- private void CheckedAlive()
- {
- if (Age >= _maxAge)
- if (Convert.ToBoolean(RandomStatic.GetNext(0, 2)))
- _isAlive = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement