4.4B Zoo Assignment

4.4B Zoo Assignment
Implementing the dependency inversion principle
Acceptance Criteria
Acceptance criteria
  • A Chimpanzee implementing IMoneyCollector can act as the booth's money collector.
1.Implementing the dependency inversion principle
4.4.8 How to Implement the Dependency Inversion Principle
Problem
Solution
A Make the Chimpanzee class implement the IMoneyCollector interface.
Class Diagram: Chimpanzee. Reference figure for implementing the dependency inversion principle.
Class Diagram: Chimpanzee
1 The Chimp's MoneyBalance property should always return 0m.
2 In the AddMoney method, write a comment to buy some bananas. The method should do nothing else.
3 Have the RemoveMoney method return 0.
B Instantiate the money collecting booth's money box as a Chimpanzee in the constructor of the MoneyCollectingBooth.
Object Diagram: Chimpanzee. Reference figure for implementing the dependency inversion principle.
Object Diagram: Chimpanzee
C Check Your Work
1 Set breakpoints where appropriate to step through the code for the money collecting booth's money box.
2 Run the WPF application.
3 Ensure that the AddMoney and RemoveMoney methods step into the Chimpanzee class and do not have the expected functionality of an actual money collector.

The Chimpanzee should be more interested in bananas than collecting money.

D In the MoneyCollectingBooth's constructor add the moneyBox of type IMoneyCollector as a parameter.
Class Diagram: MoneyCollectingBooth. Reference figure for implementing the dependency inversion principle.
Class Diagram: MoneyCollectingBooth
1 Use the passed-in IMoneyCollector parameter within the MoneyCollectingBooth's constructor instead of instantiating a new Chimpanzee.
E In the zoo's constructor, instantiate the ticket booth's money box as a Chimpanzee.
1 Check Your Work
Subtasks
  • A. Run the WPF application. Ensure that the MoneyCollectingBooth's behavior remains the same. The Chimpanzee should still like bananas more than money.
F Remove the chimp from his money collecting duties
1 In the Zoo's constructor, change the ticketBooth's Chimpanzee to an actual MoneyBox.
2 Remove the implementation of the IMoneyCollector from the Chimpanzee class and any methods/properties relating to money stuffs.
G Check Your Work
1 Set a breakpoint on the call to AddMoney in the MoneyCollectingBooth class and run the WPF application.
2 Ensure that the AddMoney method steps into the MoneyCollector class and has the expected functionality of a money collector.
Implement the dependency inversion principle in the VendingMachine
Acceptance Criteria
Acceptance criteria
  • The zoo supplies the vending machine's money collector from outside the vending machine.
2.Implement the dependency inversion principle in the VendingMachine
Problem
Solution
A Modify the Vending Machine's constructor.
Class Diagram: Vending Machine Constructor. Reference figure for implement the dependency inversion principle in the vendingmachine.
Class Diagram: Vending Machine Constructor
1 Add a moneyBox parameter of type IMoneyCollector.
2 In the constructor, assign the moneyBox field to the passed-in moneyBox.
B In the zoo's constructor create a new moneyBox for the animalSnackMachine.
C Check Your Work
1 Set breakpoints where appropriate to step through the code for the vending machine's money box.
2 Run the WPF application.
3 Ensure behavior of the Vending Machine's money box stays the same.
Give the vending machines an account.
Acceptance Criteria
Acceptance criteria
  • The vending machine uses an account implementation for its money collector.
3.Give the vending machines an account.
Problem
Solution
A Create an Account class in a new project called Accounts as specified in the diagram below.
Class Diagram: Account.. Reference figure for give the vending machines an account.
Class Diagram: Account.
B In the account's RemoveMoney method, subtract the passed-in amount from the money balance and return the passed-in amount.

This allows the account balance to go negative.

C In the zoo's constructor, instead of instantiating a MoneyBox in the animalSnackMachine, create a new Account in its place.
Object Diagram: Account. Reference figure for give the vending machines an account.
Object Diagram: Account
D Make the MoneyCollector class abstract.
E Check Your Work
1 Set breakpoints where appropriate to step through the code for the vending machine's account.
2 Run the WPF application.
3 Ensure that when stepping into the VendingMachine's AddMoney method, it steps into the Account's AddMoney method.
Select a guest to feed an animal and an animal to be fed.
Acceptance Criteria
Acceptance criteria
  • The selected guest feeds the selected animal instead of relying on hard-coded selections.
4.Select a guest to feed an animal and an animal to be fed.
Problem
Solution
A In the click event of the feedAnimalButton, get the selected guest and animal.
1 Get the selected item in the guest list box and cast it as a guest.
Guest guest = this.guestListBox.SelectedItem as Guest;
2 Get the selected item in the animal list box and cast it as an animal.
B If both the guest and the animal are not null, have the guest feed the animal and call the PopulateAnimalListBox method and the PopulateGuestListBox method.

Otherwise, tell the user that they must choose both a guest and an animal to feed an animal.

C After the if-statement to check nullability, assign the SelectedItem of the guestListBox and the SelectedItem of animalListBox to the guest and animal variable respectively in order to keep both the guest and the animal selected after the button click.
D Check Your Work
1 Run the WPF application. Click the Feed animal button.
2 A message box should appear telling the user to choose a guest and an animal.
3 Select only an animal.
4 The same message box should appear.
5 Select an animal and a guest.
6 Click the feed animal button.
7 The selected guest should feed the selected animal.
Give guests a checking account
Acceptance Criteria
Acceptance criteria
  • Guests use checking accounts that support the required negative balances.
5.Give guests a checking account
Problem
Solution
A Test the application: Choose Greg from the guest list and have him feed a shark from the animal list until he is out of money.

Ensure the following:

  • Greg's money balance starts out at $150.35.
  • The zoo has a shark which weighs 390 lbs.
1 Greg will no longer be able to feed animals when the wallet money balance reaches zero.
B Add a field to the Guest class called checkingAccount of type IMoneyCollector.
Class Diagram: Checking Account. Reference figure for give guests a checking account.
Class Diagram: Checking Account
C In the guest's constructor, instantiate the checking account as an Account.
Object Diagram: Instantiate Account. Reference figure for give guests a checking account.
Object Diagram: Instantiate Account
D Create the WithdrawMoney method.
1 Define the WithdrawMoney method in the Guest class.
2 In the method, remove the passed-in amount from the guest's checking account.
3 Add the amount removed to the guest's wallet.
E Use the WithdrawMoney method.
1 In the guest's FeedAnimal method, after getting the food price, check if the wallet's money balance is less than the food price.

If so, withdraw 5 times the amount of the food price.

2 In the guest's VisitTicketBooth method, check that the wallet's money balance is less than the ticket price and the water bottle price.

If it is, withdraw twice the amount of the TicketPrice to cover the cost..

this.wallet.MoneyBalance < (ticketBooth.TicketPrice + ticketBooth.WaterBottlePrice)
F Add the checking account balance to the guest's ToString method. Make sure that it looks like the following
Name: Age [$MoneyBalance / $Account]
Greg: 44 [$150.35 / $50.00]
G Check Your Work
1 Change Greg's MoneyBalance to $15 and the Shark's Weight to 2000.
2 Set a breakpoint at the end of the VIsitTicketBooth method.
3 Run the WPF application.
4 When the Greg visits the ticket booth, ensure that money is withdrawn from his account to his wallet ($30) in order to pay for the ticket and water bottle ($45 total).

At the end of the VisitTicketBooth method he should have $27 and his CheckingAccount should have $20.

5 Press Continue. In the application interface, choose Greg from the guest list and the shark from the animals list.
6 Click the FeedAnimal button.
7 Ensure that Greg pulls money from his checking account to cover the cost.

It's $30 to feed a Shark so his wallet balance should be $147 (he pulled out $150, plus the $27 would be $177, minus the $30 to feed would be $147) and his account balance should be -$130.00 ($20 - $150).

Guest dependency inversion principle.
Acceptance Criteria
Acceptance criteria
  • Greg's wallet can serve as Darla's account through the shared money-collection contract.
6.Guest dependency inversion principle.
Problem
Solution
A Have the Wallet class implement IMoneyCollector.
Class Diagram: Wallet. Reference figure for guest dependency inversion principle.
Class Diagram: Wallet
B Modify the Guest constructor.
Class Diagram: Guest Constructor. Reference figure for guest dependency inversion principle.
Class Diagram: Guest Constructor
1 Create an account parameter of type IMoneyCollector. Assign the checking account using the passed-in account.
C Modify the NewZoo method.
Object Diagram: Zoo Guests. Reference figure for guest dependency inversion principle.
Object Diagram: Zoo Guests
1 Before instantiating Greg, create a new Account and store it in a variable.
2 Add $50 to Greg's checking account using the account's AddMoney method.
3 When instantiating Darla pass in Greg's Wallet (using the Wallet property) into her account parameter.
D Check Your Work
1 Make the Kangaroo weight 90lbs.
2 Run the WPF application.
3 Darla's wallet should have $7.25 after paying for a ticket and water bottle and her "account" should be $27, the same as Greg's wallet.
4 In the application interface, choose Darla from the guest list and the Kangaroo from the animals list.
5 Click the FeedAnimal button.
6 Darla should successfully feed the Kangaroo using the money in her wallet.
7 Click the FeedAnimal button 5 more times.
8 Ensure that Darla pulls money from Greg's wallet to cover the cost.

Her new wallet balance should be $5.94 and her account balance and Greg's wallet should be $20.15.

9 Continue clicking the FeedAnimal button.

Eventually Darla will use up all of her money both in her wallet and in Greg's wallet (since it's actually a Wallet and not really an Account) and she will no longer be able to feed any animals.

10 Click Greg and another animal and the Feed animal button.

Greg will should pull money from his account into his wallet and Darla's "account" should be replenished as well.

Prepare and submit your work
7.Submit a zipped Visual Studio solution after completing the following
A Ensure that the application has no compiler errors or warnings.
B Make the code StyleCop compliant.
C Remove all bin and obj folders from the entire solution.
Demonstrate your work
8.Record a video of your applications showing
A Your Account Project
1 StyleCop works
2 Implements the IMoneyCollector interface
B 4D - Check Your Work
C 6D - Check Your Work
Prepare and submit your work
9.Submission Steps
A Ensure that your application has no compiler errors or warnings
B Ensure that your code is StyleCop compliant
C Remove all bin and obj folders from your solution
D Zip your solution
E Submit your zipped solution to the Feedback System
F Submit your Feedback System results link to Canvas
G Submit your video recording to your f-channel
Source credit

© 2026 Northcentral Technical College