Overview
Activity
Update the Zoo project from checkpoint 3.3 to checkpoint 3.4.
User Story
As a developer, I want to create concrete animal classes and connect them to the zoo model so the project demonstrates the programming concepts introduced in Zoo 3.4.
Acceptance Criteria
- All required new classes, enums, or interfaces exist in the correct folders.
- 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
- Create 3 new source files.
- Update 3 existing source files.
- Confirm the new files are included in the project.
- 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 3.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
ZooScenario/Business Classes/Animals/Dingo.csZooScenario/Business Classes/Animals/Hummingbird.csZooScenario/Business Classes/Animals/Platypus.cs
Files to update
ZooScenario/Business Classes/Animal.csZooScenario/Business Classes/Zoo.csZooScenario/MainWindow.xaml.cs
Create New Files
ZooScenario/Business Classes/Animals/Dingo.cs
Create this file with the following checkpoint source:
using System;
using System.Collections.Generic;
using System.Text;
namespace ZooScenario
{
/// <summary>
/// A new Dingo class of type Animal.
/// </summary>
public class Dingo : Animal
{
/// <summary>
/// A new Dingo constructor.
/// </summary>
/// <param name="gender">The gender of the Dingo.</param>
/// <param name="name">The name of the Dingo.</param>
/// <param name="age">The age of the Dingo.</param>
/// <param name="weight">The weight of the Dingo.</param>
public Dingo(Gender gender, string name, int age, double weight)
: base(name, gender, age, weight)
{
}
/// <summary>
/// The dingo eats food and gets happy.
/// </summary>
/// <param name="food">The food that the dingo is eating.</param>
public override void Eat(Food food)
{
base.Eat(food);
this.Bark();
}
}
}
ZooScenario/Business Classes/Animals/Hummingbird.cs
Create this file with the following checkpoint source:
using System;
using System.Collections.Generic;
using System.Text;
namespace ZooScenario
{
/// <summary>
/// The hummingbird class.
/// </summary>
public class Hummingbird : Animal
{
/// <summary>
/// A new hummingbird constructor.
/// </summary>
/// <param name="gender">The gender of the hummingbird.</param>
/// <param name="name">The name of the hummingbird.</param>
/// <param name="age">The age of the hummingbird.</param>
/// <param name="weight">The weight of the hummingbird.</param>
public Hummingbird(Gender gender, string name, int age, double weight)
: base(name, gender, age, weight)
{
}
/// <summary>
/// Makes the animal move.
/// </summary>
public override void Move()
{
base.Move();
this.Weight = this.Weight - (this.Weight * 0.06);
}
}
}
ZooScenario/Business Classes/Animals/Platypus.cs
Create this file with the following checkpoint source:
using System;
using System.Collections.Generic;
using System.Text;
namespace ZooScenario
{
/// <summary>
/// The platypus class.
/// </summary>
public class Platypus : Animal
{
/// <summary>
/// A new platypus constructor.
/// </summary>
/// <param name="gender">The gender of the platypus.</param>
/// <param name="name">The name of the platypus.</param>
/// <param name="age">The age of the platypus.</param>
/// <param name="weight">The weight of the platypus.</param>
public Platypus(Gender gender, string name, int age, double weight)
: base(name, gender, age, weight)
{
}
/// <summary>
/// The Platypus eats the food.
/// </summary>
/// <param name="food">The food for the platypus to eat.</param>
public override void Eat(Food food)
{
this.StashInPouch();
base.Eat(food);
}
/// <summary>
/// Make the platypus move.
/// </summary>
public override void Move()
{
base.Move();
this.Weight = this.Weight - (this.Weight * 0.02);
}
}
}
Update Existing Files
ZooScenario/Business Classes/Animal.cs
Apply the following code changes:
--- 3.3/ZooScenario/Business Classes/Animal.cs
+++ 3.4/ZooScenario/Business Classes/Animal.cs
@@ -35,11 +35,6 @@
private string name;
/// <summary>
- /// The type of the animal.
- /// </summary>
- private string type;
-
- /// <summary>
/// The weight of the animal (in pounds).
/// </summary>
private double weight;
@@ -52,12 +47,11 @@
/// <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, Gender gender, int age, double weight)
+ public Animal(string name, Gender gender, int age, double weight)
{
this.Age = age;
this.Gender = gender;
this.Name = name;
- this.Type = type;
this.Weight = weight;
}
@@ -136,44 +130,6 @@
}
/// <summary>
- /// Gets or sets the type.
- /// </summary>
- public string Type
- {
- get
- {
- return this.type;
- }
-
- set
- {
- if (value == null || value.Trim() == string.Empty)
- {
- throw new ArgumentException("Type is required.");
- }
-
- 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
- {
- throw new ArgumentException("Type must be Dingo, Hummingbird, or Platypus.");
- }
- }
- }
-
- /// <summary>
/// Gets or sets the weight.
/// </summary>
public double Weight
@@ -201,7 +157,7 @@
{
get
{
- return this.Name + " the " + this.Type;
+ return this.Name + " the " + this.GetType().Name;
}
set
@@ -266,7 +222,7 @@
/// Eats a specific amount of food.
/// </summary>
/// <param name="food">The food the animal eats.</param>
- public void Eat(Food food)
+ public virtual void Eat(Food food)
{
if (food != null && food.Weight > 0 && this.Weight > 0)
{
@@ -279,7 +235,7 @@
this.Baby.Weight += babyWeightGain;
}
- switch (this.Type)
+ switch (this.GetType().Name)
{
case "Dingo":
this.Bark();
@@ -304,7 +260,7 @@
if (this.Weight > 0)
{
- if (this.Type == "Dingo" || this.Type == "Platypus")
+ if (this.GetType().Name == "Dingo" || this.GetType().Name == "Platypus")
{
isReady = true;
}
@@ -333,7 +289,19 @@
{
if (this.Gender == Gender.Female && !this.IsPregnant)
{
- this.Baby = new Animal("Baby " + this.Name, this.Type, Gender.Female, 0, this.Weight * 0.1);
+ if (this.GetType().Name == "Dingo")
+ {
+ this.Baby = new Dingo(Gender.Female, "Baby " + this.Name, 0, this.Weight * 0.1);
+ }
+ else if (this.GetType().Name == "Hummingbird")
+ {
+ this.Baby = new Hummingbird(Gender.Female, "Baby " + this.Name, 0, this.Weight * 0.1);
+ }
+ else if (this.GetType().Name == "Platypus")
+ {
+ this.Baby = new Platypus(Gender.Female, "Baby " + this.Name, 0, this.Weight * 0.1);
+ }
+
this.Baby.HappinessLevel = 0;
}
}
@@ -341,7 +309,7 @@
/// <summary>
/// Makes the animal move.
/// </summary>
- public void Move()
+ public virtual void Move()
{
this.HappinessLevel = this.HappinessLevel + 1;
}
@@ -357,7 +325,7 @@
/// <summary>
/// Makes the animal wake up and move.
/// </summary>
- public void WakeUp()
+ public virtual void WakeUp()
{
this.Move();
}
ZooScenario/Business Classes/Zoo.cs
Apply the following code changes:
--- 3.3/ZooScenario/Business Classes/Zoo.cs
+++ 3.4/ZooScenario/Business Classes/Zoo.cs
@@ -182,11 +182,11 @@
if (value != null)
{
- if (value.Type == "Dingo" && this.DingoPen != null)
+ if (value.GetType().Name == "Dingo" && this.DingoPen != null)
{
this.DingoPen.AddAnimal(value);
}
- else if (value.Type == "Platypus" && this.PlatypusTank != null)
+ else if (value.GetType().Name == "Platypus" && this.PlatypusTank != null)
{
this.PlatypusTank.AddAnimal(value);
}
@@ -413,7 +413,7 @@
decimal foodPayment = this.Visitor.RemoveMoney(foodCost);
foodWeight = this.AnimalSnackMachine.SellFood(foodPayment);
Food food = new Food(foodWeight);
- food.Category = this.ChooseFoodCategory(this.FeaturedAnimal.Type);
+ food.Category = this.ChooseFoodCategory(this.FeaturedAnimal.GetType().Name);
this.FeaturedAnimal.Eat(food);
}
@@ -561,7 +561,7 @@
if (animal != null && animal.IsReadyToEat())
{
Food food = new Food(animal.PortionSize);
- food.Category = this.ChooseFoodCategory(animal.Type);
+ food.Category = this.ChooseFoodCategory(animal.GetType().Name);
animal.Eat(food);
animalsFed = animalsFed + 1;
}
@@ -702,11 +702,11 @@
if (this.FeaturedAnimal != null)
{
- if (this.FeaturedAnimal.Type == "Dingo" && this.DingoPen != null)
+ if (this.FeaturedAnimal.GetType().Name == "Dingo" && this.DingoPen != null)
{
moved = this.DingoPen.MoveAnimal(direction);
}
- else if (this.FeaturedAnimal.Type == "Platypus" && this.PlatypusTank != null)
+ else if (this.FeaturedAnimal.GetType().Name == "Platypus" && this.PlatypusTank != null)
{
moved = this.PlatypusTank.MoveAnimal(direction);
}
ZooScenario/MainWindow.xaml.cs
Apply the following code changes:
--- 3.3/ZooScenario/MainWindow.xaml.cs
+++ 3.4/ZooScenario/MainWindow.xaml.cs
@@ -58,7 +58,23 @@
/// <returns>The animal that was created.</returns>
private Animal CreateAnimal(string name, string type, Gender gender, int age, double weight, bool isPregnant)
{
- Animal animal = new Animal(name, type, gender, age, weight);
+ Animal animal;
+
+ switch (type.Trim().ToLower())
+ {
+ case "dingo":
+ animal = new Dingo(gender, name, age, weight);
+ break;
+ case "hummingbird":
+ animal = new Hummingbird(gender, name, age, weight);
+ break;
+ case "platypus":
+ animal = new Platypus(gender, name, age, weight);
+ break;
+ default:
+ animal = new Dingo(gender, name, age, weight);
+ break;
+ }
if (isPregnant)
{
@@ -287,7 +303,7 @@
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", Gender.Female, 4, 35.3);
+ this.comoZoo.FeaturedAnimal = new Dingo(Gender.Female, "Dolly", 4, 35.3);
this.comoZoo.FeaturedAnimal.MakePregnant();
this.comoZoo.BirthArea.Mother = this.comoZoo.FeaturedAnimal;
this.comoZoo.BirthArea.Doctor.AssignedAnimal = this.comoZoo.FeaturedAnimal;
@@ -310,7 +326,7 @@
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", Gender.Female, 5, 3.27);
+ this.sanDiegoZoo.FeaturedAnimal = new Platypus(Gender.Female, "Patti", 5, 3.27);
this.sanDiegoZoo.BirthArea.Mother = this.sanDiegoZoo.FeaturedAnimal;
this.sanDiegoZoo.BirthArea.Doctor.AssignedAnimal = this.sanDiegoZoo.FeaturedAnimal;
this.sanDiegoZoo.AddAnimal(this.sanDiegoZoo.FeaturedAnimal);
ZooScenario/ZooScenario.csproj
Verify that Visual Studio added the required files to the project:
--- 3.3/ZooScenario/ZooScenario.csproj
+++ 3.4/ZooScenario/ZooScenario.csproj
@@ -68,6 +68,9 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Business Classes\Animal.cs" />
+ <Compile Include="Business Classes\Animals\Dingo.cs" />
+ <Compile Include="Business Classes\Animals\Hummingbird.cs" />
+ <Compile Include="Business Classes\Animals\Platypus.cs" />
<Compile Include="Business Classes\BirthingRoom.cs" />
<Compile Include="Business Classes\Booth.cs" />
<Compile Include="Business Classes\Employee.cs" />
Check Your Work
- Save all files.
- Build the solution.
- Resolve compiler errors before continuing.
- Run the application.
- Exercise the new or updated behavior associated with Zoo 3.4.
- Compare your filenames, namespaces, signatures, and key values with the code shown above.
Summary
You updated the project from Zoo 3.3 End to Zoo 3.4 End by creating the required files, modifying only the files with meaningful source changes, and verifying the completed application.