begin adding csharp project code

This commit is contained in:
katiesavage 2023-07-05 17:29:46 -07:00
parent 9c55bb97ba
commit b9677d700d
17 changed files with 985 additions and 4 deletions

View file

@ -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>

View file

@ -0,0 +1,144 @@
/*
This C# console application is designed to:
- Use arrays to store student names and assignment scores.
- Use a `foreach` statement to iterate through the student names as an outer program loop.
- Use an `if` statement within the outer loop to identify the current student name and access that student's assignment scores.
- Use a `foreach` statement within the outer loop to iterate though the assignment scores array and sum the values.
- Use an algorithm within the outer loop to calculate the average exam score for each student.
- Use an `if-elseif-else` construct within the outer loop to evaluate the average exam score and assign a letter grade automatically.
- Integrate extra credit scores when calculating the student's final score and letter grade as follows:
- detects extra credit assignments based on the number of elements in the student's scores array.
- divides the values of extra credit assignments by 10 before adding extra credit scores to the sum of exam scores.
- use the following report format to report student grades:
Student Exam Score Overall Grade Extra Credit
Sophia 92.2 95.88 A 92 (3.68 pts)
*/
int examAssignments = 5;
string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" };
int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 };
int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 };
int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 };
int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 };
int[] studentScores = new int[10];
string currentStudentLetterGrade = "";
// display the header row for scores/grades
Console.Clear();
Console.WriteLine("Student\t\tExam Score\tOverall Grade\tExtra Credit\n");
/*
The outer foreach loop is used to:
- iterate through student names
- assign a student's grades to the studentScores array
- calculate exam and extra credit sums (inner foreach loop)
- calculate numeric and letter grade
- write the score report information
*/
foreach (string name in studentNames)
{
string currentStudent = name;
if (currentStudent == "Sophia")
studentScores = sophiaScores;
else if (currentStudent == "Andrew")
studentScores = andrewScores;
else if (currentStudent == "Emma")
studentScores = emmaScores;
else if (currentStudent == "Logan")
studentScores = loganScores;
int gradedAssignments = 0;
int gradedExtraCreditAssignments = 0;
int sumExamScores = 0;
int sumExtraCreditScores = 0;
decimal currentStudentGrade = 0;
decimal currentStudentExamScore = 0;
decimal currentStudentExtraCreditScore = 0;
/*
the inner foreach loop:
- sums the exam and extra credit scores
- counts the extra credit assignments
*/
foreach (int score in studentScores)
{
gradedAssignments += 1;
if (gradedAssignments <= examAssignments)
{
sumExamScores = sumExamScores + score;
}
else
{
gradedExtraCreditAssignments += 1;
sumExtraCreditScores += score;
}
}
currentStudentExamScore = (decimal)(sumExamScores) / examAssignments;
currentStudentExtraCreditScore = (decimal)(sumExtraCreditScores) / gradedExtraCreditAssignments;
currentStudentGrade = (decimal)((decimal)sumExamScores + ((decimal)sumExtraCreditScores / 10)) / examAssignments;
if (currentStudentGrade >= 97)
currentStudentLetterGrade = "A+";
else if (currentStudentGrade >= 93)
currentStudentLetterGrade = "A";
else if (currentStudentGrade >= 90)
currentStudentLetterGrade = "A-";
else if (currentStudentGrade >= 87)
currentStudentLetterGrade = "B+";
else if (currentStudentGrade >= 83)
currentStudentLetterGrade = "B";
else if (currentStudentGrade >= 80)
currentStudentLetterGrade = "B-";
else if (currentStudentGrade >= 77)
currentStudentLetterGrade = "C+";
else if (currentStudentGrade >= 73)
currentStudentLetterGrade = "C";
else if (currentStudentGrade >= 70)
currentStudentLetterGrade = "C-";
else if (currentStudentGrade >= 67)
currentStudentLetterGrade = "D+";
else if (currentStudentGrade >= 63)
currentStudentLetterGrade = "D";
else if (currentStudentGrade >= 60)
currentStudentLetterGrade = "D-";
else
currentStudentLetterGrade = "F";
// Student Exam Score Overall Grade Extra Credit
// Sophia 92.2 95.88 A 92 (3.68 pts)
Console.WriteLine($"{currentStudent}\t\t{currentStudentExamScore}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}\t{currentStudentExtraCreditScore} ({(((decimal)sumExtraCreditScores / 10) / examAssignments)} pts)");
}
// required for running in VS Code (keeps the Output windows open to view results)
Console.WriteLine("\n\rPress the Enter key to continue");
Console.ReadLine();

View file

@ -0,0 +1,132 @@
/*
This C# console application is designed to:
- Use arrays to store student names and assignment scores.
- Use a `foreach` statement to iterate through the student names as an outer program loop.
- Use an `if` statement within the outer loop to identify the current student name and access that student's assignment scores.
- Use a `foreach` statement within the outer loop to iterate though the assignment scores array and sum the values.
- Use an algorithm within the outer loop to calculate the average exam score for each student.
- Use an `if-elseif-else` construct within the outer loop to evaluate the average exam score and assign a letter grade automatically.
- Integrate extra credit scores when calculating the student's final score and letter grade as follows:
- detects extra credit assignments based on the number of elements in the student's scores array.
- divides the values of extra credit assignments by 10 before adding extra credit scores to the sum of exam scores.
- use the following report format to report student grades:
Student Grade
Sophia: 92.2 A-
Andrew: 89.6 B+
Emma: 85.6 B
Logan: 91.2 A-
*/
int examAssignments = 5;
string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" };
int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 };
int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 };
int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 };
int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 };
int[] studentScores = new int[10];
string currentStudentLetterGrade = "";
// display the header row for scores/grades
Console.Clear();
Console.WriteLine("Student\t\tGrade\tLetter Grade\n");
/*
The outer foreach loop is used to:
- iterate through student names
- assign a student's grades to the studentScores array
- sum assignment scores (inner foreach loop)
- calculate numeric and letter grade
- write the score report information
*/
foreach (string name in studentNames)
{
string currentStudent = name;
if (currentStudent == "Sophia")
studentScores = sophiaScores;
else if (currentStudent == "Andrew")
studentScores = andrewScores;
else if (currentStudent == "Emma")
studentScores = emmaScores;
else if (currentStudent == "Logan")
studentScores = loganScores;
int sumAssignmentScores = 0;
decimal currentStudentGrade = 0;
int gradedAssignments = 0;
/*
the inner foreach loop sums assignment scores
extra credit assignments are worth 10% of an exam score
*/
foreach (int score in studentScores)
{
gradedAssignments += 1;
if (gradedAssignments <= examAssignments)
sumAssignmentScores += score;
else
sumAssignmentScores += score / 10;
}
currentStudentGrade = (decimal)(sumAssignmentScores) / examAssignments;
if (currentStudentGrade >= 97)
currentStudentLetterGrade = "A+";
else if (currentStudentGrade >= 93)
currentStudentLetterGrade = "A";
else if (currentStudentGrade >= 90)
currentStudentLetterGrade = "A-";
else if (currentStudentGrade >= 87)
currentStudentLetterGrade = "B+";
else if (currentStudentGrade >= 83)
currentStudentLetterGrade = "B";
else if (currentStudentGrade >= 80)
currentStudentLetterGrade = "B-";
else if (currentStudentGrade >= 77)
currentStudentLetterGrade = "C+";
else if (currentStudentGrade >= 73)
currentStudentLetterGrade = "C";
else if (currentStudentGrade >= 70)
currentStudentLetterGrade = "C-";
else if (currentStudentGrade >= 67)
currentStudentLetterGrade = "D+";
else if (currentStudentGrade >= 63)
currentStudentLetterGrade = "D";
else if (currentStudentGrade >= 60)
currentStudentLetterGrade = "D-";
else
currentStudentLetterGrade = "F";
// Student Grade
// Sophia: 92.2 A-
Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}");
}
// required for running in VS Code (keeps the Output windows open to view results)
Console.WriteLine("\n\rPress the Enter key to continue");
Console.ReadLine();

View file

@ -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>

View 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.

View file

@ -0,0 +1,2 @@
# Challenge-project-foreach-if-array-CSharp
Starter and Solution code for the Challenge project: "Develop foreach and if-elseif-else structures to process array data in C#" from the Microsoft Learn collection "Getting started with C#"

View file

@ -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>

View file

@ -0,0 +1,118 @@
/*
This C# console application is designed to:
- Use arrays to store student names and assignment scores.
- Use a `foreach` statement to iterate through the student names as an outer program loop.
- Use an `if` statement within the outer loop to identify the current student name and access that student's assignment scores.
- Use a `foreach` statement within the outer loop to iterate though the assignment scores array and sum the values.
- Use an algorithm within the outer loop to calculate the average exam score for each student.
- Use an `if-elseif-else` construct within the outer loop to evaluate the average exam score and assign a letter grade automatically.
- Integrate extra credit scores when calculating the student's final score and letter grade as follows:
- detects extra credit assignments based on the number of elements in the student's scores array.
- divides the values of extra credit assignments by 10 before adding extra credit scores to the sum of exam scores.
- use the following report format to report student grades:
Student Grade
Sophia: 92.2 A-
Andrew: 89.6 B+
Emma: 85.6 B
Logan: 91.2 A-
*/
int examAssignments = 5;
string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" };
int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 };
int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 };
int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 };
int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 };
int[] studentScores = new int[10];
string currentStudentLetterGrade = "";
// display the header row for scores/grades
Console.Clear();
Console.WriteLine("Student\t\tGrade\n");
/*
The outer foreach loop is used to:
- iterate through student names
- assign a student's grades to the studentScores array
- sum assignment scores (inner foreach loop)
- calculate numeric and letter grade
- write the score report information
*/
foreach (string name in studentNames)
{
string currentStudent = name;
if (currentStudent == "Sophia")
studentScores = sophiaScores;
else if (currentStudent == "Andrew")
studentScores = andrewScores;
else if (currentStudent == "Emma")
studentScores = emmaScores;
else if (currentStudent == "Logan")
studentScores = loganScores;
int sumAssignmentScores = 0;
decimal currentStudentGrade = 0;
int gradedAssignments = 0;
/*
the inner foreach loop sums assignment scores
extra credit assignments are worth 10% of an exam score
*/
foreach (int score in studentScores)
{
gradedAssignments += 1;
if (gradedAssignments <= examAssignments)
sumAssignmentScores += score;
else
sumAssignmentScores += score / 10;
}
currentStudentGrade = (decimal)(sumAssignmentScores) / examAssignments;
if (currentStudentGrade >= 97)
currentStudentLetterGrade = "A+";
else if (currentStudentGrade >= 93)
currentStudentLetterGrade = "A";
else if (currentStudentGrade >= 90)
currentStudentLetterGrade = "A-";
else if (currentStudentGrade >= 87)
currentStudentLetterGrade = "B+";
else if (currentStudentGrade >= 83)
currentStudentLetterGrade = "B";
else if (currentStudentGrade >= 80)
currentStudentLetterGrade = "B-";
else if (currentStudentGrade >= 77)
currentStudentLetterGrade = "C+";
else if (currentStudentGrade >= 73)
currentStudentLetterGrade = "C";
else if (currentStudentGrade >= 70)
currentStudentLetterGrade = "C-";
else if (currentStudentGrade >= 67)
currentStudentLetterGrade = "D+";
else if (currentStudentGrade >= 63)
currentStudentLetterGrade = "D";
else if (currentStudentGrade >= 60)
currentStudentLetterGrade = "D-";
else
currentStudentLetterGrade = "F";
// Student Grade
// Sophia: 92.2 A-
Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}");
}
// required for running in VS Code (keeps the Output windows open to view results)
Console.WriteLine("\n\rPress the Enter key to continue");
Console.ReadLine();

View file

@ -0,0 +1,57 @@
using System;
// initialize variables - graded assignments
int currentAssignments = 5;
int sophia1 = 90;
int sophia2 = 86;
int sophia3 = 87;
int sophia4 = 98;
int sophia5 = 100;
int andrew1 = 92;
int andrew2 = 89;
int andrew3 = 81;
int andrew4 = 96;
int andrew5 = 90;
int emma1 = 90;
int emma2 = 85;
int emma3 = 87;
int emma4 = 98;
int emma5 = 68;
int logan1 = 90;
int logan2 = 95;
int logan3 = 87;
int logan4 = 88;
int logan5 = 96;
int sophiaSum = 0;
int andrewSum = 0;
int emmaSum = 0;
int loganSum = 0;
decimal sophiaScore;
decimal andrewScore;
decimal emmaScore;
decimal loganScore;
sophiaSum = sophia1 + sophia2 + sophia3 + sophia4 + sophia5;
andrewSum = andrew1 + andrew2 + andrew3 + andrew4 + andrew5;
emmaSum = emma1 + emma2 + emma3 + emma4 + emma5;
loganSum = logan1 + logan2 + logan3 + logan4 + logan5;
sophiaScore = (decimal)sophiaSum / currentAssignments;
andrewScore = (decimal)andrewSum / currentAssignments;
emmaScore = (decimal)emmaSum / currentAssignments;
loganScore = (decimal)loganSum / currentAssignments;
Console.WriteLine("Student\t\tGrade\n");
Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-");
Console.WriteLine("Andrew:\t\t" + andrewScore + "\tB+");
Console.WriteLine("Emma:\t\t" + emmaScore + "\tB");
Console.WriteLine("Logan:\t\t" + loganScore + "\tA-");
Console.WriteLine("Press the Enter key to continue");
Console.ReadLine();

View file

@ -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>

View 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.

View file

@ -0,0 +1,2 @@
# Guided-project-branching-looping-CSharp
Starter and Solution code for the Guided project: "Develop conditional branching and looping structures in C#" from the Microsoft Learn collection "Getting started with C#"

View file

@ -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>

View file

@ -0,0 +1,319 @@
using System;
// 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 animals age");
Console.WriteLine(" 6. Edit an animals 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 animals 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 animals 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");

View file

@ -0,0 +1,102 @@
// 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 animals age");
Console.WriteLine(" 6. Edit an animals 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();

View file

@ -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>

View file

@ -4,11 +4,14 @@ In this C# Crash Course, we'll go over the basics of C# so that you'll be ready
If you're completely new to C# and want a more comprehensive learning path, check out our [C# Curriculum](https://aka.ms/selfguidedcsharp). The projects included in that curriculum are listed below. You can open this repository as a Codespace to complete those projects.
| | Lesson | Guided Project | Challenge Project |
| :-: | :------------------------------------------------------: | ----------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------: |
| | Lesson | Guided Project | Challenge Project |
| :-: | :------------------------------------------------------: | ----------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------: |
| 01 | [Write your first code using C#](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-1/) | [Calculate and print student grades](https://learn.microsoft.com/en-us/training/modules/guided-project-calculate-print-student-grades/) | [Calculate final GPA](https://learn.microsoft.com/en-us/training/modules/guided-project-calculate-final-gpa/)
| 02 | [Create and run simple C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-2/) | [Develop foreach and if-elseif-else structures to process array data in C#](https://learn.microsoft.com/training/modules/guided-project-arrays-iteration-selection/), [Code]() | [Develop foreach and if-elseif-else structures to process array data in C#](https://learn.microsoft.com/training/modules/challenge-project-arrays-iteration-selection/), [Code]()
| 03 | [Add logic to C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-3/) | [Develop conditional branching and looping structures in C#](https://learn.microsoft.com/training/modules/guided-project-develop-conditional-branching-looping/), [Code]() | [Develop branching and looping structures in C#](https://learn.microsoft.com/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/), [Code]()
| 02 | [Create and run simple C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-2/) | [Develop foreach and if-elseif-else structures to process array data in C#](https://learn.microsoft.com/training/modules/guided-project-arrays-iteration-selection/), [Code](1-projects/guided-project) | [Develop foreach and if-elseif-else structures to process array data in C#](https://learn.microsoft.com/training/modules/challenge-project-arrays-iteration-selection/), [Code](1-projects/challenge-project)
| 03 | [Add logic to C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-3/) | [Develop conditional branching and looping structures in C#](https://learn.microsoft.com/training/modules/guided-project-develop-conditional-branching-looping/), [Code](2-projects/projects) | [Develop branching and looping structures in C#](https://learn.microsoft.com/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/), [Code]()
| 04 | [Work with variable data in C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-4/) | [Work with variable data in C#](https://learn.microsoft.com/training/modules/guided-project-work-variable-data-c-sharp/), [Code]() | [Challenge - Work with variable data in C#](https://learn.microsoft.com/training/modules/challenge-project-work-variable-data-c-sharp/), [Code]()
| 05 | [Create methods in C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-5/) | [Plan a petting zoo visit](https://learn.microsoft.com/training/modules/guided-project-visit-petting-zoo/), [Code]() | [Create a mini-game](https://learn.microsoft.com/training/modules/challenge-project-create-mini-game/), [Code]()
| 06 | [Debug C# console applications](https://learn.microsoft.com/training/paths/get-started-c-sharp-part-6/) | [Debug and handle exceptions in a C# console application using Visual Studio Code](https://learn.microsoft.com/training/modules/guided-project-debug-handle-exceptions-c-sharp-console-application/), [Code]() | [Challenge - Debug a C# console application](https://learn.microsoft.com/training/modules/challenge-project-debug-c-sharp-console-application/), [Code]()
## Topics you'll learn