15 KiB
Challenge project
Develop branching and looping structures in C#
Demonstrate your ability to develop a console app that implements selection and iteration statements to achieve app specifications.
Learning objectives
In this module, you'll demonstrate your ability to:
- Develop a C# console application that uses a combination of selection and iteration statements to implement logical workflows in accordance with supplied application data and user interactions.
- Evaluate the underlying conditions and make an informed decision when choosing between if-elseif-else and switch statements, and between foreach, for, while, and do iteration statements.
- Scope variables at an appropriate level within an application.
Introduction
Applications often use a combination of selection and iteration statements to establish code execution paths. In addition, user input and calculations influence the flow through an application. Creating a user interface that implements a design specification can be challenging.
Suppose you're a developer working on the Contoso Pets application, an application that's used to find homes for stray or abandoned pets. Some of the development work has already been completed. For example, the application's main menu and the code used to store new pet information have been developed. However, certain information isn't always available when a pet is entered in your system. You need to develop the features that ensure a complete dataset exists for each animal in your care.
In this module, you'll develop the following features of the Contoso Pets application:
- A feature that ensures animal ages and physical descriptions are complete.
- A feature that ensures animal nickname and personality descriptions are complete.
By the end of this module, your Contoso Pets application will ensure that every element in the ourAnimals array is complete.
Note
This is a challenge project module where you’ll complete an end-to-end project from a specification. This module is intended to be a test of your skills; there’s little guidance and no step-by-step instructions.
Learning Objectives
In this module, you'll demonstrate your ability to:
- Develop a C# console application that uses a combination of selection and iteration statements to implement logical workflows.
- Evaluate the underlying conditions in your application and make an informed decision between selection statement options.
- Scope variables at an appropriate level within an application.
Prepare
In this challenge project, you'll develop portions of a C# console application. You'll use boolean expressions, selection statements, and iteration statements to implement the features of a design specification. As you develop the application, you'll need to scope variables at the appropriate level.
Project specification
The Starter code project for this module includes a Program.cs file with the following code features:
-
The code declares variables used to collect and process pet data and menu item selections
-
The code declares the ourAnimals array that includes the following information for each animal in the array:
- Pet ID #.
- Pet species (cat or dog).
- Pet age (years).
- A description of the pet's physical appearance.
- A description of the pet's personality.
- The pet's nickname.
-
The code uses a for loop around a
switch-case
construct to populate elements of theourAnimals
array. -
The code includes a loop around a main menu that terminates when the user enters "exit". The main menu includes:
1. List all of our current pet information.
2. Assign values to the ourAnimals array fields.
3. Ensure animal ages and physical descriptions are complete.
4. Ensure animal nicknames and personality descriptions are complete.
5. Edit an animal’s age.
6. Edit an animal’s personality description.
7. Display all cats with a specified characteristic.
8. Display all dogs with a specified characteristic.
Enter menu item selection or type "Exit" to exit the program
-
The code reads the user's menu item selection and uses a switch statement to branch the code for each menu item number.
-
The code includes implementation for menu options 1 and 2.
-
The code displays an "under construction" message for menu options 3-8.
Your goal in this challenge is to create the app features aligned with menu options 3 and 4.
Note
New animals must be added to the ourAnimals array when they arrive. However, an animal's age and some physical characteristics for a pet may be unknown until after a veterinarian's examination. In addition, an animal's nickname and personality may be unknown when a pet first arrives. The new features that you're developing will ensure that a complete dataset exists for each animal in the ourAnimals array.
To ensure that animal ages and physical descriptions are complete, your code must:
-
Assign a valid numeric value to petAge for any animal that has been assigned data in the ourAnimals array but has not been assigned an age.
-
Assign a valid string to petPhysicalDescription for any animal that has been assigned data in the ourAnimals array but has not been assigned a physical description.
-
Verify that physical descriptions have an assigned value. Assigned values cannot have zero characters. Any further requirement is up to you.
-
To ensure that animal nicknames and personality descriptions are complete, your code must:
-
Assign a valid string to petNickname for any animal that has been assigned data in the ourAnimals array but has not been assigned a nickname.
-
Assign a valid string to petPersonalityDescription for any animal that has been assigned data in the ourAnimals array but has not been assigned a personality description.
-
Verify that nicknames and personality descriptions have an assigned value. Assigned values cannot have zero characters. Any further requirement is up to you.
Setup
Use the following steps to prepare for the Challenge project exercises:
To download a zip file containing the Starter project code, select the following link: Lab Files.
Unzip the files in your development environment. Consider using your PC as your development environment so that you have access to your code after completing this module. If you aren't using your PC as your development environment, you can unzip the files in a sandbox or hosted environment.
You're now ready to begin the Challenge project exercises. Good luck!
Exercise
Ensure that petAge and petPhysicalDescription contain valid information
The Contoso Pets application is used to help find new homes for abandoned pets. Your goal in this challenge is to develop the application features used to ensure that you have a completed dataset for each animal in the ourAnimals array.
Specification
In this challenge exercise, you need to develop a feature that ensures animal ages and physical descriptions are complete.
This feature must:
-
Be enabled inside the appropriate application branch (must not overwrite the code in the code branch for menu option 2).
-
Skip over any animal in the ourAnimals array when the value of pet ID is set to the default value.
-
Display the pet ID value and prompt the user for an updated data value if ourAnimals array data is missing or incomplete.
-
Ensure that a valid numeric value is assigned to animalAge for all animals in the ourAnimals array that have assigned data.
-
Ensure that a valid string is assigned to animalPhysicalDescription for all animals in the ourAnimals array that have assigned data.
-
Enforce the following validation rules for animalAge.
- It must be possible to convert the value entered to numeric data type.
-
Enforce the following validation rules for animalPhysicalDescription:
- Values cannot be null.
- Values cannot have zero characters.
- Any further restriction is up to the developer.
-
Inform the application user when all data requirements are met, pausing the application to ensure the message can be seen and responded to.
Check your work
To validate that your code satisfies the specified requirements
Note
You can exit the verification test before completing all of the verification steps if see a result that does not satisfy the specification requirements. To force an exit from the running program, in the Terminal panel, press Ctrl-C. After exiting the running app, complete the edits that you believe will address the issue you are working on, save your updates to the Program.cs file, and then re-build and run your code.
At the Terminal command prompt, enter 3
Verify that the Terminal panel updates with a message similar to the following:
Enter an age for ID #: c4
At the Terminal command prompt, enter one
Verify that your code repeats the prompt requesting a value for the age of the pet.
The Terminal panel should update to show the repeated prompt. The display should be similar to the following:
Enter an age for ID #: c4
one
Enter an age for ID #: c4
At the Terminal command prompt, enter 1
Verify that your code accepts 1 as a valid numeric entry and that the Terminal panel displays a message similar to the following:
Enter a physical description for ID #: c4 (size, color, breed, gender, weight, housebroken)
At the Terminal command prompt, press the Enter key (without typing any characters).
Verify that your code repeats the prompt requesting a value for the physical description of the pet.
The Terminal panel should update to show the repeated prompt. The display should be similar to the following:
Enter a physical description for ID #: c4 (size, color, gender, weight, housebroken)
Enter a physical description for ID #: c4 (size, color, gender, weight, housebroken)
At the Terminal command prompt, enter small white Siamese cat weighing about 8 pounds. litter box trained.
Verify that your code accepts small white Siamese cat weighing about 8 pounds. litter box trained. as a valid entry and that the Terminal panel displays a message similar to the following:
Age and physical description fields are complete for all of our friends.
Press the Enter key to continue
If you specified further restrictions for valid entries, run the appropriate test cases to verify your work.
Note
If your code meets the requirements you should be able to complete each step in order and see the expected results in a single test pass. If you added additional restrictions, you may need to exit the application and then run a separate test pass to complete your verification.
Once you've validated the results for this exercise, proceed to the next exercise in this challenge.
Exercise
Ensure that pet nicknames and personality descriptions are complete
The Contoso Pets app is used to help find new homes for abandoned pets. Your goal in this challenge is to develop the app features used to ensure that we have a completed dataset for each animal in the ourAnimals array.
Specification
You need to develop a feature that ensures animal nicknames and personality descriptions are complete.
This feature must:
-
Be enabled inside the appropriate application branch (must not overwrite the code in the code branch for menu option 2).
-
Skip over any animal in the ourAnimals array when the value of pet ID is set to the value default value.
-
Display the pet ID value and prompt the user for an updated data value if ourAnimals array data is missing or incomplete.
-
Ensure that a valid string is assigned to animalNickname for all animals in the ourAnimals array that have assigned data.
-
Ensure that a valid string is assigned to animalPersonalityDescription for all animals in the ourAnimals array that have assigned data.
-
Enforce the following validation rules for petNickname and petPersonalityDescription:
- Values cannot be null.
- Values cannot have zero characters.
- Any further restriction is up to the developer.
Inform the application user when all data requirements are met, pausing the application to ensure the message can be seen and responded to.
Check your work
To validate that your code satisfies the specified requirements, complete the following steps:
Build and run your app.
At the Terminal command prompt, enter 4
Verify that the Terminal panel updates with a message similar to the following:
Enter a nickname for ID #: c4
At the Terminal command prompt, press the Enter key (without typing any characters).
Verify that your code repeats the prompt requesting a value for the nickname of the pet.
The Terminal panel should update to display something similar to the following:
Enter a nickname for ID #: c4
Enter a nickname for ID #: c4
At the Terminal command prompt, enter snowflake
Verify that your code accepts snowflake as a valid entry and that the Terminal panel displays a message similar to the following:
Enter a personality description for ID #: c4 (likes or dislikes, tricks, energy level)
At the Terminal command prompt, press the Enter key (without typing any characters).
Verify that your code repeats the prompt requesting a value for the personality description of the pet.
The Terminal panel should update to display something similar to the following:
Enter a personality description for ID #: c4 (likes or dislikes, tricks, energy level)
Enter a personality description for ID #: c4 (likes or dislikes, tricks, energy level)
At the Terminal command prompt, enter loves to curl up in a warm spot
Verify that your code accepts loves to curl up in a warm spot as a valid entry and that the Terminal panel displays a message similar to the following:
Nickname and personality description fields are complete for all of our friends.
Press the Enter key to continue
If you specified further restrictions for valid entries, run the appropriate test cases to verify your work.
Congratulations if you succeeded in this challenge!
Summary
Your goal was to demonstrate the ability to develop features of an application that achieve a design specification. You needed to choose and implement the appropriate iteration and selection statement types to meet the stated requirements.
By creating nested combinations of iteration and selection statements, you built a user interface that enables the application user to enter valid pet data that filled gaps in the ourAnimals array.
Your ability to implement features of the Contoso Pets application based on a design specification demonstrates your understanding of the iteration and selection statements.
Project Files