mirror of
https://github.com/dotnet/intro-to-dotnet-web-dev.git
synced 2026-04-04 22:17:30 +00:00
Add links to C# content and project code (#37)
* started adding C# content * begin adding csharp project code * add csharp project code * added codespaces instructions * add license files * add license files * add csharp project 6
This commit is contained in:
parent
c65d9a369c
commit
66100c7304
59 changed files with 3811 additions and 4 deletions
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
228
2-csharp/lesson-4-projects/challenge-project/Final/Program.cs
Normal file
228
2-csharp/lesson-4-projects/challenge-project/Final/Program.cs
Normal file
|
|
@ -0,0 +1,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");
|
||||
21
2-csharp/lesson-4-projects/challenge-project/LICENSE
Normal file
21
2-csharp/lesson-4-projects/challenge-project/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Microsoft Learning
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
2
2-csharp/lesson-4-projects/challenge-project/README.md
Normal file
2
2-csharp/lesson-4-projects/challenge-project/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Challenge-project-Work-with-variable-data-in-CSharp
|
||||
Starter and Solution code for the **Challenge project**: "Work with variable data in C# console applications" from the Microsoft Learn collection "Getting started with C#"
|
||||
206
2-csharp/lesson-4-projects/challenge-project/Starter/Program.cs
Normal file
206
2-csharp/lesson-4-projects/challenge-project/Starter/Program.cs
Normal file
|
|
@ -0,0 +1,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");
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue