Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main()
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Live();
- }
- }
- public class Aquarium
- {
- private List<Fish> _fishes;
- private int _size;
- private Manager _manager;
- public Aquarium()
- {
- _size = 50;
- _fishes = new List<Fish>();
- _manager = new Manager(new Filler(), new Grower(), new Killer(), new Remover(), new Shower(), new Increaser(), new Decreaser());
- _manager.Filler.Fill(_fishes, _size);
- }
- public void Live()
- {
- int turn = 1;
- Console.WriteLine("Turn :" + turn++);
- _manager.Shower.ShowAllFishes(_fishes);
- bool isRunning = true;
- ConsoleKey menuLaunchKey = ConsoleKey.D0;
- string menuLaunch = "0";
- while (isRunning)
- {
- Console.WriteLine($"\nPress something for next turn... or {menuLaunch} for menu");
- if (Console.ReadKey().Key == menuLaunchKey)
- {
- Console.Clear();
- isRunning = _manager.LaunchMenu(_fishes);
- continue;
- }
- Console.Clear();
- Console.WriteLine("Turn :" + turn++);
- _manager.Grower.GrowOldFishes(_fishes);
- _manager.Killer.KillFishes(_fishes, _size);
- _manager.Remover.RemoveFishes(_fishes, _size);
- _manager.Shower.ShowAllFishes(_fishes);
- }
- }
- }
- public class Manager
- {
- private IIncreasable _increaser;
- private IDecreasable _decreaser;
- public IFillable Filler;
- public IGrowable Grower;
- public IKillable Killer;
- public IRemoveable Remover;
- public IShowable Shower;
- public Manager(IFillable filler, IGrowable grower, IKillable killer, IRemoveable remover, IShowable shower, IIncreasable increaser, IDecreasable decreaser)
- {
- _increaser = increaser;
- _decreaser = decreaser;
- Filler = filler;
- Grower = grower;
- Killer = killer;
- Remover = remover;
- Shower = shower;
- }
- public bool LaunchMenu(List<Fish> fishes)
- {
- const string CommandAddFish = "1";
- const string CommandRemoveFish = "2";
- const string CommandShowFish = "3";
- const string CommandExitMenu = "4";
- const string CommandExit = "5";
- bool hasSelected = false;
- bool isRunning = true;
- while (hasSelected == false)
- {
- Console.Clear();
- Console.WriteLine($"Press {CommandAddFish} for add fish," +
- $"\n {CommandRemoveFish} for remove," +
- $"\n {CommandShowFish} for show," +
- $"\n {CommandExitMenu} for exit the menu" +
- $"\n or {CommandExit} for close aquarium");
- string userCommand = Console.ReadLine();
- switch (userCommand)
- {
- case CommandAddFish:
- _increaser.IncreaseFishes(fishes);
- break;
- case CommandRemoveFish:
- _decreaser.DecreaseFishes(fishes);
- break;
- case CommandShowFish:
- Console.Clear();
- Shower.ShowAllFishes(fishes);
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- break;
- case CommandExitMenu:
- hasSelected = true;
- break;
- case CommandExit:
- hasSelected = true;
- isRunning = false;
- break;
- default:
- Console.WriteLine("Wrong key");
- break;
- }
- }
- return isRunning;
- }
- }
- public interface IFillable
- {
- public void Fill(List<Fish> fishes, int aquariumSize);
- }
- public class Filler : IFillable
- {
- public void Fill(List<Fish> fishes, int aquariumSize)
- {
- int fishListCount = Characteristics.PresetCharacteristics.Count;
- int minOccupiedVolume = Characteristics.PresetCharacteristics.Min(fish => fish.OccupiedSize);
- for (int size = 0; size < aquariumSize;)
- {
- Characteristics currentCharacteristic = Characteristics.PresetCharacteristics[UserUtil.GenerateRandomNumber(fishListCount)];
- if (size + currentCharacteristic.OccupiedSize > aquariumSize)
- {
- fishListCount = fishListCount > minOccupiedVolume ? fishListCount-- : minOccupiedVolume;
- continue;
- }
- fishes.Add(new Fish(currentCharacteristic));
- size += currentCharacteristic.OccupiedSize;
- }
- }
- }
- public interface IShowable
- {
- public void ShowAllFishes(List<Fish> fishes);
- }
- public class Shower : IShowable
- {
- public void ShowAllFishes(List<Fish> fishes)
- {
- foreach (var fish in fishes)
- {
- Console.ForegroundColor = fish.Characteristics.Color;
- int minLeftPosition = 30;
- int maxLeftPosition = 110;
- int minTopPosition = 1;
- int maxTopPosition = 14;
- Console.SetCursorPosition(UserUtil.GenerateRandomNumber(minLeftPosition, maxLeftPosition + 1), UserUtil.GenerateRandomNumber(minTopPosition, maxTopPosition + 1));
- Console.WriteLine(fish.Characteristics.Image);
- }
- var ordered = fishes.OrderBy(fish => fish.Characteristics.Name).ThenBy(fish => fish.Age);
- int leftPosition = 0;
- int topPosition = 4;
- Console.SetCursorPosition(leftPosition, topPosition);
- foreach (var fish in ordered)
- {
- Console.ForegroundColor = fish.Characteristics.Color;
- Console.WriteLine($"{fish.Characteristics.Name} - {fish.Age}({fish.Characteristics.LifeExpectancy})");
- }
- Console.ResetColor();
- }
- }
- public interface IGrowable
- {
- public void GrowOldFishes(List<Fish> fishes);
- }
- public class Grower : IGrowable
- {
- public void GrowOldFishes(List<Fish> fishes)
- {
- foreach (var fish in fishes)
- {
- fish.IncreaseAge();
- }
- }
- }
- public interface IKillable
- {
- public void KillFishes(List<Fish> fishes, int aquariumSize);
- }
- public class Killer : IKillable
- {
- public void KillFishes(List<Fish> fishes, int aquariumSize)
- {
- int fishCount = Characteristics.PresetCharacteristics.Count;
- int quantityPerLiter = 10;
- int maxFishCount = aquariumSize * quantityPerLiter;
- int volumeFish = fishes.Sum(fish => fish.Characteristics.OccupiedSize);
- int fullMultiplier = VerifyFullVolume(volumeFish, aquariumSize);
- if (volumeFish >= maxFishCount)
- {
- foreach (var fish in fishes)
- {
- fish.Kill();
- }
- }
- else
- {
- foreach (var fish in fishes)
- {
- int probabilityDeath = VerifyAge(fish);
- probabilityDeath *= VerifyAlone(fish, fishes);
- probabilityDeath *= fullMultiplier;
- int maxChance = 100;
- if (UserUtil.GenerateRandomNumber(maxChance + 1) < probabilityDeath)
- {
- fish.Kill();
- }
- }
- }
- }
- private int VerifyFullVolume(int volumeFish, int aquariumSize)
- {
- int multiplier = 1;
- int fullMultiplier = 2;
- if (volumeFish > aquariumSize)
- {
- multiplier = fullMultiplier;
- }
- return multiplier;
- }
- private int VerifyAge(Fish fish)
- {
- int probabilityDeath = 2;
- int firstHalf = 2;
- int secondHalf = 5;
- int longLife = 90;
- int lifePeriod = 2;
- if (fish.Age < fish.Characteristics.LifeExpectancy / lifePeriod)
- {
- probabilityDeath = firstHalf;
- }
- else if (fish.Age < fish.Characteristics.LifeExpectancy)
- {
- probabilityDeath = secondHalf;
- }
- else if (fish.Age >= fish.Characteristics.LifeExpectancy)
- {
- probabilityDeath = longLife;
- }
- return probabilityDeath;
- }
- private int VerifyAlone(Fish fish, List<Fish> fishes)
- {
- string fishName = fish.Characteristics.Name;
- int nameCount = fishes.Count(fish => fish.Characteristics.Name == fishName);
- int multiplier = 1;
- int aloneMultiplier = 2;
- if (nameCount == 1)
- {
- multiplier = aloneMultiplier;
- }
- return multiplier;
- }
- }
- public interface IRemoveable
- {
- public void RemoveFishes(List<Fish> fishes, int aquariumSize);
- }
- public class Remover : IRemoveable
- {
- public void RemoveFishes(List<Fish> fishes, int aquariumSize)
- {
- int quantityPerLiter = 10;
- int maxFishCount = aquariumSize * quantityPerLiter;
- int volumeFish = fishes.Sum(fish => fish.Characteristics.OccupiedSize);
- if (volumeFish >= maxFishCount)
- {
- Console.WriteLine("Too many fish. All died.");
- }
- int numberDead = 0;
- for (int i = fishes.Count - 1; i >= 0; i--)
- {
- if (fishes[i].IsAlive == false)
- {
- fishes.RemoveAt(i);
- numberDead++;
- }
- }
- int leftPosition = 0;
- int topPosition = 2;
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine($"fish died: {numberDead}");
- }
- }
- public interface IIncreasable
- {
- public void IncreaseFishes(List<Fish> fishes);
- }
- public class Increaser : IIncreasable
- {
- public void IncreaseFishes(List<Fish> fishes)
- {
- Characteristics.ShowListCharacteristics();
- Console.Write("\nChoose type of fish to add: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < Characteristics.PresetCharacteristics.Count)
- {
- Console.WriteLine("How many fish to add?");
- if (int.TryParse(Console.ReadLine(), out int amount) && amount > 0)
- {
- for (int i = 0; i < amount; i++)
- {
- fishes.Add(new Fish(Characteristics.PresetCharacteristics[typeNumber]));
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- }
- public interface IDecreasable
- {
- public void DecreaseFishes(List<Fish> fishes);
- }
- public class Decreaser : IDecreasable
- {
- public void DecreaseFishes(List<Fish> fishes)
- {
- Characteristics.ShowListCharacteristics();
- Console.Write("\nChoose the type of fish to remove: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < Characteristics.PresetCharacteristics.Count)
- {
- string fishName = Characteristics.PresetCharacteristics[typeNumber].Name;
- int nameCount = fishes.Count(fish => fish.Characteristics.Name == fishName);
- Console.WriteLine($"How many fish to remove? There are {nameCount} fish of this type");
- if (int.TryParse(Console.ReadLine(), out int amount) && amount > 0)
- {
- if (amount > nameCount)
- {
- Console.WriteLine("The entered quantity exceeds the quantity of fish of this type in the aquarium, delete all fish of this type");
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- }
- amount = Math.Min(amount, nameCount);
- for (int i = fishes.Count - 1; i >= 0; i--)
- {
- if (fishes[i].Characteristics.Name == fishName && amount > 0)
- {
- fishes.RemoveAt(i);
- amount--;
- }
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- }
- public class Fish
- {
- public Characteristics Characteristics { get; private set; }
- public Fish(Characteristics characteristics)
- {
- Characteristics = characteristics;
- IsAlive = true;
- InitiateAge();
- }
- public int Age { get; private set; }
- public bool IsAlive { get; private set; }
- public void IncreaseAge()
- {
- Age++;
- }
- public void Kill()
- {
- IsAlive = false;
- }
- private void InitiateAge()
- {
- int minAge = 1;
- int minLifeSpan = 10;
- int maxAge = Characteristics.LifeExpectancy > minLifeSpan ? Characteristics.LifeExpectancy - minLifeSpan : 1;
- Age = UserUtil.GenerateRandomNumber(minAge, maxAge + 1);
- }
- }
- public class Characteristics
- {
- public static List<Characteristics> PresetCharacteristics = new List<Characteristics>()
- {
- new Characteristics("Guppy", 30, 2, ">-->", ConsoleColor.Cyan),
- new Characteristics("Paracheirodon", 48, 2, ">==>",ConsoleColor.Blue),
- new Characteristics("Xiphophorus",60,4 ,"_>=>",ConsoleColor.Red),
- new Characteristics("Yucatan molly",30,4, ">WW>",ConsoleColor.DarkCyan),
- new Characteristics("Catfish", 96,8,">=>_",ConsoleColor.Gray ),
- new Characteristics("Goldfish",120,12,">=@>",ConsoleColor.DarkYellow)
- };
- public Characteristics(string name, int lifeExpectancy, int occupiedSize, string image, ConsoleColor color)
- {
- Name = name;
- LifeExpectancy = lifeExpectancy;
- OccupiedSize = occupiedSize;
- Image = image;
- Color = color;
- }
- public string Name { get; private set; }
- public int LifeExpectancy { get; private set; }
- public int OccupiedSize { get; private set; }
- public string Image { get; private set; }
- public ConsoleColor Color { get; private set; }
- public static void ShowListCharacteristics()
- {
- Console.Write("There is the following fish: ");
- for (int i = 0; i < PresetCharacteristics.Count; i++)
- {
- Console.Write($"{i} - {PresetCharacteristics[i].Name} ");
- }
- }
- }
- public class UserUtil
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int GenerateRandomNumber(int max)
- {
- return s_random.Next(max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement