avnc 018
This commit is contained in:
parent
3d7791fbec
commit
2344912a1a
@ -16,12 +16,27 @@ csharp_new_line_before_finally = false
|
||||
# csharp_new_line_between_query_expression_clauses = false
|
||||
|
||||
# IDE1006
|
||||
dotnet_naming_style.camel_case.capitalization = camel_case
|
||||
# dotnet_naming_style.camel_case.capitalization = camel_case
|
||||
# symbols
|
||||
dotnet_naming_symbols.private_symbols.applicable_accessibilities = private
|
||||
dotnet_naming_symbols.public_symbols.applicable_accessibilities = public, protected
|
||||
dotnet_naming_symbols.interface_types.applicable_kinds = interface
|
||||
# dotnet_naming_symbols.private_symbols.applicable_accessibilities = private
|
||||
# dotnet_naming_symbols.public_symbols.applicable_accessibilities = public, protected
|
||||
# dotnet_naming_symbols.interface_types.applicable_kinds = interface
|
||||
# rules
|
||||
dotnet_naming_rule.camel_case_for_private.severity = warning
|
||||
dotnet_naming_rule.camel_case_for_private.symbols = private_symbols
|
||||
dotnet_naming_rule.camel_case_for_private.style = camel_case
|
||||
|
||||
dotnet_naming_style.variable_style.capitalization = camel_case
|
||||
dotnet_naming_style.variable_style.required_prefix = _
|
||||
dotnet_naming_style.variable_style.required_suffix =
|
||||
|
||||
# Enforce custom snake_case-like naming for variable names
|
||||
# dotnet_naming_rule.snake_case_like.variables.severity = error
|
||||
# dotnet_naming_rule.snake_case_like.variables.symbols = variable
|
||||
# dotnet_naming_rule.snake_case_like.variables.style = custom_snake_case_like
|
||||
#
|
||||
# dotnet_naming_symbols.variable.applicable_kinds = field, local, parameter
|
||||
|
||||
#dotnet_naming_style.custom_snake_case_like.capitalization = camel_case
|
||||
#dotnet_naming_style.custom_snake_case_like.required_prefix = _
|
||||
#dotnet_naming_style.custom_snake_case_like.required_suffix =
|
||||
|
185
018_Conditional_branching_and_looping/018_csharp.md
Normal file
185
018_Conditional_branching_and_looping/018_csharp.md
Normal file
@ -0,0 +1,185 @@
|
||||
# Guided project - Develop conditional branching and looping structures in C#
|
||||
|
||||
Gain experience developing a console app that implements selection and
|
||||
iteration statements to achieve app specifications.
|
||||
|
||||
### Learning objectives
|
||||
|
||||
In this module, you'll practice how to:
|
||||
|
||||
- Use Visual Studio Code 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
|
||||
|
||||
It's common for developers to begin a project by developing the features that
|
||||
import or generate application data. Once your application has access to the
|
||||
data it depends on, you can begin developing the features that process data and
|
||||
generate reports.
|
||||
|
||||
Suppose you're a developer who likes to support the local community. You and
|
||||
some of your friends started a business that helps find new homes for stray or
|
||||
abandoned cats and dogs. Your business started off small, with just a couple of
|
||||
strays, but it's starting to grow. You want to create an application that will
|
||||
help you match the animals in your care with people looking for a pet. You've
|
||||
found that it's important to have a detailed description of the animals to
|
||||
share with potential owners. In addition, being able to describe the
|
||||
personality of the dog or cat makes them more appealing to potential owners.
|
||||
You decide to create an application that helps you manage information about the
|
||||
dogs and cats you're caring for.
|
||||
|
||||
This module guides you through the process of developing the data-centric
|
||||
features of the Contoso Pets application. You'll use selection and iteration
|
||||
statements to create sample data, list the animals in your care, and add new
|
||||
animals to your business. Throughout the application, you'll use variables and
|
||||
expressions to control the execution of code branches. You will also ensure
|
||||
that variables are scoped appropriately.
|
||||
|
||||
The application you develop will:
|
||||
|
||||
- Add predefined sample data to the pets array.
|
||||
- Iterate a "menu options and user selection" code block to establish the outer
|
||||
loop of your application.
|
||||
- Implement code branches corresponding to the user's menu selections.
|
||||
- Display all the information contained in the array used to store pet data
|
||||
(based on user's menu selection).
|
||||
- Iterate an "add new animal information" code block that enables the user to
|
||||
add one or more new animals to the pets array (based on user's menu selection).
|
||||
|
||||
By the end of this module, you will be able to develop code that combines
|
||||
selection and iteration statements to achieve your application design goals.
|
||||
|
||||
> Note
|
||||
> This is a guided project module where you’ll complete an end-to-end project
|
||||
by following step-by-step instructions.
|
||||
|
||||
### Learning Objectives
|
||||
|
||||
In this module, you'll practice how 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 guided project, you'll develop the initial version of a C# application.
|
||||
Your application will 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 overview
|
||||
|
||||
You're working on the Contoso Pets application, an application that helps place
|
||||
pets in new homes. The specifications for your application are:
|
||||
|
||||
- Create a C# console application.
|
||||
|
||||
- Store application data in a multidimensional string array named
|
||||
`ourAnimals`.
|
||||
|
||||
- The `ourAnimals` array includes the following "pet characteristics" for each
|
||||
animal:
|
||||
|
||||
- Pet ID #.
|
||||
- Pet species (cat or dog).
|
||||
- Pet age (years).
|
||||
- A description of the pet's physical condition/characteristics.
|
||||
- A description of the pet's personality.
|
||||
- The pet's nickname.
|
||||
|
||||
- Implement a sample dataset that represents dogs and cats currently in your
|
||||
care.
|
||||
|
||||
- Display menu options to access the main features of the application.
|
||||
|
||||
- The main features enable the following tasks:
|
||||
|
||||
- List the pet information for all animals in the ourAnimals array.
|
||||
|
||||
- Add new animals to the ourAnimals array. The following conditions apply:
|
||||
|
||||
- The pet species (dog or cat) must be entered when a new animal is added to the ourAnimals array.
|
||||
- A pet ID must be programmatically generated when a new animal is added to the ourAnimals array.
|
||||
- Some physical characteristics for a pet may be unknown until a veterinarian's examination. For example: age, breed, and neutered/spayed status.
|
||||
- An animal's nickname and personality may be unknown when a pet first arrives.
|
||||
|
||||
- Ensure animal ages and physical descriptions are complete. This may be required after a veterinarian's examination.
|
||||
|
||||
- Ensure animal nicknames and personality descriptions are complete (this action can occur after the team gets to know a pet).
|
||||
|
||||
- Edit an animal’s age (if a pet's birth date is known and the pet has a birthday while in our care).
|
||||
|
||||
- Edit an animal’s personality description (a pet may behave differently after spending more time in our care).
|
||||
|
||||
- Display all cats that meet user specified physical characteristics.
|
||||
|
||||
- Display all dogs that meet user specified physical characteristics.
|
||||
|
||||
An initial version of the application has already been completed. The Starter
|
||||
code project for this Guided project module includes a Program.cs file that
|
||||
provides 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.
|
||||
|
||||
- The code uses a for loop around an `if-elseif-else` construct to populate the
|
||||
`ourAnimals` array with a sample dataset.
|
||||
|
||||
- The code displays the following main menu options for user selection:
|
||||
```txt
|
||||
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 displays a message echoing
|
||||
their selection.
|
||||
|
||||
Your goal is to develop the features that implement the first two menu options.
|
||||
To achieve this goal, you'll complete the following tasks:
|
||||
|
||||
- Update the code that's used to create the sample data for the app.
|
||||
- Construct a loop around the main menu and create a selection statement that
|
||||
establishes a code branch for each menu option.
|
||||
- Write the code to display all ourAnimals array data (menu option 1).
|
||||
- Build a loop for entering new ourAnimals array data (menu option 2 - part 1).
|
||||
- Write code to read and save new ourAnimals array data (menu option 2 - part 2).
|
||||
|
||||
You'll test your application at each stage of the development process.
|
||||
|
||||
## Setup
|
||||
|
||||
Use the following steps to prepare for the Guided project exercises.
|
||||
|
||||
To download a zip file containing the Starter project code, select the
|
||||
following link:
|
||||
[Lab Files](https://github.com/MicrosoftLearning/Guided-project-branching-looping-CSharp/archive/refs/heads/main.zip).
|
||||
|
||||
Unzip the download 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 Guided project exercises. Good luck!
|
||||
|
||||
---
|
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,255 @@
|
||||
// the ourAnimals array will store the following:
|
||||
string animalSpecies = "";
|
||||
string animalID = "";
|
||||
string animalAge = "";
|
||||
string animalPhysicalDescription = "";
|
||||
string animalPersonalityDescription = "";
|
||||
string animalNickname = "";
|
||||
|
||||
// variables that support data entry
|
||||
int maxPets = 8;
|
||||
string? readResult;
|
||||
string menuSelection = "";
|
||||
|
||||
// array used to store runtime data, there is no persisted data
|
||||
string[,] ourAnimals = new string[maxPets, 6];
|
||||
|
||||
// create some initial ourAnimals array entries
|
||||
for (int i = 0; i < maxPets; i++) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
animalSpecies = "dog";
|
||||
animalID = "d1";
|
||||
animalAge = "2";
|
||||
animalPhysicalDescription = "medium sized cream colored female golden retriever weighing about 65 pounds. housebroken.";
|
||||
animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.";
|
||||
animalNickname = "lola";
|
||||
break;
|
||||
case 1:
|
||||
animalSpecies = "dog";
|
||||
animalID = "d2";
|
||||
animalAge = "9";
|
||||
animalPhysicalDescription = "large reddish-brown male golden retriever weighing about 85 pounds. housebroken.";
|
||||
animalPersonalityDescription = "loves to have his ears rubbed when he greets you at the door, or at any time! loves to lean-in and give doggy hugs.";
|
||||
animalNickname = "loki";
|
||||
break;
|
||||
case 2:
|
||||
animalSpecies = "cat";
|
||||
animalID = "c3";
|
||||
animalAge = "1";
|
||||
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
|
||||
animalPersonalityDescription = "friendly";
|
||||
animalNickname = "Puss";
|
||||
break;
|
||||
case 3:
|
||||
animalSpecies = "cat";
|
||||
animalID = "c4";
|
||||
animalAge = "?";
|
||||
animalPhysicalDescription = "";
|
||||
animalPersonalityDescription = "";
|
||||
animalNickname = "";
|
||||
break;
|
||||
default:
|
||||
animalSpecies = "";
|
||||
animalID = "";
|
||||
animalAge = "";
|
||||
animalPhysicalDescription = "";
|
||||
animalPersonalityDescription = "";
|
||||
animalNickname = "";
|
||||
break;
|
||||
}
|
||||
|
||||
ourAnimals[i, 0] = "ID #: " + animalID;
|
||||
ourAnimals[i, 1] = "Species: " + animalSpecies;
|
||||
ourAnimals[i, 2] = "Age: " + animalAge;
|
||||
ourAnimals[i, 3] = "Nickname: " + animalNickname;
|
||||
ourAnimals[i, 4] = "Physical description: " + animalPhysicalDescription;
|
||||
ourAnimals[i, 5] = "Personality: " + animalPersonalityDescription;
|
||||
}
|
||||
|
||||
do {
|
||||
// display the top-level menu options
|
||||
|
||||
Console.Clear();
|
||||
Console.WriteLine("Welcome to the Contoso PetFriends app. Your main menu options are:");
|
||||
Console.WriteLine(" 1. List all of our current pet information");
|
||||
Console.WriteLine(" 2. Add a new animal friend to the ourAnimals array");
|
||||
Console.WriteLine(" 3. Ensure animal ages and physical descriptions are complete");
|
||||
Console.WriteLine(" 4. Ensure animal nicknames and personality descriptions are complete");
|
||||
Console.WriteLine(" 5. Edit an animal’s age");
|
||||
Console.WriteLine(" 6. Edit an animal’s personality description");
|
||||
Console.WriteLine(" 7. Display all cats with a specified characteristic");
|
||||
Console.WriteLine(" 8. Display all dogs with a specified characteristic");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Enter your selection number (or type Exit to exit the program)");
|
||||
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
menuSelection = readResult.ToLower();
|
||||
// NOTE: We could put a do statement around the menuSelection entry to
|
||||
// ensure a valid entry, but we use a conditional statement below that
|
||||
// only processes the valid entry values, so the do statement is not
|
||||
// required here.
|
||||
}
|
||||
|
||||
// use switch-case to process the selected menu option
|
||||
switch (menuSelection) {
|
||||
case "1":
|
||||
// List all of our current pet information
|
||||
for (int i = 0; i < maxPets; i++) {
|
||||
if (ourAnimals[i, 0] != "ID #: ") {
|
||||
Console.WriteLine();
|
||||
for (int j = 0; j < 6; j++) {
|
||||
Console.WriteLine(ourAnimals[i, j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine("\n\rPress the Enter key to continue");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "2":
|
||||
// Add a new animal friend to the ourAnimals array
|
||||
string anotherPet = "y";
|
||||
int petCount = 0;
|
||||
for (int i = 0; i < maxPets; i++) {
|
||||
if (ourAnimals[i, 0] != "ID #: ") {
|
||||
petCount += 1;
|
||||
}
|
||||
}
|
||||
if (petCount < maxPets) {
|
||||
Console.WriteLine($"We currently have {petCount} pets that need homes. We can manage {(maxPets - petCount)} more.");
|
||||
}
|
||||
while (anotherPet == "y" && petCount < maxPets) {
|
||||
bool validEntry = false;
|
||||
// get species (cat or dog) - string animalSpecies is a required field
|
||||
do {
|
||||
Console.WriteLine("\n\rEnter 'dog' or 'cat' to begin a new entry");
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
animalSpecies = readResult.ToLower();
|
||||
if (animalSpecies != "dog" && animalSpecies != "cat") {
|
||||
//Console.WriteLine($"You entered: {animalSpecies}.");
|
||||
validEntry = false;
|
||||
} else {
|
||||
validEntry = true;
|
||||
}
|
||||
}
|
||||
} while (validEntry == false);
|
||||
// build the animal the ID number - for example C1, C2, D3 (for Cat 1, Cat 2, Dog 3)
|
||||
animalID = animalSpecies.Substring(0, 1) + (petCount + 1).ToString();
|
||||
// get the pet's age. can be ? at initial entry.
|
||||
do {
|
||||
int petAge;
|
||||
Console.WriteLine("Enter the pet's age or enter ? if unknown");
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
animalAge = readResult;
|
||||
if (animalAge != "?") {
|
||||
validEntry = int.TryParse(animalAge, out petAge);
|
||||
} else {
|
||||
validEntry = true;
|
||||
}
|
||||
}
|
||||
} while (validEntry == false);
|
||||
// get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank.
|
||||
do {
|
||||
Console.WriteLine("Enter a physical description of the pet (size, color, gender, weight, housebroken)");
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
animalPhysicalDescription = readResult.ToLower();
|
||||
if (animalPhysicalDescription == "") {
|
||||
animalPhysicalDescription = "tbd";
|
||||
}
|
||||
}
|
||||
} while (animalPhysicalDescription == "");
|
||||
// get a description of the pet's personality - animalPersonalityDescription can be blank.
|
||||
do {
|
||||
Console.WriteLine("Enter a description of the pet's personality (likes or dislikes, tricks, energy level)");
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
animalPersonalityDescription = readResult.ToLower();
|
||||
if (animalPersonalityDescription == "") {
|
||||
animalPersonalityDescription = "tbd";
|
||||
}
|
||||
}
|
||||
} while (animalPersonalityDescription == "");
|
||||
// get the pet's nickname. animalNickname can be blank.
|
||||
do {
|
||||
Console.WriteLine("Enter a nickname for the pet");
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
animalNickname = readResult.ToLower();
|
||||
if (animalNickname == "") {
|
||||
animalNickname = "tbd";
|
||||
}
|
||||
}
|
||||
} while (animalNickname == "");
|
||||
// store the pet information in the ourAnimals array (zero based)
|
||||
ourAnimals[petCount, 0] = "ID #: " + animalID;
|
||||
ourAnimals[petCount, 1] = "Species: " + animalSpecies;
|
||||
ourAnimals[petCount, 2] = "Age: " + animalAge;
|
||||
ourAnimals[petCount, 3] = "Nickname: " + animalNickname;
|
||||
ourAnimals[petCount, 4] = "Physical description: " + animalPhysicalDescription;
|
||||
ourAnimals[petCount, 5] = "Personality: " + animalPersonalityDescription;
|
||||
// increment petCount (the array is zero-based, so we increment the counter after adding to the array)
|
||||
petCount = petCount + 1;
|
||||
// check maxPet limit
|
||||
if (petCount < maxPets) {
|
||||
// another pet?
|
||||
Console.WriteLine("Do you want to enter info for another pet (y/n)");
|
||||
do {
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
anotherPet = readResult.ToLower();
|
||||
}
|
||||
|
||||
} while (anotherPet != "y" && anotherPet != "n");
|
||||
}
|
||||
}
|
||||
if (petCount >= maxPets) {
|
||||
Console.WriteLine("We have reached our limit on the number of pets that we can manage.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
}
|
||||
break;
|
||||
case "3":
|
||||
// Ensure animal ages and physical descriptions are complete
|
||||
Console.WriteLine("Challenge Project - please check back soon to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "4":
|
||||
// Ensure animal nicknames and personality descriptions are complete
|
||||
Console.WriteLine("Challenge Project - please check back soon to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "5":
|
||||
// Edit an animal’s age");
|
||||
Console.WriteLine("UNDER CONSTRUCTION - please check back next month to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "6":
|
||||
// Edit an animal’s personality description");
|
||||
Console.WriteLine("UNDER CONSTRUCTION - please check back next month to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "7":
|
||||
// Display all cats with a specified characteristic
|
||||
Console.WriteLine("UNDER CONSTRUCTION - please check back next month to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
case "8":
|
||||
// Display all dogs with a specified characteristic
|
||||
Console.WriteLine("UNDER CONSTRUCTION - please check back next month to see progress.");
|
||||
Console.WriteLine("Press the Enter key to continue.");
|
||||
readResult = Console.ReadLine();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
} while (menuSelection != "exit");
|
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,76 @@
|
||||
int max_pets = 8;
|
||||
//string? read_input;
|
||||
//string menu_selection = "";
|
||||
string[] description = {
|
||||
"medium sized cream colored female golden retriever weighing about 65" +
|
||||
"pounds. housebroken.",
|
||||
"large reddish-brown male golden retriever weighing about 85 pounds. " +
|
||||
"housebroken.",
|
||||
"small white female weighing about 8 pounds. litter box trained.",
|
||||
"translucid at sun.",
|
||||
};
|
||||
string[] personality = {
|
||||
"friendly",
|
||||
"loves to have his ears rubbed at any time! loves to lean-in.",
|
||||
"loves to have her belly rubbed. gives lots of kisses.",
|
||||
"sauvage and lovelly.",
|
||||
};
|
||||
string[] nickname = { "lola", "loki", "fuzz", "boby", "gogo", "besti" };
|
||||
string[] species = { "cat", "dog", "bee", "pig" };
|
||||
string[] abcd = { "a", "b", "c", "d" };
|
||||
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||
Random rand = new Random();
|
||||
|
||||
// array used to store runtime data, there is no persisted data
|
||||
string[,] our_animals = new string[max_pets, 6];
|
||||
|
||||
string rand_str(string[] choices) {
|
||||
int indx = rand.Next(choices.Length);
|
||||
return choices[indx];
|
||||
}
|
||||
|
||||
int rand_int(int[] choices) {
|
||||
int indx = rand.Next(choices.Length);
|
||||
return choices[indx];
|
||||
}
|
||||
|
||||
for (int i = 0; i < max_pets - 4; i++) {
|
||||
bool uniq_id = false;
|
||||
our_animals[i, 0] = rand_str(species);
|
||||
while (!uniq_id) {
|
||||
bool check = true;
|
||||
string id = $"{rand_str(abcd)}{rand_int(nums)}";
|
||||
for (int j = 0; j < max_pets - 4; j++) {
|
||||
if (!string.IsNullOrEmpty(our_animals[j, 1]) && our_animals[j, 1] == id) {
|
||||
uniq_id = false;
|
||||
check = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (check) {
|
||||
our_animals[i, 1] = id;
|
||||
uniq_id = true;
|
||||
}
|
||||
}
|
||||
our_animals[i, 2] = rand_str(description);
|
||||
our_animals[i, 3] = $"{rand_int(nums)}";
|
||||
our_animals[i, 4] = rand_str(personality);
|
||||
our_animals[i, 5] = rand_str(nickname);
|
||||
}
|
||||
|
||||
void print_pets() {
|
||||
for (int j = 0; j < max_pets; j++) {
|
||||
if (!string.IsNullOrEmpty(our_animals[j, 1])) {
|
||||
Console.Write(our_animals[j, 1] + ": ");
|
||||
Console.Write(our_animals[j, 0] + " - ");
|
||||
Console.Write(our_animals[j, 5] + " - ");
|
||||
int age = Int32.Parse(our_animals[j,3].ToString());
|
||||
string year_word = age > 1 ? "years" : "year";
|
||||
Console.WriteLine(our_animals[j, 3] + $" {year_word}");
|
||||
Console.WriteLine(our_animals[j, 2]);
|
||||
Console.WriteLine(our_animals[j, 4]+"\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print_pets();
|
@ -0,0 +1,90 @@
|
||||
// the ourAnimals array will store the following:
|
||||
string animalSpecies = "";
|
||||
string animalID = "";
|
||||
string animalAge = "";
|
||||
string animalPhysicalDescription = "";
|
||||
string animalPersonalityDescription = "";
|
||||
string animalNickname = "";
|
||||
|
||||
// variables that support data entry
|
||||
int maxPets = 8;
|
||||
string? readResult;
|
||||
string menuSelection = "";
|
||||
|
||||
// array used to store runtime data, there is no persisted data
|
||||
string[,] ourAnimals = new string[maxPets, 6];
|
||||
|
||||
// TODO: Convert the if-elseif-else construct to a switch statement
|
||||
|
||||
// create some initial ourAnimals array entries
|
||||
for (int i = 0; i < maxPets; i++) {
|
||||
if (i == 0) {
|
||||
animalSpecies = "dog";
|
||||
animalID = "d1";
|
||||
animalAge = "2";
|
||||
animalPhysicalDescription = "medium sized cream colored female golden retriever weighing about 65 pounds. housebroken.";
|
||||
animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.";
|
||||
animalNickname = "lola";
|
||||
} else if (i == 1) {
|
||||
animalSpecies = "dog";
|
||||
animalID = "d2";
|
||||
animalAge = "9";
|
||||
animalPhysicalDescription = "large reddish-brown male golden retriever weighing about 85 pounds. housebroken.";
|
||||
animalPersonalityDescription = "loves to have his ears rubbed when he greets you at the door, or at any time! loves to lean-in and give doggy hugs.";
|
||||
animalNickname = "loki";
|
||||
} else if (i == 2) {
|
||||
animalSpecies = "cat";
|
||||
animalID = "c3";
|
||||
animalAge = "1";
|
||||
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
|
||||
animalPersonalityDescription = "friendly";
|
||||
animalNickname = "Puss";
|
||||
} else if (i == 3) {
|
||||
animalSpecies = "cat";
|
||||
animalID = "c4";
|
||||
animalAge = "?";
|
||||
animalPhysicalDescription = "";
|
||||
animalPersonalityDescription = "";
|
||||
animalNickname = "";
|
||||
} else {
|
||||
animalSpecies = "";
|
||||
animalID = "";
|
||||
animalAge = "";
|
||||
animalPhysicalDescription = "";
|
||||
animalPersonalityDescription = "";
|
||||
animalNickname = "";
|
||||
}
|
||||
ourAnimals[i, 0] = "ID #: " + animalID;
|
||||
ourAnimals[i, 1] = "Species: " + animalSpecies;
|
||||
ourAnimals[i, 2] = "Age: " + animalAge;
|
||||
ourAnimals[i, 3] = "Nickname: " + animalNickname;
|
||||
ourAnimals[i, 4] = "Physical description: " + animalPhysicalDescription;
|
||||
ourAnimals[i, 5] = "Personality: " + animalPersonalityDescription;
|
||||
}
|
||||
|
||||
// display the top-level menu options
|
||||
|
||||
Console.Clear();
|
||||
|
||||
Console.WriteLine("Welcome to the Contoso PetFriends app. Your main menu options are:");
|
||||
Console.WriteLine(" 1. List all of our current pet information");
|
||||
Console.WriteLine(" 2. Add a new animal friend to the ourAnimals array");
|
||||
Console.WriteLine(" 3. Ensure animal ages and physical descriptions are complete");
|
||||
Console.WriteLine(" 4. Ensure animal nicknames and personality descriptions are complete");
|
||||
Console.WriteLine(" 5. Edit an animal’s age");
|
||||
Console.WriteLine(" 6. Edit an animal’s personality description");
|
||||
Console.WriteLine(" 7. Display all cats with a specified characteristic");
|
||||
Console.WriteLine(" 8. Display all dogs with a specified characteristic");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Enter your selection number (or type Exit to exit the program)");
|
||||
|
||||
readResult = Console.ReadLine();
|
||||
if (readResult != null) {
|
||||
menuSelection = readResult.ToLower();
|
||||
}
|
||||
|
||||
Console.WriteLine($"You selected menu option {menuSelection}.");
|
||||
Console.WriteLine("Press the Enter key to continue");
|
||||
|
||||
// pause code execution
|
||||
readResult = Console.ReadLine();
|
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user