Skip to content

Commit 8513f63

Browse files
authored
Working with variable data in C# Applications
1 parent d0db393 commit 8513f63

25 files changed

+511
-0
lines changed

Data in C# console/Program.cs

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
using System;
2+
3+
// ourAnimals array will store the following:
4+
string animalSpecies = "";
5+
string animalID = "";
6+
string animalAge = "";
7+
string animalPhysicalDescription = "";
8+
string animalPersonalityDescription = "";
9+
string animalNickname = "";
10+
string suggestedDonation = "";
11+
12+
// variables that support data entry
13+
int maxPets = 8;
14+
string? readResult;
15+
string menuSelection = "";
16+
decimal decimalDonation = 0.00m;
17+
18+
// array used to store runtime data
19+
string[,] ourAnimals = new string[maxPets, 7];
20+
21+
// sample data ourAnimals array entries
22+
for (int i = 0; i < maxPets; i++)
23+
{
24+
switch (i)
25+
{
26+
case 0:
27+
animalSpecies = "dog";
28+
animalID = "d1";
29+
animalAge = "2";
30+
animalPhysicalDescription = "medium sized cream colored female golden retriever weighing about 45 pounds. housebroken.";
31+
animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.";
32+
animalNickname = "lola";
33+
suggestedDonation = "85.00";
34+
break;
35+
36+
case 1:
37+
animalSpecies = "dog";
38+
animalID = "d2";
39+
animalAge = "9";
40+
animalPhysicalDescription = "large reddish-brown male golden retriever weighing about 85 pounds. housebroken.";
41+
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.";
42+
animalNickname = "gus";
43+
suggestedDonation = "49.99";
44+
break;
45+
46+
case 2:
47+
animalSpecies = "cat";
48+
animalID = "c3";
49+
animalAge = "1";
50+
animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained.";
51+
animalPersonalityDescription = "friendly";
52+
animalNickname = "snow";
53+
suggestedDonation = "40.00";
54+
break;
55+
56+
case 3:
57+
animalSpecies = "cat";
58+
animalID = "c4";
59+
animalAge = "";
60+
animalPhysicalDescription = "";
61+
animalPersonalityDescription = "";
62+
animalNickname = "lion";
63+
suggestedDonation = "";
64+
65+
break;
66+
67+
default:
68+
animalSpecies = "";
69+
animalID = "";
70+
animalAge = "";
71+
animalPhysicalDescription = "";
72+
animalPersonalityDescription = "";
73+
animalNickname = "";
74+
suggestedDonation = "";
75+
break;
76+
77+
}
78+
79+
ourAnimals[i, 0] = "ID #: " + animalID;
80+
ourAnimals[i, 1] = "Species: " + animalSpecies;
81+
ourAnimals[i, 2] = "Age: " + animalAge;
82+
ourAnimals[i, 3] = "Nickname: " + animalNickname;
83+
ourAnimals[i, 4] = "Physical description: " + animalPhysicalDescription;
84+
ourAnimals[i, 5] = "Personality: " + animalPersonalityDescription;
85+
86+
if (!decimal.TryParse(suggestedDonation, out decimalDonation)){
87+
decimalDonation = 45.00m; // if suggestedDonation NOT a number, default to 45.00
88+
}
89+
ourAnimals[i, 6] = $"Suggested Donation: {decimalDonation:C2}";
90+
}
91+
92+
// top-level menu options
93+
do
94+
{
95+
// NOTE: the Console.Clear method is throwing an exception in debug sessions
96+
Console.Clear();
97+
98+
Console.WriteLine("Welcome to the Contoso PetFriends app. Your main menu options are:");
99+
Console.WriteLine(" 1. List all of our current pet information");
100+
Console.WriteLine(" 2. Display all dogs with a specified characteristic");
101+
Console.WriteLine();
102+
Console.WriteLine("Enter your selection number (or type Exit to exit the program)");
103+
104+
readResult = Console.ReadLine();
105+
if (readResult != null)
106+
{
107+
menuSelection = readResult.ToLower();
108+
}
109+
110+
// switch-case to process the selected menu option
111+
switch (menuSelection)
112+
{
113+
case "1":
114+
// list all pet info
115+
for (int i = 0; i < maxPets; i++)
116+
{
117+
if (ourAnimals[i, 0] != "ID #: ")
118+
{
119+
Console.WriteLine();
120+
for (int j = 0; j < 7; j++)
121+
{
122+
Console.WriteLine(ourAnimals[i, j].ToString());
123+
}
124+
}
125+
}
126+
127+
Console.WriteLine("\r\nPress the Enter key to continue");
128+
readResult = Console.ReadLine();
129+
130+
break;
131+
132+
case "2":
133+
// #1 Display all dogs with a multiple search characteristics
134+
135+
string dogCharacteristic = "";
136+
137+
while (dogCharacteristic == "")
138+
{
139+
// #2 have user enter multiple comma separated characteristics to search for
140+
Console.WriteLine($"\r\nEnter one desired dog characteristic to search for");
141+
readResult = Console.ReadLine();
142+
if (readResult != null)
143+
{
144+
dogCharacteristic = readResult.ToLower().Trim();
145+
Console.WriteLine();
146+
}
147+
}
148+
149+
bool noMatchesDog = true;
150+
string dogDescription = "";
151+
152+
// #4 update to "rotating" animation with countdown
153+
string[] searchingIcons = {". ", ".. ", "..."};
154+
155+
// loop ourAnimals array to search for matching animals
156+
for (int i = 0; i < maxPets; i++)
157+
{
158+
159+
if (ourAnimals[i, 1].Contains("dog"))
160+
{
161+
162+
// Search combined descriptions and report results
163+
dogDescription = ourAnimals[i, 4] + "\r\n" + ourAnimals[i, 5];
164+
165+
for (int j = 5; j > -1 ; j--)
166+
{
167+
// #5 update "searching" message to show countdown
168+
foreach (string icon in searchingIcons)
169+
{
170+
Console.Write($"\rsearching our dog {ourAnimals[i, 3]} for {dogCharacteristic} {icon}");
171+
Thread.Sleep(250);
172+
}
173+
174+
Console.Write($"\r{new String(' ', Console.BufferWidth)}");
175+
}
176+
177+
// #3a iterate submitted characteristic terms and search description for each term
178+
179+
if (dogDescription.Contains(dogCharacteristic))
180+
{
181+
// #3b update message to reflect term
182+
// #3c set a flag "this dog" is a match
183+
Console.WriteLine($"\nOur dog {ourAnimals[i, 3]} is a match!");
184+
185+
noMatchesDog = false;
186+
}
187+
188+
// #3d if "this dog" is match write match message + dog description
189+
}
190+
}
191+
192+
if (noMatchesDog)
193+
{
194+
Console.WriteLine("None of our dogs are a match found for: " + dogCharacteristic);
195+
}
196+
197+
Console.WriteLine("\n\rPress the Enter key to continue");
198+
readResult = Console.ReadLine();
199+
200+
break;
201+
202+
default:
203+
break;
204+
}
205+
206+
} while (menuSelection != "exit");

Data in C# console/Starter.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"runtimeTarget": {
3+
"name": ".NETCoreApp,Version=v7.0",
4+
"signature": ""
5+
},
6+
"compilationOptions": {},
7+
"targets": {
8+
".NETCoreApp,Version=v7.0": {
9+
"Starter/1.0.0": {
10+
"runtime": {
11+
"Starter.dll": {}
12+
}
13+
}
14+
}
15+
},
16+
"libraries": {
17+
"Starter/1.0.0": {
18+
"type": "project",
19+
"serviceable": false,
20+
"sha512": ""
21+
}
22+
}
23+
}
Binary file not shown.
Binary file not shown.
11.8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"runtimeOptions": {
3+
"tfm": "net7.0",
4+
"framework": {
5+
"name": "Microsoft.NETCore.App",
6+
"version": "7.0.0"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
using System;
11+
using System.Reflection;
12+
13+
[assembly: System.Reflection.AssemblyCompanyAttribute("Starter")]
14+
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
15+
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
16+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
17+
[assembly: System.Reflection.AssemblyProductAttribute("Starter")]
18+
[assembly: System.Reflection.AssemblyTitleAttribute("Starter")]
19+
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
20+
21+
// Generated by the MSBuild WriteCodeFragment class.
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bf438be927056bc12aabba6ed517c4f5f118b64750c1a48ff214b1cd1386eee9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
is_global = true
2+
build_property.TargetFramework = net7.0
3+
build_property.TargetPlatformMinVersion =
4+
build_property.UsingMicrosoftNETSdkWeb =
5+
build_property.ProjectTypeGuids =
6+
build_property.InvariantGlobalization =
7+
build_property.PlatformNeutralAssembly =
8+
build_property.EnforceExtendedAnalyzerRules =
9+
build_property._SupportedPlatformList = Linux,macOS,Windows
10+
build_property.RootNamespace = Starter
11+
build_property.ProjectDir = C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\
12+
build_property.EnableComHosting =
13+
build_property.EnableGeneratedComInterfaceComImportInterop =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <auto-generated/>
2+
global using global::System;
3+
global using global::System.Collections.Generic;
4+
global using global::System.IO;
5+
global using global::System.Linq;
6+
global using global::System.Net.Http;
7+
global using global::System.Threading;
8+
global using global::System.Threading.Tasks;
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
652151dd9b681f1d1cedf40558aa47892a9e40f27b804706fd1624505ac60ad4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\bin\Debug\net7.0\Starter.exe
2+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\bin\Debug\net7.0\Starter.deps.json
3+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\bin\Debug\net7.0\Starter.runtimeconfig.json
4+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\bin\Debug\net7.0\Starter.dll
5+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\bin\Debug\net7.0\Starter.pdb
6+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.GeneratedMSBuildEditorConfig.editorconfig
7+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.AssemblyInfoInputs.cache
8+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.AssemblyInfo.cs
9+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.csproj.CoreCompileInputs.cache
10+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.dll
11+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\refint\Starter.dll
12+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.pdb
13+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\Starter.genruntimeconfig.cache
14+
C:\Users\SALIM\Desktop\Challenge-Project-variable-data-in-CSharp-main\Starter\obj\Debug\net7.0\ref\Starter.dll
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
76b6dde223730e5ed5a9558acfa65fee24df5392c2ea73afb05e57c3b4d88b2b
11.8 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)