C# - Contoso Pets adoption C# console application 1
Introduction to the Contoso Pets adoption C# console application
- The application already creates sample data on pets available for adoption, and is able to display the pet’s description information.
- The application provides searching with a single term within the description data for each dog.
- The main feature addition is search with multiple terms.
- The secondary improvement is to the “search status” animation and includes adding a search countdown in the animation.
Project specification overview
Update the existing Contoso Pets starter application to include multiple term search and improved “search status” animation features:
Add dog attribute multi-term search
- Gather user input for the pet characteristic multiple term search
- Users need to provide search terms separated by commas
- Store the search terms in an array and sort the terms alphabetically
- Within the animals array loop that identifies “dogs”:
- Iterate through the search terms to search each dog’s description
- Search the combined description for a term match
- Output each dog’s description where there’s one or more terms matched
- After exiting the “search Animals” array loop that identifies dogs:
- If no dogs were a match for any of the users provided search terms, output a no dogs matched message.
Add “search status” improvements
- Update the animation
- Adjust the searchingIcons array to resemble a spinning dial
- Adjust the animation loop so the animation shows a numeric countdown from two to zero (2.., 1.., 0..)
Starter Code Overview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
// ourAnimals array will store the following:
string animalSpecies = "";
string animalID = "";
string animalAge = "";
string animalPhysicalDescription = "";
string animalPersonalityDescription = "";
string animalNickname = "";
string suggestedDonation = "";
// variables that support data entry
int maxPets = 8;
string? readResult;
string menuSelection = "";
decimal decimalDonation = 0.00m;
// array used to store runtime data
string[,] ourAnimals = new string[maxPets, 7];
// sample data 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 45 pounds. housebroken.";
animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.";
animalNickname = "lola";
suggestedDonation = "85.00";
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 = "gus";
suggestedDonation = "49.99";
break;
case 2:
animalSpecies = "cat";
animalID = "c3";
animalAge = "1";
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
animalPersonalityDescription = "friendly";
animalNickname = "snow";
suggestedDonation = "40.00";
break;
case 3:
animalSpecies = "cat";
animalID = "c4";
animalAge = "";
animalPhysicalDescription = "";
animalPersonalityDescription = "";
animalNickname = "lion";
suggestedDonation = "";
break;
default:
animalSpecies = "";
animalID = "";
animalAge = "";
animalPhysicalDescription = "";
animalPersonalityDescription = "";
animalNickname = "";
suggestedDonation = "";
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;
if (!decimal.TryParse(suggestedDonation, out decimalDonation)){
decimalDonation = 45.00m; // if suggestedDonation NOT a number, default to 45.00
}
ourAnimals[i, 6] = $"Suggested Donation: {decimalDonation:C2}";
}
// top-level menu options
do
{
// NOTE: the Console.Clear method is throwing an exception in debug sessions
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. 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();
}
// switch-case to process the selected menu option
switch (menuSelection)
{
case "1":
// list all pet info
for (int i = 0; i < maxPets; i++)
{
if (ourAnimals[i, 0] != "ID #: ")
{
Console.WriteLine();
for (int j = 0; j < 7; j++)
{
Console.WriteLine(ourAnimals[i, j].ToString());
}
}
}
Console.WriteLine("\r\nPress the Enter key to continue");
readResult = Console.ReadLine();
break;
case "2":
// #1 Display all dogs with a multiple search characteristics
string dogCharacteristic = "";
while (dogCharacteristic == "")
{
// #2 have user enter multiple comma separated characteristics to search for
Console.WriteLine($"\r\nEnter one desired dog characteristic to search for");
readResult = Console.ReadLine();
if (readResult != null)
{
dogCharacteristic = readResult.ToLower().Trim();
Console.WriteLine();
}
}
bool noMatchesDog = true;
string dogDescription = "";
// #4 update to "rotating" animation with countdown
string[] searchingIcons = {". ", ".. ", "..."};
// loop ourAnimals array to search for matching animals
for (int i = 0; i < maxPets; i++)
{
if (ourAnimals[i, 1].Contains("dog"))
{
// Search combined descriptions and report results
dogDescription = ourAnimals[i, 4] + "\r\n" + ourAnimals[i, 5];
for (int j = 5; j > -1 ; j--)
{
// #5 update "searching" message to show countdown
foreach (string icon in searchingIcons)
{
Console.Write($"\rsearching our dog {ourAnimals[i, 3]} for {dogCharacteristic} {icon}");
Thread.Sleep(250);
}
Console.Write($"\r{new String(' ', Console.BufferWidth)}");
}
// #3a iterate submitted characteristic terms and search description for each term
if (dogDescription.Contains(dogCharacteristic))
{
// #3b update message to reflect term
// #3c set a flag "this dog" is a match
Console.WriteLine($"\nOur dog {ourAnimals[i, 3]} is a match!");
noMatchesDog = false;
}
// #3d if "this dog" is match write match message + dog description
}
}
if (noMatchesDog)
{
Console.WriteLine("None of our dogs are a match found for: " + dogCharacteristic);
}
Console.WriteLine("\n\rPress the Enter key to continue");
readResult = Console.ReadLine();
break;
default:
break;
}
} while (menuSelection != "exit");
- The starter code project for this challenge 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:
1
2
3
4
1. List all of our current pet information
2. Display all dogs with a specified characteristic
Enter menu item selection or type "Exit" to exit the program
- The application menu code reads the user’s menu item selection and displays a message echoing their selection
- Case 1: “List all of our current pet information” displays the sample data for all animals (two dogs and two cats)
- Case 2: “2. Display all dogs with a specified characteristic” is the primary area where new functionality code is added
- Under Case 2, the starter code identifies dogs and searches for a single user input term
- Before you search each dog, the console shows a simple “animation” that simulates a status for searching occurring
- Enter 1 as shown in the following terminal example:
1
2
3
4
5
6
Welcome to the Contoso PetFriends app. Your main menu options are:
1. List all of our current pet information
2. Display all dogs with a specified characteristic
Enter your selection number (or type Exit to exit the program)
1
- After pressing Enter, the menu should display again. Choose Option 2, shown in the following example:
1
2
3
4
5
6
7
8
Welcome to the Contoso PetFriends app. Your main menu options are:
1. List all of our current pet information
2. Display all dogs with a specified characteristic
Enter your selection number (or type Exit to exit the program)
2
Enter one desired dog characteristic to search for
Enter “large” for the search term and press “Enter.”
Notice the “searching” message as shown:
1
searching our dog Nickname: gus for large ...
Notice, the message runs before each search of a pet, and the periods (., .., …) at the end change in an animation.
Once the search ends, press enter to return to the menu. Then type “exit” and press “Enter” to close the application.
Implementation of the feature requirements
Add multiple term search support
Search specification
Gather multiple search terms from the user
- Allow the user to enter multiple search terms when searching for dogs
- The user needs instructions to “enter the search terms separated by commas”
- Separate out individual search terms from the user entry string and store as values in an array
- Sort the terms in the array in alphanumeric sort order
Identify dogs with descriptions with matches for one, or more, user search term
- As you identify a dog in the animalsArray, search for matches for each term the user has entered
- For a term match, output a message with the dogs name and the term that is matched
- example: Our dog Jake is a match for your search for sheppard!
- When all term searches complete for the current dog description:
- For one or more matches, output the nickname and description for the current dog
- For one or more matches, track that there has been a match so you know not to display an “no matches found for any available dogs” message (refer to the next item)
- After all dog searches complete with no matches, display a message “No matches found for any available dogs”
Enforce the following validation rules
- values can’t be null
- values can’t have zero characters
- any further restriction is up to the developer
Contoso Pets adoption app demo GIF
Search animation specification
Change the current “searching” animation icons
- Update the current animation “icons” string[] searchingIcons = {“. “, “.. “, “…”};
- Use new icons that simulate spinning
- Review the animated gif that follows for an example
- You can design the “searching” animation to display “spinning” to work differently than displayed in the animated image
- The “searching…” animation, should continue to be overwritten, after each animation completes so it stays on the same line, and so that it doesn’t display after the animation stops.
Add a countdown to the “searching” animation
- Review the previous animated image - note the countdown in the output
- “searching…retriever / 2”
- The previous number “2” displays as “1”, and finally as “0”, counting down
- Update the loop that contains the “searching” animation so that the loop can display a countdown
- Review the previous animated image - note the countdown in the output
Final Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Final
// ourAnimals array will store the following:
string animalSpecies = "";
string animalID = "";
string animalAge = "";
string animalPhysicalDescription = "";
string animalPersonalityDescription = "";
string animalNickname = "";
string suggestedDonation = "";
// variables that support data entry
int maxPets = 8;
string? readResult;
string menuSelection = "";
decimal decimalDonation = 0.00m;
// array used to store runtime data
string[,] ourAnimals = new string[maxPets, 7];
// sample data 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 45 pounds. housebroken.";
animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.";
animalNickname = "lola";
suggestedDonation = "85.00";
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 = "gus";
suggestedDonation = "49.99";
break;
case 2:
animalSpecies = "cat";
animalID = "c3";
animalAge = "1";
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
animalPersonalityDescription = "friendly";
animalNickname = "snow";
suggestedDonation = "40.00";
break;
case 3:
animalSpecies = "cat";
animalID = "c4";
animalAge = "";
animalPhysicalDescription = "";
animalPersonalityDescription = "";
animalNickname = "lion";
suggestedDonation = "";
break;
default:
animalSpecies = "";
animalID = "";
animalAge = "";
animalPhysicalDescription = "";
animalPersonalityDescription = "";
animalNickname = "";
suggestedDonation = "";
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;
if (!decimal.TryParse(suggestedDonation, out decimalDonation))
{
decimalDonation = 45.00m; // if suggestedDonation NOT a number, default to 45.00
}
ourAnimals[i, 6] = $"Suggested Donation: {decimalDonation:C2}";
}
// top-level menu options
do
{
// NOTE: the Console.Clear method is throwing an exception in debug sessions
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. 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();
}
// switch-case to process the selected menu option
switch (menuSelection)
{
case "1":
// list all pet info
for (int i = 0; i < maxPets; i++)
{
if (ourAnimals[i, 0] != "ID #: ")
{
Console.WriteLine();
for (int j = 0; j < 7; j++)
{
Console.WriteLine(ourAnimals[i, j].ToString());
}
}
}
Console.WriteLine("\r\nPress the Enter key to continue");
readResult = Console.ReadLine();
break;
case "2":
// #1 Display all dogs with a multiple search characteristics
string dogCharacteristics = "";
while (dogCharacteristics == "")
{
// #2 have user enter multiple comma separated characteristics to search for
Console.WriteLine($"\nEnter dog characteristics to search for separated by commas");
readResult = Console.ReadLine();
if (readResult != null)
{
dogCharacteristics = readResult.ToLower();
Console.WriteLine();
}
}
string[] dogSearches = dogCharacteristics.Split(",");
// trim leading and trailing spaces from each search term
for (int i = 0; i < dogSearches.Length; i++)
{
dogSearches[i] = dogSearches[i].Trim();
}
Array.Sort(dogSearches);
// #4 update to "rotating" animation with countdown
string[] searchingIcons = {" |", " /", "--", " \\", " *"};
bool matchesAnyDog = false;
string dogDescription = "";
// loops through the ourAnimals array to search for matching animals
for (int i = 0; i < maxPets; i++)
{
if (ourAnimals[i, 1].Contains("dog"))
{
// Search combined descriptions and report results
dogDescription = ourAnimals[i, 4] + "\n" + ourAnimals[i, 5];
bool matchesCurrentDog = false;
foreach (string term in dogSearches)
{
// only search if there is a term to search for
if (term != null && term.Trim() != "")
{
for (int j = 2; j > -1 ; j--)
{
// #5 update "searching" message to show countdown
foreach (string icon in searchingIcons)
{
Console.Write($"\rsearching our dog {ourAnimals[i, 3]} for {term.Trim()} {icon} {j.ToString()}");
Thread.Sleep(100);
}
Console.Write($"\r{new String(' ', Console.BufferWidth)}");
}
// #3a iterate submitted characteristic terms and search description for each term
if (dogDescription.Contains(" " + term.Trim() + " "))
{
// #3b update message to reflect current search term match
Console.WriteLine($"\rOur dog {ourAnimals[i, 3]} matches your search for {term.Trim()}");
matchesCurrentDog = true;
matchesAnyDog = true;
}
}
}
// #3d if the current dog is match, display the dog's info
if (matchesCurrentDog)
{
Console.WriteLine($"\r{ourAnimals[i, 3]} ({ourAnimals[i, 0]})\n{dogDescription}\n");
}
}
}
if (!matchesAnyDog)
{
Console.WriteLine("None of our dogs are a match found for: " + dogCharacteristics);
}
Console.WriteLine("\n\rPress the Enter key to continue");
readResult = Console.ReadLine();
break;
default:
break;
}
}
while (menuSelection != "exit");
This post is licensed under CC BY 4.0 by the author.