Skip to content

Instantly share code, notes, and snippets.

@GigaOrts
Forked from Velsimir/ООП. Зоопарк.cs
Created November 6, 2022 10:22
Show Gist options
  • Save GigaOrts/0e878d34ad9285bfa2ee67613ee225c8 to your computer and use it in GitHub Desktop.
Save GigaOrts/0e878d34ad9285bfa2ee67613ee225c8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace iJunior
{
class MainClass
{
public static void Main(string[] args)
{
Zoo zoo = new Zoo();
zoo.Menu();
}
}
class Zoo
{
private List<Aviary> _aviaries = new List<Aviary>();
private const string CommandOne = "1";
private const string CommandTwo = "2";
private const string CommandThree = "3";
private const string CommandFour = "4";
private const string CommandExit = "Exit";
public Zoo()
{
_aviaries.Add(new Aviary(1));
_aviaries.Add(new Aviary(2));
_aviaries.Add(new Aviary(3));
_aviaries.Add(new Aviary(4));
}
public void Menu()
{
bool isWorking = true;
string userInput;
Console.WriteLine($"Welcome to Zoo!");
while (isWorking)
{
Console.Clear();
Console.WriteLine($"Which aviar do you want to chose?" +
$"\n{CommandOne}" +
$"\n{CommandTwo}" +
$"\n{CommandThree}" +
$"\n{CommandFour}" +
$"\n{CommandExit}");
userInput = Console.ReadLine();
switch (userInput)
{
case CommandOne:
_aviaries[0].ShowInfo();
break;
case CommandTwo:
_aviaries[1].ShowInfo();
break;
case CommandThree:
_aviaries[2].ShowInfo();
break;
case CommandFour:
_aviaries[3].ShowInfo();
break;
case CommandExit:
isWorking = false;
break;
}
Console.ReadKey();
}
}
}
class Aviary
{
private List<Animal> _animals;
private int _maleCount;
private int _femaleCount;
private const int CommandCat = 1;
private const int CommandDog = 2;
private const int CommandCow = 3;
private const int CommandMice = 4;
private const string CommandYes = "Yes";
private const string CommandNo = "No";
public Aviary(int choseAnimal)
{
AddAnimals(choseAnimal);
CountSexAnimal();
}
public void ShowInfo()
{
Console.WriteLine($"Aviary:\n{_animals[0].Name} - {_animals[0].Discription}." +
$"\nAnimals:{_animals.Count()}\tMale: {_maleCount}\tFemale: {_femaleCount}");
ListenSound();
}
private void ListenSound()
{
bool isChosing = true;
string userInput;
do
{
Console.WriteLine("Wanna listen animals sound?");
userInput = Console.ReadLine();
switch (userInput)
{
case CommandYes:
_animals[0].MakeSound();
isChosing = false;
break;
case CommandNo:
isChosing = false;
break;
default:
Console.WriteLine("Incorrect command");
break;
}
} while (isChosing);
}
private void AddAnimals(int choseAnimal)
{
Random random = new Random();
int minAnimals = 5;
int maxAnimals = 15;
int _countOfAnimalsToCreate = random.Next(minAnimals, maxAnimals);
_animals = new List<Animal>();
for (int i = 0; i < _countOfAnimalsToCreate; i++)
{
_animals.Add(GetAnimal(choseAnimal));
}
}
private void CountSexAnimal()
{
foreach (var animal in _animals)
{
if (animal.SexFemale == true)
{
_femaleCount++;
}
else if (animal.SexMale == true)
{
_maleCount++;
}
}
}
private Animal GetAnimal(int choseAnimal)
{
switch (choseAnimal)
{
case CommandCat:
return new Cat();
case CommandDog:
return new Dog();
case CommandCow:
return new Cow();
case CommandMice:
return new Mice();
}
return new Cat();
}
}
abstract class Animal
{
private Random _random = new Random();
private int _chanseChose = 50;
public string Name { get; private set; }
public string Discription { get; private set; }
public string Sound { get; private set; }
public bool SexMale { get; private set; }
public bool SexFemale { get; private set; }
protected Animal(string name, string sound, string discription)
{
int sex;
Discription = discription;
Name = name;
Sound = sound;
sex = _random.Next(100);
if (sex >= _chanseChose)
{
SexFemale = true;
}
else
{
SexMale = true;
}
}
public void MakeSound()
{
Console.WriteLine($"{Name} make sound {Sound}");
}
}
class Cat : Animal
{
public Cat() : base("Cat", "Meow", "The cat (Felis catus) is a domestic species of small carnivorous mammal") { }
}
class Dog : Animal
{
public Dog() : base("Dog", "Bark", "The dog (Canis familiaris or Canis lupus familiaris) is a domesticated descendant of the wolf") { }
}
class Cow : Animal
{
public Cow() : base("Cow", "Moo", "Cows are peaceful animals. They are vegetarians and eat grass, giving people milk") { }
}
class Mice : Animal
{
public Mice() : base("Mice", "Squeak", "Characteristically, mice are known to have a pointed snout, small rounded ears, a body-length scaly tail, and a high breeding rate") { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment