Overview
Activity
Update the Zoo project to checkpoint 2.4.
User Story
As a developer, I want to complete the Zoo 2.4 checkpoint so the project includes the required programming concepts and behavior.
Acceptance Criteria
- Existing files contain the checkpoint changes shown in this walkthrough.
- The solution builds successfully.
- The application starts and the updated behavior can be exercised.
- Names, casing, namespaces, and member signatures match the checkpoint source.
Task List
- Update 3 existing source files.
- Build the solution and resolve any compiler errors.
- Run the application and verify the new behavior.
Exact Casing Matters
Use the filenames, class names, namespaces, method names, property names, and enum values exactly as shown.
Watch Out For
Do not copy build-output folders such as bin, obj, .vs, or package caches into your working project.
Before You Begin
Start with a clean copy of Zoo 2.3 End. Rename the extracted solution folder and solution file for the current checkpoint before making code changes.
Understand the Changes
new instructional files
existing files changed
project-file updates
Files to create
- None
Files to update
ZooScenario/Business Classes/Zoo.csZooScenario/MainWindow.xamlZooScenario/MainWindow.xaml.cs
Create New Files
No new instructional source files are required for this checkpoint.
Update Existing Files
ZooScenario/Business Classes/Zoo.cs
Apply the following code changes:
--- 2.3/ZooScenario/Business Classes/Zoo.cs
+++ 2.4/ZooScenario/Business Classes/Zoo.cs
@@ -10,6 +10,11 @@
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Encapsulation not yet taught.")]
public class Zoo
{
+ /// <summary>
+ /// The zoo's animals.
+ /// </summary>
+ public List<Animal> Animals;
+
/// <summary>
/// The zoo's vending machine which allows guests to buy snacks for animals.
/// </summary>
@@ -287,7 +292,7 @@
/// </summary>
/// <param name="direction">The direction to move.</param>
/// <returns>True if the featured animal moved; otherwise, false.</returns>
- private bool MoveFeaturedAnimal(string direction)
+ public bool MoveFeaturedAnimal(string direction)
{
bool moved = false;
@@ -304,6 +309,144 @@
}
return moved;
+ }
+
+ /// <summary>
+ /// Adds an animal to the zoo.
+ /// </summary>
+ /// <param name="animal">The animal to add to the zoo.</param>
+ public void AddAnimal(Animal animal)
+ {
+ if (animal != null && this.Animals != null)
+ {
+ this.Animals.Add(animal);
+
+ if (this.FeaturedAnimal == null)
+ {
+ this.SetFeaturedAnimal(animal);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Feeds all animals.
+ /// </summary>
+ /// <returns>The number of animals that were fed.</returns>
+ public int FeedAllAnimals()
+ {
+ int animalsFed = 0;
+
+ if (this.Animals != null)
+ {
+ foreach (Animal animal in this.Animals)
+ {
+ if (animal != null && animal.IsReadyToEat())
+ {
+ Food food = new Food();
+ food.Category = this.ChooseFoodCategory(animal.Type);
+ food.Weight = animal.GetPortionSize();
+ animal.Eat(food);
+ animalsFed = animalsFed + 1;
+ }
+ }
+ }
+
+ return animalsFed;
+ }
+
+ /// <summary>
+ /// Finds an animal by name.
+ /// </summary>
+ /// <param name="name">The name to find.</param>
+ /// <returns>The matching animal, or null if no matching animal is found.</returns>
+ public Animal FindAnimal(string name)
+ {
+ Animal foundAnimal = null;
+
+ if (this.Animals != null)
+ {
+ foreach (Animal animal in this.Animals)
+ {
+ if (animal != null && animal.Name == name)
+ {
+ foundAnimal = animal;
+ break;
+ }
+ }
+ }
+
+ return foundAnimal;
+ }
+
+ /// <summary>
+ /// Gets the average animal weight.
+ /// </summary>
+ /// <returns>The average animal weight.</returns>
+ public double GetAverageAnimalWeight()
+ {
+ double averageWeight = 0.0;
+ double totalWeight = 0.0;
+ int animalCount = this.GetAnimalCount();
+
+ if (this.Animals != null)
+ {
+ foreach (Animal animal in this.Animals)
+ {
+ totalWeight = totalWeight + animal.Weight;
+ }
+ }
+
+ if (animalCount > 0)
+ {
+ averageWeight = totalWeight / animalCount;
+ }
+
+ return averageWeight;
+ }
+
+ /// <summary>
+ /// Gets the animal count.
+ /// </summary>
+ /// <returns>The animal count.</returns>
+ public int GetAnimalCount()
+ {
+ int animalCount = 0;
+
+ if (this.Animals != null)
+ {
+ foreach (Animal animal in this.Animals)
+ {
+ if (animal != null)
+ {
+ animalCount = animalCount + 1;
+ }
+ }
+ }
+
+ return animalCount;
+ }
+
+ /// <summary>
+ /// Wakes all animals.
+ /// </summary>
+ /// <returns>The number of animals that were woken.</returns>
+ public int WakeAllAnimals()
+ {
+ int animalsWoken = 0;
+
+ if (this.Animals != null)
+ {
+ foreach (Animal animal in this.Animals)
+ {
+ if (animal != null)
+ {
+ animal.WakeUp();
+ animalsWoken = animalsWoken + 1;
+ }
+ }
+ }
+
+ return animalsWoken;
}
}
}
ZooScenario/MainWindow.xaml
Apply the following code changes:
--- 2.3/ZooScenario/MainWindow.xaml
+++ 2.4/ZooScenario/MainWindow.xaml
@@ -27,6 +27,10 @@
<Button x:Name="careForComoAnimalButton" Content="Flora, care for shared dingo" Click="careForComoAnimalButton_Click"/>
<Button x:Name="prepareComoBirthingRoomButton" Content="Prepare Como Birthing Room" Click="prepareComoBirthingRoomButton_Click"/>
<Button x:Name="fillComoVendingMachineButton" Content="Fill Como Vending Machine" Click="fillComoVendingMachineButton_Click"/>
+ <Button x:Name="wakeAllComoAnimalsButton" Content="Wake all animals" Click="wakeAllComoAnimalsButton_Click"/>
+ <Button x:Name="feedAllComoAnimalsButton" Content="Feed all animals" Click="feedAllComoAnimalsButton_Click"/>
+ <Button x:Name="getComoAnimalCountButton" Content="Get animal count" Click="getComoAnimalCountButton_Click"/>
+ <Button x:Name="getComoAverageWeightButton" Content="Get average weight" Click="getComoAverageWeightButton_Click"/>
<TextBlock Text="Dingo side-view pen:" Margin="3,10,3,0"/>
<TextBox x:Name="penDisplayTextBox" FontFamily="Consolas" Height="24" Margin="3" IsReadOnly="True"/>
<UniformGrid Columns="2">
@@ -53,6 +57,10 @@
<Button x:Name="careForSanDiegoAnimalButton" Content="Steve, care for shared platypus" Click="careForSanDiegoAnimalButton_Click"/>
<Button x:Name="prepareSanDiegoBirthingRoomButton" Content="Prepare San Diego Birthing Room" Click="prepareSanDiegoBirthingRoomButton_Click"/>
<Button x:Name="fillSanDiegoVendingMachineButton" Content="Fill San Diego Vending Machine" Click="fillSanDiegoVendingMachineButton_Click"/>
+ <Button x:Name="wakeAllSanDiegoAnimalsButton" Content="Wake all animals" Click="wakeAllSanDiegoAnimalsButton_Click"/>
+ <Button x:Name="feedAllSanDiegoAnimalsButton" Content="Feed all animals" Click="feedAllSanDiegoAnimalsButton_Click"/>
+ <Button x:Name="getSanDiegoAnimalCountButton" Content="Get animal count" Click="getSanDiegoAnimalCountButton_Click"/>
+ <Button x:Name="getSanDiegoAverageWeightButton" Content="Get average weight" Click="getSanDiegoAverageWeightButton_Click"/>
<TextBlock Text="Platypus fish tank:" Margin="3,10,3,0"/>
<TextBox x:Name="tankDisplayTextBox" FontFamily="Consolas" Height="60" Margin="3" IsReadOnly="True" AcceptsReturn="True"/>
<UniformGrid Columns="4">
ZooScenario/MainWindow.xaml.cs
Apply the following code changes:
--- 2.3/ZooScenario/MainWindow.xaml.cs
+++ 2.4/ZooScenario/MainWindow.xaml.cs
@@ -38,6 +38,117 @@
}
/// <summary>
+ /// Creates an animal.
+ /// </summary>
+ /// <param name="name">The animal name.</param>
+ /// <param name="type">The animal type.</param>
+ /// <param name="gender">The animal gender.</param>
+ /// <param name="age">The animal age.</param>
+ /// <param name="weight">The animal weight.</param>
+ /// <param name="isPregnant">Whether the animal is pregnant.</param>
+ /// <returns>The animal that was created.</returns>
+ private Animal CreateAnimal(string name, string type, string gender, int age, double weight, bool isPregnant)
+ {
+ Animal animal = new Animal();
+ animal.Age = age;
+ animal.Gender = gender;
+ animal.Name = name;
+ animal.Type = type;
+ animal.Weight = weight;
+
+ if (isPregnant)
+ {
+ animal.MakePregnant();
+ }
+
+ return animal;
+ }
+
+ /// <summary>
+ /// Feeds all animals at the Como Zoo.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void feedAllComoAnimalsButton_Click(object sender, RoutedEventArgs e)
+ {
+ int animalsFed = this.ComoZoo.FeedAllAnimals();
+ this.informationTextBox.Text = "Fed " + animalsFed + " Como Zoo animals.";
+ }
+
+ /// <summary>
+ /// Feeds all animals at the San Diego Zoo.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void feedAllSanDiegoAnimalsButton_Click(object sender, RoutedEventArgs e)
+ {
+ int animalsFed = this.SanDiegoZoo.FeedAllAnimals();
+ this.informationTextBox.Text = "Fed " + animalsFed + " San Diego Zoo animals.";
+ }
+
+ /// <summary>
+ /// Gets the Como Zoo animal count.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void getComoAnimalCountButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.informationTextBox.Text = "Como Zoo animal count: " + this.ComoZoo.GetAnimalCount() + ".";
+ }
+
+ /// <summary>
+ /// Gets the Como Zoo average animal weight.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void getComoAverageWeightButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.informationTextBox.Text = "Como Zoo average animal weight: " + this.ComoZoo.GetAverageAnimalWeight() + ".";
+ }
+
+ /// <summary>
+ /// Gets the San Diego Zoo animal count.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void getSanDiegoAnimalCountButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.informationTextBox.Text = "San Diego Zoo animal count: " + this.SanDiegoZoo.GetAnimalCount() + ".";
+ }
+
+ /// <summary>
+ /// Gets the San Diego Zoo average animal weight.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void getSanDiegoAverageWeightButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.informationTextBox.Text = "San Diego Zoo average animal weight: " + this.SanDiegoZoo.GetAverageAnimalWeight() + ".";
+ }
+
+ /// <summary>
+ /// Wakes all animals at the Como Zoo.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void wakeAllComoAnimalsButton_Click(object sender, RoutedEventArgs e)
+ {
+ int animalsWoken = this.ComoZoo.WakeAllAnimals();
+ this.informationTextBox.Text = "Woke " + animalsWoken + " Como Zoo animals.";
+ }
+
+ /// <summary>
+ /// Wakes all animals at the San Diego Zoo.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void wakeAllSanDiegoAnimalsButton_Click(object sender, RoutedEventArgs e)
+ {
+ int animalsWoken = this.SanDiegoZoo.WakeAllAnimals();
+ this.informationTextBox.Text = "Woke " + animalsWoken + " San Diego Zoo animals.";
+ }
+
+ /// <summary>
/// Creates the Como Zoo and related objects.
/// </summary>
/// <param name="sender">The object that initiated the event.</param>
@@ -45,6 +156,7 @@
private void newComoZooButton_Click(object sender, RoutedEventArgs e)
{
this.ComoZoo = new Zoo();
+ this.ComoZoo.Animals = new List<Animal>();
this.ComoZoo.AnimalSnackMachine = new VendingMachine();
this.ComoZoo.BirthArea = new BirthingRoom();
this.ComoZoo.Capacity = 1000;
@@ -76,7 +188,10 @@
this.ComoZoo.FeaturedAnimal.Type = "Dingo";
this.ComoZoo.FeaturedAnimal.Weight = 35.3;
this.ComoZoo.FeaturedAnimal.MakePregnant();
- this.ComoZoo.SetFeaturedAnimal(this.ComoZoo.FeaturedAnimal);
+ this.ComoZoo.AddAnimal(this.ComoZoo.FeaturedAnimal);
+ this.ComoZoo.AddAnimal(this.CreateAnimal("Dixie", "Dingo", "Female", 3, 33.8, true));
+ this.ComoZoo.AddAnimal(this.CreateAnimal("Pammy", "Platypus", "Female", 2, 15.5, true));
+ this.ComoZoo.AddAnimal(this.CreateAnimal("Helen", "Hummingbird", "Female", 2, 0.8, true));
this.ComoZoo.LadiesRoom.Capacity = 4;
this.ComoZoo.LadiesRoom.Gender = "Female";
@@ -104,6 +219,7 @@
private void newSanDiegoZooButton_Click(object sender, RoutedEventArgs e)
{
this.SanDiegoZoo = new Zoo();
+ this.SanDiegoZoo.Animals = new List<Animal>();
this.SanDiegoZoo.AnimalSnackMachine = new VendingMachine();
this.SanDiegoZoo.BirthArea = new BirthingRoom();
this.SanDiegoZoo.Capacity = 3000;
@@ -138,7 +254,9 @@
this.SanDiegoZoo.FeaturedAnimal.Name = "Patti";
this.SanDiegoZoo.FeaturedAnimal.Type = "Platypus";
this.SanDiegoZoo.FeaturedAnimal.Weight = 3.27;
- this.SanDiegoZoo.SetFeaturedAnimal(this.SanDiegoZoo.FeaturedAnimal);
+ this.SanDiegoZoo.AddAnimal(this.SanDiegoZoo.FeaturedAnimal);
+ this.SanDiegoZoo.AddAnimal(this.CreateAnimal("Harold", "Hummingbird", "Male", 1, 0.5, false));
+ this.SanDiegoZoo.AddAnimal(this.CreateAnimal("Diego", "Dingo", "Male", 2, 37.4, false));
// Assign field values of the restrooms.
this.SanDiegoZoo.LadiesRoom.Capacity = 12;
Check Your Work
- Save all files.
- Build the solution.
- Resolve compiler errors before continuing.
- Run the application.
- Exercise the behavior associated with Zoo 2.4.
- Compare your filenames, namespaces, signatures, and key values with the code shown above.
Summary
You completed Zoo 2.4 by creating the required files, modifying only files with meaningful source changes, and verifying the application.