TheStudentMans
TheStudentMans

Reputation: 35

Unable to get my program to accept user input

I'm developing an application for a school project that allows a Zoo to keep track of 3 animals (Lion, Tiger, or Bear) in three separate cages but I'm having issues getting the program to store the user input into the fields set within each class.

We have to have 3 classes (Lion, Tiger, Bear) that implement the animal interface which has 2 fields (Species and Age) and 2 methods (GetUniqueCharacterists and GetDescription).

My main issue is that my program will take in the input for the unique characteristic but it will not do the same for species and for age so when I call the GetDescription(); method it only will print out the unique characteristic for each animal class. I'll post my interface, one of the classes, and the program below, if anyone has any ideas I'm all ears! I've never used C# before this so bear with me.

interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace TheZoo
{
    public interface IAnimal
    {
        public string Species { get; set; }
        public int Age { get; set; }

        public void RequestUniqueCharacteristic();

        public void GetDescription();
    }
}

Lion class:

using Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheZoo
{
    internal class Lion:IAnimal
    {
        
        public string Species { get; set; }

        public int Age { get; set; }

        public string ManeColour { get; set; }

        public Lion()
        {

        }

        public Lion(int age, string species, string maneColour)
        {
            Species = species;
            Age = age;
            ManeColour = maneColour;
        }

        public void RequestUniqueCharacteristic()
        {
            
            Console.WriteLine("What colour is the Lions mane?");
            ManeColour = Console.ReadLine();
            
        }

        public void GetDescription()
        {
            Console.WriteLine($"contains a {Age}-year-old {Species} with a {ManeColour} mane.");
        }

    }
}

The output looks something like:

"contains a 0-year-old with a red mane" and the space between "old" and "with" is where the species should be.

Main:

using Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheZoo;

namespace TheZoo
{
    internal class Program
    {
        static void Main(string[] args)
        {


            //variable initialization
            string cage;
            string species;
            int age;

            //Created objects within a list
 
            List<IAnimal> list = new();
            Lion lion = new();
            Bear bear = new();
            Wolf wolf = new();

            list.Add(lion);
            list.Add(bear);
            list.Add(wolf);


            // Start of program, prompts user to input animal attributes
            for (int i = 1; i <= 3; i++)
            {

                cage = "Cage " + i;
                Console.WriteLine(cage);

                Console.WriteLine("What is the animals species? (Please capitalize the first letter)");
                species = Console.ReadLine();
                   
                if (species == "Lion" || species == "Bear" || species == "Wolf")
                {    
                Console.WriteLine("How old is the animal?");
                age = Convert.ToInt16(Console.ReadLine());  
                } 
                else
                {
                    Console.WriteLine("Please enter Lion, Bear, or Wolf with the first letter capitalized.");
                    Console.WriteLine("What is the animals species? (Please capitalize the first letter)");
                    species = Console.ReadLine();
                    Console.WriteLine("How old is the animal?");
                    age = Convert.ToInt32(Console.ReadLine());
                }

                if (species == "Lion")
                {
                    lion.RequestUniqueCharacteristic();
                    Console.WriteLine();
                }
                else if (species == "Bear")
                {
                    bear.RequestUniqueCharacteristic();
                    Console.WriteLine();
                }
                else if (species == "Wolf")
                {
                    wolf.RequestUniqueCharacteristic();
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Something went wrong. Please try again.");
                }
            }

            Console.WriteLine("-----------------------------------");

            // Prints a summary from each class (Bear, Lion, Wolf)
            foreach (IAnimal animal in list)
            {
                animal.GetDescription();
            }
            Console.ReadLine();

            Console.WriteLine("Cages 1 - 3 have been updated.");
        }
    }
}

Upvotes: 0

Views: 49

Answers (1)

William Fleetwood
William Fleetwood

Reputation: 111

No where in your code do you ever actually assign your age variable to the animal objects. Once you get the age variable from the user, you have to do i.e. lion.Age = age.

For species, you could do the same, but instead of assigning the variable in your main loop, why make it assignable at all? If a lion is always a lion, why not just make it a constant?

For example,

namespace TheZoo
{
    internal class Lion:IAnimal
    {
        // const since a lion is always a lion
        // a child class could overwrite this, for subspecies of lions
        public const string Species = "Lion"

        public int Age { get; set; }

        public string ManeColour { get; set; }

        public Lion()
        {

        }

Upvotes: 1

Related Questions