4.5B Zoo Assignment

4.5B Zoo Assignment
Add ability to remove animals and guests in WPF
Acceptance Criteria
Acceptance criteria
  • WPF users can remove animals and guests through the specified controls.
1.Add ability to remove animals and guests in WPF
Problem
Solution
A Add a button to the MainWindow underneath the animals list box. Name it removeAnimalButton and add a click event.
B In the click event, get the selected item from the animals list box and cast it as an animal, as shown in the code below
Animal animal = this.animalListBox.SelectedItem as Animal;
C If the animal exists, show a confirmation message box to the user asking if they really want to remove the selected animal, as shown in the code below.

If they do, remove the animal.

if (MessageBox.Show(string.Format("Are you sure you want to remove animal: {0}?", animal.Name), "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
D If the animal does not exist, show a message to the user telling them to select an animal to remove.
E Call the PopulateAnimalListBox method after removing the animal from the zoo.
F Follow the same process as in the previous step to add the ability to remove guests from the WPF application.
G Check Your Work
1 Run the WPF application.
2 Click the Remove animal button without selecting an animal.
3 A message box with a helpful message should appear.
4 Select an animal in the list box and click the Remove animal button.
5 The animal should be removed from both the zoo and the list box.
6 Select a guest in the list box and click the Remove guest button.
7 The guest should be removed from both the zoo and the list box.
Accept user input by adding events to window controls
Acceptance Criteria
Acceptance criteria
  • Animal-window control events transfer the user's input to the animal.
2.Accept user input by adding events to window controls
Problem
Solution
A Accept user input of the animal name.
1 In the animal window, add a lost focus event to the name text box.
2 In the event handler, set the animal's Name property to the Text property of the nameTextBox (your name for it may be different).
B Check Your Work
1 Set a breakpoint in the lost focus event of the name text box
2 Run the WPF application.
3 Click the Add animal button to launch the animal window.
4 Change the text in the name text box to Spot and tab out.
5 When the breakpoint is hit, ensure the animal's name is changed to Spot.
C Add lost focus events to the other text boxes and set the appropriate property on the animal inside the event handlers.
int.Parse(text) or double.Parse(text)double.Parse(text)
Parse
D Add a click event to the makePregnantButton.
E In the makePregnantButton event
1 Call the animal's MakePregnant method.
2 Disable the button using the IsEnabled property.
3 Set the pregnancyStatusLabel Content property to "Yes".
F Add a selection changed event to the gender combo box.

In the event handler, set the animal's Gender to the SelectedItem in the gender combo box.

G In the genderComboBox_SelectionChanged event handler, use a ternary operator to see if the animal's Gender is female and if it is, enable the Make Pregnant button, otherwise, disable it.
H Denote pregnant animals in the animalListBox by adding a P to the end of the Animal class's ToString override.

To do this, use a ternary operator to test the status if IsPregnant and if true, add " P" otherwise add string.Empty.

I Check Your Work
1 Run the WPF application and launch the animal window.
2 Change the values in the controls to following
  • Name: Spot
  • Gender: Male
  • Age: 5
  • Weight: 50
  • Is pregnant: No
3 Click OK or press the Enter key.
4 The animal, with the properties set to the entered values, should be added to the zoo and the list box.
5 Add an additional animal to the Zoo.
6 Change the Gender to Female and ensure the Make pregnant button is enabled.
7 Click the Make Pregnant button. Ensure the Is Pregnant label changes to Yes and that the button is disabled.
8 Click OK. Ensure the pregnant animal is added to the Zoo with the proper 'P' label added to it.
Add generic validation to the animal's properties
Acceptance Criteria
Acceptance criteria
  • Animal properties apply the required validation rules.
3.Add generic validation to the animal's properties
Problem
Solution
A Add validation to the animal's properties.
4.5.4 How to Use Regular Expressions
1 Age must be between 0 and 100, inclusive.

If it is not, throw an appropriate exception type (ArgumentOutOfRange) and include a user-friendly message.

2 Gender, because it is an enumeration, can only ever be set to one of the enumeration values.

Therefore, no validation is necessary.

3 Use the regular expression below to validate that Names include only letters and spaces.

If the inputted value does not match the regular expression, throw an appropriate exception type (Format) with a user-friendly message.

if (!Regex.IsMatch(value, @"^[a-zA-Z ]+$"))
4 Weight must be between 0 and 1000.

If it is not, throw an appropriate exception type and include a user-friendly message.

B Use try...catch in the event handlers in the animal window to catch any exceptions thrown by the animal when setting properties.

Show the exception's message to the user.

C Catch exceptions in the AddAnimal method in the ConsoleHelper.
1 In the AddAnimal method, define a local variable named success of type bool and assign it to false.
2 Surround the assigning of the animal's Age in a try/catch that catches an ArgumentOutOfRangeException and writes a message to the Console.
3 In the try statement, assign success to true after assigning the Age property.
4 Surround the try/catch in a while loop that only executes while success is false.
5 Repeat steps 2-4 for the Weight property.
6 Assign the success variable to false between the Age and Weight property while loops.
D Check Your Work
1 Run the WPF application and launch the animal window.
2 Test the following values in the text boxes. Ensure that a message box appears after tabbing out of each text box.
  • Name: Wa1ter Wh1te
  • Age: 200
  • Weight: 2000
3 Run the Console application and type 'add animal'.

Test the following values to make sure that validation works. Ensure the Console application continually asks for a valid value when bad values are given.

  • Name: Wa1ter Wh1te
  • Gender: M
  • Age: 1000
  • Weight: 2000
Add ability to add guests using a new window
Acceptance Criteria
Acceptance criteria
  • A separate guest window allows users to enter and add guests.
4.Add ability to add guests using a new window
Problem
Solution
A Create a new window called GuestWindow to the ZooScenario project, as specified in the diagram below.
Class Diagram: Guest Window. Reference figure for add ability to add guests using a new window.
Class Diagram: Guest Window
B Add a button called addGuestButton to the main window underneath the guest list box. Add a click event.
1 Model the addGuest's content after the addAnimalButton's click event.
C Use a grid layout to add labels and controls to the guest window as specified in the wireframe below.

Initialize the controls to the guest's values in the window_Loaded event.

Wireframe: Guest Window. Reference figure for add ability to add guests using a new window.
Wireframe: Guest Window
D Make the OK button the default button.

Make the Cancel button the cancel button. Add a click event to the OK button; in it, set the window's DialogResult to true.

E Add lost focus events to each of the text boxes and set the appropriate property on the guest to the Text of the text box.
F Add selection changed events to each of the combo boxes and set the appropriate property on the guest to the SelectedItem in the combo box.
G Add validation to the guest's properties.
1 Age must be between 0 and 120.

If it is not, throw an appropriate exception type and include a user-friendly message.

2 Name must only include letters and spaces.

If it is not, throw an appropriate exception type and include a user-friendly message.

H Catch thrown exceptions in the guest window. Show the exception's message in a message box.
I Check Your Work
1 Run the WPF application and launch the guest window.
2 Test that the validation works by attempting to enter invalid information.
J In the AddGuest method in the ConsoleHelper, add a while loop to the Age property like you did in the AddAnimal method.
K Add controls to set the guest's money balance.
1 Add buttons, labels and a combo box to the window as shown in the wireframe below.
Wireframe: MoneyBalance Controls. Reference figure for add ability to add guests using a new window.
Wireframe: MoneyBalance Controls
2 In the window_Loaded event
Subtasks
  • A. Set the moneyBalanceLabel's Content to the guest's wallet money balance.
Subtasks
  • B. Add the following values to the combo box's Items list:
Example: moneyAmountComboBox.Items.Add(1);
  • 1
  • 5
  • 10
  • 20
Subtasks
  • C. Assign the moneyAmountComboBox's SelectItem property to the moneyAmountComboBox's Items property's 1st item in the array [0].
3 Add a click event to the addMoneyButton.
Subtasks
  • A. In it, add money to the guest's wallet money balance by getting the Text of the combo box and passing it into the wallet's AddMoney method.
  • B. Reset the moneyBalanceLabel's Content to the current money balance.
4 Add a click event to the subtractMoneyButton.
Subtasks
  • A. In it, remove money from the guest's wallet money balance by getting the Text of the combo box and passing it into the wallet's RemoveMoney method.
  • B. Reset the moneyBalanceLabel's content to the current money balance.
5 Add buttons, labels and a combo box to the window as shown in the wireframe below to set the account's money balance.

Model the account after the previous steps that set the wallet's money balance.

Wireframe: Account. Reference figure for add ability to add guests using a new window.
Wireframe: Account
Subtasks
  • A. In the window_Loaded event handler, assign the combo box's SelectedItem property to the first position in Items and add the following values to the combo box:
  • 1
  • 5
  • 10
  • 20
  • 50
  • 100
L Check Your Work
1 Run the WPF application and launch the guest window.
2 Ensure the Wallet balance and Account balance both show 1 in their combo boxes.
3 Choose an amount from the combo box and click the add money button.
4 The money balance should go up by that amount.
5 Do the same with the remove money button. The money balance should go down by the chosen amount.
6 Repeat steps 2-4 with the account. Make sure the balance reflects the amount you are adding and subtracting.
7 Change the values in the text boxes.
8 Click OK or press the Enter key.
9 The guest, with the properties set to the entered values, should be added to the zoo and the list box.
Remove creation of animals and guests from NewZoo method
Acceptance Criteria
Acceptance criteria
  • NewZoo no longer creates the animals and guests that users now supply themselves.
5.Remove creation of animals and guests from NewZoo method
Problem
Solution
A In the zoo's NewZoo method, remove all of the code that creates and adds animals and guests.

Only the zoo and the employees should be created.

B Check Your Work
1 Run the WPF application.
2 Ensure that no guests or animals appear in the list boxes.
3 Creating and adding guests and animals should still work as before.
Demonstrate your work
6.Record a video of your applications showing
A The Console Application
1 Type 'add animal'
2 Make sure an error messages is written to the console after entering the following values
Subtasks
  • A. Name: 5+r33+
  • B. Gender: M
  • C. Age: 1000
  • D. Weight: 2000
B The WPF Application
1 Starts with no Animals or Guests
2 Launch the AnimalWindow
Subtasks
  • A. Make sure a message box with an error message appears after entering the following values and tabbing out:
  • A.1. Name: L!n+
  • A.2. Age: 259
  • A.3. Weight: 2023
  • B. Make sure the Make Pregnant button is:
  • B.1. Disabled if the Gender is Male
  • B.2. Enabled if the Gender is Female
  • C. Enter valid values for all fields and select Female for the Gender, then click the Make Pregnant button.
  • D. Make sure the IsPregnant label changed to Yes and that the button is disabled.
  • E. Add the Animal to the Zoo and ensure the list box reflects the values you've entered
3 Launch the GuestWindow
Subtasks
  • A. Make sure a message box with an error message appears after entering the following values and tabbing out:
  • A.1. Name: D-FL8
  • A.2. Age: 247
  • B. Enter valid values for the Name and Age fields
  • C. For both the Wallet Balance and Account Balance fields:
  • C.1. Ensure both show 1 in their combo boxes
  • C.2. Click the add money button. The money balance should go up by that amount
  • C.3. Do the same with the remove money button. The money balance should go down by the chosen amount
  • C.4. Repeat Steps 2-3 for different combo box values.
  • D. Give the Guest some value for their Wallet and Account Money Balance
  • E. Add the Guest to the Zoo and ensure the list box reflects the values you've entered
C 1G - Check Your Work
Prepare and submit your work
7.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