Overview

Activity

Update the Zoo project from checkpoint 3.2 to checkpoint 3.3.

User Story

As a developer, I want to introduce enumerations and update the application to use strongly typed values so the project demonstrates the programming concepts introduced in Zoo 3.3.

Acceptance Criteria

Task List

  1. Create 2 new source files.
  2. Update 4 existing source files.
  3. Confirm the new files are included in the project.
  4. Build the solution and resolve any compiler errors.
  5. 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 3.2 End. Rename the extracted solution folder and solution file for the current checkpoint before making code changes.

Understand the Changes

2
new instructional files
4
existing files changed
1
project-file updates

Files to create

Files to update

Create New Files

Temporary screenshot for Zoo 3.3: Create New Files
Temporary screenshot location: Create New Files.

ZooScenario/Enums/Direction.cs

Create this file with the following checkpoint source:

using System;
using System.Collections.Generic;
using System.Text;

namespace ZooScenario
{
    /// <summary>
    /// The valid directions an animal can move.
    /// </summary>
    public enum Direction
    {
        /// <summary>
        /// Move upward.
        /// </summary>
        Up,

        /// <summary>
        /// Move downward.
        /// </summary>
        Down,

        /// <summary>
        /// Move left.
        /// </summary>
        Left,

        /// <summary>
        /// Move right.
        /// </summary>
        Right,
    }
}

ZooScenario/Enums/Gender.cs

Create this file with the following checkpoint source:

using System;
using System.Collections.Generic;
using System.Text;

namespace ZooScenario
{
    /// <summary>
    /// The valid gender values used by zoo animals.
    /// </summary>
    public enum Gender
    {
        /// <summary>
        /// Female animal.
        /// </summary>
        Female,

        /// <summary>
        /// Male animal.
        /// </summary>
        Male,
    }
}

Update Existing Files

Temporary screenshot for Zoo 3.3: Update Existing Files
Temporary screenshot location: Update Existing Files.

ZooScenario/Business Classes/Animal.cs

Apply the following code changes:

--- 3.2/ZooScenario/Business Classes/Animal.cs
+++ 3.3/ZooScenario/Business Classes/Animal.cs
@@ -22,7 +22,7 @@
         /// <summary>
         /// The gender of the animal.
         /// </summary>
-        private string gender;
+        private Gender gender;
 
         /// <summary>
         /// The happiness level of the animal.
@@ -52,7 +52,7 @@
         /// <param name="gender">The animal gender.</param>
         /// <param name="age">The animal age.</param>
         /// <param name="weight">The animal weight.</param>
-        public Animal(string name, string type, string gender, int age, double weight)
+        public Animal(string name, string type, Gender gender, int age, double weight)
         {
             this.Age = age;
             this.Gender = gender;
@@ -101,7 +101,7 @@
         /// <summary>
         /// Gets or sets the gender.
         /// </summary>
-        public string Gender
+        public Gender Gender
         {
             get
             {
@@ -110,29 +110,7 @@
 
             set
             {
-                if (value == null)
-                {
-                    throw new ArgumentException("Gender is required.");
-                }
-
-                string cleanedGender = value.Trim().ToLower();
-
-                if (cleanedGender == "female")
-                {
-                    this.gender = "Female";
-                }
-                else if (cleanedGender == "male")
-                {
-                    this.gender = "Male";
-                }
-                else if (cleanedGender == "unknown")
-                {
-                    this.gender = "Unknown";
-                }
-                else
-                {
-                    throw new ArgumentException("Gender must be Female, Male, or Unknown.");
-                }
+                this.gender = value;
             }
         }
 
@@ -353,9 +331,9 @@
         /// </summary>
         public void MakePregnant()
         {
-            if (this.Gender == "Female" && !this.IsPregnant)
-            {
-                this.Baby = new Animal("Baby " + this.Name, this.Type, "Unknown", 0, this.Weight * 0.1);
+            if (this.Gender == Gender.Female && !this.IsPregnant)
+            {
+                this.Baby = new Animal("Baby " + this.Name, this.Type, Gender.Female, 0, this.Weight * 0.1);
                 this.Baby.HappinessLevel = 0;
             }
         }

ZooScenario/Business Classes/Zoo.cs

Apply the following code changes:

--- 3.2/ZooScenario/Business Classes/Zoo.cs
+++ 3.3/ZooScenario/Business Classes/Zoo.cs
@@ -88,8 +88,8 @@
                 this.BirthArea = new BirthingRoom(doctor);
                 this.Capacity = capacity;
                 this.DingoPen = new Pen(5);
-                this.LadiesRoom = new Restroom(restroomCapacity, "Female");
-                this.MensRoom = new Restroom(restroomCapacity, "Male");
+                this.LadiesRoom = new Restroom(restroomCapacity, Gender.Female.ToString());
+                this.MensRoom = new Restroom(restroomCapacity, Gender.Male.ToString());
                 this.Name = name;
                 this.PlatypusTank = new Tank(3, 3);
                 this.TicketBooth = new Booth(new Employee("Sam", 42), ticketPrice);

ZooScenario/MainWindow.xaml

Apply the following code changes:

--- 3.2/ZooScenario/MainWindow.xaml
+++ 3.3/ZooScenario/MainWindow.xaml
@@ -60,7 +60,7 @@
             <Button x:Name="wakeAllSanDiegoAnimalsButton" Content="Wake all animals" Click="wakeAllSanDiegoAnimalsButton_Click"/>
             <TextBlock Text="Add San Diego dingo:" Margin="3,10,3,0"/>
             <TextBox x:Name="dingoNameTextBox" Height="24" Margin="3" Text="Daisy"/>
-            <TextBox x:Name="dingoGenderTextBox" Height="24" Margin="3" Text="Female"/>
+            <ComboBox x:Name="dingoGenderComboBox" Height="24" Margin="3"/>
             <TextBox x:Name="dingoAgeTextBox" Height="24" Margin="3" Text="1"/>
             <TextBox x:Name="dingoWeightTextBox" Height="24" Margin="3" Text="29.5"/>
             <CheckBox x:Name="dingoPregnantCheckBox" Content="Pregnant" Margin="3"/>

ZooScenario/MainWindow.xaml.cs

Apply the following code changes:

--- 3.2/ZooScenario/MainWindow.xaml.cs
+++ 3.3/ZooScenario/MainWindow.xaml.cs
@@ -34,6 +34,16 @@
         public MainWindow()
         {
             this.InitializeComponent();
+            this.PopulateDingoGenderComboBox();
+        }
+
+        /// <summary>
+        /// Populates the dingo gender combo box on window load.
+        /// </summary>
+        private void PopulateDingoGenderComboBox()
+        {
+            this.dingoGenderComboBox.ItemsSource = Enum.GetValues(typeof(Gender));
+            this.dingoGenderComboBox.SelectedItem = Gender.Female;
         }
 
         /// <summary>
@@ -46,7 +56,7 @@
         /// <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)
+        private Animal CreateAnimal(string name, string type, Gender gender, int age, double weight, bool isPregnant)
         {
             Animal animal = new Animal(name, type, gender, age, weight);
 
@@ -65,26 +75,33 @@
         /// <param name="e">The event arguments for the event.</param>
         private void addDingoFromTextBoxesButton_Click(object sender, RoutedEventArgs e)
         {
-            string message = this.AddDingoToSanDiegoZoo(
-                this.dingoNameTextBox.Text,
-                this.dingoGenderTextBox.Text,
-                this.dingoAgeTextBox.Text,
-                this.dingoWeightTextBox.Text,
-                this.dingoPregnantCheckBox.IsChecked == true);
-
-            this.informationTextBox.Text = message;
+            if (this.dingoGenderComboBox.SelectedItem is Gender selectedGender)
+            {
+                string message = this.AddDingoToSanDiegoZoo(
+                    this.dingoNameTextBox.Text,
+                    selectedGender,
+                    this.dingoAgeTextBox.Text,
+                    this.dingoWeightTextBox.Text,
+                    this.dingoPregnantCheckBox.IsChecked == true);
+
+                this.informationTextBox.Text = message;
+            }
+            else
+            {
+                this.informationTextBox.Text = "Choose a dingo gender.";
+            }
         }
 
         /// <summary>
         /// Adds a dingo to the San Diego Zoo using text box values.
         /// </summary>
         /// <param name="nameText">The dingo name text.</param>
-        /// <param name="genderText">The dingo gender text.</param>
+        /// <param name="gender">The selected dingo gender.</param>
         /// <param name="ageText">The dingo age text.</param>
         /// <param name="weightText">The dingo weight text.</param>
         /// <param name="isPregnant">Whether the dingo should be pregnant.</param>
         /// <returns>A message for the user.</returns>
-        private string AddDingoToSanDiegoZoo(string nameText, string genderText, string ageText, string weightText, bool isPregnant)
+        private string AddDingoToSanDiegoZoo(string nameText, Gender gender, string ageText, string weightText, bool isPregnant)
         {
             string message = "Create the San Diego Zoo before adding a dingo.";
 
@@ -94,7 +111,7 @@
                 {
                     int age = int.Parse(ageText.Trim());
                     double weight = double.Parse(weightText.Trim());
-                    Animal dingo = this.CreateAnimal(nameText, "Dingo", genderText, age, weight, isPregnant);
+                    Animal dingo = this.CreateAnimal(nameText, "Dingo", gender, age, weight, isPregnant);
                     this.sanDiegoZoo.AddAnimal(dingo);
                     message = dingo.DisplayName + " was added to the San Diego Zoo.";
                 }
@@ -270,14 +287,14 @@
             this.comoZoo = new Zoo("Como Zoo", 1000, 4, 0.75m, 15.00m, comoDoctor, comoVisitor);
             this.comoZoo.AnimalSnackMachine.FoodStock = 250.0;
             this.comoZoo.AnimalSnackMachine.MoneyBalance = 42.75m;
-            this.comoZoo.FeaturedAnimal = new Animal("Dolly", "Dingo", "Female", 4, 35.3);
+            this.comoZoo.FeaturedAnimal = new Animal("Dolly", "Dingo", Gender.Female, 4, 35.3);
             this.comoZoo.FeaturedAnimal.MakePregnant();
             this.comoZoo.BirthArea.Mother = this.comoZoo.FeaturedAnimal;
             this.comoZoo.BirthArea.Doctor.AssignedAnimal = 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.AddAnimal(this.CreateAnimal("Dixie", "Dingo", Gender.Female, 3, 33.8, true));
+            this.comoZoo.AddAnimal(this.CreateAnimal("Pammy", "Platypus", Gender.Female, 2, 15.5, true));
+            this.comoZoo.AddAnimal(this.CreateAnimal("Helen", "Hummingbird", Gender.Female, 2, 0.8, true));
             this.penDisplayTextBox.Text = this.comoZoo.DingoPenDisplay;
         }
 
@@ -293,12 +310,12 @@
             this.sanDiegoZoo = new Zoo("San Diego Zoo", 3000, 12, 1.20m, 25.50m, sanDiegoDoctor, sanDiegoVisitor);
             this.sanDiegoZoo.AnimalSnackMachine.FoodStock = 3.5;
             this.sanDiegoZoo.AnimalSnackMachine.MoneyBalance = 56.25m;
-            this.sanDiegoZoo.FeaturedAnimal = new Animal("Patti", "Platypus", "Female", 5, 3.27);
+            this.sanDiegoZoo.FeaturedAnimal = new Animal("Patti", "Platypus", Gender.Female, 5, 3.27);
             this.sanDiegoZoo.BirthArea.Mother = this.sanDiegoZoo.FeaturedAnimal;
             this.sanDiegoZoo.BirthArea.Doctor.AssignedAnimal = 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));
+            this.sanDiegoZoo.AddAnimal(this.CreateAnimal("Harold", "Hummingbird", Gender.Male, 1, 0.5, false));
+            this.sanDiegoZoo.AddAnimal(this.CreateAnimal("Diego", "Dingo", Gender.Male, 2, 37.4, false));
             this.tankDisplayTextBox.Text = this.sanDiegoZoo.PlatypusTankDisplay;
         }
 

ZooScenario/ZooScenario.csproj

Verify that Visual Studio added the required files to the project:

--- 3.2/ZooScenario/ZooScenario.csproj
+++ 3.3/ZooScenario/ZooScenario.csproj
@@ -87,6 +87,8 @@
       <DependentUpon>App.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="Enums\Direction.cs" />
+    <Compile Include="Enums\Gender.cs" />
     <Compile Include="MainWindow.xaml.cs">
       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>

Check Your Work

Temporary screenshot for Zoo 3.3: Check Your Work
Temporary screenshot location: Check Your Work.
  1. Save all files.
  2. Build the solution.
  3. Resolve compiler errors before continuing.
  4. Run the application.
  5. Exercise the new or updated behavior associated with Zoo 3.3.
  6. Compare your filenames, namespaces, signatures, and key values with the code shown above.

Summary

You updated the project from Zoo 3.2 End to Zoo 3.3 End by creating the required files, modifying only the files with meaningful source changes, and verifying the completed application.