Overview

Activity

Update the Zoo project from checkpoint 2.7 to checkpoint 3.1.

User Story

As a developer, I want to introduce additional animal behavior and application interactions so the project demonstrates the programming concepts introduced in Zoo 3.1.

Acceptance Criteria

Task List

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

Understand the Changes

0
new instructional files
4
existing files changed
0
project-file updates

Files to create

Files to update

Create New Files

No new instructional source files are required for this checkpoint.

Update Existing Files

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

ZooScenario/App.xaml

Apply the following code changes:

--- 2.7/ZooScenario/App.xaml
+++ 3.1/ZooScenario/App.xaml
@@ -4,6 +4,5 @@
              xmlns:local="clr-namespace:ZooScenario"
              StartupUri="MainWindow.xaml">
     <Application.Resources>
-
     </Application.Resources>
 </Application>

ZooScenario/Business Classes/Animal.cs

Apply the following code changes:

--- 2.7/ZooScenario/Business Classes/Animal.cs
+++ 3.1/ZooScenario/Business Classes/Animal.cs
@@ -111,7 +111,23 @@
 
             set
             {
-                this.gender = value;
+                if (value != null)
+                {
+                    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";
+                    }
+                }
             }
         }
 
@@ -127,7 +143,10 @@
 
             set
             {
-                this.name = value;
+                if (value != null && value.Trim() != string.Empty)
+                {
+                    this.name = value.Trim();
+                }
             }
         }
 
@@ -143,7 +162,27 @@
 
             set
             {
-                this.type = value;
+                if (value != null && value.Trim() != string.Empty)
+                {
+                    string cleanedType = value.Trim().ToLower();
+
+                    if (cleanedType == "dingo")
+                    {
+                        this.type = "Dingo";
+                    }
+                    else if (cleanedType == "hummingbird")
+                    {
+                        this.type = "Hummingbird";
+                    }
+                    else if (cleanedType == "platypus")
+                    {
+                        this.type = "Platypus";
+                    }
+                    else
+                    {
+                        this.type = value.Trim();
+                    }
+                }
             }
         }
 
@@ -178,9 +217,9 @@
 
             set
             {
-                if (value != null && value != string.Empty)
-                {
-                    this.Name = value;
+                if (value != null && value.Trim() != string.Empty)
+                {
+                    this.Name = value.Trim();
                 }
             }
         }

ZooScenario/MainWindow.xaml

Apply the following code changes:

--- 2.7/ZooScenario/MainWindow.xaml
+++ 3.1/ZooScenario/MainWindow.xaml
@@ -58,6 +58,13 @@
             <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"/>
+            <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"/>
+            <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"/>
+            <Button x:Name="addDingoFromTextBoxesButton" Content="Add dingo from text boxes" Click="addDingoFromTextBoxesButton_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"/>

ZooScenario/MainWindow.xaml.cs

Apply the following code changes:

--- 2.7/ZooScenario/MainWindow.xaml.cs
+++ 3.1/ZooScenario/MainWindow.xaml.cs
@@ -59,6 +59,84 @@
         }
 
         /// <summary>
+        /// Adds a student-created dingo to 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 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;
+        }
+
+        /// <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="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)
+        {
+            string message = "Create the San Diego Zoo before adding a dingo.";
+
+            if (this.sanDiegoZoo != null)
+            {
+                string cleanedName = nameText.Trim();
+                string cleanedGender = genderText.Trim().ToLower();
+                string cleanedAge = ageText.Trim();
+                string cleanedWeight = weightText.Trim();
+
+                if (cleanedName == string.Empty)
+                {
+                    message = "Enter a name for the dingo.";
+                }
+                else if (cleanedGender != "female" && cleanedGender != "male")
+                {
+                    message = "Enter female or male for the dingo gender.";
+                }
+                else if (!this.IsWholeNumberText(cleanedAge))
+                {
+                    message = "Enter a whole number for the dingo age.";
+                }
+                else if (!this.IsDecimalNumberText(cleanedWeight))
+                {
+                    message = "Enter a number for the dingo weight.";
+                }
+                else
+                {
+                    int age = int.Parse(cleanedAge);
+                    double weight = double.Parse(cleanedWeight);
+
+                    if (age < 0)
+                    {
+                        message = "Enter a nonnegative age for the dingo.";
+                    }
+                    else if (weight <= 0)
+                    {
+                        message = "Enter a positive weight for the dingo.";
+                    }
+                    else
+                    {
+                        Animal dingo = this.CreateAnimal(cleanedName, "Dingo", cleanedGender, age, weight, isPregnant);
+                        this.sanDiegoZoo.AddAnimal(dingo);
+                        message = dingo.DisplayName + " was added to the San Diego Zoo.";
+                    }
+                }
+            }
+
+            return message;
+        }
+
+        /// <summary>
         /// Feeds all animals at the Como Zoo.
         /// </summary>
         /// <param name="sender">The object that initiated the event.</param>
@@ -118,6 +196,84 @@
         private void getSanDiegoAverageWeightButton_Click(object sender, RoutedEventArgs e)
         {
             this.informationTextBox.Text = "San Diego Zoo average animal weight: " + this.sanDiegoZoo.AverageAnimalWeight + ".";
+        }
+
+        /// <summary>
+        /// Determines whether text can be read as a decimal number.
+        /// </summary>
+        /// <param name="text">The text to check.</param>
+        /// <returns>A value indicating whether the text can be read as a decimal number.</returns>
+        private bool IsDecimalNumberText(string text)
+        {
+            string cleanedText = text.Trim();
+            bool isNumberText = cleanedText != string.Empty;
+            int decimalPointCount = 0;
+            int startIndex = 0;
+
+            if (cleanedText.Length > 0 && cleanedText[0] == '-')
+            {
+                startIndex = 1;
+            }
+
+            if (startIndex == cleanedText.Length)
+            {
+                isNumberText = false;
+            }
+
+            for (int i = startIndex; i < cleanedText.Length && isNumberText; i++)
+            {
+                char currentCharacter = cleanedText[i];
+
+                if (currentCharacter == '.')
+                {
+                    decimalPointCount++;
+
+                    if (decimalPointCount > 1)
+                    {
+                        isNumberText = false;
+                    }
+                }
+                else if (currentCharacter < '0' || currentCharacter > '9')
+                {
+                    isNumberText = false;
+                }
+            }
+
+            return isNumberText;
+        }
+
+        /// <summary>
+        /// Determines whether text can be read as a whole number.
+        /// </summary>
+        /// <param name="text">The text to check.</param>
+        /// <returns>A value indicating whether the text can be read as a whole number.</returns>
+        private bool IsWholeNumberText(string text)
+        {
+            string cleanedText = text.Trim();
+            bool isNumberText = cleanedText != string.Empty;
+            int startIndex = 0;
+
+            if (cleanedText.Length > 0 && cleanedText[0] == '-')
+            {
+                startIndex = 1;
+            }
+
+            if (startIndex == cleanedText.Length)
+            {
+                isNumberText = false;
+            }
+
+            for (int i = startIndex; i < cleanedText.Length && isNumberText; i++)
+            {
+                char currentCharacter = cleanedText[i];
+
+                if (currentCharacter < '0' || currentCharacter > '9')
+                {
+                    isNumberText = false;
+                }
+            }
+
+            return isNumberText;
         }
 
         /// <summary>

Check Your Work

Temporary screenshot for Zoo 3.1: 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.1.
  6. Compare your filenames, namespaces, signatures, and key values with the code shown above.

Summary

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