Last active
December 14, 2020 21:28
-
-
Save Hellhackee/ee5e69ac7e218a2f423886fcd10da1c2 to your computer and use it in GitHub Desktop.
CS Light Lesson 36 (aquarium)
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
bool isOpen = true; | |
string input; | |
int inputFishIndex; | |
Aquarium aquarium = new Aquarium(); | |
while (isOpen) | |
{ | |
aquarium.ShowInfo(); | |
Console.WriteLine("Доступные команды: "); | |
Console.WriteLine(" 1 - Поместить новую рыбу в аквариум\n 2 - Убрать рыбу\n 3 - Показать всех рыб\n 4 - Отойти от аквариума"); | |
Console.Write("Введите номер команды: "); | |
input = Console.ReadLine(); | |
switch (input) | |
{ | |
case "1": | |
aquarium.AddFish(); | |
break; | |
case "2": | |
Console.Write("Введите номер рыбы, которую хотите достать: "); | |
inputFishIndex = Convert.ToInt32(Console.ReadLine()); | |
aquarium.RemoveFish(inputFishIndex); | |
break; | |
case "3": | |
aquarium.ShowAllFishes(); | |
break; | |
case "4": | |
isOpen = false; | |
break; | |
default: | |
Console.WriteLine("Такой команды нет."); | |
break; | |
} | |
if (isOpen) | |
{ | |
aquarium.RemoveDeadFishes(); | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
} | |
class Aquarium | |
{ | |
static private Random _random = new Random(); | |
private List<Fish> _fishes = new List<Fish>(); | |
private int _maxFishesCount = 30; | |
private int _basicFishesCount = 25; | |
private int _maxHealthChangePerDay = 3; | |
private int _minHealthChangePerDay = 1; | |
private int _currentFishIndex = 1; | |
public Aquarium() | |
{ | |
Init(); | |
} | |
private void Init() | |
{ | |
for (int i = 0; i < _basicFishesCount; i++, _currentFishIndex++) | |
{ | |
Fish fish = new Fish(_currentFishIndex); | |
_fishes.Add(fish); | |
} | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine("Максимальное количество рыб в аквариуме " + _maxFishesCount + ", сейчас в аквариуме " + _fishes.Count + " рыб."); | |
} | |
public void ShowAllFishes() | |
{ | |
foreach (var fish in _fishes) | |
{ | |
fish.ShowInfo(); | |
} | |
} | |
public void AddFish() | |
{ | |
if (_fishes.Count < _maxFishesCount) | |
{ | |
Fish fish = new Fish(_currentFishIndex); | |
_currentFishIndex++; | |
_fishes.Add(fish); | |
Console.Write("Добавлена рыба: "); | |
fish.ShowInfo(); | |
} | |
else | |
{ | |
Console.WriteLine("Извините, но аквариум полностью заполнен."); | |
} | |
} | |
public bool TryGetFish(int index, out Fish fish) | |
{ | |
fish = _fishes.Find(x => x.Index == index); | |
if (fish != null) | |
return true; | |
else | |
return false; | |
} | |
public void RemoveFish(int inputFishIndex) | |
{ | |
if (TryGetFish(inputFishIndex, out Fish fishToDelete)) | |
{ | |
_fishes.Remove(fishToDelete); | |
} | |
else | |
{ | |
Console.WriteLine("Извините, но рыбы под таким номером нет."); | |
} | |
} | |
public void RemoveDeadFishes() | |
{ | |
int heathValueToChange = _random.Next(_minHealthChangePerDay, _maxHealthChangePerDay + 1); | |
Console.WriteLine("Прошло " + heathValueToChange + " дней."); | |
List<Fish> aliveFishes = new List<Fish>(); | |
foreach (var fish in _fishes) | |
{ | |
fish.ReduceHealth(heathValueToChange); | |
if (!fish.IsDead) | |
{ | |
aliveFishes.Add(fish); | |
} | |
} | |
_fishes = aliveFishes; | |
} | |
} | |
class Fish | |
{ | |
static private Random _random = new Random(); | |
private int _health; | |
private int _maxFishHealth = 10; | |
private int _minFishHealth = 2; | |
private int _index; | |
private bool _isDead = false; | |
public Fish(int index) | |
{ | |
_health = _random.Next(_minFishHealth, _maxFishHealth + 1); | |
_index = index; | |
} | |
public int Index | |
{ | |
get { return _index; } | |
} | |
public bool IsDead | |
{ | |
get { return _isDead; } | |
} | |
public void ReduceHealth(int health) | |
{ | |
_health -= health; | |
if (_health <= 0) | |
_isDead = true; | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine("Рыба номер: " + _index + " Осталось жить " + _health + " дней"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment