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 Home_Work
- {
- class Program
- {
- static void Main()
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Observe();
- }
- }
- class Aquarium
- {
- private List<Fish> _fishes;
- private int _maxCountFish;
- public Aquarium()
- {
- _fishes = new List<Fish>();
- _maxCountFish = 10;
- }
- public void Observe()
- {
- bool isObserving = true;
- while (isObserving == true)
- {
- ShowAllFish();
- ShowMenu();
- switch (Console.ReadLine())
- {
- case "1":
- AddFish();
- break;
- case "2":
- RemoveFish();
- break;
- case "3":
- RemoveAllDeadFish();
- break;
- case "4":
- default:
- SkipTime();
- break;
- case "5":
- isObserving = false;
- break;
- }
- Console.Clear();
- }
- }
- private void ShowAllFish()
- {
- if (_fishes.Count > 0)
- {
- foreach(Fish fish in _fishes)
- {
- fish.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine("Аквариум пуст.");
- }
- Console.WriteLine();
- }
- private void ShowMenu()
- {
- Console.WriteLine("\n1. Добавить рыбку.\n" +
- "2. Убрать рыбку.\n" +
- "3. Убрать мертвых рыбок.\n" +
- "4. Пропустить время.\n" +
- "5. Выйти.\n\n");
- }
- private void AddFish()
- {
- if (_fishes.Count < _maxCountFish)
- {
- Fish newFish;
- string name;
- Console.Write("Введите имя для новой рыбки: ");
- name = Console.ReadLine();
- FishCreator fishCreator = new FishCreator(name);
- newFish = fishCreator.ChooseFish();
- _fishes.Add(newFish);
- }
- else
- {
- Console.WriteLine("Аквариум заполнен!");
- }
- }
- private void RemoveFish()
- {
- string userInput;
- Console.Write("Введите индекс рыбки, которую хотите достать: ");
- userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out int index) && index > 0 && index <= _fishes.Count)
- {
- Console.WriteLine("Рыбка удалена.");
- _fishes.RemoveAt(index - 1);
- }
- else
- {
- Console.WriteLine("Рыбки с таким индексом нет.");
- }
- }
- private void RemoveAllDeadFish()
- {
- for(int i = 0; i < _fishes.Count; i++)
- {
- if(_fishes[i].IsAlive == false)
- {
- _fishes.RemoveAt(i);
- i--;
- }
- }
- }
- private void SkipTime()
- {
- foreach(Fish fish in _fishes)
- {
- fish.GrowUp();
- }
- }
- }
- class Fish
- {
- private int _maxAge;
- private int _age;
- private string _typeName;
- private string _name;
- public Fish(string name, string typeName = "Необычная рыбка", int maxAge = 15)
- {
- _name = name;
- _typeName = typeName;
- _age = 0;
- _maxAge = maxAge;
- }
- public bool IsAlive => _age < _maxAge;
- public string TypeName => _typeName;
- public void ShowInfo()
- {
- string statusLive;
- if (IsAlive == true)
- {
- statusLive = "жива";
- }
- else
- {
- statusLive = "мертва";
- }
- Console.WriteLine($"{_typeName} {_name} живёт у вас уже {_age} лет. Рыбка {statusLive}.");
- }
- public void GrowUp()
- {
- _age++;
- }
- }
- class Сockerel : Fish
- {
- public Сockerel(string name, string typeName = "Петушок", int maxAge = 5) : base(name, typeName, maxAge) { }
- }
- class Scalar : Fish
- {
- public Scalar(string name, string typeName = "Скалярия", int maxAge = 10) : base(name, typeName, maxAge) { }
- }
- class Barb : Fish
- {
- public Barb(string name, string typeName = "Барбус", int maxAge = 4) : base(name, typeName, maxAge) { }
- }
- class Danio : Fish
- {
- public Danio(string name, string typeName = "Данио", int maxAge = 4) : base(name, typeName, maxAge) { }
- }
- class Ancistrus : Fish
- {
- public Ancistrus(string name, string typeName = "Анциструс", int maxAge = 7) : base(name, typeName, maxAge) { }
- }
- class FishCreator
- {
- private List<Fish> _fishes;
- public FishCreator(string name)
- {
- _fishes= new List<Fish>();
- _fishes.Add(new Сockerel(name));
- _fishes.Add(new Scalar(name));
- _fishes.Add(new Barb(name));
- _fishes.Add(new Danio(name));
- _fishes.Add(new Ancistrus(name));
- }
- public Fish ChooseFish()
- {
- Fish selectedFish = null;
- int index;
- ShowAllFish();
- index = GetIndexFish();
- selectedFish = _fishes[index];
- return selectedFish;
- }
- private void ShowAllFish()
- {
- int index = 1;
- foreach (Fish fish in _fishes)
- {
- Console.WriteLine($"{index}. {fish.TypeName}.");
- index++;
- }
- Console.WriteLine();
- }
- private int GetIndexFish()
- {
- int number = 0;
- bool isGetIndex = false;
- string userInput;
- while (isGetIndex == false)
- {
- Console.Write("Введите индекс вида рыбки, которую вы хотите добавить: ");
- userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out number))
- {
- if (number > 0 && number <= _fishes.Count)
- {
- isGetIndex = true;
- }
- else
- {
- Console.WriteLine("Введите корректный индекс.");
- }
- }
- else
- {
- Console.WriteLine("Введите целое число.");
- }
- }
- return number - 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement