From b0d8089d495861729c164fbc207cf5117d7de8f0 Mon Sep 17 00:00:00 2001 From: Leana BESSON Date: Mon, 5 Jun 2023 11:56:22 +0200 Subject: [PATCH] =?UTF-8?q?Mis=20=C3=A0=20jour=20avec=20ajout=20de=20test?= =?UTF-8?q?=20pour=20les=20Int=20et=20float?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Console/Program.cs | 853 +++++++++++++++++++---------------- Sources/Model/Animal.cs | 33 +- Sources/Model/Entite.cs | 208 ++++----- Sources/Model/Espece.cs | 2 +- Sources/Model/Stub.cs | 14 - Sources/Model/Theque.cs | 46 +- Sources/Model/Veterinaire.cs | 4 +- 7 files changed, 632 insertions(+), 528 deletions(-) diff --git a/Sources/Console/Program.cs b/Sources/Console/Program.cs index 738fd3a..b8f791b 100644 --- a/Sources/Console/Program.cs +++ b/Sources/Console/Program.cs @@ -1,4 +1,5 @@ -using Model; +using Microsoft.Win32.SafeHandles; +using Model; using System.Collections.ObjectModel; using System.IO; using System.Runtime.CompilerServices; @@ -12,358 +13,391 @@ class Program { static private Theque Theque { get; set; } = Stub.LoadTheque(); static void Main(string[] args) - { - - MenusPrincipal(); - } - - static private void MenusPrincipal() - { - while (true) - { - Console.WriteLine("MENUS PRINCIPAL"); - Console.WriteLine("\t1- Les espèces"); - Console.WriteLine("\t2- Vos animaux"); - Console.WriteLine("\t9- Quitter"); - - Console.Write("\n\tEntrer votre choix : "); - int choix = Convert.ToInt32(Console.ReadLine()); - - switch (choix) - { - case 1: - Console.Clear(); - MenusEspece(); - break; - case 2: - Console.Clear(); - MenusAnimal(); - break; - case 9: - return; - default: - Console.WriteLine("\tChoix incorrect\n"); - break; - } - } - } - - static private void MenusEspece() - { - while (true) - { - Console.WriteLine("LES ESPECES"); - Console.WriteLine("\t1- Afficher les espèces"); - Console.WriteLine("\t2- Sélectionner une espèce"); - Console.WriteLine("\t9- Retour"); - - Console.Write("\n\tEntrer votre choix : "); - int choix = Convert.ToInt32(Console.ReadLine()); - - switch (choix) - { - case 1: - Console.Clear(); - AfficherListeEspece(); - break; - case 2: - Console.Clear(); - SelectionnerEspece(); - break; - case 9: - Console.Clear(); - return; - default: - Console.WriteLine("\tChoix incorrect\n"); - break; - } - } - } - - static private void AfficherListeEspece() - { - Console.WriteLine("LISTE DES ESPECES : "); - foreach (Espece espece in Theque.ListeEspeces) - { - Console.WriteLine("\t" + espece.Nom + " (" + espece.NomScientifique + ")"); - } - Console.WriteLine("\n"); - } - - static private void SelectionnerEspece() - { - string choix = ""; - while (choix != "-1") - { - AfficherListeEspece(); - - Console.Write("\n\tEntrer le nom de l'espèce à sélectionner (-1 pour annuler) : "); - choix = Console.ReadLine(); - - Espece? espece = Theque.RechercherEspece(choix); - - if (espece != null) - { - AfficherEspece(espece); - } - else Console.WriteLine("\tChoix incorrect\n"); - } - } - - static private void AfficherEspece(Espece espece) - { - Console.WriteLine("\n" + espece.Nom); - Console.WriteLine("\tNom scientifique : " + espece.NomScientifique); - Console.WriteLine("\tEspérance de vie : " + espece.EsperanceVie); - Console.WriteLine("\tPoids moyen : " + espece.PoidsMoyen); - Console.WriteLine("\tTaille moyenne : " + espece.TailleMoyenne); - Console.WriteLine("\tComportement : " + espece.Comportement); - Console.WriteLine("\tSanté : " + espece.Sante); - Console.WriteLine("\tEducation : " + espece.Education); - Console.WriteLine("\tEntretien : " + espece.Entretien); - Console.WriteLine("\tCout : " + espece.Cout); - Console.WriteLine("\tConseil : " + espece.Conseil); - - AfficherListeRace(espece); - - while (true) - { - Console.WriteLine("\n\t1- Sélectionner une race"); - Console.WriteLine("\t9- Retour"); - - Console.Write("\n\tEntrer votre choix : "); - int decision = Convert.ToInt32(Console.ReadLine()); - - switch (decision) - { - case 1: - SelectionnerRace(espece); - break; - case 9: - return; - default: - Console.WriteLine("\tChoix incorrect\n"); - break; - } - } - } - - static private void AfficherListeRace(Espece espece) - { - Console.WriteLine("\nLISTE DES RACES : "); - if (espece.ListeRaces != null) - { - foreach (Race race in espece.ListeRaces) - { - Console.WriteLine("\t" + race.Nom + " (" + race.NomScientifique + ")"); - } - Console.WriteLine("\n"); - } - else Console.WriteLine("\tAucune race connue.\n"); - } - - static private void SelectionnerRace(Espece espece) - { - string choix = ""; - while (choix != "-1") - { - Console.Write("\n\tEntrer le nom de la race à sélectionner (-1 pour annuler) : "); - choix = Console.ReadLine(); - - if (choix != "-1") - { - Race? race = espece.RechercherRace(choix); - - if (race != null) - { - AfficherRace(race); - } - else Console.WriteLine("\tChoix incorrect\n"); - } - } - } - - static private void AfficherRace(Race race) - { - Console.WriteLine("\n " + race.Nom); - Console.WriteLine("\tNom scientifique : " + race.NomScientifique); - Console.WriteLine("\tEspérance de vie : " + race.EsperanceVie); - Console.WriteLine("\tPoids moyen : " + race.PoidsMoyen); - Console.WriteLine("\tTaille moyenne : " + race.TailleMoyenne); - Console.WriteLine("\tComportement : " + race.Comportement); - Console.WriteLine("\tSante : " + race.Sante); - Console.WriteLine("\tEducation : " + race.Education); - Console.WriteLine("\tEntretien : " + race.Entretien); - Console.WriteLine("\tCout : " + race.Cout); - Console.WriteLine("\tConseil : " + race.Conseil + "\n\n"); - } - - static private void MenusAnimal() - { - while (true) - { - Console.WriteLine("LES ANIMAUX"); - Console.WriteLine("\t1- Afficher les animaux"); - Console.WriteLine("\t2- Ajouter un animal"); - Console.WriteLine("\t3- Sélectionner un animal"); - Console.WriteLine("\t9- Retour"); - - Console.Write("\n\tEntrer votre choix : "); - int choix = Convert.ToInt32(Console.ReadLine()); - - switch (choix) - { - case 1: - Console.Clear(); - AfficherListeAnimaux(); - break; - case 2: - Console.Clear(); - Animal animal = Theque.AjouterAnimal(); - ModifierNom(animal); - ModifierAnimal(animal); - break; - case 3: - Console.Clear(); - SelectionnerAnimal(); - break; - case 9: - Console.Clear(); - return; - default: - Console.WriteLine("\tChoix incorrect\n"); - break; - } - } - } - - static private void AfficherListeAnimaux() - { - Console.WriteLine("VOS ANIMAUX : "); - foreach (Animal animal in Theque.ListeAnimaux) - { - Console.WriteLine(animal.Nom); - } - } - - static private void SelectionnerAnimal() - { - string choix = ""; - while (choix != "-1") - { - AfficherListeAnimaux(); - - Console.Write("\n\tEntrer le nom de l'animal à sélectionner (-1 pour annuler) : "); - choix = Console.ReadLine(); - - Animal? animal = Theque.RechercherAnimal(choix); - - if (animal != null) - { - AfficherAnimal(animal); - } - else Console.WriteLine("\tChoix incorrect\n"); - } - } - - static private void AfficherAnimal(Animal animal) - { - Console.Clear(); - while (true) - { - Console.WriteLine("\n" + animal.Nom); - if (animal.Espece != null) Console.WriteLine("\tEspece : " + animal.Espece.Nom); - if (animal.Race != null) Console.WriteLine("\tRace : " + animal.Race.Nom); - Console.WriteLine("\tDate de naissance : " + animal.DateNaissance); - Console.WriteLine("\tSexe : " + animal.Sexe); - Console.WriteLine("\tDate d'adoption : " + animal.DateAdoption); - Console.WriteLine("\tTaille : " + animal.Taille); - Console.WriteLine("\tPoids : " + animal.Poids); - Console.WriteLine("\tAlimentation : " + animal.Alimentation); - Console.WriteLine("\tPETSITTER : "); - AfficherEntite(animal.Petsitter); - Console.WriteLine("\tCHENIL : "); - AfficherEntite(animal.Chenil); - Console.WriteLine("\tVETERINAIRE : "); - AfficherEntite(animal.Veterinaire); - Console.WriteLine("\tMAGASIN ALIMENTAIRE : "); - AfficherEntite(animal.MagasinAlimentaire); - Console.WriteLine("\tREFUGE, ELEVAGE, CHENIL DE PROVENANCE : "); - AfficherEntite(animal.Provenance); - - - Console.WriteLine("\n\t1- Modifier"); - Console.WriteLine("\t2- Supprimer"); - Console.WriteLine("\t9- Retour"); - - Console.Write("\n\tEntrer votre choix : "); - int decision = Convert.ToInt32(Console.ReadLine()); - - switch (decision) - { - case 1: - ModifierAnimal(animal); - break; - case 2: - Theque.SupprimerAnimal(animal); - return; - case 9: - return; - default: - Console.WriteLine("\tChoix incorrect\n"); - break; - } - } - } - - static private void AfficherEntite(Entite entite) - { - Console.WriteLine("\t\tNom : " + entite.Nom); - Console.WriteLine("\t\tAdresse : " + entite.Adresse + "," + Convert.ToInt32(entite.CodePostal) + " " + entite.Ville); - Console.WriteLine("\t\tNuméro de téléphone : " + entite.NumTel); - } - - static private void ModifierAnimal(Animal animal) - { - while (true) - { - Console.WriteLine("MODIFIER L'ANIMAL ", animal.Nom); - Console.WriteLine("\t1- Nom"); - Console.WriteLine("\t2- Espece"); - Console.WriteLine("\t3- Race"); - Console.WriteLine("\t4- Date de naissance"); - Console.WriteLine("\t5- Sexe"); - Console.WriteLine("\t6- Date d'adoption"); - Console.WriteLine("\t7- Taille"); - Console.WriteLine("\t8- Poids"); - Console.WriteLine("\t9- Alimentation"); - Console.WriteLine("\t10- Petsitter"); - Console.WriteLine("\t11- Chenil"); - Console.WriteLine("\t12- Vétérinaire"); - Console.WriteLine("\t13- Magasin alimentaire"); - Console.WriteLine("\t14- Refuge, élevage et chenil de provenance"); - Console.WriteLine("\t19- Retour"); - - Console.Write("\n\tEntrer votre choix : "); - int decision = Convert.ToInt32(Console.ReadLine()); - - switch (decision) - { - case 1: - ModifierNom(animal); - break; - case 2: - ModifierEspece(animal); - break; - case 3: - ModifierRace(animal); - break; - case 4: - ModifierDateNaissance(animal); - break; - case 5: - ModifierSexe(animal); - break; + { + + MenusPrincipal(); + } + + static private void MenusPrincipal() + { + while (true) + { + Console.WriteLine("MENUS PRINCIPAL"); + Console.WriteLine("\t1- Les espèces"); + Console.WriteLine("\t2- Vos animaux"); + Console.WriteLine("\t9- Quitter"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while(!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + Console.Clear(); + MenusEspece(); + break; + case 2: + Console.Clear(); + MenusAnimal(); + break; + case 9: + return; + default: + Console.WriteLine("\tChoix incorrect\n"); + break; + } + } + } + + static private void MenusEspece() + { + while (true) + { + Console.WriteLine("LES ESPECES"); + Console.WriteLine("\t1- Afficher les espèces"); + Console.WriteLine("\t2- Sélectionner une espèce"); + Console.WriteLine("\t9- Retour"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + Console.Clear(); + AfficherListeEspece(); + break; + case 2: + Console.Clear(); + SelectionnerEspece(); + break; + case 9: + Console.Clear(); + return; + default: + Console.WriteLine("\tChoix incorrect\n"); + break; + } + } + } + + static private void AfficherListeEspece() + { + Console.WriteLine("LISTE DES ESPECES : "); + foreach (Espece espece in Theque.ListeEspeces) + { + Console.WriteLine("\t" + espece.Nom + " (" + espece.NomScientifique + ")"); + } + Console.WriteLine("\n"); + } + + static private void SelectionnerEspece() + { + string? choix = null; + while (choix != "-1") + { + AfficherListeEspece(); + + Console.Write("\n\tEntrer le nom de l'espèce à sélectionner (-1 pour annuler) : "); + choix = Console.ReadLine(); + + if(choix != null) + { + Espece? espece = Theque.RechercherEspece(choix); + + if (espece != null) + { + AfficherEspece(espece); + } + else Console.WriteLine("\tChoix incorrect\n"); + } + } + } + + static private void AfficherEspece(Espece espece) + { + Console.WriteLine("\n" + espece.Nom); + Console.WriteLine("\tNom scientifique : " + espece.NomScientifique); + Console.WriteLine("\tEspérance de vie : " + espece.EsperanceVie); + Console.WriteLine("\tPoids moyen : " + espece.PoidsMoyen); + Console.WriteLine("\tTaille moyenne : " + espece.TailleMoyenne); + Console.WriteLine("\tComportement : " + espece.Comportement); + Console.WriteLine("\tSanté : " + espece.Sante); + Console.WriteLine("\tEducation : " + espece.Education); + Console.WriteLine("\tEntretien : " + espece.Entretien); + Console.WriteLine("\tCout : " + espece.Cout); + Console.WriteLine("\tConseil : " + espece.Conseil); + + AfficherListeRace(espece); + + while (true) + { + Console.WriteLine("\n\t1- Sélectionner une race"); + Console.WriteLine("\t9- Retour"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + SelectionnerRace(espece); + break; + case 9: + return; + default: + Console.WriteLine("\tChoix incorrect\n"); + break; + } + } + } + + static private void AfficherListeRace(Espece espece) + { + Console.WriteLine("\nLISTE DES RACES : "); + if (espece.ListeRaces != null) + { + foreach (Race race in espece.ListeRaces) + { + Console.WriteLine("\t" + race.Nom + " (" + race.NomScientifique + ")"); + } + Console.WriteLine("\n"); + } + else Console.WriteLine("\tAucune race connue.\n"); + } + + static private void SelectionnerRace(Espece espece) + { + string? choix = ""; + while (choix != "-1") + { + Console.Write("\n\tEntrer le nom de la race à sélectionner (-1 pour annuler) : "); + choix = Console.ReadLine(); + + if (choix != "-1") + { + Race? race = espece.RechercherRace(choix); + + if (race != null) + { + AfficherRace(race); + } + else Console.WriteLine("\tChoix incorrect\n"); + } + } + } + + static private void AfficherRace(Race race) + { + Console.WriteLine("\n " + race.Nom); + Console.WriteLine("\tNom scientifique : " + race.NomScientifique); + Console.WriteLine("\tEspérance de vie : " + race.EsperanceVie); + Console.WriteLine("\tPoids moyen : " + race.PoidsMoyen); + Console.WriteLine("\tTaille moyenne : " + race.TailleMoyenne); + Console.WriteLine("\tComportement : " + race.Comportement); + Console.WriteLine("\tSante : " + race.Sante); + Console.WriteLine("\tEducation : " + race.Education); + Console.WriteLine("\tEntretien : " + race.Entretien); + Console.WriteLine("\tCout : " + race.Cout); + Console.WriteLine("\tConseil : " + race.Conseil + "\n\n"); + } + + static private void MenusAnimal() + { + while (true) + { + Console.WriteLine("LES ANIMAUX"); + Console.WriteLine("\t1- Afficher les animaux"); + Console.WriteLine("\t2- Ajouter un animal"); + Console.WriteLine("\t3- Sélectionner un animal"); + Console.WriteLine("\t9- Retour"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + Console.Clear(); + AfficherListeAnimaux(); + break; + case 2: + Console.Clear(); + Animal animal = Theque.AjouterAnimal(); + ModifierNom(animal); + ModifierAnimal(animal); + break; + case 3: + Console.Clear(); + SelectionnerAnimal(); + break; + case 9: + Console.Clear(); + return; + default: + Console.WriteLine("\tChoix incorrect\n"); + break; + } + } + } + + static private void AfficherListeAnimaux() + { + Console.WriteLine("VOS ANIMAUX : "); + foreach (Animal animal in Theque.ListeAnimaux) + { + Console.WriteLine(animal.Nom); + } + } + + static private void SelectionnerAnimal() + { + string? choix = ""; + while (choix != "-1") + { + AfficherListeAnimaux(); + + Console.Write("\n\tEntrer le nom de l'animal à sélectionner (-1 pour annuler) : "); + choix = Console.ReadLine(); + + Animal? animal = Theque.RechercherAnimal(choix); + + if (animal != null) + { + AfficherAnimal(animal); + } + else Console.WriteLine("\tChoix incorrect\n"); + } + } + + static private void AfficherAnimal(Animal animal) + { + Console.Clear(); + while (true) + { + Console.WriteLine("\n" + animal.Nom); + if (animal.Espece != null) Console.WriteLine("\tEspece : " + animal.Espece.Nom); + if (animal.Race != null) Console.WriteLine("\tRace : " + animal.Race.Nom); + Console.WriteLine("\tDate de naissance : " + animal.DateNaissance); + Console.WriteLine("\tSexe : " + animal.Sexe); + Console.WriteLine("\tDate d'adoption : " + animal.DateAdoption); + Console.WriteLine("\tTaille : " + animal.Taille); + Console.WriteLine("\tPoids : " + animal.Poids); + Console.WriteLine("\tAlimentation : " + animal.Alimentation); + Console.WriteLine("\tPETSITTER : "); + AfficherEntite(animal.Petsitter); + Console.WriteLine("\tCHENIL : "); + AfficherEntite(animal.Chenil); + Console.WriteLine("\tVETERINAIRE : "); + AfficherEntite(animal.Veterinaire); + Console.WriteLine("\tMAGASIN ALIMENTAIRE : "); + AfficherEntite(animal.MagasinAlimentaire); + Console.WriteLine("\tREFUGE, ELEVAGE, CHENIL DE PROVENANCE : "); + AfficherEntite(animal.Provenance); + + + Console.WriteLine("\n\t1- Modifier"); + Console.WriteLine("\t2- Supprimer"); + Console.WriteLine("\t9- Retour"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + ModifierAnimal(animal); + break; + case 2: + Theque.SupprimerAnimal(animal); + return; + case 9: + return; + default: + Console.WriteLine("\tChoix incorrect\n"); + break; + } + } + } + + static private void AfficherEntite(Entite entite) + { + Console.WriteLine("\t\tNom : " + entite.Nom); + Console.WriteLine("\t\tAdresse : " + entite.Adresse + "," + Convert.ToInt32(entite.CodePostal) + " " + entite.Ville); + Console.WriteLine("\t\tNuméro de téléphone : " + entite.NumTel); + } + + static private void ModifierAnimal(Animal animal) + { + while (true) + { + Console.WriteLine("MODIFIER L'ANIMAL ", animal.Nom); + Console.WriteLine("\t1- Nom"); + Console.WriteLine("\t2- Espece"); + Console.WriteLine("\t3- Race"); + Console.WriteLine("\t4- Date de naissance"); + Console.WriteLine("\t5- Sexe"); + Console.WriteLine("\t6- Date d'adoption"); + Console.WriteLine("\t7- Taille"); + Console.WriteLine("\t8- Poids"); + Console.WriteLine("\t9- Alimentation"); + Console.WriteLine("\t10- Petsitter"); + Console.WriteLine("\t11- Chenil"); + Console.WriteLine("\t12- Vétérinaire"); + Console.WriteLine("\t13- Magasin alimentaire"); + Console.WriteLine("\t14- Refuge, élevage et chenil de provenance"); + Console.WriteLine("\t19- Retour"); + + Console.Write("\n\tEntrer votre choix : "); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } + + switch (Convert.ToInt32(choix)) + { + case 1: + ModifierNom(animal); + break; + case 2: + ModifierEspece(animal); + break; + case 3: + ModifierRace(animal); + break; + case 4: + ModifierDateNaissance(animal); + break; + case 5: + ModifierSexe(animal); + break; case 6: ModifierDateAdoption(animal); break; @@ -402,36 +436,39 @@ class Program static private void ModifierNom(Animal animal) { - while (animal.Nom.Length <= 0 || animal.Nom.StartsWith(" ")) + Console.Write("\tNom : "); + string nom = Console.ReadLine()??""; + + while(!animal.NomValidate(nom)) { - Console.Write("\tNom : "); - animal.Nom = Console.ReadLine(); + Console.WriteLine("\nNom incorrect. Nom : "); + nom = Console.ReadLine() ??""; } + + animal.Nom = nom; } static private void ModifierEspece(Animal animal) { - Espece? espece = null; - while (espece == null) + Console.Write("\tEspèce (appuyer sur entrée pour passer): "); + string? nomEspece = Console.ReadLine(); + Espece? espece = Theque.RechercherEspece(nomEspece); + + while (nomEspece != null && espece == null) { - Console.Write("\tEspèce : "); - string nomEspece = Console.ReadLine(); + Console.Write("\tEspèce inconnue. Espèce : "); + nomEspece = Console.ReadLine(); espece = Theque.RechercherEspece(nomEspece); - if (espece == null) - { - Console.WriteLine("Espece inconnue\n"); - } } - animal.Espece = espece; } static private void ModifierSexe(Animal animal) { - string sexe = "sexe"; - while (sexe != "Male" && sexe != "Femelle" && sexe != "") + string? sexe = null; + while (sexe != "Male" && sexe != "Femelle" && sexe != null) { - Console.Write("\tSexe [Male|Femelle|] (appuyer sur entrer pour passer) : "); + Console.Write("\tSexe [Male|Femelle] (appuyer sur entrer pour passer) : "); sexe = Console.ReadLine(); } animal.Sexe = sexe; @@ -440,17 +477,27 @@ class Program static private void ModifierTaille(Animal animal) { Console.Write("\tTaille (appuyer sur entrer pour passer) : "); - string taille = Console.ReadLine(); - if (taille == "") animal.Taille = null; - else animal.Taille = Single.Parse(taille); + string? taille = Console.ReadLine(); + + while(taille != null && !Theque.FloatValidate(taille)) + { + Console.WriteLine("\tTaille incorrect. Taille : "); + taille = Console.ReadLine(); + } + animal.Taille = Convert.ToSingle(taille); } static private void ModifierPoids(Animal animal) { Console.Write("\tPoids (appuyer sur entrer pour passer) : "); - string poids = Console.ReadLine(); - if (poids == "") animal.Poids = null; - else animal.Poids = Single.Parse(poids); + string? poids = Console.ReadLine(); + + while (poids != null && !Theque.FloatValidate(poids)) + { + Console.WriteLine("\tTaille incorrect. Poids : "); + poids = Console.ReadLine(); + } + animal.Poids = Convert.ToSingle(poids); } static private void ModifierAlimentation(Animal animal) @@ -462,22 +509,49 @@ class Program static private void ModifierDateNaissance(Animal animal) { Console.Write("\tDate de naissance (appuyer sur entrer pour passer) : "); - animal.DateNaissance = Console.ReadLine(); + string? dateNaissance = Console.ReadLine(); + + while (dateNaissance != null && !Theque.DateTimeValidate(dateNaissance)) + { + Console.WriteLine("\tTaille incorrect. Date de naissance : "); + dateNaissance = Console.ReadLine(); + } + animal.DateNaissance = Convert.ToDateTime(dateNaissance); } static private void ModifierDateAdoption(Animal animal) { Console.Write("\tDate d'adoption (appuyer sur entrer pour passer) : "); - animal.DateAdoption = Console.ReadLine(); + string? dateAdoption = Console.ReadLine(); + + while (dateAdoption != null && !Theque.DateTimeValidate(dateAdoption)) + { + Console.WriteLine("\tTaille incorrect. Date d'adoption : "); + dateAdoption = Console.ReadLine(); + } + + animal.DateAdoption = Convert.ToDateTime(dateAdoption); } static private void ModifierRace(Animal animal) { - Console.Write("\tRace (appuyer sur entrer pour passer) : "); - string nomRace = Console.ReadLine(); - animal.Race = animal.Espece.RechercherRace(nomRace); + if (animal.Espece != null) + { + Console.Write("\tRace (appuyer sur entrée pour passer): "); + string? nomRace = Console.ReadLine(); + Race? race = animal.Espece.RechercherRace(nomRace); + + while (nomRace != null && race == null) + { + Console.Write("\tRace inconnue. Race : "); + nomRace = Console.ReadLine(); + race = animal.Espece.RechercherRace(nomRace); + } + animal.Race = race; + } + else Console.WriteLine("\tL'animal ne peut pas avoir une race sans espèce"); } static private void ModifierEntite(Entite entite) @@ -492,9 +566,14 @@ class Program Console.WriteLine("\t9- Retour"); Console.Write("\n\tEntrer votre choix : "); - int decision = Convert.ToInt32(Console.ReadLine()); + string? choix = Console.ReadLine(); + while (!Theque.IntValidate(choix)) + { + Console.Write("\n\tChoix incorrect. Entrer votre choix : "); + choix = Console.ReadLine(); + } - switch (decision) + switch (Convert.ToInt32(choix)) { case 1: ModifierNomEntite(entite); @@ -531,12 +610,16 @@ class Program static private void ModifierCodePostalEntite(Entite entite) { - int? codePostal = null; - while(codePostal != null || (codePostal < 10000 && codePostal > 99999)) { + Console.Write("\tCode postal (appuyer sur entrer pour passer) : "); + string? codePostal = Console.ReadLine(); + + while((codePostal != null && !Theque.IntValidate(codePostal)) || + (codePostal != null && Theque.IntValidate(codePostal) && Convert.ToInt32(codePostal) < 10000 && Convert.ToInt32(codePostal) > 99999)) { Console.Write("\tCode postal (appuyer sur entrer pour passer) : "); - codePostal = Convert.ToInt32(Console.ReadLine()); + codePostal = Console.ReadLine(); } - entite.CodePostal = codePostal; + + entite.CodePostal = Convert.ToInt32(codePostal); } static private void ModifierVilleEntite(Entite entite) diff --git a/Sources/Model/Animal.cs b/Sources/Model/Animal.cs index e80817e..3cb737f 100644 --- a/Sources/Model/Animal.cs +++ b/Sources/Model/Animal.cs @@ -24,7 +24,7 @@ namespace Model if (nom == value) return; nom = value; - NomIsValid = NomValidate(); + NomIsValid = NomValidate(nom); OnPropertyChanged(nameof(Nom)); } } @@ -44,7 +44,7 @@ namespace Model private bool nomIsValid; [DataMember(Name = "naissance")] - public string DateNaissance + public DateTime? DateNaissance { get => dateNaissance; set @@ -55,7 +55,7 @@ namespace Model OnPropertyChanged(nameof(DateNaissance)); } } - private string dateNaissance; + private DateTime? dateNaissance; [DataMember(Name = "sexe")] public string Sexe @@ -71,7 +71,7 @@ namespace Model private string sexe; [DataMember(Name = "adoption")] - public string DateAdoption + public DateTime? DateAdoption { get => dateAdoption; set @@ -82,7 +82,7 @@ namespace Model OnPropertyChanged(nameof(DateAdoption)); } } - private string dateAdoption; + private DateTime? dateAdoption; [DataMember(Name = "taille")] public float? Taille @@ -113,7 +113,7 @@ namespace Model private float? poids; [DataMember(Name = "alimentation")] - public string Alimentation + public string? Alimentation { get => alimentation; set @@ -124,7 +124,7 @@ namespace Model OnPropertyChanged(nameof(Alimentation)); } } - private string alimentation; + private string? alimentation; [DataMember(Name = "espèce")] public Espece? Espece @@ -225,7 +225,7 @@ namespace Model private Entite? petsitter = new Entite(); [DataMember(Name = "image")] - public string Image + public string? Image { get => image; set @@ -236,21 +236,14 @@ namespace Model OnPropertyChanged(nameof(Image)); } } - private string image; + private string? image; - public Animal(string nom = "", string dateNaissance = "Inconnue", string sexe = "Inconnu", string dateAdpotion = "Inconnue", float? taille = null, float? poids = null, string alimentation = "Inconnue") + public Animal() { - Nom = nom; - DateNaissance = dateNaissance; - Sexe = sexe; - DateAdoption = dateAdpotion; - Taille = taille; - Poids = poids; - Alimentation = alimentation; NomIsValid = false; } - void OnPropertyChanged(string propertyName) + private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { @@ -258,9 +251,9 @@ namespace Model } } - bool NomValidate() + public bool NomValidate(string? nom) { - if(Nom.Length <= 0 || Nom.StartsWith(" ")) return false; + if (nom == null || nom.Length <= 0 || nom.StartsWith(" ")) return false; return true; } } diff --git a/Sources/Model/Entite.cs b/Sources/Model/Entite.cs index 747597e..19934ef 100644 --- a/Sources/Model/Entite.cs +++ b/Sources/Model/Entite.cs @@ -1,105 +1,105 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.ComponentModel.DataAnnotations.Schema; -using System.IO.Pipes; -using System.Linq; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations.Schema; +using System.IO.Pipes; +using System.Linq; using System.Runtime.Serialization; -using System.Text; -using System.Threading.Tasks; - -namespace Model -{ - [DataContract(Name = "entite")] - public class Entite : INotifyPropertyChanged - { - public event PropertyChangedEventHandler? PropertyChanged; - - [DataMember(Name = "nom")] - public string Nom - { - get => nom; - set - { - if (nom == value) - return; - nom = value; - OnPropertyChanged(nameof(Nom)); - } - } - private string nom; - - [DataMember(Name = "adresse")] - public string Adresse - { - get => adresse; - set - { - if (adresse == value) - return; - adresse = value; - OnPropertyChanged(nameof(Adresse)); - } - } - private string adresse; - - [DataMember(Name = "codePostal")] - public int? CodePostal - { - get => codePostal; - set - { - if (codePostal == value) - return; - codePostal = value; - OnPropertyChanged(nameof(CodePostal)); - } - } - private int? codePostal; - - [DataMember(Name = "ville")] - public string Ville - { - get => ville; - set - { - if (ville == value) - return; - ville = value; - OnPropertyChanged(nameof(Ville)); - } - } - private string ville; - - [DataMember(Name = "numTel")] - public int? NumTel - { - get => numTel; - set - { - if(numTel == value) - return; - numTel = value; - OnPropertyChanged(nameof(NumTel)); - } - } - private int? numTel; - - public Entite(string nom = "", string adresse = "", int? codePostal = null, string ville = "", int? numTel = null) - { - Nom = nom; - Adresse = adresse; - CodePostal = codePostal; - Ville = ville; - NumTel = numTel; - } - - public void OnPropertyChanged(string propertyName) - { - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); - } - } - } -} +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + [DataContract(Name = "entite")] + public class Entite : INotifyPropertyChanged + { + public event PropertyChangedEventHandler? PropertyChanged; + + [DataMember(Name = "nom")] + public string? Nom + { + get => nom; + set + { + if (nom == value) + return; + nom = value; + OnPropertyChanged(nameof(Nom)); + } + } + private string? nom; + + [DataMember(Name = "adresse")] + public string? Adresse + { + get => adresse; + set + { + if (adresse == value) + return; + adresse = value; + OnPropertyChanged(nameof(Adresse)); + } + } + private string? adresse; + + [DataMember(Name = "codePostal")] + public int? CodePostal + { + get => codePostal; + set + { + if (codePostal == value) + return; + codePostal = value; + OnPropertyChanged(nameof(CodePostal)); + } + } + private int? codePostal; + + [DataMember(Name = "ville")] + public string? Ville + { + get => ville; + set + { + if (ville == value) + return; + ville = value; + OnPropertyChanged(nameof(Ville)); + } + } + private string? ville; + + [DataMember(Name = "numTel")] + public int? NumTel + { + get => numTel; + set + { + if(numTel == value) + return; + numTel = value; + OnPropertyChanged(nameof(NumTel)); + } + } + private int? numTel; + + public Entite(string nom = "", string adresse = "", int? codePostal = null, string ville = "", int? numTel = null) + { + Nom = nom; + Adresse = adresse; + CodePostal = codePostal; + Ville = ville; + NumTel = numTel; + } + + public void OnPropertyChanged(string propertyName) + { + if (PropertyChanged != null) + { + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } + } +} diff --git a/Sources/Model/Espece.cs b/Sources/Model/Espece.cs index 4a8d5bd..09460fd 100644 --- a/Sources/Model/Espece.cs +++ b/Sources/Model/Espece.cs @@ -74,7 +74,7 @@ namespace Model return Nom; } - public Race? RechercherRace(string choix) + public Race? RechercherRace(string? choix) { if (ListeRaces != null && choix != "") { diff --git a/Sources/Model/Stub.cs b/Sources/Model/Stub.cs index cbde0e3..72d102e 100644 --- a/Sources/Model/Stub.cs +++ b/Sources/Model/Stub.cs @@ -23,20 +23,6 @@ namespace Model return ListeEspeces; } - public static List LoadZootheque() - { - List listeAnimaux = new List(); - - listeAnimaux.Add(new("Kiki")); - listeAnimaux.Add(new("PouetPouet")); - listeAnimaux.Add(new("Chouchou")); - listeAnimaux.Add(new("Pupuce")); - listeAnimaux.Add(new("AucuneIdée")); - listeAnimaux.Add(new("Minou")); - - return listeAnimaux; - } - public static Theque LoadTheque() { Theque theque = new Theque(); diff --git a/Sources/Model/Theque.cs b/Sources/Model/Theque.cs index 5bce620..2282fc0 100644 --- a/Sources/Model/Theque.cs +++ b/Sources/Model/Theque.cs @@ -36,7 +36,7 @@ namespace Model ListeAnimaux.Remove(animal); } - public Animal? RechercherAnimal(string choix) + public Animal? RechercherAnimal(string? choix) { foreach (Animal animal in ListeAnimaux) { @@ -48,7 +48,7 @@ namespace Model return null; } - public Espece? RechercherEspece(string choix) + public Espece? RechercherEspece(string? choix) { foreach (Espece espece in ListeEspeces) { @@ -59,5 +59,47 @@ namespace Model } return null; } + + public bool IntValidate(string? response) + { + if (response == null) return false; + try + { + int number = Convert.ToInt32(response); + return true; + } + catch (Exception ex) + { + return false; + } + } + + public bool FloatValidate(string? response) + { + if (response == null) return false; + try + { + float numFloat = Convert.ToSingle(response); + return true; + } + catch (Exception ex) + { + return false; + } + } + + public bool DateTimeValidate(string? response) + { + if (response == null) return false; + try + { + DateTime date = Convert.ToDateTime(response); + return true; + } + catch (Exception ex) + { + return false; + } + } } } diff --git a/Sources/Model/Veterinaire.cs b/Sources/Model/Veterinaire.cs index 3efc124..4b84965 100644 --- a/Sources/Model/Veterinaire.cs +++ b/Sources/Model/Veterinaire.cs @@ -12,7 +12,7 @@ namespace Model public class Veterinaire : Entite { [DataMember(Name = "clinique")] - public string Clinique + public string? Clinique { get => clinique; set @@ -23,6 +23,6 @@ namespace Model OnPropertyChanged(nameof(Clinique)); } } - private string clinique; + private string? clinique; } }