In: Computer Science
C#
The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species.
Sample Session
Cage 1
What is the animal’s species? Lion
How old is it? 6
What colour is its mane? Brown
Cage 2
What is the animal’s species? Wolf
How old is it? 9
How fast can it run (in km/h)? 20
Cage 3
What is the animal’s species? Bear
How old is it? 12
Is it a grizzly bear (true/false)? No
=====
Cage 1 contains a 6-year-old lion with a brown mane.
Cage 2 contains a 9-year-old wolf that runs 20 km/h.
Cage 3 contains a 12-year-old non-grizzly bear.
Hi
Please find the code below
// Interface Animal
public interface Animal
{
// Properties for age, species with the corresponding
datatype
int age { get; set; }
String species { get; set; }
// declaration for methods
void RequestUniqueCharacteristic();
void GetDescription();
}
//Lion class
public class Lions : Animal
{
// implement the properties
public int age { get; set; }
public string species { get ; set ; }
// unique field for lion
public string maneColour { get; set; }
// implement getdescription()
public void GetDescription()
{
Console.WriteLine($" {this.age}-year-old lion with a
{this.maneColour} mane.");
}
// implement requestUniqueCharacteristic
void Animal.RequestUniqueCharacteristic()
{
Console.WriteLine("What colour is its mane?");
this.maneColour = Console.ReadLine();
}
}
public class Wolves : Animal
{
// implement the properties
public int age { get; set; }
public string species { get; set; }
// unique field for wolf
public int speed { get; set; }
// implement getdescription()
public void GetDescription()
{
Console.WriteLine($" {this.age}-year-old wolf that runs
{this.speed} km/h.");
}
// implement requestUniqueCharacteristic
void Animal.RequestUniqueCharacteristic()
{
Console.WriteLine("How fast can it run (in km/h)?");
this.speed = Convert.ToInt32(Console.ReadLine());
}
}
public class Bears : Animal
{
// implement the properties
public int age { get; set; }
public string species { get; set; }
//unique field for bear
public bool isGrizzly { get; set; }
// implement getdescription()
public void GetDescription()
{
if (this.isGrizzly)
Console.WriteLine($" {this.age}-year-old grizzly bear.");
else
Console.WriteLine($" {this.age}-year-old non-grizzly bear.");
}
// implement requestUniqueCharacteristic
void Animal.RequestUniqueCharacteristic()
{
Console.WriteLine("Is it a grizzly bear (true/false)");
var boolString = (Console.ReadLine());
if (boolString == "true")
this.isGrizzly = true;
else
this.isGrizzly = false;
}
}
// program class
class Program
{
// main function
static void Main(string[] args)
{
// List of Animal
List<Animal> animals = new List<Animal>();
// counter initialise with 1
int i = 1;
// iterate 3 times to take the information for 3 cages as per the
requirement mentioned
while (i < 4)
{
Animal animal = null;
// ask for the information of cage
Console.WriteLine($"Enter the information for Cage {i}");
Console.WriteLine("What is the animal’s species?");
var species = Console.ReadLine();
if (species.Trim().ToLower() == "lion")
animal = new Lions();
if (species.Trim().ToLower() == "wolf")
animal = new Wolves();
if (species.Trim().ToLower() == "bear")
animal = new Bears();
animal.species = species;
Console.WriteLine("How old is it? ");
// store the details
animal.age = Convert.ToInt32(Console.ReadLine());
animal.RequestUniqueCharacteristic();
// add the animal in animals list
animals.Add(animal);
// increment the counter
i++;
}
for (i = 0; i < animals.Count(); i++)
{
// diplay the cage index
Console.Write($"Cage {i + 1} contains a");
// call getdescription
animals[i].GetDescription();
}
} }
OUTPUT:
Thanks
Hope it helps!!