-
-
Save GigaOrts/ec80bae7ad0ce29f8ab6ab8468c4c7c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace iJunior | |
{ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
Aquarium aquarium = new Aquarium(); | |
aquarium.TurnOn(); | |
} | |
} | |
class Aquarium | |
{ | |
private const string AddInfo = "1 - Добавить"; | |
private const string RemoveInfo = "2 - Убрать"; | |
private const string WaitInfo = "3 - Подождать"; | |
private const string ExitInfo = "4 - Выключить"; | |
/* Вывод в консоль был такой: | |
В аквариуме пока никого нет :) | |
Что вы хотите сделать? | |
Добавить | |
Убрать | |
Подождать | |
Выключить | |
- не ясно что нажимать | |
- если и поймут, то надо вводить целые слова с большой буквы аж | |
*/ | |
private List<Fish> _fishes; | |
private int _maxFishIn = 7; | |
private int _currentDay = 1; | |
private int _indexFish = 1; | |
private string _userInput; | |
public Aquarium() | |
{ | |
_fishes = new List<Fish>(); | |
} | |
public void TurnOn() | |
{ | |
bool _isWorking = true; | |
while (_isWorking) | |
{ | |
Console.Clear(); | |
ShowAllFish(); | |
Console.WriteLine($"Что вы хотите сделать?\n{AddInfo}\n{RemoveInfo}\n{WaitInfo}\n{ExitInfo}"); | |
_userInput = Console.ReadLine(); | |
switch (_userInput) | |
{ | |
case "1": | |
AddFish(); | |
break; | |
case "2": | |
PullOutFish(); | |
break; | |
case "3": | |
break; | |
case "4": | |
_isWorking = false; | |
break; | |
default: | |
Console.WriteLine("Неверная комманда"); | |
Console.ReadKey(); | |
break; | |
} | |
SkipDay(); | |
} | |
} | |
private void ShowAllFish() | |
{ | |
if (_fishes.Count > 0) | |
{ | |
for (int i = 0; i < _fishes.Count(); i++) | |
{ | |
_fishes[i].ShowLifeStatus(_currentDay); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("В аквариуме пока никого нет :)"); | |
} | |
} | |
private void AddFish() | |
{ | |
if (_fishes.Count < _maxFishIn) | |
{ | |
_fishes.Add(new Fish(_currentDay, _indexFish)); | |
_indexFish++; | |
} | |
else | |
{ | |
Console.WriteLine("В аквариуме не осталось места!"); | |
Console.ReadKey(); | |
} | |
} | |
private void DeleteFish(Fish fish) | |
{ | |
_fishes.Remove(fish); | |
} | |
private void PullOutFish() | |
{ | |
if (_fishes.Count > 0 && TryGetFish(out Fish fish)) | |
{ | |
DeleteFish(fish); | |
} | |
else | |
{ | |
Console.WriteLine("Вы хотите убрать рыбку, которой нет...(✿◠‿◠)"); | |
Console.ReadKey(); | |
} | |
} | |
/* В GetFish вызывается GetIndex -> они оба предупреждают что что-то не так с вводом | |
* | |
* Разделяй ответственности в методах. Не позволяй им друг друга дублировать | |
* Иначе в чём смысл собирать всё в методы, можно писать процедурщину строку за строкой) (нет) | |
* | |
* ----------------------------------------------------------------------- | |
* | |
* Никогда не допускай, чтобы твои private поля, именованные с _ светились в других классах, они же private lol, | |
* в этих случаях делают паблик свойство для private поля с именем с большой буквы. | |
*/ | |
private bool TryGetFish(out Fish findedFish) | |
{ | |
int index = GetIndex(); | |
foreach (Fish fish in _fishes) | |
{ | |
if (fish._index == index) | |
{ | |
findedFish = fish; | |
return true; | |
} | |
} | |
findedFish = null; | |
return false; | |
} | |
/* Плохое применение переменной isCorrect, если нужен бесконечный цикл, можно оставить true для ясности | |
* но тк менторы не одобряют такое, просто напишем дефолтный бесконечный парсер | |
* ---------------------------------------------------------------------------------------------------- | |
* Опустим то, что _userInput хранится как "состояние" класса Аквариум :) | |
* Лучше было реализацию проги делать в мейне. | |
*/ | |
private int GetIndex() | |
{ | |
int index = 1; | |
do | |
{ | |
Console.Write("Какую рыбку вы хотите выбрать? Необходимо ввести ее номер: "); | |
_userInput = Console.ReadLine(); | |
} while (int.TryParse(_userInput, out index) == false); | |
return index; | |
} | |
private void SkipDay() | |
{ | |
_currentDay++; | |
} | |
} | |
class Fish | |
{ | |
private int _lifeCycleInDays = 15; | |
private int _birthday; | |
private int _lifeTime; | |
private bool _isDead = false; | |
public int _index { get; private set; } | |
public Fish(int birthday, int index) | |
{ | |
_birthday = birthday; | |
_index = index; | |
} | |
public void ShowLifeStatus(int currentDay) | |
{ | |
if (IsFishDead(currentDay)) | |
{ | |
Console.WriteLine($"Номер рыбки {_index}) - мертва"); | |
} | |
else | |
{ | |
Console.WriteLine($"Номер рыбки {_index}) Она живет в аквариуме дней - {_lifeTime}"); | |
} | |
} | |
private bool IsFishDead(int currentDay) | |
{ | |
_lifeTime = currentDay - _birthday; | |
if (_lifeTime >= _lifeCycleInDays) | |
{ | |
_isDead = true; | |
return _isDead; | |
} | |
return _isDead; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment