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()
- {
- Zoo zoo = new Zoo();
- zoo.Work();
- }
- }
- public class Zoo
- {
- private List<Animals> _animals;
- private List<Animals>[] _cages;
- private string[] _cagesName;
- public Zoo()
- {
- _animals = new List<Animals>
- {
- new Animal1() ,
- new Animal2() ,
- new Animal3() ,
- new Animal4()
- };
- _cages = new List<Animals>[_animals.Count];
- _cagesName = new string[_cages.Length];
- foreach (var animal in _animals.OfType<IIncreaseable>())
- {
- animal.IncreaseRequested += Increase;
- }
- foreach (var animal in _animals.OfType<IRenovateable>())
- {
- animal.RenovateRequested += Renovate;
- }
- FillCages();
- }
- public void Work()
- {
- bool isWorking = true;
- Console.WriteLine("Welcome in Zoo");
- while (isWorking)
- {
- DisplayCages();
- isWorking = ProcessUserInput();
- }
- }
- private void DisplayCages()
- {
- Console.WriteLine($"There are {_cages.Length} cages");
- for (int i = 0; i < _cagesName.Length; i++)
- {
- Console.WriteLine($"Cage {i + 1} {_cagesName[i]}");
- }
- }
- private bool ProcessUserInput()
- {
- const int CommandExit = 5;
- Console.WriteLine($"\nWhich cage do you want to visit? or {CommandExit} for exit..");
- int.TryParse(Console.ReadLine(), out int userInput);
- if (userInput == CommandExit)
- {
- return false;
- }
- if (TrySelectCage(userInput - 1))
- {
- _cagesName[userInput - 1] = "contains " + _animals[userInput - 1].Name;
- return true;
- }
- return true;
- }
- private bool TrySelectCage(int index)
- {
- if (index >= 0 && index < _cages.Length)
- {
- ShowCage(index);
- Interact(index);
- return true;
- }
- else
- {
- Console.WriteLine("No such cage");
- return false;
- }
- }
- private void ShowCage(int index)
- {
- Console.WriteLine($"There are {_cages[index].Count} {_animals[index].Name} contains in the cage, " +
- $"their gender is:\n{GetAnimalsGender(_cages[index])}");
- _animals[index].Speaks();
- }
- private string GetAnimalsGender(List<Animals> animals)
- {
- var genders = animals.Select(animal => animal.Gender).Distinct().OrderBy(gender => gender);
- string animalsGender = string.Join(", ", genders);
- return animalsGender;
- }
- private void Interact(int index)
- {
- _animals[index].InteractWithVisitor(index);
- }
- private void FillCages()
- {
- for (int i = 0; i < _cages.Length; i++)
- {
- _cages[i] = new List<Animals>();
- for (int j = 0; j < UserUtil.GetRandomNumber(_animals[i].MinCountCage, _animals[i].MaxCountCage + 1); j++)
- {
- _cages[i].Add(_animals[i].Clone());
- }
- }
- }
- private void Increase(int index)
- {
- _cages[index].Add(_animals[index].Clone());
- }
- private void Renovate(int index)
- {
- int numberRemoved = 3;
- int numberAdded = 3;
- for (int i = 0; i < UserUtil.GetRandomNumber(numberAdded + 1); i++)
- {
- if (_cages[index].Count == _animals[index].MaxCountCage)
- {
- continue;
- }
- _cages[index].Add(_animals[index].Clone());
- }
- for (int i = 0; i < UserUtil.GetRandomNumber(numberRemoved + 1); i++)
- {
- if (_cages[index].Count == _animals[index].MinCountCage)
- {
- continue;
- }
- _cages[index].RemoveAt(UserUtil.GetRandomNumber(_cages[index].Count));
- }
- }
- }
- public abstract class Animals
- {
- public string Name { get; protected set; }
- public string Gender { get; protected set; }
- public int MaxCountCage { get; protected set; }
- public int MinCountCage { get; protected set; }
- public abstract void Speaks();
- public abstract string InitiateGender();
- public abstract Animals Clone();
- public virtual void InteractWithVisitor(int index)
- {
- Console.WriteLine("You return to the cage selection. Press something..\n");
- Console.ReadKey(true);
- }
- }
- public interface IIncreaseable
- {
- event Action<int> IncreaseRequested;
- }
- public class Animal1 : Animals, IIncreaseable
- {
- public event Action<int> IncreaseRequested;
- public Animal1()
- {
- Name = "Flamibia";
- Gender = InitiateGender();
- MaxCountCage = 100;
- MinCountCage = 70;
- }
- public override void Speaks()
- {
- Console.WriteLine($"You can't hear anything from the {Name} cage");
- }
- public override string InitiateGender()
- {
- int genderCount = 784;
- Gender = "Gender " + (UserUtil.GetRandomNumber(genderCount) + 1);
- return Gender;
- }
- public override Animals Clone()
- {
- return new Animal1();
- }
- public override void InteractWithVisitor(int index)
- {
- int maxStep = 3;
- for (int i = 0; i < maxStep; i++)
- {
- Console.WriteLine($"You are coming back. You hear a voice of {Name}. Press something..");
- Console.ReadKey(true);
- }
- Console.WriteLine($"You became {Name}. You understand what {Name} say. Press something..");
- IncreaseRequested?.Invoke(index);
- Console.ReadKey(true);
- Console.WriteLine("You were reborn.. Press something..\n");
- Console.ReadKey(true);
- }
- }
- public class Animal2 : Animals
- {
- public Animal2()
- {
- Name = "Qoster";
- Gender = InitiateGender();
- MaxCountCage = 12;
- MinCountCage = 8;
- }
- public override void Speaks()
- {
- Console.WriteLine($"You can hear: cheepcheepcheepcheepcheepcheep");
- }
- public override string InitiateGender()
- {
- string[] genders = { "XX", "XT", "TT", "ZZ" };
- Gender = genders[UserUtil.GetRandomNumber(genders.Length)];
- return Gender;
- }
- public override Animals Clone()
- {
- return new Animal2();
- }
- }
- public interface IRenovateable
- {
- event Action<int> RenovateRequested;
- }
- public class Animal3 : Animals, IRenovateable
- {
- public event Action<int> RenovateRequested;
- public Animal3()
- {
- Name = "Kedenee";
- Gender = InitiateGender();
- MaxCountCage = 7;
- MinCountCage = 1;
- }
- public override void Speaks()
- {
- Console.WriteLine("You hear just quiet music");
- }
- public override string InitiateGender()
- {
- Gender = "Genderless";
- return Gender;
- }
- public override Animals Clone()
- {
- return new Animal3();
- }
- public override void InteractWithVisitor(int index)
- {
- RenovateRequested?.Invoke(index);
- Console.WriteLine("You wanted to come back. But you were eaten. You were reborn.. Press something..\n");
- Console.ReadKey(true);
- }
- }
- public class Animal4 : Animals
- {
- public Animal4()
- {
- Name = "Wooni";
- Gender = InitiateGender();
- MaxCountCage = 20;
- MinCountCage = 10;
- }
- public override void Speaks()
- {
- string[] sounds =
- {
- "let's have a drink",
- "do you have cigarettes!",
- "have some change!"
- };
- Console.WriteLine($"You can hear: {sounds[UserUtil.GetRandomNumber(sounds.Length)]}");
- }
- public override string InitiateGender()
- {
- int genderCount = 2;
- Gender = "Parent " + (UserUtil.GetRandomNumber(genderCount) + 1);
- return Gender;
- }
- public override Animals Clone()
- {
- return new Animal4();
- }
- }
- public static class UserUtil
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int GetRandomNumber(int max)
- {
- return s_random.Next(max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement