I have a C# program that utilizes console to perform actions in a zoo. Currently I am being asked to use a private static method to show children of animals I have in my zoo. Then children of their children etc... I have another method that when typed in the console first finds the specific animal, then calls another static WalkTree method with the passed in animal. The WalkTree method is to perform a recursive method and output a tree like diagram of the children found to the console. Each level will need to be kicked out like so to show the "family tree".
> show children Bella
Bella: Dingo (10, 40.2)
Coco: Dingo (7, 38.3)
Brutus: Dingo (3, 36)
Maggie: Dingo (7, 34.8)
Toby: Dingo (4, 42.5)
Steve: Dingo (4, 41.1)
Lucy: Dingo (7, 36.5)
Ted: Dingo (7, 39.7)
Each level of the tree should add two spaces to the prefix, like the above example.
/// Shows a list of children of the animals in the zoo.
private static void ShowChildren(Zoo zoo, string name)
{
// Find the animal with the passed-in name.
Animal animal = zoo.FindAnimal(name);
// Then call the WalkTree method and pass in the animal and an empty string.
WalkTree(animal, string.Empty);
}
/// Walks down a tree of parents and children in the zoo.
private static void WalkTree(Animal animal, string prefix)
{
prefix = " ";
Console.WriteLine(animal);
foreach (Animal a in animal.Children)
{
WalkTree(a, prefix);
}
}
So far this is where I am at. I can only use recursion to output the parent, children and children children in a list.
> show children Bella
Bella: Dingo (10, 40.2)
Coco: Dingo (7, 38.3)
Brutus: Dingo (3, 36)
Maggie: Dingo (7, 34.8)
Toby: Dingo (4, 42.5)
Steve: Dingo (4, 41.1)
Lucy: Dingo (7, 36.5)
Ted: Dingo (7, 39.7)
Thanks in advance and let me know if you guys have any questions!