Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- public class Program
- {
- public static void Main()
- {
- int sleepTime = 1000;
- Aquarium aquarium = new Aquarium();
- bool isLive = true;
- while(isLive)
- {
- aquarium.Update();
- isLive = aquarium.IsLive();
- Thread.Sleep(sleepTime);
- }
- }
- }
- public class Aquarium
- {
- private List<Fish> _fishes;
- private Random _random;
- private long _years;
- private int _lifeAmountMinimum = 15;
- private int _lifeAmountMaximum = 80;
- private int _lifeDecreasePerIterationMinimum = 1;
- private int _lifeDecreasePerIterationMaximum = 5;
- private string[] _names =
- {
- "Char", "Guppy", "Rainbowfish", "Arapaima", "Sea butterfly",
- "Clown fish", "Surgeonfish", "Squid", "Tuna", "Suckerfish"
- };
- public Aquarium()
- {
- _random = new Random();
- _fishes = new List<Fish>();
- _years = 1;
- CreateFishes();
- }
- public void Update()
- {
- Console.Clear();
- PrintFishes();
- ReadKeys();
- DecreaseLife();
- ++_years;
- }
- public bool IsLive()
- {
- if (_fishes.Count > 0)
- {
- Console.WriteLine("Still live");
- return true;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("†††FISHES DEAD†††");
- return false;
- }
- }
- private void CreateFishes()
- {
- for (int i = 0, amountFishes = 2; i < amountFishes; ++i)
- AddFish();
- }
- private void AddFish()
- {
- int nameIndex = _random.Next(_names.Length);
- int life = _random.Next(_lifeAmountMinimum, _lifeAmountMaximum);
- int lifeDecreasePerIteration = _random.Next(_lifeDecreasePerIterationMinimum, _lifeDecreasePerIterationMaximum);
- _fishes.Add(new Fish(_names[nameIndex], life, lifeDecreasePerIteration));
- }
- private void RemoveFish(int index)
- {
- _fishes.RemoveAt(index);
- }
- private void DecreaseLife()
- {
- for (int i = 0; i < _fishes.Count; ++i)
- {
- _fishes[i].DecreaseLife();
- if (_fishes[i].IsDead())
- RemoveFish(i);
- }
- }
- private void PrintFishes()
- {
- Console.WriteLine($"Year : {_years}");
- Console.WriteLine($"Amount of fishes : {_fishes.Count}");
- Console.WriteLine($"\nInput to number : ");
- Console.WriteLine($"0. Add Fish.");
- Console.WriteLine($"1. Remove Fish.");
- Console.WriteLine("\n Fishes : ");
- for (int i = 0; i < _fishes.Count; ++i)
- Console.WriteLine($"{i}.\n{_fishes[i]}");
- }
- private void ReadKeys()
- {
- if (Console.KeyAvailable)
- {
- ConsoleKey key = Console.ReadKey(true).Key;
- if (key.Equals(ConsoleKey.D0))
- {
- AddFish();
- }
- else if (key.Equals(ConsoleKey.D1))
- {
- int index = InputIndex();
- RemoveFish(index);
- }
- }
- }
- private int InputIndex()
- {
- Console.Clear();
- Console.WriteLine($"Amount of fishes : {_fishes.Count}");
- bool isParse = false;
- int result = 0;
- do
- {
- Console.WriteLine("Please input a index of fish : ");
- isParse = int.TryParse(Console.ReadLine(), out result);
- }
- while (isParse != true);
- return result;
- }
- }
- public class Fish
- {
- private string _name;
- private int _life;
- private int _lifeDecreasePerIteration;
- public Fish(string name, int life, int lifeDecreasePerIteration)
- {
- _name = name;
- _life = life;
- _lifeDecreasePerIteration = lifeDecreasePerIteration;
- }
- public void DecreaseLife()
- {
- _life -= _lifeDecreasePerIteration;
- }
- public bool IsDead()
- {
- return _life <= 0;
- }
- public override string ToString()
- {
- string result = "";
- result += "Name fish : " + _name + ".\n";
- result += "Age to live : " + _life + ".\n";
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement