-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
232 lines (202 loc) · 8.56 KB
/
Copy pathProgram.cs
File metadata and controls
232 lines (202 loc) · 8.56 KB
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
229
230
231
232
// #1 the ourAnimals array will store the following:
string animalSpecies = "";
string animalID = "";
string animalAge = "";
string animalPhysicalDescription = "";
string animalPersonalityDescription = "";
string animalNickname = "";
string suggestedDonation = "";
// #2 variables that support data entry
int maxPets = 8;
string? readResult;
string menuSelection = "";
decimal decimalDonation = 0.00m;
// #3 array used to store runtime data, there is no persisted data
string[,] ourAnimals = new string[maxPets, 7];
// #4 create 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";
break;
case 2:
animalSpecies = "cat";
animalID = "c3";
animalAge = "1";
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
animalPersonalityDescription = "friendly";
animalNickname = "snow";
break;
case 3:
animalSpecies = "cat";
animalID = "c4";
animalAge = "3";
animalPhysicalDescription = "Medium sized, long hair, yellow, female, about 10 pounds. Uses litter box.";
animalPersonalityDescription = "A people loving cat that likes to sit on your lap.";
animalNickname = "Lion";
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;
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}";
}
// #5 display the 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();
}
// use 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]);
}
}
}
Console.WriteLine("\n\rPress the Enter key to continue");
readResult = Console.ReadLine();
break;
case "2":
// Display all dogs with a specified characteristic
string dogCharacteristic = "";
while (dogCharacteristic == "")
{
// have the user enter physical characteristics to search for
Console.WriteLine($"\nEnter the search terms separated by commas");
readResult = Console.ReadLine();
if (readResult != null)
{
dogCharacteristic = readResult.Trim();
}
}
string[] terms = dogCharacteristic
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim().ToLower())
.Where(t => t.Length > 0)
.ToArray();
while (terms.Length == 0)
{
Console.WriteLine("Please enter at least one non-empty search term.");
readResult = Console.ReadLine();
dogCharacteristic = (readResult ?? "").Trim();
terms = dogCharacteristic
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim().ToLower())
.Where(t => t.Length > 0)
.ToArray();
}
Array.Sort(terms, StringComparer.Ordinal);
// >>> Clean spinner countdown: rotates icons, ticks down 2 → 1 → 0 <<<
string[] spinnerIcons = { "|", "/", "-", "\\" };
int spinSeconds = Math.Max(1, terms.Length);
int termIndex = -1;
int iconIndex = 0;
for (int remaining = spinSeconds; remaining >= 0; remaining--)
{
// move to the next search term each second
termIndex = (termIndex + 1) % terms.Length;
string currentTerm = terms[termIndex];
// spin for one second
int framesPerSecond = 6;
for (int f = 0; f < framesPerSecond; f++)
{
string countdown = (remaining == 0) ? "0" : $"{remaining}/{spinSeconds}";
Console.Write($"\rsearching for {currentTerm} {spinnerIcons[iconIndex % spinnerIcons.Length]} {countdown} ");
iconIndex++;
System.Threading.Thread.Sleep(1000 / framesPerSecond);
}
}
// clear the spinner line after finishing
Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");
bool noMatchesDog = true;
string dogDescription = "";
// #6 loop through the ourAnimals array to search for matching animals
for (int i = 0; i < maxPets; i++)
{
if (ourAnimals[i, 1].Contains("dog"))
{
// #7 Search combined descriptions and report results
dogDescription = ourAnimals[i, 4] + "\n" + ourAnimals[i, 5];
string combinedLower = dogDescription.ToLower();
bool matchedThisDog = false;
foreach (var term in terms)
{
if (combinedLower.Contains(term))
{
string nickLabeled = ourAnimals[i, 3] ?? "";
string dogName = nickLabeled.StartsWith("Nickname: ", StringComparison.OrdinalIgnoreCase)
? nickLabeled.Substring("Nickname: ".Length)
: nickLabeled;
Console.WriteLine($"\nOur dog {dogName} is a match for your search for {term}!");
matchedThisDog = true;
}
}
if (matchedThisDog)
{
Console.WriteLine(ourAnimals[i, 3]);
Console.WriteLine(dogDescription);
noMatchesDog = false;
}
}
}
if (noMatchesDog)
{
Console.WriteLine("No matches found for any available dogs");
}
Console.WriteLine("Press the Enter key to continue.");
readResult = Console.ReadLine();
break;
default:
break;
}
} while (menuSelection != "exit");