Overview
Activity
Update the Zoo project to checkpoint 2.5.
User Story
As a developer, I want to complete the Zoo 2.5 checkpoint so the project includes the required programming concepts and behavior.
Acceptance Criteria
- 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
- Update 8 existing source files.
- 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.4 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
- None
Files to update
ZooScenario/Business Classes/Animal.csZooScenario/Business Classes/Employee.csZooScenario/Business Classes/Food.csZooScenario/Business Classes/Guest.csZooScenario/Business Classes/VendingMachine.csZooScenario/Business Classes/Wallet.csZooScenario/Business Classes/Zoo.csZooScenario/MainWindow.xaml.cs
Create New Files
No new instructional source files are required for this checkpoint.
Update Existing Files
ZooScenario/Business Classes/Animal.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Animal.cs
+++ 2.5/ZooScenario/Business Classes/Animal.cs
@@ -26,11 +26,6 @@
public string Gender;
/// <summary>
- /// The happiness level of the animal.
- /// </summary>
- public int HappinessLevel;
-
- /// <summary>
/// The name of the animal.
/// </summary>
public string Name;
@@ -46,11 +41,16 @@
public double Weight;
/// <summary>
+ /// The happiness level of the animal.
+ /// </summary>
+ private int happinessLevel;
+
+ /// <summary>
/// Makes the animal bark.
/// </summary>
public void Bark()
{
- this.HappinessLevel = this.HappinessLevel + 1;
+ this.happinessLevel = this.happinessLevel + 1;
}
/// <summary>
@@ -59,14 +59,14 @@
/// <param name="food">The food the animal eats.</param>
public void Eat(Food food)
{
- if (food != null && food.Weight > 0 && this.Weight > 0)
+ if (food != null && food.GetWeight() > 0 && this.Weight > 0)
{
- double weightGain = food.Weight * 0.8;
+ double weightGain = food.GetWeight() * 0.8;
this.Weight += weightGain;
if (this.GetIsPregnant() && this.Baby != null)
{
- double babyWeightGain = food.Weight * 0.1;
+ double babyWeightGain = food.GetWeight() * 0.1;
this.Baby.Weight += babyWeightGain;
}
@@ -145,7 +145,7 @@
this.Baby = new Animal();
this.Baby.Age = 0;
this.Baby.Gender = "Unknown";
- this.Baby.HappinessLevel = 0;
+ this.Baby.happinessLevel = 0;
this.Baby.Name = "Baby " + this.Name;
this.Baby.Type = this.Type;
this.Baby.Weight = this.Weight * 0.1;
@@ -157,7 +157,7 @@
/// </summary>
public void Move()
{
- this.HappinessLevel = this.HappinessLevel + 1;
+ this.happinessLevel = this.happinessLevel + 1;
}
/// <summary>
@@ -165,7 +165,7 @@
/// </summary>
public void StashInPouch()
{
- this.HappinessLevel = this.HappinessLevel + 1;
+ this.happinessLevel = this.happinessLevel + 1;
}
/// <summary>
ZooScenario/Business Classes/Employee.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Employee.cs
+++ 2.5/ZooScenario/Business Classes/Employee.cs
@@ -68,7 +68,7 @@
Animal animal = this.GetAssignedAnimal();
Food food = new Food();
- food.Weight = 1.0;
+ food.SetWeight(1.0);
if (animal != null)
{
ZooScenario/Business Classes/Food.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Food.cs
+++ 2.5/ZooScenario/Business Classes/Food.cs
@@ -18,6 +18,27 @@
/// <summary>
/// The weight of the food.
/// </summary>
- public double Weight;
+ private double weight;
+
+ /// <summary>
+ /// Gets the weight of the food.
+ /// </summary>
+ /// <returns>The weight of the food.</returns>
+ public double GetWeight()
+ {
+ return this.weight;
+ }
+
+ /// <summary>
+ /// Sets the weight of the food.
+ /// </summary>
+ /// <param name="weight">The weight of the food.</param>
+ public void SetWeight(double weight)
+ {
+ if (weight > 0)
+ {
+ this.weight = weight;
+ }
+ }
}
}
ZooScenario/Business Classes/Guest.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Guest.cs
+++ 2.5/ZooScenario/Business Classes/Guest.cs
@@ -41,7 +41,7 @@
/// <returns>The guest's current money balance.</returns>
public decimal GetMoneyBalance()
{
- return this.Wallet.MoneyBalance;
+ return this.Wallet.GetMoneyBalance();
}
/// <summary>
ZooScenario/Business Classes/VendingMachine.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/VendingMachine.cs
+++ 2.5/ZooScenario/Business Classes/VendingMachine.cs
@@ -11,19 +11,24 @@
public class VendingMachine
{
/// <summary>
+ /// The maximum food capacity.
+ /// </summary>
+ private readonly double foodCapacity = 250.0;
+
+ /// <summary>
/// The price of food (per pound).
/// </summary>
- public decimal FoodPricePerPound;
+ private decimal foodPricePerPound;
/// <summary>
/// The amount of food currently in stock (in pounds).
/// </summary>
- public double FoodStock;
+ private double foodStock;
/// <summary>
/// The amount of money currently in the vending machine.
/// </summary>
- public decimal MoneyBalance;
+ private decimal moneyBalance;
/// <summary>
/// Adds a bag of food to the vending machine.
@@ -33,7 +38,7 @@
{
if (poundsOfFood > 0)
{
- this.FoodStock = this.FoodStock + poundsOfFood;
+ this.foodStock = this.foodStock + poundsOfFood;
}
}
@@ -56,7 +61,7 @@
{
if (moneyAmount > 0)
{
- this.MoneyBalance += moneyAmount;
+ this.moneyBalance += moneyAmount;
}
}
@@ -67,7 +72,7 @@
/// <returns>The cost of the food.</returns>
public decimal DetermineFoodCost(double maxFoodWeight)
{
- decimal maxFoodCost = (decimal)maxFoodWeight * this.FoodPricePerPound;
+ decimal maxFoodCost = (decimal)maxFoodWeight * this.foodPricePerPound;
decimal foodCost = Math.Round(maxFoodCost, 2);
return foodCost;
}
@@ -78,7 +83,7 @@
/// <param name="targetStock">The target food stock.</param>
public void FillToTarget(double targetStock)
{
- while (this.FoodStock < targetStock)
+ while (this.foodStock < targetStock)
{
this.AddFoodBag(5.0);
}
@@ -92,11 +97,11 @@
{
string stockLevel;
- if (this.FoodStock <= 0)
+ if (this.foodStock <= 0)
{
stockLevel = "Empty";
}
- else if (this.FoodStock < 10)
+ else if (this.foodStock < 10)
{
stockLevel = "Low";
}
@@ -122,6 +127,51 @@
}
/// <summary>
+ /// Gets the food stock.
+ /// </summary>
+ /// <returns>The food stock.</returns>
+ public double GetFoodStock()
+ {
+ return this.foodStock;
+ }
+
+ /// <summary>
+ /// Sets the food price per pound.
+ /// </summary>
+ /// <param name="foodPricePerPound">The food price per pound.</param>
+ public void SetFoodPricePerPound(decimal foodPricePerPound)
+ {
+ if (foodPricePerPound > 0)
+ {
+ this.foodPricePerPound = foodPricePerPound;
+ }
+ }
+
+ /// <summary>
+ /// Sets the food stock.
+ /// </summary>
+ /// <param name="foodStock">The food stock.</param>
+ public void SetFoodStock(double foodStock)
+ {
+ if (foodStock >= 0 && foodStock <= this.foodCapacity)
+ {
+ this.foodStock = foodStock;
+ }
+ }
+
+ /// <summary>
+ /// Sets the money balance.
+ /// </summary>
+ /// <param name="moneyBalance">The money balance.</param>
+ public void SetMoneyBalance(decimal moneyBalance)
+ {
+ if (moneyBalance >= 0)
+ {
+ this.moneyBalance = moneyBalance;
+ }
+ }
+
+ /// <summary>
/// Sells food from the vending machine.
/// </summary>
/// <param name="payment">The amount of money paid.</param>
@@ -130,17 +180,17 @@
{
double foodWeight = 0.0;
- if (payment > 0 && this.FoodPricePerPound > 0 && this.FoodStock > 0)
+ if (payment > 0 && this.foodPricePerPound > 0 && this.foodStock > 0)
{
this.AddMoney(payment);
- foodWeight = (double)(payment / this.FoodPricePerPound);
+ foodWeight = (double)(payment / this.foodPricePerPound);
- if (foodWeight > this.FoodStock)
+ if (foodWeight > this.foodStock)
{
- foodWeight = this.FoodStock;
+ foodWeight = this.foodStock;
}
- this.FoodStock -= foodWeight;
+ this.foodStock -= foodWeight;
}
return foodWeight;
ZooScenario/Business Classes/Wallet.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Wallet.cs
+++ 2.5/ZooScenario/Business Classes/Wallet.cs
@@ -13,7 +13,7 @@
/// <summary>
/// The amount of money currently contained within the wallet.
/// </summary>
- public decimal MoneyBalance;
+ private decimal moneyBalance;
/// <summary>
/// Adds money to the wallet.
@@ -23,8 +23,17 @@
{
if (moneyAmount > 0)
{
- this.MoneyBalance = this.MoneyBalance + moneyAmount;
+ this.moneyBalance = this.moneyBalance + moneyAmount;
}
+ }
+
+ /// <summary>
+ /// Gets the money balance.
+ /// </summary>
+ /// <returns>The money balance.</returns>
+ public decimal GetMoneyBalance()
+ {
+ return this.moneyBalance;
}
/// <summary>
@@ -36,7 +45,7 @@
{
decimal amountRemoved;
- if (amount > 0.00m)
+ if (amount > 0.00m && amount <= this.moneyBalance)
{
amountRemoved = amount;
}
@@ -45,7 +54,7 @@
amountRemoved = 0.00m;
}
- this.MoneyBalance = this.MoneyBalance - amountRemoved;
+ this.moneyBalance = this.moneyBalance - amountRemoved;
return amountRemoved;
}
@@ -61,8 +70,20 @@
if (ticketCount > 0)
{
decimal totalTicketPrice = ticketPrice * ticketCount;
- this.MoneyBalance = this.MoneyBalance - totalTicketPrice;
+ this.RemoveMoney(totalTicketPrice);
}
+ }
+ }
+
+ /// <summary>
+ /// Sets the money balance.
+ /// </summary>
+ /// <param name="moneyBalance">The money balance.</param>
+ public void SetMoneyBalance(decimal moneyBalance)
+ {
+ if (moneyBalance >= 0)
+ {
+ this.moneyBalance = moneyBalance;
}
}
}
ZooScenario/Business Classes/Zoo.cs
Apply the following code changes:
--- 2.4/ZooScenario/Business Classes/Zoo.cs
+++ 2.5/ZooScenario/Business Classes/Zoo.cs
@@ -116,7 +116,7 @@
foodWeight = this.AnimalSnackMachine.SellFood(foodPayment);
Food food = new Food();
food.Category = this.ChooseFoodCategory(this.FeaturedAnimal.Type);
- food.Weight = foodWeight;
+ food.SetWeight(foodWeight);
this.FeaturedAnimal.Eat(food);
}
@@ -344,7 +344,7 @@
{
Food food = new Food();
food.Category = this.ChooseFoodCategory(animal.Type);
- food.Weight = animal.GetPortionSize();
+ food.SetWeight(animal.GetPortionSize());
animal.Eat(food);
animalsFed = animalsFed + 1;
}
ZooScenario/MainWindow.xaml.cs
Apply the following code changes:
--- 2.4/ZooScenario/MainWindow.xaml.cs
+++ 2.5/ZooScenario/MainWindow.xaml.cs
@@ -174,13 +174,13 @@
this.ComoZoo.BirthArea.Temperature = 77.0;
this.ComoZoo.BirthArea.Doctor = new Employee();
- this.ComoZoo.AnimalSnackMachine.FoodPricePerPound = 0.75m;
- this.ComoZoo.AnimalSnackMachine.FoodStock = 250.0;
- this.ComoZoo.AnimalSnackMachine.MoneyBalance = 42.75m;
+ this.ComoZoo.AnimalSnackMachine.SetFoodPricePerPound(0.75m);
+ this.ComoZoo.AnimalSnackMachine.SetFoodStock(250.0);
+ this.ComoZoo.AnimalSnackMachine.SetMoneyBalance(42.75m);
this.ComoZoo.Visitor.Age = 12;
this.ComoZoo.Visitor.Name = "Julia";
- this.ComoZoo.Visitor.Wallet.MoneyBalance = 25.00m;
+ this.ComoZoo.Visitor.Wallet.SetMoneyBalance(25.00m);
this.ComoZoo.FeaturedAnimal.Age = 4;
this.ComoZoo.FeaturedAnimal.Gender = "Female";
@@ -239,14 +239,14 @@
this.SanDiegoZoo.BirthArea.Doctor = new Employee();
// Assign field values of the animal snack machine.
- this.SanDiegoZoo.AnimalSnackMachine.FoodPricePerPound = 1.20m;
- this.SanDiegoZoo.AnimalSnackMachine.FoodStock = 3.5;
- this.SanDiegoZoo.AnimalSnackMachine.MoneyBalance = 56.25m;
+ this.SanDiegoZoo.AnimalSnackMachine.SetFoodPricePerPound(1.20m);
+ this.SanDiegoZoo.AnimalSnackMachine.SetFoodStock(3.5);
+ this.SanDiegoZoo.AnimalSnackMachine.SetMoneyBalance(56.25m);
// Assign field values of the visitor.
this.SanDiegoZoo.Visitor.Age = 10;
this.SanDiegoZoo.Visitor.Name = "Dakota";
- this.SanDiegoZoo.Visitor.Wallet.MoneyBalance = 35.00m;
+ this.SanDiegoZoo.Visitor.Wallet.SetMoneyBalance(35.00m);
// Assign field values of the featured animal.
this.SanDiegoZoo.FeaturedAnimal.Age = 5;
Check Your Work
- Save all files.
- Build the solution.
- Resolve compiler errors before continuing.
- Run the application.
- Exercise the behavior associated with Zoo 2.5.
- Compare your filenames, namespaces, signatures, and key values with the code shown above.
Summary
You completed Zoo 2.5 by creating the required files, modifying only files with meaningful source changes, and verifying the application.