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)
- {
- const string AddFishCommand = "1";
- const string RemoveFishCommand = "2";
- const string ExitCommand = "3";
- Aquarium aquarium = new Aquarium();
- string userInput;
- bool isWorking = true;
- while (isWorking)
- {
- aquarium.ShowAllFish();
- Console.SetCursorPosition(0, 10);
- Console.WriteLine($"{AddFishCommand})Добавить рыбку.\n{RemoveFishCommand})Убрать рыбку.\n{ExitCommand})Выйти из программы.");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case AddFishCommand:
- aquarium.BeginAging();
- aquarium.AddFish();
- break;
- case RemoveFishCommand:
- aquarium.BeginAging();
- aquarium.RemoveFish();
- break;
- case ExitCommand:
- isWorking = false;
- break;
- }
- }
- }
- }
- class Aquarium
- {
- private List<Fish> _fishTypes = new List<Fish>() { new Fish("Гуппи", 0, 5), new Fish("Акара", 0, 10), new Fish("Золотая", 0, 15), new Fish("Немо", 0, 20) };
- private List<Fish> _fishInAquarium = new List<Fish>();
- public void AddFish()
- {
- if (TryAddFish())
- {
- Fish fish = _fishTypes[Utils.GetRandomNumber(_fishTypes.Count - 1)].Clone();
- _fishInAquarium.Add(fish);
- }
- }
- public bool TryAddFish()
- {
- int maxPlace = 6;
- if (_fishInAquarium.Count >= maxPlace)
- {
- Console.WriteLine("Аквариум переполнен.");
- return false;
- }
- else
- {
- Console.WriteLine($"Еще есть место для {(maxPlace - 1) - _fishInAquarium.Count} рыбок.");
- return true;
- }
- }
- public void RemoveFish()
- {
- if (TryRemoveFish(out Fish fish))
- {
- _fishInAquarium.Remove(fish);
- }
- Console.Clear();
- }
- private bool TryRemoveFish(out Fish fish)
- {
- ShowAllFish();
- Console.WriteLine("Какую рыбку вы хотите убрать?");
- int userInput = ConvertToInt();
- for (int i = 0; i < _fishInAquarium.Count; i++)
- {
- if (i + 1 == userInput)
- {
- fish = _fishInAquarium[i];
- return true;
- }
- }
- fish = null;
- Console.WriteLine("Рыбка с таким номером не найдена не найдена. Введите любую клавишу.");
- Console.ReadKey();
- return false;
- }
- public void BeginAging()
- {
- 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)
- {
- Console.WriteLine("Введите любое число:");
- 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);
- }
- }
- class Utils
- {
- private static Random _random = new Random();
- public static int GetRandomNumber(int max)
- {
- return _random.Next(max + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement