Overview

Activity

Update the Zoo project to checkpoint 2.1.

User Story

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

Acceptance Criteria

Task List

  1. Update 6 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.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
6
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 2.1: Update Existing Files
Temporary screenshot location: Update Existing Files.

ZooScenario/App.xaml.cs

Apply the following code changes:

--- 1.7/ZooScenario/App.xaml.cs
+++ 2.1/ZooScenario/App.xaml.cs
@@ -1,9 +1,5 @@
-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.7/ZooScenario/Business Classes/Animal.cs
+++ 2.1/ZooScenario/Business Classes/Animal.cs
@@ -59,24 +59,28 @@
         /// <param name="food">The food the animal eats.</param>
         public void Eat(Food food)
         {
-            if (food.Weight > 0)
+            if (food != null && food.Weight > 0 && this.Weight > 0)
             {
                 double weightGain = food.Weight * 0.8;
                 this.Weight += weightGain;
 
-                if (this.GetIsPregnant())
+                if (this.GetIsPregnant() && this.Baby != null)
                 {
                     double babyWeightGain = food.Weight * 0.1;
                     this.Baby.Weight += babyWeightGain;
                 }
 
-                if (this.Type == "Dingo")
+                switch (this.Type)
                 {
-                    this.Bark();
-                }
-                else if (this.Type == "Platypus")
-                {
-                    this.StashInPouch();
+                    case "Dingo":
+                        this.Bark();
+                        break;
+                    case "Platypus":
+                        this.StashInPouch();
+                        break;
+                    default:
+                        this.Move();
+                        break;
                 }
             }
         }
@@ -97,6 +101,25 @@
         public double GetPortionSize()
         {
             return this.Weight * 0.02;
+        }
+
+        /// <summary>
+        /// Gets whether the animal is ready to eat.
+        /// </summary>
+        /// <returns>Whether the animal is ready to eat.</returns>
+        public bool IsReadyToEat()
+        {
+            bool isReady = false;
+
+            if (this.Weight > 0)
+            {
+                if (this.Type == "Dingo" || this.Type == "Platypus")
+                {
+                    isReady = true;
+                }
+            }
+
+            return isReady;
         }
 
         /// <summary>

ZooScenario/Business Classes/Food.cs

Apply the following code changes:

--- 1.7/ZooScenario/Business Classes/Food.cs
+++ 2.1/ZooScenario/Business Classes/Food.cs
@@ -11,6 +11,11 @@
     public class Food
     {
         /// <summary>
+        /// The category of the food.
+        /// </summary>
+        public string Category;
+
+        /// <summary>
         /// The weight of the food.
         /// </summary>
         public double Weight;

ZooScenario/Business Classes/VendingMachine.cs

Apply the following code changes:

--- 1.7/ZooScenario/Business Classes/VendingMachine.cs
+++ 2.1/ZooScenario/Business Classes/VendingMachine.cs
@@ -73,15 +73,64 @@
         }
 
         /// <summary>
+        /// Gets the stock status of the vending machine.
+        /// </summary>
+        /// <returns>The stock status of the vending machine.</returns>
+        public string GetStockStatus()
+        {
+            string stockLevel;
+
+            if (this.FoodStock <= 0)
+            {
+                stockLevel = "Empty";
+            }
+            else if (this.FoodStock < 10)
+            {
+                stockLevel = "Low";
+            }
+            else
+            {
+                stockLevel = "Ready";
+            }
+
+            switch (stockLevel)
+            {
+                case "Empty":
+                    stockLevel = "Empty";
+                    break;
+                case "Low":
+                    stockLevel = "Low";
+                    break;
+                default:
+                    stockLevel = "Ready";
+                    break;
+            }
+
+            return stockLevel;
+        }
+
+        /// <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;
+            double foodWeight = 0.0;
+
+            if (payment > 0 && this.FoodPricePerPound > 0 && this.FoodStock > 0)
+            {
+                this.AddMoney(payment);
+                foodWeight = (double)(payment / this.FoodPricePerPound);
+
+                if (foodWeight > this.FoodStock)
+                {
+                    foodWeight = this.FoodStock;
+                }
+
+                this.FoodStock -= foodWeight;
+            }
+
             return foodWeight;
         }
     }

ZooScenario/Business Classes/Zoo.cs

Apply the following code changes:

--- 1.7/ZooScenario/Business Classes/Zoo.cs
+++ 2.1/ZooScenario/Business Classes/Zoo.cs
@@ -61,8 +61,11 @@
         /// <param name="ticketCount">The number of tickets the visitor buys.</param>
         public void OpenForVisitor(int ticketCount)
         {
-            this.SellTicket(ticketCount);
-            this.FeaturedAnimal.WakeUp();
+            if (this.FeaturedAnimal != null && this.Visitor != null && this.TicketBooth != null)
+            {
+                this.SellTicket(ticketCount);
+                this.FeaturedAnimal.WakeUp();
+            }
         }
 
         /// <summary>
@@ -71,7 +74,7 @@
         /// <param name="ticketCount">The number of tickets to sell.</param>
         public void SellTicket(int ticketCount)
         {
-            if (ticketCount > 0)
+            if (ticketCount > 0 && this.Visitor != null && this.TicketBooth != null)
             {
                 this.Visitor.BuyTicket(this.TicketBooth.TicketPrice, ticketCount);
                 this.TicketBooth.SellTicket(ticketCount);
@@ -85,18 +88,23 @@
         public double FeedFeaturedAnimal()
         {
             double foodWeight = 0.0;
-            double maxFoodWeight = this.FeaturedAnimal.GetPortionSize();
-            decimal foodCost = this.AnimalSnackMachine.DetermineFoodCost(maxFoodWeight);
-            decimal visitorMoneyBalance = this.Visitor.GetMoneyBalance();
 
-            if (visitorMoneyBalance >= foodCost)
+            if (this.FeaturedAnimal != null && this.Visitor != null && this.AnimalSnackMachine != null)
             {
-                decimal foodPayment = this.Visitor.RemoveMoney(foodCost);
-                foodWeight = this.AnimalSnackMachine.SellFood(foodPayment);
-                Food food = new Food();
-                food.Weight = foodWeight;
+                double maxFoodWeight = this.FeaturedAnimal.GetPortionSize();
+                decimal foodCost = this.AnimalSnackMachine.DetermineFoodCost(maxFoodWeight);
+                decimal visitorMoneyBalance = this.Visitor.GetMoneyBalance();
 
-                this.FeaturedAnimal.Eat(food);
+                if (this.FeaturedAnimal.IsReadyToEat() && visitorMoneyBalance >= foodCost)
+                {
+                    decimal foodPayment = this.Visitor.RemoveMoney(foodCost);
+                    foodWeight = this.AnimalSnackMachine.SellFood(foodPayment);
+                    Food food = new Food();
+                    food.Category = this.ChooseFoodCategory(this.FeaturedAnimal.Type);
+                    food.Weight = foodWeight;
+
+                    this.FeaturedAnimal.Eat(food);
+                }
             }
 
             return foodWeight;
@@ -108,7 +116,10 @@
         /// <param name="temperatureIncrease">The amount to increase the temperature.</param>
         public void PrepareBirthingRoom(double temperatureIncrease)
         {
-            this.BirthArea.WakeMotherUp(this.BirthArea.Mother, temperatureIncrease);
+            if (this.BirthArea != null && this.BirthArea.Mother != null)
+            {
+                this.BirthArea.WakeMotherUp(this.BirthArea.Mother, temperatureIncrease);
+            }
         }
 
         /// <summary>
@@ -138,5 +149,30 @@
         {
             this.FeaturedAnimal = animal;
         }
+
+        /// <summary>
+        /// Chooses the food category for an animal type.
+        /// </summary>
+        /// <param name="animalType">The animal type.</param>
+        /// <returns>The food category.</returns>
+        private string ChooseFoodCategory(string animalType)
+        {
+            string foodCategory;
+
+            switch (animalType)
+            {
+                case "Dingo":
+                    foodCategory = "Meat";
+                    break;
+                case "Platypus":
+                    foodCategory = "Insects";
+                    break;
+                default:
+                    foodCategory = "General";
+                    break;
+            }
+
+            return foodCategory;
+        }
     }
 }

ZooScenario/MainWindow.xaml

Apply the following code changes:

--- 1.7/ZooScenario/MainWindow.xaml
+++ 2.1/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="Julia, feed dingo" Click="feedComoAnimalButton_Click"/>
+            <Button x:Name="feedComoAnimalButton" Content="Darla, feed dingo" Click="feedComoAnimalButton_Click"/>
             <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"/>
@@ -43,7 +43,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="Dakota, feed platypus" Click="feedSanDiegoAnimalButton_Click"/>
+            <Button x:Name="feedSanDiegoAnimalButton" Content="Dave, feed platypus" Click="feedSanDiegoAnimalButton_Click"/>
             <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"/>

Check Your Work

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

Summary

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