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 Task48._1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Work();
- }
- }
- class Aquarium
- {
- private static int _maxCountFish = 10;
- private List<Fish> _aquariumFish = new List<Fish>(_maxCountFish);
- private List<Fish> _availableFish = new List<Fish>();
- private Random _random = new Random();
- private int _index = 0;
- public void Work()
- {
- int index = 1;
- bool isExit = false;
- string meny =
- "Добавить --- добавить рыбу в аквариум\n" +
- "Убрать ----- убрать рыбу из аквариума\n" +
- "Уйти ------- бросить этот аквариум и пойди делать более важные дела\n" +
- "====================================================================";
- string requestCommand =
- "Введите команду: ";
- while (isExit == false)
- {
- ChooseFish();
- Show(_aquariumFish);
- Console.WriteLine(meny);
- Console.Write(requestCommand);
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "Добавить":
- AddFish(ref index);
- break;
- case "Убрать":
- RemoveFish();
- break;
- case "Уйти":
- isExit = true;
- break;
- default:
- Console.WriteLine("Нет такой команды. Вы пропускаете ход.");
- break;
- }
- if (_aquariumFish.Count > 0)
- {
- foreach (Fish fish in _aquariumFish)
- {
- bool isLife = fish.CheckAge();
- if (isLife == true)
- fish.UseAging();
- else
- fish.Death();
- }
- }
- _availableFish.Clear();
- }
- }
- private void AddFish(ref int index)
- {
- if (_aquariumFish.Count <= _maxCountFish)
- {
- Show(_availableFish);
- string requestIndex =
- "Введите индекс рыбы которую хотите добавить: ";
- int userInput = GetInt(requestIndex);
- foreach (Fish fish in _availableFish)
- {
- if (fish.Index == userInput)
- {
- fish.ChangeIndex(index);
- _aquariumFish.Add(fish);
- index++;
- }
- }
- }
- else
- {
- Console.WriteLine("В аквариуме нет места, если хотите добавить новую рыбу уберите старую.");
- }
- }
- private void RemoveFish()
- {
- if (_aquariumFish.Count >= 0)
- {
- string requestIndex =
- "Введите индекс рыбы которую хотите добавить: ";
- int userInput = GetInt(requestIndex);
- foreach (Fish fish in _aquariumFish)
- {
- if (fish.Index == userInput)
- {
- _aquariumFish.Remove(fish);
- break;
- }
- }
- }
- else
- {
- Console.WriteLine("В аквариуме нет рыбы, некого убирать.");
- }
- }
- private void Show(List<Fish> fishes)
- {
- if (fishes.Count > 0)
- {
- foreach (Fish fish in fishes)
- {
- fish.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine(
- "В аквариуме нет рыбы.\n" +
- "=======================");
- }
- }
- private void ChooseFish()
- {
- int minAge = 5;
- int maxAge = 50;
- int index = 1;
- bool isAlive = true;
- _availableFish.Add(new Fish(index++, "Рыба клоун", _random.Next(minAge, maxAge), isAlive,1));
- _availableFish.Add(new Fish(index++, "Cудак", _random.Next(minAge, maxAge), isAlive,2));
- _availableFish.Add(new Fish(index++, "Берш", _random.Next(minAge, maxAge), isAlive,3));
- _availableFish.Add(new Fish(index++, "Окунь", _random.Next(minAge, maxAge), isAlive,4));
- _availableFish.Add(new Fish(index++, "Ерш", _random.Next(minAge, maxAge), isAlive,5));
- }
- private int GetInt(string requestInputNumber)
- {
- string errorConversion = "Ошибка,вы вели не цифры! Попробуйте снова.";
- string userInput;
- bool resultConverted = false;
- int number = 0;
- while (resultConverted == false)
- {
- Console.Write(requestInputNumber);
- userInput = Console.ReadLine();
- resultConverted = int.TryParse(userInput, out int numberConvert);
- if (resultConverted != true)
- Console.WriteLine(errorConversion);
- else
- number = numberConvert;
- }
- return number;
- }
- }
- class Fish
- {
- public string Name { get; protected set; }
- public int Age { get; protected set; }
- public int Index { get; protected set; }
- public bool IsAlive { get; protected set; }
- public int Aging { get; protected set; }
- public Fish(int index, string name, int age, bool isAlive, int aging)
- {
- Name = name;
- Age = age;
- Index = index;
- IsAlive = isAlive;
- Aging = aging;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"№{Index}.{Name} - возраст - {Age}");
- }
- public void Death()
- {
- IsAlive = false;
- }
- public void UseAging()
- {
- Age -= Aging;
- }
- public bool CheckAge()
- {
- if (Age > 0)
- return true;
- else
- return false;
- }
- public void ChangeIndex(int newIndex)
- {
- Index = newIndex;
- }
- }
- class Clown : Fish
- {
- public Clown(string name, int age, int index, bool isAlive, int aging) : base(index, name, age, isAlive, aging) { }
- }
- class Zander : Fish
- {
- public Zander(string name, int age, int index, bool isAlive, int aging) : base(index, name, age, isAlive, aging) { }
- }
- class Bersh : Fish
- {
- public Bersh(string name, int age, int index, bool isAlive, int aging) : base(index, name, age, isAlive, aging) { }
- }
- class Perch : Fish
- {
- public Perch(string name, int age, int index, bool isAlive, int aging) : base(index, name, age, isAlive, aging) { }
- }
- class Ruff : Fish
- {
- public Ruff(string name, int age, int index, bool isAlive, int aging) : base(index, name, age, isAlive, aging) { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement