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();
- _manager.Fill(_fishes, _size);
- }
- public void Live()
- {
- int turn = 1;
- Console.WriteLine("Turn :" + turn++);
- _manager.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.GrowOldFishes(_fishes);
- _manager.KillFishes(_fishes, _size);
- _manager.RemoveFishes(_fishes, _size);
- _manager.ShowAllFishes(_fishes);
- }
- }
- }
- public class Manager
- {
- private FishFactory _fishFactory;
- private List<Characteristics> _openedCharacteristics;
- public Manager()
- {
- _fishFactory = new FishFactory();
- _openedCharacteristics = new List<Characteristics>();
- _fishFactory.OpenCharacteristic(_openedCharacteristics);
- }
- 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:
- IncreaseFishes(fishes);
- break;
- case CommandRemoveFish:
- DecreaseFishes(fishes);
- break;
- case CommandShowFish:
- CallShowFishCommand(fishes);
- break;
- case CommandExitMenu:
- hasSelected = true;
- break;
- case CommandExit:
- CallExitCommand(out hasSelected, out isRunning);
- break;
- default:
- CallDefaultCommand();
- break;
- }
- }
- return isRunning;
- }
- public void Fill(List<Fish> fishes, int aquariumSize)
- {
- int fishListCount = _openedCharacteristics.Count;
- int minOccupiedVolume = _openedCharacteristics.Min(fish => fish.OccupiedSize);
- for (int size = 0; size < aquariumSize;)
- {
- Fish fish = _fishFactory.CreateFish(UserUtil.GenerateRandomNumber(fishListCount));
- if (size + fish.Characteristics.OccupiedSize > aquariumSize)
- {
- fishListCount = fishListCount > minOccupiedVolume ? fishListCount-- : minOccupiedVolume;
- continue;
- }
- fishes.Add(fish);
- size += fish.Characteristics.OccupiedSize;
- }
- }
- public void ShowAllFishes(List<Fish> fishes)
- {
- foreach (Fish 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 (Fish fish in ordered)
- {
- Console.ForegroundColor = fish.Characteristics.Color;
- Console.WriteLine($"{fish.Characteristics.Name} - {fish.Age}({fish.Characteristics.LifeExpectancy})");
- }
- Console.ResetColor();
- }
- public void GrowOldFishes(List<Fish> fishes)
- {
- foreach (Fish fish in fishes)
- {
- fish.IncreaseAge();
- }
- }
- 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 void KillFishes(List<Fish> fishes, int aquariumSize)
- {
- int quantityPerLiter = 10;
- int maxFishCount = aquariumSize * quantityPerLiter;
- int volumeFish = fishes.Sum(fish => fish.Characteristics.OccupiedSize);
- int fullMultiplier = VerifyFullVolume(volumeFish, aquariumSize);
- if (volumeFish >= maxFishCount)
- {
- foreach (Fish fish in fishes)
- {
- fish.Kill();
- }
- }
- else
- {
- foreach (Fish 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;
- }
- private void IncreaseFishes(List<Fish> fishes)
- {
- string action = "add";
- int? typeNumber = GetFishType(action);
- if (typeNumber.HasValue == false)
- {
- return;
- }
- int amount = GetFishAmount(action);
- for (int i = 0; i < amount; i++)
- {
- fishes.Add(_fishFactory.CreateFish((int)typeNumber));
- }
- }
- private void DecreaseFishes(List<Fish> fishes)
- {
- string action = "remove";
- int? typeNumber = GetFishType(action);
- if (typeNumber.HasValue == false)
- {
- return;
- }
- string fishName = _openedCharacteristics[(int)typeNumber].Name;
- int nameCount = fishes.Count(fish => fish.Characteristics.Name == fishName);
- Console.WriteLine($"There are {nameCount} fish of this type");
- int amount = GetFishAmount(action);
- 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--;
- }
- }
- }
- private int? GetFishType(string action)
- {
- _fishFactory.ShowListCharacteristics();
- Console.Write($"\nChoose the type of fish to {action}: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < _openedCharacteristics.Count)
- {
- return typeNumber;
- }
- else
- {
- Console.WriteLine("Wrong type. Press something...");
- Console.ReadKey();
- return null;
- }
- }
- private int GetFishAmount(string action)
- {
- Console.WriteLine($"How many fish to {action}? ");
- if (int.TryParse(Console.ReadLine(), out int amount) && amount > 0)
- {
- return amount;
- }
- else
- {
- Console.WriteLine("Wrong amount. Press something...");
- Console.ReadKey();
- return 0;
- }
- }
- private void CallShowFishCommand(List<Fish> fishes)
- {
- Console.Clear();
- ShowAllFishes(fishes);
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- }
- private void CallExitCommand(out bool hasSelected, out bool isRunning)
- {
- hasSelected = true;
- isRunning = false;
- }
- private void CallDefaultCommand()
- {
- Console.WriteLine("Wrong key");
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- }
- }
- public class FishFactory
- {
- private CharacteristicsProvider _characteristicsProvider;
- public FishFactory()
- {
- _characteristicsProvider = new CharacteristicsProvider();
- }
- public Fish CreateFish(int index)
- {
- if (index >= 0 && index < _characteristicsProvider.GetCharacteristicsCount())
- {
- Characteristics characteristics = _characteristicsProvider.GetCharacteristics(index);
- return new Fish(characteristics);
- }
- else
- {
- return null;
- }
- }
- public void ShowListCharacteristics()
- {
- Console.Write("There is the following fish: ");
- for (int i = 0; i < _characteristicsProvider.GetCharacteristicsCount(); i++)
- {
- Console.Write($"{i} - {_characteristicsProvider.GetCharacteristics(i).Name} ");
- }
- }
- public List<Characteristics> OpenCharacteristic(List<Characteristics>characteristics)
- {
- for (int i = 0; i < _characteristicsProvider.GetCharacteristicsCount(); i++)
- {
- characteristics.Add(_characteristicsProvider.GetCharacteristics(i));
- }
- return characteristics;
- }
- }
- public class CharacteristicsProvider
- {
- private List<Characteristics> _presetCharacteristics = new List<Characteristics>();
- public CharacteristicsProvider()
- {
- _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 GetCharacteristics(int index) => _presetCharacteristics[index];
- public int GetCharacteristicsCount() => _presetCharacteristics.Count;
- }
- public class Characteristics
- {
- 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 class Fish
- {
- public Fish(Characteristics characteristics)
- {
- Characteristics = characteristics;
- IsAlive = true;
- InitiateAge();
- }
- public Characteristics Characteristics { get; private set; }
- 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 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