Overview

Activity

Update the Zoo project to checkpoint 1.5.

User Story

As a developer, I want to complete the Zoo 1.5 checkpoint so the project includes the required programming concepts and behavior.

Acceptance Criteria

Task List

  1. Update 8 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 1.4 End. Rename the extracted solution folder and solution file for the current checkpoint before making code changes.

Understand the Changes

0
new instructional files
8
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 1.5: Update Existing Files
Temporary screenshot location: Update Existing Files.

ZooScenario/App.xaml.cs

Apply the following code changes:

--- 1.4/ZooScenario/App.xaml.cs
+++ 1.5/ZooScenario/App.xaml.cs
@@ -1,5 +1,9 @@
+using System;
+using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
+using System.Linq;
+using System.Threading.Tasks;
 using System.Windows;
 
 namespace ZooScenario

ZooScenario/Business Classes/Animal.cs

Apply the following code changes:

--- 1.4/ZooScenario/Business Classes/Animal.cs
+++ 1.5/ZooScenario/Business Classes/Animal.cs
@@ -16,6 +16,11 @@
         public int Age;
 
         /// <summary>
+        /// The animal's baby.
+        /// </summary>
+        public Animal Baby;
+
+        /// <summary>
         /// The gender of the animal.
         /// </summary>
         public string Gender;
@@ -24,11 +29,6 @@
         /// The happiness level of the animal.
         /// </summary>
         public int HappinessLevel;
-
-        /// <summary>
-        /// A value indicating whether or not the animal is pregnant.
-        /// </summary>
-        public bool IsPregnant;
 
         /// <summary>
         /// The name of the animal.
@@ -46,20 +46,73 @@
         public double Weight;
 
         /// <summary>
-        /// Feeds the animal.
+        /// Makes the animal bark.
         /// </summary>
-        /// <param name="weightGain">The amount of weight the animal gains.</param>
-        /// <param name="happinessGain">The amount of happiness the animal gains.</param>
-        public void Eat(double weightGain, int happinessGain)
+        public void Bark()
         {
-            if (weightGain > 0)
+            this.HappinessLevel = this.HappinessLevel + 1;
+        }
+
+        /// <summary>
+        /// Eats a specific amount of food.
+        /// </summary>
+        /// <param name="foodWeight">The weight of the food the animal eats.</param>
+        public void Eat(double foodWeight)
+        {
+            if (foodWeight > 0)
             {
-                this.Weight = this.Weight + weightGain;
+                double weightGain = foodWeight * 0.8;
+                this.Weight += weightGain;
+
+                if (this.GetIsPregnant())
+                {
+                    double babyWeightGain = foodWeight * 0.1;
+                    this.Baby.Weight += babyWeightGain;
+                }
+
+                if (this.Type == "Dingo")
+                {
+                    this.Bark();
+                }
+                else if (this.Type == "Platypus")
+                {
+                    this.StashInPouch();
+                }
             }
+        }
 
-            if (happinessGain > 0)
+        /// <summary>
+        /// Gets whether the animal is pregnant.
+        /// </summary>
+        /// <returns>Whether the animal has a baby.</returns>
+        public bool GetIsPregnant()
+        {
+            return this.Baby != null;
+        }
+
+        /// <summary>
+        /// Returns the maximum portion size the animal can eat.
+        /// </summary>
+        /// <returns>The maximum portion size for the animal.</returns>
+        public double GetPortionSize()
+        {
+            return this.Weight * 0.02;
+        }
+
+        /// <summary>
+        /// Makes the animal pregnant.
+        /// </summary>
+        public void MakePregnant()
+        {
+            if (this.Gender == "Female" && !this.GetIsPregnant())
             {
-                this.HappinessLevel = this.HappinessLevel + happinessGain;
+                this.Baby = new Animal();
+                this.Baby.Age = 0;
+                this.Baby.Gender = "Unknown";
+                this.Baby.HappinessLevel = 0;
+                this.Baby.Name = "Baby " + this.Name;
+                this.Baby.Type = this.Type;
+                this.Baby.Weight = this.Weight * 0.1;
             }
         }
 
@@ -67,6 +120,14 @@
         /// Makes the animal move.
         /// </summary>
         public void Move()
+        {
+            this.HappinessLevel = this.HappinessLevel + 1;
+        }
+
+        /// <summary>
+        /// Stores food in a pouch.
+        /// </summary>
+        public void StashInPouch()
         {
             this.HappinessLevel = this.HappinessLevel + 1;
         }

ZooScenario/Business Classes/Guest.cs

Apply the following code changes:

--- 1.4/ZooScenario/Business Classes/Guest.cs
+++ 1.5/ZooScenario/Business Classes/Guest.cs
@@ -36,6 +36,26 @@
         }
 
         /// <summary>
+        /// Gets the money balance from the guest's wallet.
+        /// </summary>
+        /// <returns>The guest's current money balance.</returns>
+        public decimal GetMoneyBalance()
+        {
+            return this.Wallet.MoneyBalance;
+        }
+
+        /// <summary>
+        /// Removes money from the guest's wallet.
+        /// </summary>
+        /// <param name="amount">The amount of money to remove.</param>
+        /// <returns>The amount of money that was removed.</returns>
+        public decimal RemoveMoney(decimal amount)
+        {
+            decimal amountRemoved = this.Wallet.RemoveMoney(amount);
+            return amountRemoved;
+        }
+
+        /// <summary>
         /// Visits the zoo.
         /// </summary>
         /// <param name="ticketPrice">The price of one ticket.</param>

ZooScenario/Business Classes/VendingMachine.cs

Apply the following code changes:

--- 1.4/ZooScenario/Business Classes/VendingMachine.cs
+++ 1.5/ZooScenario/Business Classes/VendingMachine.cs
@@ -56,8 +56,33 @@
         {
             if (moneyAmount > 0)
             {
-                this.MoneyBalance = this.MoneyBalance + moneyAmount;
+                this.MoneyBalance += moneyAmount;
             }
+        }
+
+        /// <summary>
+        /// Determines the cost of the food.
+        /// </summary>
+        /// <param name="maxFoodWeight">The maximum weight of the food.</param>
+        /// <returns>The cost of the food.</returns>
+        public decimal DetermineFoodCost(double maxFoodWeight)
+        {
+            decimal maxFoodCost = (decimal)maxFoodWeight * this.FoodPricePerPound;
+            decimal foodCost = Math.Round(maxFoodCost, 2);
+            return foodCost;
+        }
+
+        /// <summary>
+        /// Sells food from the vending machine.
+        /// </summary>
+        /// <param name="payment">The amount of money paid.</param>
+        /// <returns>The weight of food sold.</returns>
+        public double SellFood(decimal payment)
+        {
+            this.AddMoney(payment);
+            double foodWeight = (double)(payment * this.FoodPricePerPound);
+            this.FoodStock -= foodWeight;
+            return foodWeight;
         }
     }
 }

ZooScenario/Business Classes/Wallet.cs

Apply the following code changes:

--- 1.4/ZooScenario/Business Classes/Wallet.cs
+++ 1.5/ZooScenario/Business Classes/Wallet.cs
@@ -28,6 +28,28 @@
         }
 
         /// <summary>
+        /// Removes money from the wallet.
+        /// </summary>
+        /// <param name="amount">The amount of money to remove.</param>
+        /// <returns>The amount of money that was removed.</returns>
+        public decimal RemoveMoney(decimal amount)
+        {
+            decimal amountRemoved;
+
+            if (amount > 0.00m)
+            {
+                amountRemoved = amount;
+            }
+            else
+            {
+                amountRemoved = 0.00m;
+            }
+
+            this.MoneyBalance = this.MoneyBalance - amountRemoved;
+            return amountRemoved;
+        }
+
+        /// <summary>
         /// Removes the ticket price from the wallet.
         /// </summary>
         /// <param name="ticketPrice">The price of one ticket.</param>

ZooScenario/Business Classes/Zoo.cs

Apply the following code changes:

--- 1.4/ZooScenario/Business Classes/Zoo.cs
+++ 1.5/ZooScenario/Business Classes/Zoo.cs
@@ -81,13 +81,22 @@
         /// <summary>
         /// Feeds the featured animal.
         /// </summary>
-        /// <param name="weightGain">The amount of weight the animal gains.</param>
-        /// <param name="happinessGain">The amount of happiness the animal gains.</param>
-        /// <param name="moneyAmount">The amount of money added to the snack machine.</param>
-        public void FeedFeaturedAnimal(double weightGain, int happinessGain, decimal moneyAmount)
+        /// <returns>The weight of the food given to the animal.</returns>
+        public double FeedFeaturedAnimal()
         {
-            this.FeaturedAnimal.Eat(weightGain, happinessGain);
-            this.AnimalSnackMachine.AddMoney(moneyAmount);
+            double foodWeight = 0.0;
+            double maxFoodWeight = this.FeaturedAnimal.GetPortionSize();
+            decimal foodCost = this.AnimalSnackMachine.DetermineFoodCost(maxFoodWeight);
+            decimal visitorMoneyBalance = this.Visitor.GetMoneyBalance();
+
+            if (visitorMoneyBalance >= foodCost)
+            {
+                decimal foodPayment = this.Visitor.RemoveMoney(foodCost);
+                foodWeight = this.AnimalSnackMachine.SellFood(foodPayment);
+                this.FeaturedAnimal.Eat(foodWeight);
+            }
+
+            return foodWeight;
         }
 
         /// <summary>

ZooScenario/MainWindow.xaml

Apply the following code changes:

--- 1.4/ZooScenario/MainWindow.xaml
+++ 1.5/ZooScenario/MainWindow.xaml
@@ -23,7 +23,7 @@
             <!-- Start Como Zoo buttons -->
             <Button x:Name="newComoZooButton" Content="New Como Zoo" Click="newComoZooButton_Click"/>
             <Button x:Name="openComoZooButton" Content="Open Como Zoo" Click="openComoZooButton_Click"/>
-            <Button x:Name="feedComoAnimalButton" Content="Feed Como Animal" Click="feedComoAnimalButton_Click"/>
+            <Button x:Name="feedComoAnimalButton" Content="Julia, feed dingo" Click="feedComoAnimalButton_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"/>
             <!-- End Como Zoo buttons -->
@@ -42,7 +42,7 @@
             <!-- Start San Diego Zoo buttons -->
             <Button x:Name="newSanDiegoZooButton" Content="New San Diego Zoo" Click="newSanDiegoZooButton_Click"/>
             <Button x:Name="openSanDiegoZooButton" Content="Open San Diego Zoo" Click="openSanDiegoZooButton_Click"/>
-            <Button x:Name="feedSanDiegoAnimalButton" Content="Feed San Diego Animal" Click="feedSanDiegoAnimalButton_Click"/>
+            <Button x:Name="feedSanDiegoAnimalButton" Content="Dakota, feed platypus" Click="feedSanDiegoAnimalButton_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"/>
             <!-- End San Diego Zoo buttons -->

ZooScenario/MainWindow.xaml.cs

Apply the following code changes:

--- 1.4/ZooScenario/MainWindow.xaml.cs
+++ 1.5/ZooScenario/MainWindow.xaml.cs
@@ -70,10 +70,10 @@
 
             this.ComoZoo.FeaturedAnimal.Age = 4;
             this.ComoZoo.FeaturedAnimal.Gender = "Female";
-            this.ComoZoo.FeaturedAnimal.IsPregnant = true;
             this.ComoZoo.FeaturedAnimal.Name = "Dolly";
             this.ComoZoo.FeaturedAnimal.Type = "Dingo";
             this.ComoZoo.FeaturedAnimal.Weight = 35.3;
+            this.ComoZoo.FeaturedAnimal.MakePregnant();
 
             this.ComoZoo.LadiesRoom.Capacity = 4;
             this.ComoZoo.LadiesRoom.Gender = "Female";
@@ -128,7 +128,6 @@
             // Assign field values of the featured animal.
             this.SanDiegoZoo.FeaturedAnimal.Age = 5;
             this.SanDiegoZoo.FeaturedAnimal.Gender = "Female";
-            this.SanDiegoZoo.FeaturedAnimal.IsPregnant = false;
             this.SanDiegoZoo.FeaturedAnimal.Name = "Patti";
             this.SanDiegoZoo.FeaturedAnimal.Type = "Platypus";
             this.SanDiegoZoo.FeaturedAnimal.Weight = 3.27;
@@ -169,8 +168,9 @@
         /// <param name="e">The event arguments for the event.</param>
         private void feedComoAnimalButton_Click(object sender, RoutedEventArgs e)
         {
-            this.ComoZoo.FeedFeaturedAnimal(0.75, 3, 4.25m);
-            this.informationTextBox.Text = this.ComoZoo.FeaturedAnimal.Name + " has been fed.";
+            double foodWeight = this.ComoZoo.FeedFeaturedAnimal();
+            this.informationTextBox.Text = this.ComoZoo.Visitor.Name + " fed " + this.ComoZoo.FeaturedAnimal.Name
+                + " " + foodWeight + " pounds of food.";
         }
 
         /// <summary>
@@ -210,11 +210,12 @@
         /// Feeds the San Diego Zoo's featured animal.
         /// </summary>
         /// <param name="sender">The object that initiated the event.</param>
-        /// <param name="e">The event arguments for the event.</param>
+        /// <param name="e">The event arguments for the event.</param>>
         private void feedSanDiegoAnimalButton_Click(object sender, RoutedEventArgs e)
         {
-            this.SanDiegoZoo.FeedFeaturedAnimal(0.25, 2, 2.50m);
-            this.informationTextBox.Text = this.SanDiegoZoo.FeaturedAnimal.Name + " has been fed.";
+            double foodWeight = this.SanDiegoZoo.FeedFeaturedAnimal();
+            this.informationTextBox.Text = this.SanDiegoZoo.Visitor.Name + " fed " + this.SanDiegoZoo.FeaturedAnimal.Name
+                + " " + foodWeight + " pounds of food.";
         }
 
         /// <summary>

Check Your Work

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

Summary

You completed Zoo 1.5 by creating the required files, modifying only files with meaningful source changes, and verifying the application.