1.5 Zoo Learning Activity

Overview
1.Prepare for Zoo 1.5
A Returning Values from Methods

In Zoo 1.5, move the portion and price calculations into the objects that own their data. Return the amount actually removed from a wallet, pass that payment to the vending machine, and use Boolean and string return values to answer questions about the zoo.

B Getting Started
1 Copy your completed Zoo 1.4 solution, then rename the copied folder and solution file for Zoo 1.5. Open the copied solution in Visual Studio.
2 Build your starting solution before making the following changes.
C Starting Point — End of Zoo 1.4

The feeding handlers already calculate a portion from Weight and PortionSizePercentage, calculate its price, and call RemoveMoney, AddMoney, RemoveFood, and Eat. Pregnancy is already represented by the Baby reference. The starting-state reference below uses the members present in the supplied Zoo 1.4 end project.

Starting Zoo 1.5 class diagram carried forward from Zoo 1.4. Animal has Baby and percentage fields; feeding and money methods accept parameters.
Starting structure — supplied Zoo 1.4 End
  • New
  • Changed
  • Removed
1 In MainWindow.xaml.cs, confirm these calls already appear in the matching creation methods after the FeaturedAnimal fields, including Weight and Type, have been assigned. Add a missing call once so the starting state matches the completed Zoo 1.4 project.

In CreateComoZoo():

this.ComoZoo.FeaturedAnimal.MakePregnant();

In CreateSanDiegoZoo():

this.SanDiegoZoo.FeaturedAnimal.MakePregnant();
2 Create each zoo and inspect its FeaturedAnimal. Dolly and Patti are both Female. Confirm their Baby references are non-null, with starting Baby weights of approximately 3.53 and 0.327 respectively.
Use Returned Values Throughout the Feeding Transaction
Acceptance Criteria
  • The animal returns its portion size using its existing percentage field.
  • The vending machine returns a price rounded to cents.
  • The wallet returns zero, the requested amount, or its remaining balance as appropriate.
  • Guest returns the wallet’s result to MainWindow.
  • Both feeding handlers deposit the returned payment and use the calculated portion for food removal and eating.
2.Return the Food Portion Size
Animal class with a new public GetPortionSize method that returns double.
Animal returns its portion size
  • New
  • Changed
  • Removed
MainWindow calls GetPortionSize on FeaturedAnimal and receives a double.
GetPortionSize call and return
  • New
  • Changed
  • Removed

Class change: add public double GetPortionSize() to Animal. Keep Weight and PortionSizePercentage. Call flow: MainWindow calls FeaturedAnimal.GetPortionSize(); Animal returns its calculated portion; MainWindow stores the result in foodWeight.

Problem

Both feeding handlers reach into the animal to calculate its portion. The animal can provide that answer itself.

Solution

Add Animal.GetPortionSize() and use its returned value in both feeding handlers.

A Make the Changes
1 In Animal.cs, add GetPortionSize() inside the Animal class, after the fields and before Eat. Use the existing PortionSizePercentage field so the calculation stays with the animal.
public double GetPortionSize()
{
    return this.Weight * this.PortionSizePercentage;
}
2 In MainWindow.xaml.cs, replace the first statement in darlaFeedDingoButton_Click with this call. Keep the local variable named foodWeight.
double foodWeight = this.ComoZoo.FeaturedAnimal.GetPortionSize();
3 Replace the first statement in daveFeedPlatypusButton_Click with the corresponding San Diego call.
double foodWeight = this.SanDiegoZoo.FeaturedAnimal.GetPortionSize();
B Check Your Work
1 Set a breakpoint on the first statement of darlaFeedDingoButton_Click.
2 Click New Como Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
3 Click Darla, feed dingo. Press F11 on the GetPortionSize() call.
4 Inside Animal.GetPortionSize(), confirm this.Weight is approximately 35.3 and this.PortionSizePercentage is approximately 0.02.
5 Press Shift+F11 to return to the handler. Complete the assignment with F10 if needed; confirm foodWeight is approximately 0.706.
6 Repeat at the first statement of daveFeedPlatypusButton_Click after creating San Diego Zoo. Confirm Patti’s this.Weight is approximately 3.27 and the returned foodWeight is approximately 0.0654.
7 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
3.Return the Food Price
VendingMachine class with a new DetermineFoodCost method receiving maxFoodWeight as double and returning decimal.
VendingMachine returns a rounded price
  • New
  • Changed
  • Removed
MainWindow gets the portion size from FeaturedAnimal, then passes it to DetermineFoodCost on AnimalSnackMachine and receives a decimal.
DetermineFoodCost call and return
  • New
  • Changed
  • Removed

Class change: add public decimal DetermineFoodCost(double maxFoodWeight) to VendingMachine. Call flow: MainWindow passes foodWeight to AnimalSnackMachine.DetermineFoodCost; the vending machine calculates and rounds the price; MainWindow stores the returned value in foodCost.

Problem

The handler also reaches into the vending machine to calculate food cost. Move that calculation into the vending machine and round the returned price once.

Solution

Add VendingMachine.DetermineFoodCost(double maxFoodWeight) and use its rounded return value in both feeding handlers.

A Make the Changes
1 In VendingMachine.cs, add DetermineFoodCost(double maxFoodWeight) after AddFoodBag. The existing using System; makes Math.Round available.
public decimal DetermineFoodCost(double maxFoodWeight)
{
    decimal maxFoodCost = (decimal)maxFoodWeight * this.FoodPricePerPound;

    decimal foodCost = Math.Round(maxFoodCost, 2);

    return foodCost;
}
2 In darlaFeedDingoButton_Click, replace the foodCost calculation with the following statement immediately after the foodWeight assignment.
decimal foodCost = this.ComoZoo.AnimalSnackMachine.DetermineFoodCost(foodWeight);
3 In daveFeedPlatypusButton_Click, make the same replacement using SanDiegoZoo.
decimal foodCost = this.SanDiegoZoo.AnimalSnackMachine.DetermineFoodCost(foodWeight);
B Check Your Work
1 Set a breakpoint inside VendingMachine.DetermineFoodCost before its calculation.
2 Click New Como Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
3 Click Darla, feed dingo. Step through the multiplication with F10. Confirm the unrounded cost is 0.5295m.
4 Step through rounding and the return. Confirm the handler’s foodCost becomes 0.53m.
5 Click New San Diego Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
6 Click Dave, feed platypus. Repeat the trace: the unrounded cost is 0.07848m and returned foodCost is 0.08m.
7 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
4.Return the Amount Actually Removed
Wallet RemoveMoney changes from returning void to returning decimal.
Guest and Wallet return the actual payment
  • New
  • Changed
  • Removed
MainWindow calls RemoveMoney on Visitor. Visitor calls RemoveMoney on Wallet, receives a decimal, and returns that decimal to MainWindow.
Payment returned through Guest
  • New
  • Changed
  • Removed

Class changes: change RemoveMoney(decimal amount) from void to decimal in both Wallet and Guest. Call flow: MainWindow calls Visitor.RemoveMoney(foodCost); Guest calls Wallet.RemoveMoney(amount); Wallet returns the amount removed; Guest returns that same value to MainWindow.

Problem

A requested payment may exceed the wallet’s balance. The caller needs to know how much money was actually removed.

Solution

Change Wallet.RemoveMoney and Guest.RemoveMoney to return decimal, passing the actual payment back to the feeding handler.

A Make the Changes
1 In Wallet.cs, replace RemoveMoney(decimal amount) with the following version. Change the return type from void to decimal. Each branch assigns amountRemoved; the method then subtracts and returns that amount.
public decimal RemoveMoney(decimal amount)
{
    decimal amountRemoved;

    if (amount <= 0.00m)
    {
        amountRemoved = 0.00m;
    }
    else if (this.MoneyBalance >= amount)
    {
        amountRemoved = amount;
    }
    else
    {
        amountRemoved = this.MoneyBalance;
    }

    this.MoneyBalance -= amountRemoved;

    return amountRemoved;
}
2 In Guest.cs, replace RemoveMoney(decimal amount) with this version. Store the wallet’s returned value, then return it to Guest’s caller.
public decimal RemoveMoney(decimal amount)
{
    decimal amountRemoved = this.Wallet.RemoveMoney(amount);

    return amountRemoved;
}

The first branch rejects zero or negative requests. The second branch pays the full request when funds are sufficient. The final branch pays the remaining balance when funds are insufficient.

B Check Your Work
1 Set a breakpoint at the first statement of Wallet.RemoveMoney. Create Como Zoo and click Darla, feed dingo.
2 At the breakpoint, open Watch. Set this.MoneyBalance to 5.25m and amount to 0.53m.
3 Use F10 to reach the return statement. Confirm amountRemoved is 0.53m and this.MoneyBalance is 4.72m.
4 Continue, recreate Como Zoo and trigger the breakpoint again. Set this.MoneyBalance to 0.20m and amount to 0.53m. Confirm the returned amountRemoved is 0.20m and the balance is 0.00m.
5 Repeat independently with balance 5.25m and requested amount of 0.00m. Confirm amountRemoved is 0.00m and the balance remains 5.25m.
6 Repeat with balance 5.25m and amount of -1.00m. Confirm amountRemoved is 0.00m and the balance remains 5.25m.
7 Read Guest.RemoveMoney and confirm it returns the amount returned by Wallet.RemoveMoney.
8 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
5.Use the Returned Payment in Both Feeding Handlers
A Make the Changes
1 In each feeding handler in MainWindow.xaml.cs, replace the standalone Visitor.RemoveMoney(foodCost) call with a decimal payment assignment. Pass payment to AddMoney so the machine receives only the money the wallet removed.
2 Complete darlaFeedDingoButton_Click as shown. The last two calls continue using foodWeight.
public void darlaFeedDingoButton_Click(object sender, RoutedEventArgs e)
{
    double foodWeight = this.ComoZoo.FeaturedAnimal.GetPortionSize();
    decimal foodCost = this.ComoZoo.AnimalSnackMachine.DetermineFoodCost(foodWeight);
    decimal payment = this.ComoZoo.Visitor.RemoveMoney(foodCost);

    this.ComoZoo.AnimalSnackMachine.AddMoney(payment);
    this.ComoZoo.AnimalSnackMachine.RemoveFood(foodWeight);
    this.ComoZoo.FeaturedAnimal.Eat(foodWeight);
}
3 Complete daveFeedPlatypusButton_Click as shown, using SanDiegoZoo throughout.
public void daveFeedPlatypusButton_Click(object sender, RoutedEventArgs e)
{
    double foodWeight = this.SanDiegoZoo.FeaturedAnimal.GetPortionSize();
    decimal foodCost = this.SanDiegoZoo.AnimalSnackMachine.DetermineFoodCost(foodWeight);
    decimal payment = this.SanDiegoZoo.Visitor.RemoveMoney(foodCost);

    this.SanDiegoZoo.AnimalSnackMachine.AddMoney(payment);
    this.SanDiegoZoo.AnimalSnackMachine.RemoveFood(foodWeight);
    this.SanDiegoZoo.FeaturedAnimal.Eat(foodWeight);
}
For either zoo, MainWindow obtains the portion and rounded cost, requests money through Guest and Wallet, deposits the actual payment, removes food, and feeds the animal. Partial payment does not reduce the portion.
Complete feeding transaction for either zoo
  • New
  • Changed
  • Removed

Follow the complete feeding call flow for either zoo:

  • MainWindow asks the featured Animal for foodWeight.
  • MainWindow asks the VendingMachine for foodCost using that weight.
  • MainWindow asks Guest to remove the cost; Guest asks Wallet and returns the actual payment.
  • MainWindow passes payment to the VendingMachine’s AddMoney.
  • MainWindow passes foodWeight to the VendingMachine’s RemoveFood.
  • MainWindow passes that same foodWeight to the Animal’s Eat.
B Check Your Work
1 Stop and restart the application. Create both zoos before clicking a feeding button.
2 Set breakpoints in both feeding handlers so you can inspect local variables after each assignment and fields after each call.
3 Use a newly created zoo for each of the next two checks; do not give birth before feeding.
C Check Your Work — Como Zoo — Dolly
1 Click New Como Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
2 Click Darla, feed dingo. Step through this handler’s assignments and calls with F10.
3 After the portion assignment, confirm foodWeight is approximately 0.706.
4 After the cost and payment assignments, confirm foodCost and payment are both 0.53m.
5 Confirm this.ComoZoo.Visitor.Wallet.MoneyBalance is 4.72m.
6 Confirm this.ComoZoo.AnimalSnackMachine.MoneyBalance is 0.53m.
7 Confirm this.ComoZoo.AnimalSnackMachine.FoodStock is approximately 249.294.
8 Step into Animal.Eat with F11. After the calculations, confirm weightGain is approximately 0.5648 and babyWeightGain is approximately 0.0706.
9 After the updates, confirm this.Weight is approximately 35.8648 and this.Baby.Weight is approximately 3.6006.
10 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
D Check Your Work — San Diego Zoo — Patti
1 Click New San Diego Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
2 Click Dave, feed platypus. Step through this handler’s assignments and calls with F10.
3 After the portion assignment, confirm foodWeight is approximately 0.0654.
4 After the cost and payment assignments, confirm foodCost and payment are both 0.08m.
5 Confirm this.SanDiegoZoo.Visitor.Wallet.MoneyBalance is 3.67m.
6 Confirm this.SanDiegoZoo.AnimalSnackMachine.MoneyBalance is 0.08m.
7 Confirm this.SanDiegoZoo.AnimalSnackMachine.FoodStock is approximately 249.9346.
8 Step into Animal.Eat with F11. After the calculations, confirm weightGain is approximately 0.05232 and babyWeightGain is approximately 0.00654.
9 After the updates, confirm this.Weight is approximately 3.32232 and this.Baby.Weight is approximately 0.33354.
10 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
6.Return Pregnancy Status and Exit Early
Animal removes the IsPregnant field and adds GetIsPregnant returning bool.
Pregnancy status and the early return
  • New
  • Changed
  • Removed
MainWindow calls MakePregnant on FeaturedAnimal, and the Animal calls GetIsPregnant on itself and receives a bool.
MakePregnant checks the existing Baby
  • New
  • Changed
  • Removed

Class change: add public bool GetIsPregnant() to Animal and use it in the existing void MakePregnant() method.

Zoo 1.4 already uses Baby == null in MakePregnant. Give that test a named method, then return early when pregnancy already exists.

A Make the Changes
1 In Animal.cs, add GetIsPregnant() after the fields and before GetPortionSize(). It returns true when Baby references an Animal.
public bool GetIsPregnant()
{
    return this.Baby != null;
}
2 Replace MakePregnant() with the following version. Put the early-return condition at the beginning, then move the existing Baby creation statements outside the old if block.
public void MakePregnant()
{
    if (this.GetIsPregnant())
    {
        return;
    }

    this.Baby = new Animal();
    this.Baby.Sex = "Male";
    this.Baby.Name = string.Empty;
    this.Baby.Age = 0;
    this.Baby.Weight = this.Weight * this.BabyWeightPercentage;
    this.Baby.Type = this.Type;
}

Call flow from MainWindow:

  • CreateComoZoo() or CreateSanDiegoZoo() calls the featured animal’s MakePregnant().
  • MakePregnant() calls GetIsPregnant() on that same Animal.
  • If Baby != null, the returned value is true and MakePregnant() ends immediately.
  • If Baby == null, the returned value is false and the existing Baby creation and field assignments run.
B Check Your Work
1 Set a breakpoint at the start of Animal.MakePregnant(). Create Como Zoo.
2 At the first call, inspect this.Name as Dolly and this.Baby as null. Step into GetIsPregnant() and confirm it returns false.
3 Step through baby creation. Confirm this.Baby.Weight is approximately 3.53.
4 While paused, use the Immediate window to call this.MakePregnant() again on Dolly. Step through the guard and confirm GetIsPregnant() now returns true and the same baby is retained.
5 Repeat for a newly created San Diego Zoo. Confirm Patti’s initial baby weight is approximately 0.327 and a repeated call keeps that baby.
6 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
7.Return Machine Status and Formatted Temperature
VendingMachine.IsFull returns a bool. Zoo.GetTemperatureDisplay returns its birthing-room temperature formatted to one decimal place. These methods are not called by MainWindow in the supplied end code.
Boolean and string return values
  • New
  • Changed
  • Removed

Class changes: VendingMachine.IsFull returns bool and Zoo.GetTemperatureDisplay returns string.

Add two more methods from the completed Zoo 1.5 design. One answers a Boolean question; the other returns text formatted for display.

A Make the Changes
1 In VendingMachine.cs, add IsFull() after DetermineFoodCost and before RemoveFood. The comparison reports whether the stock has reached or exceeded capacity.
public bool IsFull()
{
    return this.FoodStock >= this.FoodCapacity;
}
2 In Zoo.cs, add GetTemperatureDisplay() after the Visitor field and before the class’s closing brace. It returns the birthing-room temperature with one decimal place.
public string GetTemperatureDisplay()
{
    return this.BirthArea.Temperature.ToString("0.0");
}
B Check Your Work
1 In MainWindow.xaml.cs, set a breakpoint on the closing brace of newComoZooButton_Click by clicking its left margin.
2 Press F5 to start or continue the application. Click New Como Zoo.
3 When Visual Studio pauses, open Debug > Windows > Watch > Watch 1. Enter each full expression below in a separate Watch row.
4 Evaluate this.ComoZoo.AnimalSnackMachine.IsFull() in Watch. Confirm true.
5 Continue and feed Dolly once. Pause after the handler’s feeding calls and evaluate the same expression: confirm false.
6 Continue and refill Como’s machine. Pause after the refill call and confirm the same expression returns true.
7 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
8 Click New San Diego Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.
9 Confirm the San Diego temperature display is 77.0. Increase once and confirm 77.5.
10 Read Zoo.GetTemperatureDisplay() and confirm it formats the room temperature to one decimal place.
8.Review the Completed Design
Ending Zoo 1.5 class diagram. Animal adds GetPortionSize and GetIsPregnant and removes IsPregnant. Guest adds GetMoneyBalance and RemoveMoney returns decimal. Wallet RemoveMoney returns decimal. VendingMachine adds DetermineFoodCost and SellFood returns double.
Ending structure — supplied Zoo 1.5 End
  • New
  • Changed
  • Removed

The completed Zoo 1.5 structure retains the starting fields and relationships. Confirm these additions and changes:

  • Animal adds double GetPortionSize() and bool GetIsPregnant().
  • Animal.MakePregnant() remains void and now returns early when GetIsPregnant() is true.
  • Wallet.RemoveMoney(decimal amount) returns decimal and handles nonpositive requests, full payment, and partial payment.
  • Guest.RemoveMoney(decimal amount) returns the wallet’s decimal result.
  • VendingMachine adds decimal DetermineFoodCost(double maxFoodWeight) and bool IsFull().
  • Zoo adds string GetTemperatureDisplay().
  • Both feeding handlers use GetPortionSize(), DetermineFoodCost(foodWeight), and the returned payment.
  • Animal.Eat, VendingMachine.AddMoney, VendingMachine.RemoveFood, and VendingMachine.AddFoodBag retain their Zoo 1.4 implementations.
  • BirthingRoom.Animal retains its existing name. XAML controls and their Click handlers retain their existing names.
  • IsFull() and GetTemperatureDisplay() are available but are not called by the supplied MainWindow code.
A Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Inspect each new or changed value-returning method. Confirm its XML documentation includes a description of its return value.
3 From new zoos, run the two feeding checks above and compare their exact rounded payments and approximate weights.
4 Recreate San Diego Zoo. Increase the temperature once and then decrease once: confirm 77.5 and 77.0.
5 Compare the source names with GetPortionSize, DetermineFoodCost, RemoveMoney, GetIsPregnant, IsFull, GetTemperatureDisplay, Sex, PortionSizePercentage. Confirm spelling and capitalization match the callers.
6 Save all files and remove test breakpoints.
9.Submit Zoo 1.5
A Confirm all modified C# files pass the project’s StyleCop checks with no warnings.
B Close Visual Studio and create a ZIP file containing the complete Zoo 1.5 solution folder.
C Submit the ZIP file through the Feedback System.
D Submit the Feedback System URL in Canvas.