BatJew
BatJew

Reputation: 1

Trying to create a list from a multi-propertied abstract class in C#

If i have an abstract class named Animal with three properties (Name, Age, and Female) and I want to make a list of animals in a method in a class named Zoo, by making each instance in the list a different Animal how would i do that? Below is also my code so far:

using System;
using System.Collections.Generic;

public abstract class Animal
{
    abstract public string AName();
    abstract public int Age();
    abstract public bool Female();
}

public class Zoo : Animal
{
    public string ZName;
    public string City;
    public int Capacity;
    public List<Animal> Animals = new List<Animal>();

    public Zoo(string name, string city, int capacity)
    {
        ZName = name;
        City = city;
        Capacity = capacity;
    }

    public void addAnimal(Animal animal)
    {
        List<Animal>.Add(animal.Age(), animal.AName(), animal.Female());
    }

This is the UML Diagram I have to work off of

This is the UML Diagram I have to work off of

Upvotes: 0

Views: 510

Answers (2)

sr28
sr28

Reputation: 5126

I think you're trying to use the abstract Animal class with the wrong thing. It looks like you're trying to do something a bit more like this:

public class Zoo
{
    public string ZName;
    public string City;
    public int Capacity;
    public List<Animal> Animals = new List<Animal>();

    public Zoo(string name, string city, int capacity)
    {
        ZName = name;
        City = city;
        Capacity = capacity;
    }

    public void addAnimal(Animal animal)
    {
        Animals.Add(animal);
    }
}

public abstract class Animal
{
    public abstract string AName { get; set; }
    public abstract int Age { get; set; }
    public abstract bool Female { get; set; }
}

public class Tiger : Animal
{
    public override string AName { get; set; }
    public override int Age { get; set; }
    public override bool Female { get; set; }
}

public class Lion : Animal
{
    public override string AName { get; set; }
    public override int Age { get; set; }
    public override bool Female { get; set; }
}

I created a little console app to demo this using the classes above:

    static void Main(string[] args)
    {
        var myZoo = new Zoo("Fun Zoo", "New York", 20);
        myZoo.addAnimal(new Tiger { Age = 10, AName = "Frank", Female = true });
        myZoo.addAnimal(new Lion { Age = 11, AName = "Fred", Female = false });

        Console.WriteLine("Here's the new zoo:");
        Console.WriteLine("Zoo Name: " + myZoo.ZName + ", City: " + myZoo.City + ", Capacity: " + myZoo.Capacity);
        Console.WriteLine();
        Console.WriteLine("Animals at the zoo:");
        foreach (var a in myZoo.Animals)
        {
            Console.WriteLine("Name: " + a.AName + ", Age: " + a.Age + ", Is Female: " + a.Female);
        }

        Console.ReadLine();
    }

Results:

enter image description here

Upvotes: 2

ewhiting
ewhiting

Reputation: 256

You shouldn't be extending Animal with Zoo, I think you want something more like:

public class Zoo {
    public string ZName;
    public string City;
    public int Capacity;
    public List<Animal> Animals = new List<Animal>();

    public Zoo(string name, string city, int capacity, List<Animal> animals)
    {
        ZName = name;
        City = city;
        Capacity = capacity;
        Animals = animals
    }

}

and initialize the zoo with an already instantiated List of Animals

Upvotes: 1

Related Questions