Advertisement
Vapio

task36

Jun 2nd, 2021 (edited)
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         int sleepTime = 1000;
  10.         Aquarium aquarium = new Aquarium();
  11.         bool isLive = true;
  12.  
  13.         while(isLive)
  14.         {
  15.             aquarium.Update();
  16.             isLive = aquarium.IsLive();
  17.             Thread.Sleep(sleepTime);
  18.         }
  19.     }
  20. }
  21.  
  22. public class Aquarium
  23. {
  24.     private List<Fish> _fishes;
  25.     private Random _random;
  26.     private long _years;
  27.  
  28.     private int _lifeAmountMinimum = 15;
  29.     private int _lifeAmountMaximum = 80;
  30.     private int _lifeDecreasePerIterationMinimum = 1;
  31.     private int _lifeDecreasePerIterationMaximum = 5;
  32.     private string[] _names =
  33.     {
  34.         "Char", "Guppy", "Rainbowfish", "Arapaima", "Sea butterfly",
  35.         "Clown fish", "Surgeonfish", "Squid", "Tuna", "Suckerfish"
  36.     };
  37.  
  38.     public Aquarium()
  39.     {
  40.         _random = new Random();
  41.         _fishes = new List<Fish>();
  42.         _years = 1;
  43.         CreateFishes();
  44.     }
  45.  
  46.     public void Update()
  47.     {
  48.         Console.Clear();
  49.         PrintFishes();
  50.         ReadKeys();
  51.         DecreaseLife();
  52.         ++_years;
  53.     }
  54.  
  55.     public bool IsLive()
  56.     {
  57.         if (_fishes.Count > 0)
  58.         {
  59.             Console.WriteLine("Still live");
  60.             return true;
  61.         }
  62.         else
  63.         {
  64.             Console.ForegroundColor = ConsoleColor.Red;
  65.             Console.WriteLine("†††FISHES DEAD†††");
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     private void CreateFishes()
  71.     {
  72.         for (int i = 0, amountFishes = 2; i < amountFishes; ++i)
  73.             AddFish();
  74.     }
  75.  
  76.     private void AddFish()
  77.     {
  78.         int nameIndex = _random.Next(_names.Length);
  79.         int life = _random.Next(_lifeAmountMinimum, _lifeAmountMaximum);
  80.         int lifeDecreasePerIteration = _random.Next(_lifeDecreasePerIterationMinimum, _lifeDecreasePerIterationMaximum);
  81.  
  82.         _fishes.Add(new Fish(_names[nameIndex], life, lifeDecreasePerIteration));
  83.     }
  84.  
  85.     private void RemoveFish(int index)
  86.     {
  87.         _fishes.RemoveAt(index);
  88.     }
  89.  
  90.     private void DecreaseLife()
  91.     {
  92.         for (int i = 0; i < _fishes.Count; ++i)
  93.         {
  94.             _fishes[i].DecreaseLife();
  95.  
  96.             if (_fishes[i].IsDead())
  97.                 RemoveFish(i);
  98.         }
  99.     }
  100.  
  101.     private void PrintFishes()
  102.     {
  103.         Console.WriteLine($"Year : {_years}");
  104.         Console.WriteLine($"Amount of fishes : {_fishes.Count}");
  105.         Console.WriteLine($"\nInput to number : ");
  106.         Console.WriteLine($"0. Add Fish.");
  107.         Console.WriteLine($"1. Remove Fish.");
  108.         Console.WriteLine("\n Fishes : ");
  109.  
  110.         for (int i = 0; i < _fishes.Count; ++i)
  111.             Console.WriteLine($"{i}.\n{_fishes[i]}");
  112.     }
  113.  
  114.     private void ReadKeys()
  115.     {
  116.         if (Console.KeyAvailable)
  117.         {
  118.             ConsoleKey key = Console.ReadKey(true).Key;
  119.            
  120.             if (key.Equals(ConsoleKey.D0))
  121.             {
  122.                 AddFish();
  123.             }
  124.             else if (key.Equals(ConsoleKey.D1))
  125.             {
  126.                 int index = InputIndex();
  127.                 RemoveFish(index);
  128.             }
  129.         }
  130.     }
  131.  
  132.     private int InputIndex()
  133.     {
  134.         Console.Clear();
  135.         Console.WriteLine($"Amount of fishes : {_fishes.Count}");
  136.  
  137.         bool isParse = false;
  138.         int result = 0;
  139.  
  140.         do
  141.         {
  142.             Console.WriteLine("Please input a index of fish : ");
  143.             isParse = int.TryParse(Console.ReadLine(), out result);
  144.         }
  145.         while (isParse != true);
  146.  
  147.         return result;
  148.     }
  149. }
  150.  
  151. public class Fish
  152. {
  153.     private string _name;
  154.     private int _life;
  155.     private int _lifeDecreasePerIteration;
  156.  
  157.     public Fish(string name, int life, int lifeDecreasePerIteration)
  158.     {
  159.         _name = name;
  160.         _life = life;
  161.         _lifeDecreasePerIteration = lifeDecreasePerIteration;
  162.     }
  163.  
  164.     public void DecreaseLife()
  165.     {
  166.         _life -= _lifeDecreasePerIteration;
  167.     }
  168.  
  169.     public bool IsDead()
  170.     {
  171.         return _life <= 0;
  172.     }
  173.  
  174.     public override string ToString()
  175.     {
  176.         string result = "";
  177.         result += "Name fish : " + _name + ".\n";
  178.         result += "Age to live : " + _life + ".\n";
  179.         return result;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement