diff --git a/ContosoApp/DisplayPets.cs b/ContosoApp/DisplayPets.cs deleted file mode 100644 index 83a1d57..0000000 --- a/ContosoApp/DisplayPets.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ContosoApp -{ - internal class DisplayPets - { - public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); - } -} diff --git a/ContosoApp/NewBusinessLogic.cs b/ContosoApp/NewBusinessLogic.cs deleted file mode 100644 index d18a2d0..0000000 --- a/ContosoApp/NewBusinessLogic.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ContosoApp -{ - internal class NewBusinessLogic - { - private PetsRepository? _repo; - private DisplayPets? _stdout; - - // Constructor to initialize _repo and _stdout - public NewBusinessLogic(PetsRepository repo, DisplayPets stdout) - { - _repo = repo; - _stdout = stdout; - } - - public void ProvideUserAllItems() - { - if (_repo == null || _stdout == null) return; - - foreach (var animal in _repo.GetAllPets()) - { - _stdout.DisplayPetInformation(animal); - } - } - - /*public Pet ProvideUserAllItems() - { - if (_repo == null) return null; - if (_stdout == null) return null; - - foreach(Pet animal in _repo.GetAllPets()) - { - _stdout.DisplayPetInformation(animal); - return animal; - } - return null; - }*/ - - } -} diff --git a/ContosoApp/Pet.cs b/ContosoApp/Pet.cs index 6d750cd..d4a1ccb 100644 --- a/ContosoApp/Pet.cs +++ b/ContosoApp/Pet.cs @@ -1,22 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +public sealed class Pet { - public class Pet - { - // Defining private properties to encapsulate the data accessibility - public string? AnimalID { get; set; } - public string? AnimalSpecies { get; set; } - public string? AnimalAge { get; set; } - public string? AnimalPhysicalDescription { get; set; } - public string? AnimalPersonalityDescription { get; set; } - public string? AnimalNickname { get; set; } + public int? AnimalAge { get; set; } + public string? AnimalId { get; set; } + public string? AnimalNickname { get; set; } + public string? AnimalPersonalityDescription { get; set; } + public string? AnimalPhysicalDescription { get; set; } + public string? AnimalSpecies { get; set; } - public override string ToString() => $"{AnimalID} {AnimalSpecies} {AnimalAge} {AnimalPhysicalDescription} {AnimalPersonalityDescription} {AnimalNickname}"; - - } + public override string ToString() + => $"{AnimalId} {AnimalSpecies} {AnimalAge} {AnimalPhysicalDescription} {AnimalPersonalityDescription} {AnimalNickname}"; } diff --git a/ContosoApp/PetID.cs b/ContosoApp/PetID.cs index e77547e..12c8901 100644 --- a/ContosoApp/PetID.cs +++ b/ContosoApp/PetID.cs @@ -1,10 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp -{ - public record struct PetWithID(string Id, Pet Pet); -} +public record struct PetWithID(string Id, Pet Pet); diff --git a/ContosoApp/PetManager.cs b/ContosoApp/PetManager.cs deleted file mode 100644 index bcd049d..0000000 --- a/ContosoApp/PetManager.cs +++ /dev/null @@ -1,297 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ContosoApp -{ - internal class PetManager - { - string? readResult = ""; - bool validEntry = false; - int petAge = 0; - - // Defining a method that creates animal species - public void CreatePet(Pet pet) - { - // Prompt the user to enter the following animal species a dog, cat, rabbit - do - { - Console.WriteLine("\n\rEnter 'dog' or 'cat' or 'rabbit' to begin a new entry"); - readResult = Console.ReadLine(); - if (readResult != null) - { - pet.AnimalSpecies = readResult.ToLower(); - MakePet(); - //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; - } - - } while (validEntry == false); - - // get the pet's age. can be ? at initial entry. - do - { - Console.WriteLine("Enter the pet's age or enter ? if unknown"); - readResult = Console.ReadLine(); - - if (readResult != null) - { - pet.AnimalAge = readResult; - if (pet.AnimalAge != "?") - { - validEntry = int.TryParse(pet.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, houebroken): "); - readResult = Console.ReadLine(); - if (readResult != null) - { - pet.AnimalPhysicalDescription = readResult.ToLower(); - if (pet.AnimalPhysicalDescription == "") - { - pet.AnimalPhysicalDescription = "tbd"; - } - } - - } while (pet.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) - { - pet.AnimalPersonalityDescription = readResult.ToLower(); - if (pet.AnimalPersonalityDescription == "") - { - pet.AnimalPersonalityDescription = "tbd"; - } - } - - } while (pet.AnimalPersonalityDescription == ""); - - // get the pet's nickname. animalNickname can be blank. - do - { - // User input for nickname - Console.WriteLine("Enter a nickname for the pet"); - readResult = Console.ReadLine(); - if (readResult != null) - { - pet.AnimalNickname = readResult.ToLower(); - if (pet.AnimalNickname == "") - { - pet.AnimalNickname = "tbd"; - } - } - - } while (pet.AnimalNickname == ""); - } - - // Defining a method to take user input for physical description if it's left empty - - /****** soon *******/ - - - public PetsRepository EditCompleteAge(Pet age, PetsRepository petRepository) - { - // Searching for a specific pet by ID - //PetsRepository petRepository = new PetsRepository(); - - do - { - Console.WriteLine("Enter pet ID: "); - readResult = Console.ReadLine(); - // deleted the extra pet object - petRepository.FindPetById(readResult); - - Console.WriteLine("Complete pet age: "); - readResult = Console.ReadLine(); - - if (readResult != null) - { - age.AnimalAge = readResult; - - if (age.AnimalAge == null || age.AnimalAge == "?") - { - validEntry = int.TryParse(age.AnimalAge, out petAge); // Modified - } - - else - { - validEntry = true; - } - } - - return petRepository; - - } while (validEntry == false); - - /* do - { - Console.WriteLine("Complete pet age: "); - readResult= Console.ReadLine(); - - if (readResult != null) - { - validEntry = int.TryParse(readResult, out petAge); - } - - } while (validEntry == false);*/ - } - - public void MakePet() - { - var allowedSpecies = new[] { "dog", "cat", "rabbit" }; - validEntry = allowedSpecies.Contains(readResult); - } - } -} - -/* - // Defining a method that creates animal species - public void CreatePet() - { - string? animalSpecies = ""; - string? animalAge = ""; - string? animalPhysicalDescription = ""; - string? animalPersonalityDescription = ""; - string? animalNickname = ""; - - // Prompt the user to enter the following animal species a dog, cat, rabbit - do - { - Console.WriteLine("\n\rEnter 'dog' or 'cat' or 'rabbit' to begin a new entry"); - readResult = Console.ReadLine(); - if (readResult != null) - { - animalSpecies = readResult.ToLower(); - if (animalSpecies != "dog" && animalSpecies != "cat" && animalSpecies != "rabbit") - { - validEntry = false; - } - else - { - validEntry = true; - } - } - - } while (validEntry == false); - - // get the pet's age. can be ? at initial entry. - do - { - 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, houebroken): "); - 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 - { - // User input for nickname - Console.WriteLine("Enter a nickname for the pet"); - readResult = Console.ReadLine(); - if (readResult != null) - { - animalNickname = readResult.ToLower(); - if (animalNickname == "") - { - animalNickname = "tbd"; - } - } - - } while (animalNickname == ""); - Pet pet = new Pet(animalSpecies, animalAge, animalPhysicalDescription, animalPersonalityDescription, animalNickname); - } - - public void CompletePetAge() - { - - } - - - // Defining a method to take user input for physical description if it's left empty - public PetsRepository EditCompleteDescriptions() - { - do - { - Console.WriteLine("Enter pet ID: "); - readResult = Console.ReadLine(); - - if (readResult != null) - { - validEntry = int.TryParse(readResult, out petAge); - } - - PetsRepository petID = new PetsRepository(); - foreach (var ID in petID.GetAllID()) - { - if (readResult == ID) - { - Console.WriteLine($"The animal ID is: {ID}"); - return petID; - } - } - return null; - - } while (validEntry == false); - } -*/ \ No newline at end of file diff --git a/ContosoApp/PetShopConsoleFrontend.cs b/ContosoApp/PetShopConsoleFrontend.cs new file mode 100644 index 0000000..c8dd69f --- /dev/null +++ b/ContosoApp/PetShopConsoleFrontend.cs @@ -0,0 +1,153 @@ +namespace ContosoApp; + +public sealed class PetShopConsoleFrontend +{ + public Pet CreatePet() + { + string readResult; + bool validEntry = false; + var newPet = new Pet(); + + do + { + Console.WriteLine("\n\rEnter 'dog' or 'cat' or 'rabbit' to begin a new entry"); + readResult = Console.ReadLine(); + if (readResult != null) + { + newPet.AnimalSpecies = readResult.ToLower(); + validEntry = Validator.IsValidSpecies(readResult); + } + } while (validEntry == false); + + // get the pet's age. can be ? at initial entry. + do + { + Console.WriteLine("Enter the pet's age or enter ? if unknown"); + readResult = Console.ReadLine(); + + if (readResult == null) + { + continue; + } + + switch (readResult) + { + case "?": + newPet.AnimalAge = null; + validEntry = true; + break; + + case string when int.TryParse(readResult, out var petAge): + newPet.AnimalAge = petAge; + validEntry = true; + break; + } + } 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, houebroken): "); + readResult = Console.ReadLine(); + if (readResult != null) + { + newPet.AnimalPhysicalDescription = readResult.ToLower(); + if (newPet.AnimalPhysicalDescription == "") + { + newPet.AnimalPhysicalDescription = "tbd"; + } + } + + } while (newPet.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) + { + newPet.AnimalPersonalityDescription = readResult.ToLower(); + if (newPet.AnimalPersonalityDescription == "") + { + newPet.AnimalPersonalityDescription = "tbd"; + } + } + + } while (newPet.AnimalPersonalityDescription == ""); + + // get the pet's nickname. animalNickname can be blank. + do + { + // User input for nickname + Console.WriteLine("Enter a nickname for the pet"); + readResult = Console.ReadLine(); + if (readResult != null) + { + newPet.AnimalNickname = readResult.ToLower(); + if (newPet.AnimalNickname == "") + { + newPet.AnimalNickname = "tbd"; + } + } + + } while (newPet.AnimalNickname == ""); + + return newPet; + } + + public void DisplayPetInformation(Pet pet) + => Console.WriteLine(pet.ToString()); + + public Guid FindPetById() + { + Guid parsed; + string readResult; + + do + { + Console.WriteLine("Enter pet ID: "); + readResult = Console.ReadLine(); + } while (Guid.TryParse(readResult, out parsed) == false); + + return Guid.Parse(readResult); + } + + public void ProvideUserAllItems(IEnumerable animals) + { + foreach (var animal in animals) + { + DisplayPetInformation(animal); + } + } + + public int? UpdateAge(Pet pet) + { + Console.WriteLine($"Complete age for pet '{pet.AnimalNickname}': "); + + int? newAge = null; + bool valid = false; + + do + { + var readResult = Console.ReadLine(); + + switch (readResult) + { + case null: + case "?": + newAge = null; + valid = true; + break; + + case string when int.TryParse(readResult, out var petAge): + newAge = petAge; + valid = true; + break; + } + } + while (false == valid); + + return newAge; + } +} diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs new file mode 100644 index 0000000..7fab613 --- /dev/null +++ b/ContosoApp/PetShopProgram.cs @@ -0,0 +1,95 @@ +namespace ContosoApp; + +public sealed class PetShopProgram +{ + private readonly PetShopConsoleFrontend _frontendConsole = new(); + private readonly PetsRepository _petsRepository = new(); + + public void Run() + { + string? menuSelection = ""; + string readResult; + + do + { + Console.Clear(); + + Console.WriteLine(""" + Welcome to the Contoso PetFriends app. Your main menu options are: + 1. List all of our current pet information + 2. Add a new animal friend to the ourAnimals dictionary + 3. Ensure animal ages and physical descriptions are complete + 4. Ensure animal nicknames and personality descriptions are complete + 5. Edit an animal’s age + 6. Edit an animal’s personality description + 7. Display all cats with a specified characteristic + "8. Display all dogs with a specified characteristic + + Enter your selection number (or type Exit to exit the program) + """); + + // display the top-level menu options + + 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. + } + + switch (menuSelection) + { + case "1": + _frontendConsole.ProvideUserAllItems(_petsRepository.GetAllPets()); + Console.WriteLine("Press the Enter key to continue."); + readResult = Console.ReadLine(); + break; + + case "2": + var newPet = _frontendConsole.CreatePet(); + _petsRepository.AddPet(newPet); + Console.WriteLine("Press the Enter key to continue."); + readResult = Console.ReadLine(); + break; + + case "3": + + break; + + case "4": + + Console.WriteLine("The animal physical descriptions are complete"); + readResult = Console.ReadLine(); + break; + + case "5": + var petId = _frontendConsole.FindPetById(); + var pet = _petsRepository.FindPetById(petId); + var newAge = _frontendConsole.UpdateAge(pet); + _petsRepository.UpdatePet(petId, newAge); + Console.WriteLine("The animal age is complete"); + readResult = Console.ReadLine(); + break; + + case "6": + + break; + + case "7": + + break; + + case "8": + + break; + + default: + + break; + } + + } while (menuSelection != "exit"); + } +} diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 945f6d0..93450b0 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -1,60 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +public sealed class PetsRepository { - internal class PetsRepository //: IEnumerable> - { - // Defining a dictionary to store the animal and its attributes data. - private Dictionary ourPets; + private readonly Dictionary _petsStorage = new(); - public PetsRepository() - { - ourPets = new Dictionary(); - } + public void AddPet(Pet pet) + { + _petsStorage.Add(Guid.NewGuid(), pet); + } - - public IEnumerable GetAllPets() => ourPets.Values; + public Pet? FindPetById(Guid petId) + { + _petsStorage.TryGetValue(petId, out Pet? pet); + return pet; + } - public void AddPet(Pet pet) - { - - //string animalID = GetNextID().ToString(); - ourPets.Add(Guid.NewGuid(), pet); - } + public IEnumerable GetAllPets() + => _petsStorage.Values; - // The business logic - // Defining a method that is resposible mplementing for searching and finding pets by ID - public PetsRepository FindPetById(string petId) - { - //PetsRepository petID = new PetsRepository(); - foreach (var ID in ourPets.Keys) - { - if (ID.ToString() == petId) - { - Console.WriteLine($"The animal ID is: {petId}"); - } - } - return this; - } + public void UpdatePet(Guid petId, int? age) + { + if (false == _petsStorage.TryGetValue(petId, out Pet? pet)) + throw new ArgumentException("Pet not found", nameof(petId)); + pet.AnimalAge = age; } } - -/*public int GetNextID() - { - // Generating an ID for the animals - Guid IDnumber = new Guid(); - int generatedNumber = IDnumber; - return generatedNumber; - }*/ -/*Random IDnumber = Random.Shared; - int generatedNumber = IDnumber.Next(100, 1000); - - return generatedNumber;*/ - -/*string animalID = GetNextID().ToString(); - ourPets?.Add(animalID, pet);*/ \ No newline at end of file diff --git a/ContosoApp/Program.cs b/ContosoApp/Program.cs index 1e27de7..5a47101 100644 --- a/ContosoApp/Program.cs +++ b/ContosoApp/Program.cs @@ -1,103 +1,12 @@ // See https://aka.ms/new-console-template for more information -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +class Program { - class Program + public static void Main(string[] args) { - public static void Main(string[] args) - { - string readResult; - string? menuSelection = ""; - - PetManager parsePet = new PetManager(); - // Add animal friend to the ourAnimals dictionary - Pet pet = new Pet(); - PetsRepository repository = new PetsRepository(); - DisplayPets pets = new DisplayPets(); - NewBusinessLogic allPets = new NewBusinessLogic(repository, pets); - do - { - 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 dictionary"); - 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 animal’s age"); - Console.WriteLine(" 6. Edit an animal’s 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)"); - - // display the top-level menu options - - 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": - allPets.ProvideUserAllItems(); - Console.WriteLine("Press the Enter key to continue."); - readResult = Console.ReadLine(); - break; - - case "2": - repository.AddPet(pet); - parsePet.CreatePet(pet); - Console.WriteLine("Press the Enter key to continue."); - readResult = Console.ReadLine(); - break; - - case "3": - - break; - - case "4": - - Console.WriteLine("The animal physical descriptions are complete"); - readResult = Console.ReadLine(); - break; - - case "5": - parsePet.EditCompleteAge(pet, repository); - Console.WriteLine("The animal age is complete"); - readResult = Console.ReadLine(); - break; - - case "6": - - break; - - case "7": - - break; - - case "8": - - break; - - default: - - break; - } - - } while (menuSelection != "exit"); - } + var psp = new PetShopProgram(); + psp.Run(); } } @@ -109,26 +18,26 @@ public static void Main(string[] args) bool validEntry; if (readResult != null) { - do - { - animalSpecies = readResult.ToLower(); - - switch (animalSpecies) - { - case "dog": - validEntry = true; - break; +do +{ + animalSpecies = readResult.ToLower(); - case "cat": - validEntry = true; - break; - default: - Console.WriteLine("Invalid, input!"); - validEntry = false; - break; - } - break; - } while (validEntry == false); + switch (animalSpecies) + { + case "dog": + validEntry = true; + break; + + case "cat": + validEntry = true; + break; + default: + Console.WriteLine("Invalid, input!"); + validEntry = false; + break; + } + break; +} while (validEntry == false); }*/ /*int petCount = 4; @@ -137,27 +46,27 @@ public static void Main(string[] args) Console.WriteLine($"{petCount} {maxPets - petCount}");*/ /*string[][] jaggedArray = new string[][] { - new string[] { "one1", "two1", "three1", "four1", "five1", "six1" }, - new string[] { "one2", "two2", "three2", "four2", "five2", "six2" }, - new string[] { "one3", "two3", "three3", "four3", "five3", "six3" }, - new string[] { "one4", "two4", "three4", "four4", "five4", "six4" }, - new string[] { "one5", "two5", "three5", "four5", "five5", "six5" }, - new string[] { "one6", "two6", "three6", "four6", "five6", "six6" }, - new string[] { "one7", "two7", "three7", "four7", "five7", "six7" }, - new string[] { "one8", "two8", "three8", "four8", "five8", "six8" } +new string[] { "one1", "two1", "three1", "four1", "five1", "six1" }, +new string[] { "one2", "two2", "three2", "four2", "five2", "six2" }, +new string[] { "one3", "two3", "three3", "four3", "five3", "six3" }, +new string[] { "one4", "two4", "three4", "four4", "five4", "six4" }, +new string[] { "one5", "two5", "three5", "four5", "five5", "six5" }, +new string[] { "one6", "two6", "three6", "four6", "five6", "six6" }, +new string[] { "one7", "two7", "three7", "four7", "five7", "six7" }, +new string[] { "one8", "two8", "three8", "four8", "five8", "six8" } }; foreach (string[] array in jaggedArray) { - foreach (string value in array) - { - Console.WriteLine(value); - } - Console.WriteLine(); +foreach (string value in array) +{ + Console.WriteLine(value); +} +Console.WriteLine(); }*/ /*try { - Test(); +Test(); } catch (Exception ex) { @@ -170,25 +79,25 @@ public static void Main(string[] args) /*public static async void Test() { - throw new Exception(); +throw new Exception(); }*/ /* public static bool ValidateIPAddress(string? IP) - { - List collection = new List(); +{ + List collection = new List(); - foreach (var item in collection) + foreach (var item in collection) + { + if (item.Length != 4) { - if (item.Length != 4) - { - Console.WriteLine("Please enter less than 4 digits!"); - return false; - } - else - { - collection.Add(item); - } + Console.WriteLine("Please enter less than 4 digits!"); + return false; + } + else + { + collection.Add(item); } - - return true; } +/ + return true; +} */ \ No newline at end of file diff --git a/ContosoApp/Validator.cs b/ContosoApp/Validator.cs new file mode 100644 index 0000000..6a00fdf --- /dev/null +++ b/ContosoApp/Validator.cs @@ -0,0 +1,9 @@ +namespace ContosoApp; + +public sealed class Validator +{ + static readonly private string[] AllowedSpecies = new[] { "dog", "cat", "rabbit" }; + + public static bool IsValidSpecies(string readResult) + => AllowedSpecies.Contains(readResult); +} diff --git a/TestingCode/Program.cs b/TestingCode/Program.cs index 1e497c7..df64474 100644 --- a/TestingCode/Program.cs +++ b/TestingCode/Program.cs @@ -122,7 +122,7 @@ public void CompletePetAge(Pet pet) pet.AnimalAge = readResult.Trim().ToLower(); validEntry = int.TryParse(pet.AnimalAge, out petAge); } - + } while (!validEntry); } /* else if (string.IsNullOrEmpty(pet.AnimalAge)) @@ -139,7 +139,7 @@ public void DisplayAge(Pet pet) foreach (var age in ages) { Console.WriteLine($"The animal complete age is {ages[0]}"); - } + } } }*/ @@ -157,7 +157,7 @@ class Program { static void Main(string[] args) { - + // ************* Exercise - Complete a challenge activity to improve code readability ************* /* The following code has a message and the letters are being reversed @@ -166,27 +166,27 @@ and then dsiplaying the total number */ string originalMessage = "The quick brown fox jumps over the lazy dog."; - + char[] convertedMessage = originalMessage.ToCharArray(); Array.Reverse(convertedMessage); int letterCount = 0; - - foreach (char letter in convertedMessage) - { - if (letter == 'o') + + foreach (char letter in convertedMessage) + { + if (letter == 'o') { letterCount++; - } + } } - + string newMessage = new String(convertedMessage); - + Console.WriteLine(newMessage); Console.WriteLine($"'o' appears {letterCount} times."); Pet pet = new Pet(); - pet. + //pet. } // ************* Exercise - Use whitespace to make your code easier to read ************* @@ -236,7 +236,7 @@ and then dsiplaying the total number /* The following code creates five random OrderIDs - to test the fraud detection process. OrderIDs + to test the fraud detection process. OrderIDs consist of a letter from A to E, and a three digit number. Ex. A123. */ @@ -385,7 +385,7 @@ public class Pet Console.WriteLine($"Renew now and save {10}%!"); } - else if (daysUntilExpiration <= 10) + else if (daysUntilExpiration <= 10) { Console.WriteLine("Your subscription will expire soon. Renew now!"); @@ -396,7 +396,7 @@ public class Pet Console.WriteLine(); } - else + else { Console.WriteLine("Your subscription has expired."); }*/ @@ -464,7 +464,7 @@ public class Pet { Console.WriteLine("You won a trip"); } - else + else { Console.WriteLine("You won a kitten"); }*/ @@ -768,7 +768,7 @@ public static void Main(string[] args) // Retrieving the property causes the 'get' access // or to be called. - /* Console.WriteLine($"Time in hours: {t.Hours}"); + /* Console.WriteLine($"Time in hours: {t.Hours}"); } }*/ // The example displays the following output: