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.
The program is given below. The comments are provided for the better understanding of the logic.
using System;
using System.Collections.Generic;
public class Program
{
    public static void Main(string[] args)
    {
        //Declare the variables for holding temporary values..
        string species;
        string age;
        //Declare the animal interface
        Animal animal = null;
        //Declare the animals list.
        List<Animal> animalsList = new List<Animal>(3);
        //Start a loop 3 times to accept inputs of 3 cages.
        for(int i = 1; i <=3; i++)
        {
            //Get the inputs for species and age.
            string cageLabel = "Cage " + i;
            Console.WriteLine(cageLabel);
            Console.Write("What is the animal’s species? ");
            species = Console.ReadLine();
            Console.Write("How old is it? ");
            age = Console.ReadLine();
            //Depending on what species is entered, the below switch statement will instantiate a corrresponding class (Lion, Bear or Wolf)
            //It also calls the RequestUniqueCharacteristic method which will get the input of the unique characteristic
            //In case of wrong species is entered, the control will enter the default block and display an error message.
            switch (species.ToLower().Trim())
            {
                case "lion":
                    animal = new Lion(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                case "wolf":
                    animal = new Wolf(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                case "bear":
                    animal = new Bear(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                default:
                    Console.WriteLine("Invalid Species");
                    break;
            }
        }
        //After all the inputs are received, display all the 3 cage and animal information.
        Console.WriteLine("=====");
        foreach (Animal a in animalsList)
        {
            a.GetDescription(); 
        }
        Console.ReadLine();
    }
}
//Create the animal interface as below.
//It has 2 abstract methods.
interface Animal
{
    void RequestUniqueCharacteristic();
    void GetDescription();
}
//The Lion Class is created as below.
class Lion : Animal
{
    private string cage;
    private int age;
    private string species;
    private string maneColour;
    //Default Constructor
    public Lion(string cage, string age, string species)
    {
        this.cage = cage;
        //The age is received as string from the main program.  Convert to int and store it in age.
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("What colour is its mane? ");
        maneColour = Console.ReadLine().ToLower();
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        Console.WriteLine(cage +  " contains a " + age + "-year-old " + species + " with a " + maneColour + " mane.");
    }
}
//The Bear Class is created as below.
class Bear : Animal
{
    private string cage;
    private int age;
    private string species;
    private bool isGrizzly;
    //Default Constructor
    public Bear(string cage, string age, string species)
    {
        this.cage = cage;
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("Is it a grizzly bear (true/false)? ");
        string characteristic = Console.ReadLine().ToLower().Trim();
        //The user can enter yes/no or true/false
        //Check it accordingly and store true or false in the field.
        if (characteristic == "yes" || characteristic == "true")
            isGrizzly = true;
        else
            isGrizzly = false;
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        //Depending on whether the bear is grizzly or not, display the information accordingly.
        if (isGrizzly)
            Console.WriteLine(cage + " contains a " + age + "-year-old grizzly " + species + ".");
        else
            Console.WriteLine(cage + " contains a " + age + "-year-old non-grizzly " + species + ".");
    }
}
//The Wolf Class is created as below.
class Wolf : Animal
{
    private string cage;
    private int age;
    private string species;
    private int speed;
    //Default Constructor
    public Wolf(string cage, string age, string species)
    {
        this.cage = cage;
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("How fast can it run (in km/h)? ");
        //Convert it into int before storing
        speed = Convert.ToInt32(Console.ReadLine());
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        Console.WriteLine(cage + " contains a " + age + "-year-old " + species + " that runs " + speed + " km/h.");
    }
}
The screenshots of the code and output are provided below.





