Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Work();
- }
- }
- class Aquarium
- {
- private static Random _random = new Random();
- private List<Fish> _fishTypes;
- private List<Fish> _fishInAquarium;
- private int _maxPlace = 6;
- public Aquarium()
- {
- _fishTypes = new List<Fish>() { new Fish("Гуппи", 0, 5), new Fish("Акара", 0, 10), new Fish("Золотая", 0, 15), new Fish("Немо", 0, 20) };
- _fishInAquarium = new List<Fish>();
- }
- public void Work()
- {
- const string AddFishCommand = "1";
- const string RemoveFishCommand = "2";
- const string ExitCommand = "3";
- string userInput;
- bool isWorking = true;
- while (isWorking)
- {
- ShowAllFish();
- Console.SetCursorPosition(0, 10);
- Console.WriteLine($"{AddFishCommand})Добавить рыбку.\n{RemoveFishCommand})Убрать рыбку.\n{ExitCommand})Выйти из программы.");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case AddFishCommand:
- AddFish();
- break;
- case RemoveFishCommand:
- RemoveFish();
- break;
- case ExitCommand:
- isWorking = false;
- break;
- }
- RaisingAgeFish();
- }
- }
- public void AddFish()
- {
- if (IsFull())
- {
- Fish fish = _fishTypes[_random.Next(_fishTypes.Count - 1)].Clone();
- _fishInAquarium.Add(fish);
- }
- }
- public bool IsFull()
- {
- if (_fishInAquarium.Count >= _maxPlace)
- {
- Console.WriteLine("Аквариум переполнен.");
- return false;
- }
- else
- {
- Console.WriteLine($"Еще есть место для {(_maxPlace - 1) - _fishInAquarium.Count} рыбок.");
- return true;
- }
- }
- public void RemoveFish()
- {
- if (TrySearchFish(out Fish fish))
- {
- _fishInAquarium.Remove(fish);
- }
- Console.Clear();
- }
- private bool TrySearchFish(out Fish fish)
- {
- ShowAllFish();
- Console.WriteLine("Какую рыбку вы хотите убрать?");
- int userInput = ConvertToInt();
- int summand = 1;
- for (int i = 0; i < _fishInAquarium.Count; i++)
- {
- if (i + summand == userInput)
- {
- fish = _fishInAquarium[i];
- return true;
- }
- }
- fish = null;
- Console.WriteLine("Рыбка с таким номером не найдена не найдена. Введите любую клавишу.");
- Console.ReadKey();
- return false;
- }
- public void RaisingAgeFish()
- {
- Fish fishRemove = null;
- foreach (Fish fish in _fishInAquarium)
- {
- if (fish.Age >= fish.YearsOfLife)
- {
- fishRemove = fish;
- Console.WriteLine($"К сожалению погибла рыбка {fishRemove.Name}");
- }
- else
- {
- fish.AddAge();
- }
- }
- _fishInAquarium.Remove(fishRemove);
- }
- public void ShowAllFish()
- {
- for (int i = 0; i < _fishInAquarium.Count; i++)
- {
- Console.WriteLine($"{i + 1}){_fishInAquarium[i].Name}|Осталось жить {_fishInAquarium[i].YearsOfLife - _fishInAquarium[i].Age} лет.");
- }
- }
- private int ConvertToInt()
- {
- int templateNumber;
- string userInput = string.Empty;
- while (int.TryParse(userInput, out templateNumber) == false)
- {
- userInput = Console.ReadLine();
- }
- return templateNumber;
- }
- }
- class Fish
- {
- public Fish(string name, int age, int yearsOfLife)
- {
- Name = name;
- Age = age;
- YearsOfLife = yearsOfLife;
- }
- public string Name { get; private set; }
- public int Age { get; private set; }
- public int YearsOfLife { get; private set; }
- public void AddAge()
- {
- Age++;
- }
- public Fish Clone()
- {
- return new Fish(Name, Age, YearsOfLife);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement