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 Aquarium
- {
- class Program
- {
- static void Main(string[] args)
- {
- int maxFishQuantity = 10;
- Aquarium aquarium = new Aquarium(maxFishQuantity);
- aquarium.ToStream();
- }
- }
- class Aquarium
- {
- private List<Fish> _fishes;
- public int Size { get; private set; }
- public Aquarium(int size)
- {
- _fishes = new List<Fish>();
- Size = size;
- }
- public void ToStream()
- {
- bool isStream = true;
- while (isStream)
- {
- ShowFishes();
- ShowMenu();
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- AddFish();
- break;
- case "2":
- RemoveFish();
- break;
- case "9":
- isStream = false;
- break;
- case "0":
- SkipWeek();
- FixOverPopulation();
- MeetDeath();
- break;
- default:
- Console.WriteLine($"Ошибка ввода команды.");
- break;
- }
- Console.Write($"нажмите любую для продолжения ...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void ShowMenu()
- {
- Console.WriteLine($"-------------------");
- Console.WriteLine($"1. Добавить рыбку\n2. Отсадить рыбку\n0. Завершить ход\n9. Выйти");
- Console.Write($"\nВведите команду: ");
- }
- private void DrawLine(string text)
- {
- string line = "";
- for (int i = 0; i < text.Length; i++)
- {
- line += "-";
- }
- Console.WriteLine($"{line}");
- }
- private void ShowFishes()
- {
- string title = " рыбки в аквариуме ";
- if (_fishes.Count == 0)
- {
- title = " аквариум пустой ";
- }
- else if (_fishes.Count > Size)
- {
- title = "аквариум перенаселен";
- }
- DrawLine(title);
- Console.WriteLine($"{title} {_fishes.Count}/{Size}");
- DrawLine(title);
- for (int i = 0; i < _fishes.Count; i++)
- {
- Console.Write($"{i + 1:d2}. ");
- _fishes[i].ShowStats();
- }
- }
- private void AddFish()
- {
- Console.WriteLine($"Добавить в аквариум рыбку под номером:");
- Console.Write($"1. Склярия 2.Барбус 3.Гуппи : ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- _fishes.Add(new AngelFish());
- Console.WriteLine($"в аквариум запустили рыбку склярия");
- break;
- case "2":
- _fishes.Add(new Barbus());
- Console.WriteLine($"в аквариум запустили рыбку барбус");
- break;
- case "3":
- _fishes.Add(new Guppy());
- Console.WriteLine($"в аквариум запустили рыбку гуппи");
- break;
- }
- }
- private void RemoveFish()
- {
- int number;
- Console.Write($"Введите номер рыбки, которую нужно отсадить: ");
- if (TryGetNumber(out number))
- {
- if(number >= 1 && number <= _fishes.Count)
- {
- _fishes.RemoveAt(number-1);
- Console.WriteLine($"рыбку отсадили");
- }
- }
- else
- {
- Console.WriteLine($"ошибка выбора номера рыбки");
- }
- }
- private bool TryGetNumber(out int number)
- {
- bool isNumber = int.TryParse(Console.ReadLine(), out number);
- return isNumber;
- }
- private void SkipWeek()
- {
- for (int i = 0; i < _fishes.Count; i++)
- {
- _fishes[i].SkipWeek();
- }
- }
- private void MeetDeath()
- {
- for (int i = _fishes.Count; i > 0; i--)
- {
- if (_fishes[i - 1].isDead())
- {
- Console.WriteLine($"{_fishes[i - 1].Name} не будет больше радовать Вас");
- _fishes.RemoveAt(i - 1);
- }
- }
- }
- private void FixOverPopulation()
- {
- Random rand = new Random();
- while (_fishes.Count > Size)
- {
- _fishes.RemoveAt(rand.Next(_fishes.Count));
- Console.WriteLine($"перенаселение в аквариуме привело к гибели одной из рыбок");
- }
- }
- }
- abstract class Fish
- {
- private static Random rand = new Random();
- protected string Sort;
- public string Name { get; protected set; }
- public int LifeTime { get; protected set; }
- public int Age { get; private set; }
- public Fish()
- {
- LifeTime = CreateLifeTime();
- Age = 0;
- }
- public void ShowStats()
- {
- Console.WriteLine($"{Name} {Sort}, возраст {Age} недель");
- }
- public string TakeOutSort(string[] sort)
- {
- string choosenSort = sort[rand.Next(sort.Length)];
- return choosenSort;
- }
- virtual public int CreateLifeTime()
- {
- int minLifeSpan = 5;
- int maxLifeSpan = 10;
- return rand.Next(minLifeSpan, maxLifeSpan + 1);
- }
- public void SkipWeek()
- {
- Age++;
- }
- public bool isDead()
- {
- return Age > LifeTime;
- }
- }
- class AngelFish : Fish
- {
- public AngelFish() : base()
- {
- Name = "скалярия";
- string[] sort =
- {
- "дымчато-вуалевая",
- "золотисто-перламутровая",
- "золотая",
- "мраморная",
- "шлейфовая",
- "черная",
- "Кои"
- };
- Sort = TakeOutSort(sort);
- }
- }
- class Barbus : Fish
- {
- public Barbus() : base()
- {
- Name = "барбус";
- Sort = TakeOutSort(LoadSort());
- }
- private string[] LoadSort()
- {
- string[] sort =
- {
- "суматранский",
- "мшистый",
- "огненный",
- "вишневый",
- "флуоресцентный",
- };
- return sort;
- }
- }
- class Guppy : Fish
- {
- public Guppy() : base()
- {
- Name = "гуппи";
- string[] sort =
- {
- "вуалевая",
- "ковровая",
- "ленточная",
- "сетчатая",
- "смарагдовая"
- };
- Sort = TakeOutSort(sort);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement