Overview
Activity
Update the Zoo project to checkpoint 2.3.
User Story
As a developer, I want to complete the Zoo 2.3 checkpoint so the project includes the required programming concepts and behavior.
Acceptance Criteria
- All required new source files 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 2 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 2.2 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/Pen.csZooScenario/Business Classes/Tank.cs
Files to update
ZooScenario/Business Classes/Zoo.csZooScenario/MainWindow.xamlZooScenario/MainWindow.xaml.cs
Create New Files
ZooScenario/Business Classes/Pen.cs
Create this file with the following checkpoint source:
using System;
using System.Collections.Generic;
using System.Text;
namespace ZooScenario
{
/// <summary>
/// The class which is used to represent a pen.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Encapsulation not yet taught.")]
public class Pen
{
/// <summary>
/// The animal spaces in the pen.
/// </summary>
public Animal[] AnimalSpaces;
/// <summary>
/// Adds an animal to the pen.
/// </summary>
/// <param name="animal">The animal to add to the pen.</param>
public void AddAnimal(Animal animal)
{
if (this.AnimalSpaces != null && this.AnimalSpaces.Length > 0)
{
this.AnimalSpaces[0] = animal;
}
}
/// <summary>
/// Gets the display location map for the pen.
/// </summary>
/// <returns>The display location map for the pen.</returns>
public string GetDisplayLocation()
{
string penDisplay = string.Empty;
if (this.AnimalSpaces != null)
{
for (int index = 0; index < this.AnimalSpaces.Length; index++)
{
if (this.AnimalSpaces[index] != null)
{
penDisplay = penDisplay + "X";
}
else
{
penDisplay = penDisplay + ".";
}
}
}
return penDisplay;
}
/// <summary>
/// Moves the animal in the requested direction.
/// </summary>
/// <param name="direction">The direction to move.</param>
/// <returns>True if the animal moved; otherwise, false.</returns>
public bool MoveAnimal(string direction)
{
bool moved = false;
int animalIndex = this.FindAnimalIndex();
if (animalIndex >= 0)
{
switch (direction)
{
case "Left":
if (animalIndex > 0)
{
this.AnimalSpaces[animalIndex - 1] = this.AnimalSpaces[animalIndex];
this.AnimalSpaces[animalIndex] = null;
moved = true;
}
break;
case "Right":
if (animalIndex < this.AnimalSpaces.Length - 1)
{
this.AnimalSpaces[animalIndex + 1] = this.AnimalSpaces[animalIndex];
this.AnimalSpaces[animalIndex] = null;
moved = true;
}
break;
}
}
return moved;
}
/// <summary>
/// Finds the animal index.
/// </summary>
/// <returns>The animal index, or -1 if the animal is not found.</returns>
private int FindAnimalIndex()
{
int foundIndex = -1;
if (this.AnimalSpaces != null)
{
for (int index = 0; index < this.AnimalSpaces.Length; index++)
{
if (this.AnimalSpaces[index] != null)
{
foundIndex = index;
break;
}
}
}
return foundIndex;
}
}
}
ZooScenario/Business Classes/Tank.cs
Create this file with the following checkpoint source:
using System;
using System.Collections.Generic;
using System.Text;
namespace ZooScenario
{
/// <summary>
/// The class which is used to represent a tank.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Encapsulation not yet taught.")]
public class Tank
{
/// <summary>
/// The animal spaces in the tank.
/// </summary>
public Animal[,] AnimalSpaces;
/// <summary>
/// Adds an animal to the tank.
/// </summary>
/// <param name="animal">The animal to add to the tank.</param>
public void AddAnimal(Animal animal)
{
if (this.AnimalSpaces != null && this.AnimalSpaces.GetLength(0) > 0 && this.AnimalSpaces.GetLength(1) > 0)
{
this.AnimalSpaces[0, 0] = animal;
}
}
/// <summary>
/// Gets the display location map for the tank.
/// </summary>
/// <returns>The display location map for the tank.</returns>
public string GetDisplayLocation()
{
string tankDisplay = string.Empty;
if (this.AnimalSpaces != null)
{
for (int row = 0; row < this.AnimalSpaces.GetLength(0); row++)
{
for (int column = 0; column < this.AnimalSpaces.GetLength(1); column++)
{
if (this.AnimalSpaces[row, column] != null)
{
tankDisplay = tankDisplay + "X";
}
else
{
tankDisplay = tankDisplay + ".";
}
if (column < this.AnimalSpaces.GetLength(1) - 1)
{
tankDisplay = tankDisplay + " ";
}
}
if (row < this.AnimalSpaces.GetLength(0) - 1)
{
tankDisplay = tankDisplay + Environment.NewLine;
}
}
}
return tankDisplay;
}
/// <summary>
/// Moves the animal in the requested direction.
/// </summary>
/// <param name="direction">The direction to move.</param>
/// <returns>True if the animal moved; otherwise, false.</returns>
public bool MoveAnimal(string direction)
{
bool moved = false;
int row = this.FindAnimalRow();
int column = this.FindAnimalColumn();
if (row >= 0 && column >= 0)
{
int newRow = row;
int newColumn = column;
switch (direction)
{
case "Up":
newRow = row - 1;
break;
case "Down":
newRow = row + 1;
break;
case "Left":
newColumn = column - 1;
break;
case "Right":
newColumn = column + 1;
break;
}
if (this.IsValidLocation(newRow, newColumn))
{
this.AnimalSpaces[newRow, newColumn] = this.AnimalSpaces[row, column];
this.AnimalSpaces[row, column] = null;
moved = true;
}
}
return moved;
}
/// <summary>
/// Finds the animal column.
/// </summary>
/// <returns>The animal column, or -1 if the animal is not found.</returns>
private int FindAnimalColumn()
{
int foundColumn = -1;
if (this.AnimalSpaces != null)
{
for (int row = 0; row < this.AnimalSpaces.GetLength(0); row++)
{
for (int column = 0; column < this.AnimalSpaces.GetLength(1); column++)
{
if (this.AnimalSpaces[row, column] != null)
{
foundColumn = column;
break;
}
}
if (foundColumn >= 0)
{
break;
}
}
}
return foundColumn;
}
/// <summary>
/// Finds the animal row.
/// </summary>
/// <returns>The animal row, or -1 if the animal is not found.</returns>
private int FindAnimalRow()
{
int foundRow = -1;
if (this.AnimalSpaces != null)
{
for (int row = 0; row < this.AnimalSpaces.GetLength(0); row++)
{
for (int column = 0; column < this.AnimalSpaces.GetLength(1); column++)
{
if (this.AnimalSpaces[row, column] != null)
{
foundRow = row;
break;
}
}
if (foundRow >= 0)
{
break;
}
}
}
return foundRow;
}
/// <summary>
/// Gets whether the location is valid.
/// </summary>
/// <param name="row">The row to check.</param>
/// <param name="column">The column to check.</param>
/// <returns>Whether the location is valid.</returns>
private bool IsValidLocation(int row, int column)
{
bool isValid = false;
if (this.AnimalSpaces != null)
{
if (row >= 0 && row < this.AnimalSpaces.GetLength(0) && column >= 0 && column < this.AnimalSpaces.GetLength(1))
{
isValid = true;
}
}
return isValid;
}
}
}
Update Existing Files
ZooScenario/Business Classes/Zoo.cs
Apply the following code changes:
--- 2.2/ZooScenario/Business Classes/Zoo.cs
+++ 2.3/ZooScenario/Business Classes/Zoo.cs
@@ -31,6 +31,11 @@
public Animal FeaturedAnimal;
/// <summary>
+ /// The zoo's dingo pen.
+ /// </summary>
+ public Pen DingoPen;
+
+ /// <summary>
/// The zoo's ladies' restroom.
/// </summary>
public Restroom LadiesRoom;
@@ -44,6 +49,11 @@
/// The name of the zoo.
/// </summary>
public string Name;
+
+ /// <summary>
+ /// The zoo's platypus tank.
+ /// </summary>
+ public Tank PlatypusTank;
/// <summary>
/// The zoo's ticket booth.
@@ -148,6 +158,18 @@
public void SetFeaturedAnimal(Animal animal)
{
this.FeaturedAnimal = animal;
+
+ if (animal != null)
+ {
+ if (animal.Type == "Dingo" && this.DingoPen != null)
+ {
+ this.DingoPen.AddAnimal(animal);
+ }
+ else if (animal.Type == "Platypus" && this.PlatypusTank != null)
+ {
+ this.PlatypusTank.AddAnimal(animal);
+ }
+ }
}
/// <summary>
@@ -191,5 +213,97 @@
return totalFoodWeight;
}
+
+ /// <summary>
+ /// Gets the dingo pen display.
+ /// </summary>
+ /// <returns>The dingo pen display.</returns>
+ public string GetDingoPenDisplay()
+ {
+ string display = string.Empty;
+
+ if (this.DingoPen != null)
+ {
+ display = this.DingoPen.GetDisplayLocation();
+ }
+
+ return display;
+ }
+
+ /// <summary>
+ /// Gets the platypus tank display.
+ /// </summary>
+ /// <returns>The platypus tank display.</returns>
+ public string GetPlatypusTankDisplay()
+ {
+ string display = string.Empty;
+
+ if (this.PlatypusTank != null)
+ {
+ display = this.PlatypusTank.GetDisplayLocation();
+ }
+
+ return display;
+ }
+
+ /// <summary>
+ /// Moves the featured animal down.
+ /// </summary>
+ /// <returns>True if the featured animal moved; otherwise, false.</returns>
+ public bool MoveFeaturedAnimalDown()
+ {
+ return this.MoveFeaturedAnimal("Down");
+ }
+
+ /// <summary>
+ /// Moves the featured animal left.
+ /// </summary>
+ /// <returns>True if the featured animal moved; otherwise, false.</returns>
+ public bool MoveFeaturedAnimalLeft()
+ {
+ return this.MoveFeaturedAnimal("Left");
+ }
+
+ /// <summary>
+ /// Moves the featured animal right.
+ /// </summary>
+ /// <returns>True if the featured animal moved; otherwise, false.</returns>
+ public bool MoveFeaturedAnimalRight()
+ {
+ return this.MoveFeaturedAnimal("Right");
+ }
+
+ /// <summary>
+ /// Moves the featured animal up.
+ /// </summary>
+ /// <returns>True if the featured animal moved; otherwise, false.</returns>
+ public bool MoveFeaturedAnimalUp()
+ {
+ return this.MoveFeaturedAnimal("Up");
+ }
+
+ /// <summary>
+ /// Moves the featured animal.
+ /// </summary>
+ /// <param name="direction">The direction to move.</param>
+ /// <returns>True if the featured animal moved; otherwise, false.</returns>
+ private bool MoveFeaturedAnimal(string direction)
+ {
+ bool moved = false;
+
+ if (this.FeaturedAnimal != null)
+ {
+ if (this.FeaturedAnimal.Type == "Dingo" && this.DingoPen != null)
+ {
+ moved = this.DingoPen.MoveAnimal(direction);
+ }
+ else if (this.FeaturedAnimal.Type == "Platypus" && this.PlatypusTank != null)
+ {
+ moved = this.PlatypusTank.MoveAnimal(direction);
+ }
+ }
+
+ return moved;
+ }
}
}
ZooScenario/MainWindow.xaml
Apply the following code changes:
--- 2.2/ZooScenario/MainWindow.xaml
+++ 2.3/ZooScenario/MainWindow.xaml
@@ -27,6 +27,12 @@
<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"/>
+ <TextBlock Text="Dingo side-view pen:" Margin="3,10,3,0"/>
+ <TextBox x:Name="penDisplayTextBox" FontFamily="Consolas" Height="24" Margin="3" IsReadOnly="True"/>
+ <UniformGrid Columns="2">
+ <Button x:Name="moveDingoLeftButton" Content="Left" Click="moveDingoLeftButton_Click"/>
+ <Button x:Name="moveDingoRightButton" Content="Right" Click="moveDingoRightButton_Click"/>
+ </UniformGrid>
<!-- End Como Zoo buttons -->
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
@@ -47,6 +53,14 @@
<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"/>
+ <TextBlock Text="Platypus fish tank:" Margin="3,10,3,0"/>
+ <TextBox x:Name="tankDisplayTextBox" FontFamily="Consolas" Height="60" Margin="3" IsReadOnly="True" AcceptsReturn="True"/>
+ <UniformGrid Columns="4">
+ <Button x:Name="movePlatypusUpButton" Content="Up" Click="movePlatypusUpButton_Click"/>
+ <Button x:Name="movePlatypusDownButton" Content="Down" Click="movePlatypusDownButton_Click"/>
+ <Button x:Name="movePlatypusLeftButton" Content="Left" Click="movePlatypusLeftButton_Click"/>
+ <Button x:Name="movePlatypusRightButton" Content="Right" Click="movePlatypusRightButton_Click"/>
+ </UniformGrid>
<!-- End San Diego Zoo buttons -->
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
ZooScenario/MainWindow.xaml.cs
Apply the following code changes:
--- 2.2/ZooScenario/MainWindow.xaml.cs
+++ 2.3/ZooScenario/MainWindow.xaml.cs
@@ -48,6 +48,8 @@
this.ComoZoo.AnimalSnackMachine = new VendingMachine();
this.ComoZoo.BirthArea = new BirthingRoom();
this.ComoZoo.Capacity = 1000;
+ this.ComoZoo.DingoPen = new Pen();
+ this.ComoZoo.DingoPen.AnimalSpaces = new Animal[5];
this.ComoZoo.FeaturedAnimal = new Animal();
this.ComoZoo.LadiesRoom = new Restroom();
this.ComoZoo.MensRoom = new Restroom();
@@ -74,6 +76,7 @@
this.ComoZoo.FeaturedAnimal.Type = "Dingo";
this.ComoZoo.FeaturedAnimal.Weight = 35.3;
this.ComoZoo.FeaturedAnimal.MakePregnant();
+ this.ComoZoo.SetFeaturedAnimal(this.ComoZoo.FeaturedAnimal);
this.ComoZoo.LadiesRoom.Capacity = 4;
this.ComoZoo.LadiesRoom.Gender = "Female";
@@ -90,6 +93,7 @@
this.ComoZoo.BirthArea.Doctor.Name = "Flora";
this.ComoZoo.BirthArea.Doctor.Number = 98;
this.ComoZoo.BirthArea.Doctor.AssignAnimal(this.ComoZoo.GetFeaturedAnimal());
+ this.penDisplayTextBox.Text = this.ComoZoo.GetDingoPenDisplay();
}
/// <summary>
@@ -103,6 +107,8 @@
this.SanDiegoZoo.AnimalSnackMachine = new VendingMachine();
this.SanDiegoZoo.BirthArea = new BirthingRoom();
this.SanDiegoZoo.Capacity = 3000;
+ this.SanDiegoZoo.PlatypusTank = new Tank();
+ this.SanDiegoZoo.PlatypusTank.AnimalSpaces = new Animal[3, 3];
this.SanDiegoZoo.FeaturedAnimal = new Animal();
this.SanDiegoZoo.LadiesRoom = new Restroom();
this.SanDiegoZoo.MensRoom = new Restroom();
@@ -132,6 +138,7 @@
this.SanDiegoZoo.FeaturedAnimal.Name = "Patti";
this.SanDiegoZoo.FeaturedAnimal.Type = "Platypus";
this.SanDiegoZoo.FeaturedAnimal.Weight = 3.27;
+ this.SanDiegoZoo.SetFeaturedAnimal(this.SanDiegoZoo.FeaturedAnimal);
// Assign field values of the restrooms.
this.SanDiegoZoo.LadiesRoom.Capacity = 12;
@@ -150,6 +157,7 @@
this.SanDiegoZoo.BirthArea.Doctor.Name = "Steve";
this.SanDiegoZoo.BirthArea.Doctor.Number = 24;
this.SanDiegoZoo.BirthArea.Doctor.AssignAnimal(this.SanDiegoZoo.GetFeaturedAnimal());
+ this.tankDisplayTextBox.Text = this.SanDiegoZoo.GetPlatypusTankDisplay();
}
/// <summary>
@@ -214,6 +222,72 @@
}
/// <summary>
+ /// Moves the Como Zoo's featured animal left.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void moveDingoLeftButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.ComoZoo.MoveFeaturedAnimalLeft();
+ this.penDisplayTextBox.Text = this.ComoZoo.GetDingoPenDisplay();
+ }
+
+ /// <summary>
+ /// Moves the Como Zoo's featured animal right.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void moveDingoRightButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.ComoZoo.MoveFeaturedAnimalRight();
+ this.penDisplayTextBox.Text = this.ComoZoo.GetDingoPenDisplay();
+ }
+
+ /// <summary>
+ /// Moves the San Diego Zoo's featured animal down.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void movePlatypusDownButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.SanDiegoZoo.MoveFeaturedAnimalDown();
+ this.tankDisplayTextBox.Text = this.SanDiegoZoo.GetPlatypusTankDisplay();
+ }
+
+ /// <summary>
+ /// Moves the San Diego Zoo's featured animal left.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void movePlatypusLeftButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.SanDiegoZoo.MoveFeaturedAnimalLeft();
+ this.tankDisplayTextBox.Text = this.SanDiegoZoo.GetPlatypusTankDisplay();
+ }
+
+ /// <summary>
+ /// Moves the San Diego Zoo's featured animal right.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void movePlatypusRightButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.SanDiegoZoo.MoveFeaturedAnimalRight();
+ this.tankDisplayTextBox.Text = this.SanDiegoZoo.GetPlatypusTankDisplay();
+ }
+
+ /// <summary>
+ /// Moves the San Diego Zoo's featured animal up.
+ /// </summary>
+ /// <param name="sender">The object that initiated the event.</param>
+ /// <param name="e">The event arguments for the event.</param>
+ private void movePlatypusUpButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.SanDiegoZoo.MoveFeaturedAnimalUp();
+ this.tankDisplayTextBox.Text = this.SanDiegoZoo.GetPlatypusTankDisplay();
+ }
+
+ /// <summary>
/// Opens the San Diego Zoo for a visitor.
/// </summary>
/// <param name="sender">The object that initiated the event.</param>
ZooScenario/ZooScenario.csproj
Verify that Visual Studio added the required files to the project:
--- 2.2/ZooScenario/ZooScenario.csproj
+++ 2.3/ZooScenario/ZooScenario.csproj
@@ -73,7 +73,9 @@
<Compile Include="Business Classes\Employee.cs" />
<Compile Include="Business Classes\Food.cs" />
<Compile Include="Business Classes\Guest.cs" />
+ <Compile Include="Business Classes\Pen.cs" />
<Compile Include="Business Classes\Restroom.cs" />
+ <Compile Include="Business Classes\Tank.cs" />
<Compile Include="Business Classes\VendingMachine.cs" />
<Compile Include="Business Classes\Wallet.cs" />
<Compile Include="Business Classes\Zoo.cs" />
Check Your Work
- Save all files.
- Build the solution.
- Resolve compiler errors before continuing.
- Run the application.
- Exercise the behavior associated with Zoo 2.3.
- Compare your filenames, namespaces, signatures, and key values with the code shown above.
Summary
You completed Zoo 2.3 by creating the required files, modifying only files with meaningful source changes, and verifying the application.