From 6bad545407daf0702bec743d63fb6952c6d7b0a0 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 20:50:05 +0200 Subject: [PATCH 01/11] format/spaces --- ContosoApp/Pet.cs | 1 - ContosoApp/PetManager.cs | 35 +++++++++++++++++------------------ ContosoApp/PetsRepository.cs | 21 +++++++++------------ ContosoApp/Program.cs | 8 ++++---- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/ContosoApp/Pet.cs b/ContosoApp/Pet.cs index 6d750cd..56273d3 100644 --- a/ContosoApp/Pet.cs +++ b/ContosoApp/Pet.cs @@ -17,6 +17,5 @@ public class Pet public string? AnimalNickname { get; set; } public override string ToString() => $"{AnimalID} {AnimalSpecies} {AnimalAge} {AnimalPhysicalDescription} {AnimalPersonalityDescription} {AnimalNickname}"; - } } diff --git a/ContosoApp/PetManager.cs b/ContosoApp/PetManager.cs index bcd049d..881f159 100644 --- a/ContosoApp/PetManager.cs +++ b/ContosoApp/PetManager.cs @@ -11,7 +11,7 @@ internal class PetManager string? readResult = ""; bool validEntry = false; int petAge = 0; - + // Defining a method that creates animal species public void CreatePet(Pet pet) { @@ -26,7 +26,7 @@ public void CreatePet(Pet pet) MakePet(); //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; } - + } while (validEntry == false); // get the pet's age. can be ? at initial entry. @@ -101,10 +101,9 @@ public void CreatePet(Pet pet) } // Defining a method to take user input for physical description if it's left empty - - /****** soon *******/ - - + + /****** soon *******/ + public PetsRepository EditCompleteAge(Pet age, PetsRepository petRepository) { // Searching for a specific pet by ID @@ -138,18 +137,18 @@ public PetsRepository EditCompleteAge(Pet age, PetsRepository petRepository) return petRepository; } while (validEntry == false); - - /* do - { - Console.WriteLine("Complete pet age: "); - readResult= Console.ReadLine(); - if (readResult != null) - { - validEntry = int.TryParse(readResult, out petAge); - } + /* do + { + Console.WriteLine("Complete pet age: "); + readResult= Console.ReadLine(); - } while (validEntry == false);*/ + if (readResult != null) + { + validEntry = int.TryParse(readResult, out petAge); + } + + } while (validEntry == false);*/ } public void MakePet() @@ -264,10 +263,10 @@ public void CreatePet() public void CompletePetAge() { - + } - + // Defining a method to take user input for physical description if it's left empty public PetsRepository EditCompleteDescriptions() { diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 945f6d0..c93bac5 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -16,14 +16,12 @@ public PetsRepository() ourPets = new Dictionary(); } - public IEnumerable GetAllPets() => ourPets.Values; public void AddPet(Pet pet) { - - //string animalID = GetNextID().ToString(); - ourPets.Add(Guid.NewGuid(), pet); + //string animalID = GetNextID().ToString(); + ourPets.Add(Guid.NewGuid(), pet); } // The business logic @@ -40,21 +38,20 @@ public PetsRepository FindPetById(string petId) } return this; } - } } /*public int GetNextID() - { - // Generating an ID for the animals - Guid IDnumber = new Guid(); - int generatedNumber = IDnumber; - return generatedNumber; - }*/ +{ +// 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 + ourPets?.Add(animalID, pet);*/ \ No newline at end of file diff --git a/ContosoApp/Program.cs b/ContosoApp/Program.cs index 1e27de7..a4a074d 100644 --- a/ContosoApp/Program.cs +++ b/ContosoApp/Program.cs @@ -18,7 +18,7 @@ public static void Main(string[] args) // Add animal friend to the ourAnimals dictionary Pet pet = new Pet(); PetsRepository repository = new PetsRepository(); - DisplayPets pets = new DisplayPets(); + DisplayPets pets = new DisplayPets(); NewBusinessLogic allPets = new NewBusinessLogic(repository, pets); do { @@ -43,8 +43,8 @@ public static void Main(string[] args) { 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 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 @@ -64,7 +64,7 @@ public static void Main(string[] args) break; case "3": - + break; case "4": From 171d4628980493c60d4b7f345afbcbdf3124b30b Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 20:50:47 +0200 Subject: [PATCH 02/11] using and namespaces --- ContosoApp/DisplayPets.cs | 13 +- ContosoApp/NewBusinessLogic.cs | 61 +++-- ContosoApp/Pet.cs | 27 +-- ContosoApp/PetID.cs | 11 +- ContosoApp/PetManager.cs | 421 ++++++++++++++++----------------- ContosoApp/PetsRepository.cs | 65 +++-- ContosoApp/Program.cs | 247 ++++++++++--------- 7 files changed, 398 insertions(+), 447 deletions(-) diff --git a/ContosoApp/DisplayPets.cs b/ContosoApp/DisplayPets.cs index 83a1d57..fdfb3c6 100644 --- a/ContosoApp/DisplayPets.cs +++ b/ContosoApp/DisplayPets.cs @@ -1,13 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +internal class DisplayPets { - internal class DisplayPets - { - public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); - } + public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); } diff --git a/ContosoApp/NewBusinessLogic.cs b/ContosoApp/NewBusinessLogic.cs index d18a2d0..3615222 100644 --- a/ContosoApp/NewBusinessLogic.cs +++ b/ContosoApp/NewBusinessLogic.cs @@ -1,45 +1,38 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +internal class NewBusinessLogic { - internal class NewBusinessLogic + private PetsRepository? _repo; + private DisplayPets? _stdout; + + // Constructor to initialize _repo and _stdout + public NewBusinessLogic(PetsRepository repo, DisplayPets stdout) { - private PetsRepository? _repo; - private DisplayPets? _stdout; + _repo = repo; + _stdout = 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; - public void ProvideUserAllItems() + foreach (var animal in _repo.GetAllPets()) { - if (_repo == null || _stdout == null) return; - - foreach (var animal in _repo.GetAllPets()) - { - _stdout.DisplayPetInformation(animal); - } + _stdout.DisplayPetInformation(animal); } + } - /*public Pet ProvideUserAllItems() - { - if (_repo == null) return null; - if (_stdout == null) return null; + /*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; - }*/ + foreach(Pet animal in _repo.GetAllPets()) + { + _stdout.DisplayPetInformation(animal); + return animal; + } + return null; + }*/ - } } diff --git a/ContosoApp/Pet.cs b/ContosoApp/Pet.cs index 56273d3..25c8018 100644 --- a/ContosoApp/Pet.cs +++ b/ContosoApp/Pet.cs @@ -1,21 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +public 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; } + // 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 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 index 881f159..bd7a727 100644 --- a/ContosoApp/PetManager.cs +++ b/ContosoApp/PetManager.cs @@ -1,296 +1,289 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +internal class PetManager { - internal class PetManager - { - string? readResult = ""; - bool validEntry = false; - int petAge = 0; + string? readResult = ""; + bool validEntry = false; + int petAge = 0; - // Defining a method that creates animal species - public void CreatePet(Pet pet) + // 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 { - // 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) { - 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"; - } + pet.AnimalSpecies = readResult.ToLower(); + MakePet(); + //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; + } - } while (validEntry == false); + } 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(); + // 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) + if (readResult != null) + { + pet.AnimalAge = readResult; + if (pet.AnimalAge != "?") + { + validEntry = int.TryParse(pet.AnimalAge, out petAge); + } + else { - pet.AnimalAge = readResult; - if (pet.AnimalAge != "?") - { - validEntry = int.TryParse(pet.AnimalAge, out petAge); - } - else - { - validEntry = true; - } + validEntry = true; } + } - } while (validEntry == false); + } while (validEntry == false); - // get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank. - do + // 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) { - 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 = readResult.ToLower(); - if (pet.AnimalPhysicalDescription == "") - { - pet.AnimalPhysicalDescription = "tbd"; - } + pet.AnimalPhysicalDescription = "tbd"; } + } - } while (pet.AnimalPhysicalDescription == ""); + } while (pet.AnimalPhysicalDescription == ""); - // get a description of the pet's personality - animalPersonalityDescription can be blank. - do + // 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) { - 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 = readResult.ToLower(); - if (pet.AnimalPersonalityDescription == "") - { - pet.AnimalPersonalityDescription = "tbd"; - } + pet.AnimalPersonalityDescription = "tbd"; } + } - } while (pet.AnimalPersonalityDescription == ""); + } while (pet.AnimalPersonalityDescription == ""); - // get the pet's nickname. animalNickname can be blank. - do + // 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) { - // 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 = readResult.ToLower(); - if (pet.AnimalNickname == "") - { - pet.AnimalNickname = "tbd"; - } + pet.AnimalNickname = "tbd"; } + } - } while (pet.AnimalNickname == ""); - } + } while (pet.AnimalNickname == ""); + } + + // Defining a method to take user input for physical description if it's left empty - // Defining a method to take user input for physical description if it's left empty + /****** soon *******/ - /****** soon *******/ + public PetsRepository EditCompleteAge(Pet age, PetsRepository petRepository) + { + // Searching for a specific pet by ID + //PetsRepository petRepository = new PetsRepository(); - public PetsRepository EditCompleteAge(Pet age, PetsRepository petRepository) + do { - // Searching for a specific pet by ID - //PetsRepository petRepository = new PetsRepository(); + Console.WriteLine("Enter pet ID: "); + readResult = Console.ReadLine(); + // deleted the extra pet object + petRepository.FindPetById(readResult); - 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(); - Console.WriteLine("Complete pet age: "); - readResult = Console.ReadLine(); + if (readResult != null) + { + age.AnimalAge = readResult; - if (readResult != null) + if (age.AnimalAge == null || age.AnimalAge == "?") { - age.AnimalAge = readResult; - - if (age.AnimalAge == null || age.AnimalAge == "?") - { - validEntry = int.TryParse(age.AnimalAge, out petAge); // Modified - } + validEntry = int.TryParse(age.AnimalAge, out petAge); // Modified + } - else - { - validEntry = true; - } + else + { + validEntry = true; } + } - return petRepository; + return petRepository; - } while (validEntry == false); + } while (validEntry == false); - /* do - { - Console.WriteLine("Complete pet age: "); - readResult= Console.ReadLine(); + /* do + { + Console.WriteLine("Complete pet age: "); + readResult= Console.ReadLine(); - if (readResult != null) - { - validEntry = int.TryParse(readResult, out petAge); - } + if (readResult != null) + { + validEntry = int.TryParse(readResult, out petAge); + } - } while (validEntry == false);*/ - } + } while (validEntry == false);*/ + } - public void MakePet() - { - var allowedSpecies = new[] { "dog", "cat", "rabbit" }; - validEntry = allowedSpecies.Contains(readResult); - } + public void MakePet() + { + var allowedSpecies = new[] { "dog", "cat", "rabbit" }; + validEntry = allowedSpecies.Contains(readResult); } } /* - // Defining a method that creates animal species - public void CreatePet() +// 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 { - 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) { - 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 { - animalSpecies = readResult.ToLower(); - if (animalSpecies != "dog" && animalSpecies != "cat" && animalSpecies != "rabbit") - { - validEntry = false; - } - else - { - validEntry = true; - } + validEntry = true; } + } - } while (validEntry == false); + } 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(); + // 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) + if (readResult != null) + { + animalAge = readResult; + if (animalAge != "?") + { + validEntry = int.TryParse(animalAge, out petAge); + } + else { - animalAge = readResult; - if (animalAge != "?") - { - validEntry = int.TryParse(animalAge, out petAge); - } - else - { - validEntry = true; - } + validEntry = true; } + } - } while (validEntry == false); + } while (validEntry == false); - // get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank. - do + // 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) { - 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 = readResult.ToLower(); - if (animalPhysicalDescription == "") - { - animalPhysicalDescription = "tbd"; - } + animalPhysicalDescription = "tbd"; } + } - } while (animalPhysicalDescription == ""); + } while (animalPhysicalDescription == ""); - // get a description of the pet's personality - animalPersonalityDescription can be blank. - do + // 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) { - 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 = readResult.ToLower(); - if (animalPersonalityDescription == "") - { - animalPersonalityDescription = "tbd"; - } + animalPersonalityDescription = "tbd"; } + } - } while (animalPersonalityDescription == ""); + } while (animalPersonalityDescription == ""); - // get the pet's nickname. animalNickname can be blank. - do + // 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) { - // User input for nickname - Console.WriteLine("Enter a nickname for the pet"); - readResult = Console.ReadLine(); - if (readResult != null) + animalNickname = readResult.ToLower(); + if (animalNickname == "") { - animalNickname = readResult.ToLower(); - if (animalNickname == "") - { - animalNickname = "tbd"; - } + animalNickname = "tbd"; } + } - } while (animalNickname == ""); - Pet pet = new Pet(animalSpecies, animalAge, animalPhysicalDescription, animalPersonalityDescription, animalNickname); - } + } while (animalNickname == ""); + Pet pet = new Pet(animalSpecies, animalAge, animalPhysicalDescription, animalPersonalityDescription, animalNickname); + } - public void CompletePetAge() - { + public void CompletePetAge() + { - } + } - // Defining a method to take user input for physical description if it's left empty - public PetsRepository EditCompleteDescriptions() + // Defining a method to take user input for physical description if it's left empty + public PetsRepository EditCompleteDescriptions() + { + do { - do - { - Console.WriteLine("Enter pet ID: "); - readResult = Console.ReadLine(); + Console.WriteLine("Enter pet ID: "); + readResult = Console.ReadLine(); - if (readResult != null) - { - validEntry = int.TryParse(readResult, out petAge); - } + if (readResult != null) + { + validEntry = int.TryParse(readResult, out petAge); + } - PetsRepository petID = new PetsRepository(); - foreach (var ID in petID.GetAllID()) + PetsRepository petID = new PetsRepository(); + foreach (var ID in petID.GetAllID()) + { + if (readResult == ID) { - if (readResult == ID) - { - Console.WriteLine($"The animal ID is: {ID}"); - return petID; - } + Console.WriteLine($"The animal ID is: {ID}"); + return petID; } - return null; + } + return null; - } while (validEntry == false); - } + } while (validEntry == false); + } */ \ No newline at end of file diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index c93bac5..2c4afc0 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -1,57 +1,50 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ContosoApp; -namespace ContosoApp +internal class PetsRepository //: IEnumerable> { - internal class PetsRepository //: IEnumerable> - { - // Defining a dictionary to store the animal and its attributes data. - private Dictionary ourPets; + // Defining a dictionary to store the animal and its attributes data. + private Dictionary ourPets; - public PetsRepository() - { - ourPets = new Dictionary(); - } + public PetsRepository() + { + ourPets = new Dictionary(); + } - public IEnumerable GetAllPets() => ourPets.Values; + public IEnumerable GetAllPets() => ourPets.Values; - public void AddPet(Pet pet) - { - //string animalID = GetNextID().ToString(); - ourPets.Add(Guid.NewGuid(), pet); - } + public void AddPet(Pet pet) + { + //string animalID = GetNextID().ToString(); + ourPets.Add(Guid.NewGuid(), pet); + } - // The business logic - // Defining a method that is resposible mplementing for searching and finding pets by ID - public PetsRepository FindPetById(string petId) + // 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) { - //PetsRepository petID = new PetsRepository(); - foreach (var ID in ourPets.Keys) + if (ID.ToString() == petId) { - if (ID.ToString() == petId) - { - Console.WriteLine($"The animal ID is: {petId}"); - } + Console.WriteLine($"The animal ID is: {petId}"); } - return this; } + return this; } } /*public int GetNextID() { // Generating an ID for the animals - Guid IDnumber = new Guid(); - int generatedNumber = IDnumber; - return generatedNumber; +Guid IDnumber = new Guid(); +int generatedNumber = IDnumber; +return generatedNumber; }*/ /*Random IDnumber = Random.Shared; - int generatedNumber = IDnumber.Next(100, 1000); +int generatedNumber = IDnumber.Next(100, 1000); - return generatedNumber;*/ +return generatedNumber;*/ /*string animalID = GetNextID().ToString(); - ourPets?.Add(animalID, pet);*/ \ No newline at end of file +ourPets?.Add(animalID, pet);*/ \ No newline at end of file diff --git a/ContosoApp/Program.cs b/ContosoApp/Program.cs index a4a074d..541ff92 100644 --- a/ContosoApp/Program.cs +++ b/ContosoApp/Program.cs @@ -1,103 +1,96 @@ // 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 { - 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) { - 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; + 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 "2": + repository.AddPet(pet); + parsePet.CreatePet(pet); + Console.WriteLine("Press the Enter key to continue."); + readResult = Console.ReadLine(); + break; - case "3": + case "3": - break; + break; - case "4": + case "4": - Console.WriteLine("The animal physical descriptions are complete"); - readResult = Console.ReadLine(); - break; + 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 "5": + parsePet.EditCompleteAge(pet, repository); + Console.WriteLine("The animal age is complete"); + readResult = Console.ReadLine(); + break; - case "6": + case "6": - break; + break; - case "7": + case "7": - break; + break; - case "8": + case "8": - break; + break; - default: + default: - break; - } + break; + } - } while (menuSelection != "exit"); - } + } while (menuSelection != "exit"); } } @@ -109,26 +102,26 @@ public static void Main(string[] args) bool validEntry; if (readResult != null) { - do - { - animalSpecies = readResult.ToLower(); +do +{ + animalSpecies = readResult.ToLower(); - switch (animalSpecies) - { - case "dog": - validEntry = true; - break; - - 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 +130,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 +163,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 From 97b6c063dd6bd4e174e5ebc7f4930b1d861faa7c Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 20:52:14 +0200 Subject: [PATCH 03/11] public sealed --- ContosoApp/DisplayPets.cs | 2 +- ContosoApp/NewBusinessLogic.cs | 2 +- ContosoApp/Pet.cs | 2 +- ContosoApp/PetManager.cs | 2 +- ContosoApp/PetsRepository.cs | 2 +- TestingCode/Program.cs | 34 +++++++++++++++++----------------- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/ContosoApp/DisplayPets.cs b/ContosoApp/DisplayPets.cs index fdfb3c6..497d0d5 100644 --- a/ContosoApp/DisplayPets.cs +++ b/ContosoApp/DisplayPets.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -internal class DisplayPets +public sealed class DisplayPets { public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); } diff --git a/ContosoApp/NewBusinessLogic.cs b/ContosoApp/NewBusinessLogic.cs index 3615222..ed2f4f7 100644 --- a/ContosoApp/NewBusinessLogic.cs +++ b/ContosoApp/NewBusinessLogic.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -internal class NewBusinessLogic +public sealed class NewBusinessLogic { private PetsRepository? _repo; private DisplayPets? _stdout; diff --git a/ContosoApp/Pet.cs b/ContosoApp/Pet.cs index 25c8018..02553d8 100644 --- a/ContosoApp/Pet.cs +++ b/ContosoApp/Pet.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -public class Pet +public sealed class Pet { // Defining private properties to encapsulate the data accessibility public string? AnimalID { get; set; } diff --git a/ContosoApp/PetManager.cs b/ContosoApp/PetManager.cs index bd7a727..1a65b94 100644 --- a/ContosoApp/PetManager.cs +++ b/ContosoApp/PetManager.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -internal class PetManager +public sealed class PetManager { string? readResult = ""; bool validEntry = false; diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 2c4afc0..39a5d7e 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -internal class PetsRepository //: IEnumerable> +public sealed class PetsRepository //: IEnumerable> { // Defining a dictionary to store the animal and its attributes data. private Dictionary ourPets; 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: From 19a82e49f003b9dd526fe5e8ebcad9312ef7228f Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 20:57:42 +0200 Subject: [PATCH 04/11] program is just the static entry point --- ContosoApp/PetShopProgram.cs | 98 ++++++++++++++++++++++++++++++++++++ ContosoApp/Program.cs | 90 ++------------------------------- 2 files changed, 101 insertions(+), 87 deletions(-) create mode 100644 ContosoApp/PetShopProgram.cs diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs new file mode 100644 index 0000000..14e44dc --- /dev/null +++ b/ContosoApp/PetShopProgram.cs @@ -0,0 +1,98 @@ +namespace ContosoApp; + +public sealed class PetShopProgram +{ + private readonly NewBusinessLogic _allPets; + private readonly PetManager _parsePet = new PetManager(); + private string? _menuSelection = ""; + private Pet _pet = new Pet(); + private readonly DisplayPets _pets = new DisplayPets(); + private string _readResult; + private readonly PetsRepository _repository = new PetsRepository(); + + public PetShopProgram() + { + _allPets = new NewBusinessLogic(_repository, _pets); + } + + public void Run() + { + 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"); + } +} diff --git a/ContosoApp/Program.cs b/ContosoApp/Program.cs index 541ff92..5a47101 100644 --- a/ContosoApp/Program.cs +++ b/ContosoApp/Program.cs @@ -5,92 +5,8 @@ class Program { 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(); } } @@ -181,7 +97,7 @@ public static void Main(string[] args) collection.Add(item); } } - +/ return true; } */ \ No newline at end of file From 2be6e390a8a9df7ced4b532556e682856d307076 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 21:01:14 +0200 Subject: [PATCH 05/11] BUG pet state --- ContosoApp/PetShopProgram.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs index 14e44dc..85a4ee1 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -3,12 +3,11 @@ public sealed class PetShopProgram { private readonly NewBusinessLogic _allPets; - private readonly PetManager _parsePet = new PetManager(); + private readonly PetManager _parsePet = new(); private string? _menuSelection = ""; - private Pet _pet = new Pet(); - private readonly DisplayPets _pets = new DisplayPets(); + private readonly DisplayPets _pets = new(); private string _readResult; - private readonly PetsRepository _repository = new PetsRepository(); + private readonly PetsRepository _repository = new(); public PetShopProgram() { @@ -54,8 +53,9 @@ public void Run() break; case "2": - _repository.AddPet(_pet); - _parsePet.CreatePet(_pet); + var newPet = new Pet(); + _repository.AddPet(newPet); + _parsePet.CreatePet(newPet); Console.WriteLine("Press the Enter key to continue."); _readResult = Console.ReadLine(); break; @@ -71,7 +71,8 @@ public void Run() break; case "5": - _parsePet.EditCompleteAge(_pet, _repository); + var incompletePet = new Pet(); + _parsePet.EditCompleteAge(incompletePet, _repository); Console.WriteLine("The animal age is complete"); _readResult = Console.ReadLine(); break; From da630742f916e09180a38ef0aa3e2fe5ff94e2c5 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 21:31:47 +0200 Subject: [PATCH 06/11] renamed field for ui menuSelection and readResult are only used in main function reordered methods; PetsRepository must not be returned --- ContosoApp/NewBusinessLogic.cs | 1 - ContosoApp/PetShopProgram.cs | 34 +++++++++++++++++----------------- ContosoApp/PetsRepository.cs | 8 ++++---- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ContosoApp/NewBusinessLogic.cs b/ContosoApp/NewBusinessLogic.cs index ed2f4f7..2cc026c 100644 --- a/ContosoApp/NewBusinessLogic.cs +++ b/ContosoApp/NewBusinessLogic.cs @@ -34,5 +34,4 @@ public void ProvideUserAllItems() } return null; }*/ - } diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs index 85a4ee1..e0a638c 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -3,19 +3,20 @@ public sealed class PetShopProgram { private readonly NewBusinessLogic _allPets; + private readonly DisplayPets _frontendConsole = new(); private readonly PetManager _parsePet = new(); - private string? _menuSelection = ""; - private readonly DisplayPets _pets = new(); - private string _readResult; - private readonly PetsRepository _repository = new(); + private readonly PetsRepository _petsRepository = new(); public PetShopProgram() { - _allPets = new NewBusinessLogic(_repository, _pets); + _allPets = new NewBusinessLogic(_petsRepository, _frontendConsole); } public void Run() { + string? menuSelection = ""; + string readResult; + do { Console.Clear(); @@ -34,30 +35,29 @@ public void Run() // display the top-level menu options - _readResult = Console.ReadLine(); - if (_readResult != null) + readResult = Console.ReadLine(); + if (readResult != null) { - _menuSelection = _readResult.ToLower(); + 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) + switch (menuSelection) { case "1": _allPets.ProvideUserAllItems(); Console.WriteLine("Press the Enter key to continue."); - _readResult = Console.ReadLine(); + readResult = Console.ReadLine(); break; case "2": var newPet = new Pet(); - _repository.AddPet(newPet); + _petsRepository.AddPet(newPet); _parsePet.CreatePet(newPet); Console.WriteLine("Press the Enter key to continue."); - _readResult = Console.ReadLine(); + readResult = Console.ReadLine(); break; case "3": @@ -67,14 +67,14 @@ public void Run() case "4": Console.WriteLine("The animal physical descriptions are complete"); - _readResult = Console.ReadLine(); + readResult = Console.ReadLine(); break; case "5": var incompletePet = new Pet(); - _parsePet.EditCompleteAge(incompletePet, _repository); + _parsePet.EditCompleteAge(incompletePet, _petsRepository); Console.WriteLine("The animal age is complete"); - _readResult = Console.ReadLine(); + readResult = Console.ReadLine(); break; case "6": @@ -94,6 +94,6 @@ public void Run() break; } - } while (_menuSelection != "exit"); + } while (menuSelection != "exit"); } } diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 39a5d7e..6e8234c 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -10,8 +10,6 @@ public PetsRepository() ourPets = new Dictionary(); } - public IEnumerable GetAllPets() => ourPets.Values; - public void AddPet(Pet pet) { //string animalID = GetNextID().ToString(); @@ -20,7 +18,7 @@ public void AddPet(Pet pet) // The business logic // Defining a method that is resposible mplementing for searching and finding pets by ID - public PetsRepository FindPetById(string petId) + public void FindPetById(string petId) { //PetsRepository petID = new PetsRepository(); foreach (var ID in ourPets.Keys) @@ -30,8 +28,10 @@ public PetsRepository FindPetById(string petId) Console.WriteLine($"The animal ID is: {petId}"); } } - return this; } + + public IEnumerable GetAllPets() + => ourPets.Values; } /*public int GetNextID() From 6e5ad062d497a8aee52c889265be7417e697e44a Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 21:40:16 +0200 Subject: [PATCH 07/11] 1st pass: starting moving code that deals with Console in one only class --- ContosoApp/DisplayPets.cs | 135 +++++++++++++++- ContosoApp/NewBusinessLogic.cs | 37 ----- ContosoApp/PetManager.cs | 282 +-------------------------------- ContosoApp/PetShopProgram.cs | 13 +- 4 files changed, 139 insertions(+), 328 deletions(-) delete mode 100644 ContosoApp/NewBusinessLogic.cs diff --git a/ContosoApp/DisplayPets.cs b/ContosoApp/DisplayPets.cs index 497d0d5..9c14739 100644 --- a/ContosoApp/DisplayPets.cs +++ b/ContosoApp/DisplayPets.cs @@ -2,5 +2,138 @@ public sealed class DisplayPets { - public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); + public void DisplayPetInformation(Pet pet) + => Console.WriteLine(pet.ToString()); + + public void ProvideUserAllItems(IEnumerable animals) + { + foreach (var animal in animals) + { + DisplayPetInformation(animal); + } + } + + public void CreatePet(Pet pet) + { + string readResult; + bool validEntry = false; + int petAge; + + // 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(); + PetManager.MakePet(ref validEntry, readResult); + //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 == ""); + } + + public void EditCompleteAge(Pet age, PetsRepository petRepository) + { + string readResult; + bool validEntry = false; + int petAge; + + 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); + } + + else + { + validEntry = true; + } + } + } while (validEntry == false); + } } diff --git a/ContosoApp/NewBusinessLogic.cs b/ContosoApp/NewBusinessLogic.cs deleted file mode 100644 index 2cc026c..0000000 --- a/ContosoApp/NewBusinessLogic.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace ContosoApp; - -public sealed 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/PetManager.cs b/ContosoApp/PetManager.cs index 1a65b94..8e7cf2e 100644 --- a/ContosoApp/PetManager.cs +++ b/ContosoApp/PetManager.cs @@ -2,288 +2,10 @@ public sealed class PetManager { - string? readResult = ""; - bool validEntry = false; - int petAge = 0; + static readonly string[] allowedSpecies = new[] { "dog", "cat", "rabbit" }; - // Defining a method that creates animal species - public void CreatePet(Pet pet) + public static void MakePet(ref bool validEntry, string readResult) { - // 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/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs index e0a638c..f842c12 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -2,16 +2,9 @@ public sealed class PetShopProgram { - private readonly NewBusinessLogic _allPets; private readonly DisplayPets _frontendConsole = new(); - private readonly PetManager _parsePet = new(); private readonly PetsRepository _petsRepository = new(); - public PetShopProgram() - { - _allPets = new NewBusinessLogic(_petsRepository, _frontendConsole); - } - public void Run() { string? menuSelection = ""; @@ -47,7 +40,7 @@ public void Run() switch (menuSelection) { case "1": - _allPets.ProvideUserAllItems(); + _frontendConsole.ProvideUserAllItems(_petsRepository.GetAllPets()); Console.WriteLine("Press the Enter key to continue."); readResult = Console.ReadLine(); break; @@ -55,7 +48,7 @@ public void Run() case "2": var newPet = new Pet(); _petsRepository.AddPet(newPet); - _parsePet.CreatePet(newPet); + _frontendConsole.CreatePet(newPet); Console.WriteLine("Press the Enter key to continue."); readResult = Console.ReadLine(); break; @@ -72,7 +65,7 @@ public void Run() case "5": var incompletePet = new Pet(); - _parsePet.EditCompleteAge(incompletePet, _petsRepository); + _frontendConsole.EditCompleteAge(incompletePet, _petsRepository); Console.WriteLine("The animal age is complete"); readResult = Console.ReadLine(); break; From 2608be3a991899355ed3d18152a7ce034dc75dd3 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 21:44:26 +0200 Subject: [PATCH 08/11] thinking what to do --- ...splayPets.cs => PetShopConsoleFrontend.cs} | 26 +++++++++---------- ContosoApp/PetShopProgram.cs | 26 ++++++++++--------- ContosoApp/PetsRepository.cs | 10 +++---- .../{PetManager.cs => TODO_Refactor.cs} | 2 +- 4 files changed, 33 insertions(+), 31 deletions(-) rename ContosoApp/{DisplayPets.cs => PetShopConsoleFrontend.cs} (97%) rename ContosoApp/{PetManager.cs => TODO_Refactor.cs} (88%) diff --git a/ContosoApp/DisplayPets.cs b/ContosoApp/PetShopConsoleFrontend.cs similarity index 97% rename from ContosoApp/DisplayPets.cs rename to ContosoApp/PetShopConsoleFrontend.cs index 9c14739..f9f5e57 100644 --- a/ContosoApp/DisplayPets.cs +++ b/ContosoApp/PetShopConsoleFrontend.cs @@ -1,18 +1,7 @@ namespace ContosoApp; -public sealed class DisplayPets +public sealed class PetShopConsoleFrontend { - public void DisplayPetInformation(Pet pet) - => Console.WriteLine(pet.ToString()); - - public void ProvideUserAllItems(IEnumerable animals) - { - foreach (var animal in animals) - { - DisplayPetInformation(animal); - } - } - public void CreatePet(Pet pet) { string readResult; @@ -27,7 +16,7 @@ public void CreatePet(Pet pet) if (readResult != null) { pet.AnimalSpecies = readResult.ToLower(); - PetManager.MakePet(ref validEntry, readResult); + TODO_Refactor.MakePet(ref validEntry, readResult); //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; } @@ -104,6 +93,9 @@ public void CreatePet(Pet pet) } while (pet.AnimalNickname == ""); } + public void DisplayPetInformation(Pet pet) + => Console.WriteLine(pet.ToString()); + public void EditCompleteAge(Pet age, PetsRepository petRepository) { string readResult; @@ -136,4 +128,12 @@ public void EditCompleteAge(Pet age, PetsRepository petRepository) } } while (validEntry == false); } + + public void ProvideUserAllItems(IEnumerable animals) + { + foreach (var animal in animals) + { + DisplayPetInformation(animal); + } + } } diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs index f842c12..e6124a3 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -2,7 +2,7 @@ public sealed class PetShopProgram { - private readonly DisplayPets _frontendConsole = new(); + private readonly PetShopConsoleFrontend _frontendConsole = new(); private readonly PetsRepository _petsRepository = new(); public void Run() @@ -14,17 +14,19 @@ public void Run() { 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)"); + 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 diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 6e8234c..40c2264 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -3,17 +3,17 @@ public sealed class PetsRepository //: IEnumerable> { // Defining a dictionary to store the animal and its attributes data. - private Dictionary ourPets; + private Dictionary _petsStorage; public PetsRepository() { - ourPets = new Dictionary(); + _petsStorage = new Dictionary(); } public void AddPet(Pet pet) { //string animalID = GetNextID().ToString(); - ourPets.Add(Guid.NewGuid(), pet); + _petsStorage.Add(Guid.NewGuid(), pet); } // The business logic @@ -21,7 +21,7 @@ public void AddPet(Pet pet) public void FindPetById(string petId) { //PetsRepository petID = new PetsRepository(); - foreach (var ID in ourPets.Keys) + foreach (var ID in _petsStorage.Keys) { if (ID.ToString() == petId) { @@ -31,7 +31,7 @@ public void FindPetById(string petId) } public IEnumerable GetAllPets() - => ourPets.Values; + => _petsStorage.Values; } /*public int GetNextID() diff --git a/ContosoApp/PetManager.cs b/ContosoApp/TODO_Refactor.cs similarity index 88% rename from ContosoApp/PetManager.cs rename to ContosoApp/TODO_Refactor.cs index 8e7cf2e..10a9c6c 100644 --- a/ContosoApp/PetManager.cs +++ b/ContosoApp/TODO_Refactor.cs @@ -1,6 +1,6 @@ namespace ContosoApp; -public sealed class PetManager +public sealed class TODO_Refactor { static readonly string[] allowedSpecies = new[] { "dog", "cat", "rabbit" }; From 141b3aeea6e318e62180ad3f2b2521e501c76344 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 21:53:39 +0200 Subject: [PATCH 09/11] a frontend class must not use repositories --- ContosoApp/PetShopConsoleFrontend.cs | 47 ++++++++++++++-------------- ContosoApp/PetShopProgram.cs | 5 ++- ContosoApp/PetsRepository.cs | 35 ++++----------------- 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/ContosoApp/PetShopConsoleFrontend.cs b/ContosoApp/PetShopConsoleFrontend.cs index f9f5e57..d1c6530 100644 --- a/ContosoApp/PetShopConsoleFrontend.cs +++ b/ContosoApp/PetShopConsoleFrontend.cs @@ -2,20 +2,19 @@ public sealed class PetShopConsoleFrontend { - public void CreatePet(Pet pet) + public Pet CreatePet() { string readResult; bool validEntry = false; - int petAge; + var newPet = new 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(); + newPet.AnimalSpecies = readResult.ToLower(); TODO_Refactor.MakePet(ref validEntry, readResult); //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; } @@ -25,15 +24,16 @@ public void CreatePet(Pet pet) // 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) { - pet.AnimalAge = readResult; - if (pet.AnimalAge != "?") + newPet.AnimalAge = readResult; + if (newPet.AnimalAge != "?") { - validEntry = int.TryParse(pet.AnimalAge, out petAge); + validEntry = int.TryParse(newPet.AnimalAge, out petAge); } else { @@ -50,14 +50,14 @@ public void CreatePet(Pet pet) readResult = Console.ReadLine(); if (readResult != null) { - pet.AnimalPhysicalDescription = readResult.ToLower(); - if (pet.AnimalPhysicalDescription == "") + newPet.AnimalPhysicalDescription = readResult.ToLower(); + if (newPet.AnimalPhysicalDescription == "") { - pet.AnimalPhysicalDescription = "tbd"; + newPet.AnimalPhysicalDescription = "tbd"; } } - } while (pet.AnimalPhysicalDescription == ""); + } while (newPet.AnimalPhysicalDescription == ""); // get a description of the pet's personality - animalPersonalityDescription can be blank. do @@ -66,14 +66,14 @@ public void CreatePet(Pet pet) readResult = Console.ReadLine(); if (readResult != null) { - pet.AnimalPersonalityDescription = readResult.ToLower(); - if (pet.AnimalPersonalityDescription == "") + newPet.AnimalPersonalityDescription = readResult.ToLower(); + if (newPet.AnimalPersonalityDescription == "") { - pet.AnimalPersonalityDescription = "tbd"; + newPet.AnimalPersonalityDescription = "tbd"; } } - } while (pet.AnimalPersonalityDescription == ""); + } while (newPet.AnimalPersonalityDescription == ""); // get the pet's nickname. animalNickname can be blank. do @@ -83,31 +83,32 @@ public void CreatePet(Pet pet) readResult = Console.ReadLine(); if (readResult != null) { - pet.AnimalNickname = readResult.ToLower(); - if (pet.AnimalNickname == "") + newPet.AnimalNickname = readResult.ToLower(); + if (newPet.AnimalNickname == "") { - pet.AnimalNickname = "tbd"; + newPet.AnimalNickname = "tbd"; } } - } while (pet.AnimalNickname == ""); + } while (newPet.AnimalNickname == ""); + + return newPet; } public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); - public void EditCompleteAge(Pet age, PetsRepository petRepository) + public void EditCompleteAge(Pet age) { string readResult; bool validEntry = false; - int petAge; do { Console.WriteLine("Enter pet ID: "); readResult = Console.ReadLine(); // deleted the extra pet object - petRepository.FindPetById(readResult); + //UNDONE petRepository.FindPetById(readResult); Console.WriteLine("Complete pet age: "); readResult = Console.ReadLine(); @@ -118,7 +119,7 @@ public void EditCompleteAge(Pet age, PetsRepository petRepository) if (age.AnimalAge == null || age.AnimalAge == "?") { - validEntry = int.TryParse(age.AnimalAge, out petAge); + validEntry = int.TryParse(age.AnimalAge, out var petAge); } else diff --git a/ContosoApp/PetShopProgram.cs b/ContosoApp/PetShopProgram.cs index e6124a3..2429229 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -48,9 +48,8 @@ Enter your selection number (or type Exit to exit the program) break; case "2": - var newPet = new Pet(); + var newPet = _frontendConsole.CreatePet(); _petsRepository.AddPet(newPet); - _frontendConsole.CreatePet(newPet); Console.WriteLine("Press the Enter key to continue."); readResult = Console.ReadLine(); break; @@ -67,7 +66,7 @@ Enter your selection number (or type Exit to exit the program) case "5": var incompletePet = new Pet(); - _frontendConsole.EditCompleteAge(incompletePet, _petsRepository); + _frontendConsole.EditCompleteAge(incompletePet); Console.WriteLine("The animal age is complete"); readResult = Console.ReadLine(); break; diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 40c2264..e0aadfd 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -2,49 +2,26 @@ public sealed class PetsRepository //: IEnumerable> { - // Defining a dictionary to store the animal and its attributes data. - private Dictionary _petsStorage; - - public PetsRepository() - { - _petsStorage = new Dictionary(); - } + private readonly Dictionary _petsStorage = new Dictionary(); public void AddPet(Pet pet) { - //string animalID = GetNextID().ToString(); _petsStorage.Add(Guid.NewGuid(), pet); } - // The business logic - // Defining a method that is resposible mplementing for searching and finding pets by ID public void FindPetById(string petId) { - //PetsRepository petID = new PetsRepository(); - foreach (var ID in _petsStorage.Keys) + foreach (var id in _petsStorage.Keys) { - if (ID.ToString() == petId) + if (id.ToString() != petId) { - Console.WriteLine($"The animal ID is: {petId}"); + continue; } + + // UNDONE } } public IEnumerable GetAllPets() => _petsStorage.Values; } - -/*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 From cd6ba5deaee2f39cab837d4d439c10bd5c0a8be1 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 22:15:17 +0200 Subject: [PATCH 10/11] Update age workflow --- ContosoApp/PetShopConsoleFrontend.cs | 60 ++++++++++++++++------------ ContosoApp/PetShopProgram.cs | 6 ++- ContosoApp/PetsRepository.cs | 25 ++++++------ ContosoApp/TODO_Refactor.cs | 11 ----- ContosoApp/Validator.cs | 9 +++++ 5 files changed, 61 insertions(+), 50 deletions(-) delete mode 100644 ContosoApp/TODO_Refactor.cs create mode 100644 ContosoApp/Validator.cs diff --git a/ContosoApp/PetShopConsoleFrontend.cs b/ContosoApp/PetShopConsoleFrontend.cs index d1c6530..83cdade 100644 --- a/ContosoApp/PetShopConsoleFrontend.cs +++ b/ContosoApp/PetShopConsoleFrontend.cs @@ -15,10 +15,8 @@ public Pet CreatePet() if (readResult != null) { newPet.AnimalSpecies = readResult.ToLower(); - TODO_Refactor.MakePet(ref validEntry, readResult); - //validEntry = pet.AnimalSpecies == "dog" || pet.AnimalSpecies == "cat" || pet.AnimalSpecies == "rabbit"; + validEntry = Validator.IsValidSpecies(readResult); } - } while (validEntry == false); // get the pet's age. can be ? at initial entry. @@ -98,36 +96,18 @@ public Pet CreatePet() public void DisplayPetInformation(Pet pet) => Console.WriteLine(pet.ToString()); - public void EditCompleteAge(Pet age) + public Guid FindPetById() { + Guid parsed; string readResult; - bool validEntry = false; do { Console.WriteLine("Enter pet ID: "); readResult = Console.ReadLine(); - // deleted the extra pet object - //UNDONE petRepository.FindPetById(readResult); - - Console.WriteLine("Complete pet age: "); - readResult = Console.ReadLine(); - - if (readResult != null) - { - age.AnimalAge = readResult; + } while (Guid.TryParse(readResult, out parsed) == false); - if (age.AnimalAge == null || age.AnimalAge == "?") - { - validEntry = int.TryParse(age.AnimalAge, out var petAge); - } - - else - { - validEntry = true; - } - } - } while (validEntry == false); + return Guid.Parse(readResult); } public void ProvideUserAllItems(IEnumerable animals) @@ -137,4 +117,34 @@ public void ProvideUserAllItems(IEnumerable 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 index 2429229..7fab613 100644 --- a/ContosoApp/PetShopProgram.cs +++ b/ContosoApp/PetShopProgram.cs @@ -65,8 +65,10 @@ Enter your selection number (or type Exit to exit the program) break; case "5": - var incompletePet = new Pet(); - _frontendConsole.EditCompleteAge(incompletePet); + 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; diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index e0aadfd..269b6c2 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -1,27 +1,28 @@ namespace ContosoApp; -public sealed class PetsRepository //: IEnumerable> +public sealed class PetsRepository { - private readonly Dictionary _petsStorage = new Dictionary(); + private readonly Dictionary _petsStorage = new(); public void AddPet(Pet pet) { _petsStorage.Add(Guid.NewGuid(), pet); } - public void FindPetById(string petId) + public Pet? FindPetById(Guid petId) { - foreach (var id in _petsStorage.Keys) - { - if (id.ToString() != petId) - { - continue; - } - - // UNDONE - } + _petsStorage.TryGetValue(petId, out Pet? pet); + return pet; } public IEnumerable GetAllPets() => _petsStorage.Values; + + 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.ToString(); + } } diff --git a/ContosoApp/TODO_Refactor.cs b/ContosoApp/TODO_Refactor.cs deleted file mode 100644 index 10a9c6c..0000000 --- a/ContosoApp/TODO_Refactor.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ContosoApp; - -public sealed class TODO_Refactor -{ - static readonly string[] allowedSpecies = new[] { "dog", "cat", "rabbit" }; - - public static void MakePet(ref bool validEntry, string readResult) - { - validEntry = allowedSpecies.Contains(readResult); - } -} 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); +} From 3c1951229f048f75743d54c70e263db486e7b016 Mon Sep 17 00:00:00 2001 From: lucac Date: Thu, 11 Jul 2024 22:19:48 +0200 Subject: [PATCH 11/11] Age is usually a number --- ContosoApp/Pet.cs | 14 +++++++------- ContosoApp/PetShopConsoleFrontend.cs | 25 ++++++++++++++----------- ContosoApp/PetsRepository.cs | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ContosoApp/Pet.cs b/ContosoApp/Pet.cs index 02553d8..d4a1ccb 100644 --- a/ContosoApp/Pet.cs +++ b/ContosoApp/Pet.cs @@ -2,13 +2,13 @@ public sealed 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 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/PetShopConsoleFrontend.cs b/ContosoApp/PetShopConsoleFrontend.cs index 83cdade..c8dd69f 100644 --- a/ContosoApp/PetShopConsoleFrontend.cs +++ b/ContosoApp/PetShopConsoleFrontend.cs @@ -22,23 +22,26 @@ public Pet CreatePet() // 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) + if (readResult == null) { - newPet.AnimalAge = readResult; - if (newPet.AnimalAge != "?") - { - validEntry = int.TryParse(newPet.AnimalAge, out petAge); - } - else - { - validEntry = true; - } + 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. diff --git a/ContosoApp/PetsRepository.cs b/ContosoApp/PetsRepository.cs index 269b6c2..93450b0 100644 --- a/ContosoApp/PetsRepository.cs +++ b/ContosoApp/PetsRepository.cs @@ -23,6 +23,6 @@ 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.ToString(); + pet.AnimalAge = age; } }