From 86b2c5f62471000802571063f28c0e23ddac071b Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 8 Apr 2024 15:26:37 +0200 Subject: [PATCH] added the examples of the 4th cm (162 lines long) --- Qwirkle/Qwirkle/cm3.cs | 92 ----------------------- Qwirkle/cm4.cs | 162 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 92 deletions(-) delete mode 100644 Qwirkle/Qwirkle/cm3.cs create mode 100644 Qwirkle/cm4.cs diff --git a/Qwirkle/Qwirkle/cm3.cs b/Qwirkle/Qwirkle/cm3.cs deleted file mode 100644 index 97b5f39..0000000 --- a/Qwirkle/Qwirkle/cm3.cs +++ /dev/null @@ -1,92 +0,0 @@ -/*// LE FICHIER VOUS CHIE À LA GUEULE ET NE PEUT PAS ETRE COMPILÉ : C'EST NORMAL !! - -// types primitifs : int, double, short, ... -// types .NET : timespan, math, datetime, random, ... -// creation de 5 types difféents : classes(heap), structure(stack), interfaces(heap), delegate(heap), enum(stack) - -// ClassLibrary (classic one) (can't be executed without a project) - -namespace MyNamespace -{ - - public enum Jeremy // Flemme de le faire mais en gros on peut faire des combinaisons avec et c'est pété - { - chiant, - tresChiant, - vraimentTresChiant - } - - public class Class1 - { - private int nbPoil; // if not define, takes a default value (0 for int, ...) - - public Class1(string nom, int nbPoil) - { - this.Nom = nom; - this.NbPoils = nbPoil; - } - - *//*public string GetNom() - { - if (name == null) - { - return "Unknown"; - } - return name; - } - - public void SetNom(string newname) - { - if (name == newname) - { - return; - } - - name = newname; - }*//* - - public string Nom // Méthode plus lisible que de faire les getters et setters à part - { - get - { - if (name == null) - { - return "Unknown"; - } - - return name; - } - - set - { - if (name == value) - { - return; - } - - name = value; - } - } - private string name; - - public int NbPoils // Méthode un peu barbare mais vas-y on gagne de la place - { - get; - set; - } - } -} - -// Console app (example) - -using MyNamespace; - -Class1 obj1 = new Class1("Chewy", 987654321); -Class1 obj2 = new Class1("Yoda", 256); - -Class1[] objects = new Class1[4]; // Tableau - -Class1[,] matrixObjects = new Class1[2, 4]; - -obj2.Nom = "Maitre Yoda"; -string leNom = obj2.Nom;*/ \ No newline at end of file diff --git a/Qwirkle/cm4.cs b/Qwirkle/cm4.cs new file mode 100644 index 0000000..87a68a5 --- /dev/null +++ b/Qwirkle/cm4.cs @@ -0,0 +1,162 @@ +// Pas de console.WriteLine() dans les classes + +// Mot clé abstract dans la déclaration de la méthode et la déclaration de la classe pour faire une méthode virtuelle pure et une classe abstraite +// => ON utilise pas normalement + +using panier; + +namespace Animaux // Héritage +{ + public class Animal + { + public string Name {get; private set;} + + public Animal(string name) + { + this.Name = name; + } + + public virtual string Crie() + { + return "GRR"; + } + } + + public class Chien : Animal + { + public Chien(string name) : base(name) {} + + public override string Crie() + { + return $"{base.Crie()} + Wouf"; + } + } +} + + +namespace panier // Interfaces +{ + public class Article : IEqualTable
+ { + public string Name { get; set; } + public float Price { get; set; } + + public Article(string name, float price) + { + Name = name; + Price = price; + } + + public override bool Equals(object? obj) + { + if(ReferenceEquals(obj, null)) return false; + if(ReferenceEquals(obj, this)) return true; + if(obj.GetType() != typeof(Article)) return false; + return Equals(obj as Article); + } + + public bool Equals(Article? other) + { + return other.Name.Equals(Name); + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + } + + public class PanierDAchat + { + + /* + 1) Créer une collection privée readonly + 2) Créer une ReadOnlyCollection public avec un set privé + 3) On l'initialise dans le constructeur + */ + + public ReadOnlyCollection
Articles { get; private set; }; + private List
Articles = new List
(); + + public ICalculPromo Promo {get; private set; } + + public float TotalPrice() + { + return Promo.GetTotal(this); + } + + public void AddArticle(Article article) + { + if(!Articles.Contains(article)) + { + Articles.Add(article); + } + } + } + + public interface ICalculPromo + { + float GetTotal(PanierDAchat panier); + } + + public class SansPromotion : ICalculPromo + { + public float GetTotal(PanierDAchat panier) + { + return panier.Articles.Sum(a => a.Price); + } + } + + public class SansTVA : ICalculPromo + { + public float GetTotal(PanierDAchat panier) + { + return panier.Articles.Sum(a => a.Price) * 0.8f; + } + } + + public class TroisPourLePrixDeDeux : ICalculPromo + { + public float GetTotal(PanierDAchat panier) + { + var sortedArticles = panier.Articles.OrderByDescending(a => a.Price); + int count = sortedArticles.count(); + sortedArticles.take((int)(count / 3) * 3).Sum(a => a.Price); + } + } +} + + +PanierDAchat p = new PanierDAchat(); + +p.Promo = new SansPromotion(); +p.Promo = new SansTVA(); + + + +namespace TestUnitairePanier +{ + public class UnitTest1 + { + [Fact] + public void Test_AddArticle() + { + PanierDAchat p = new PanierDAchat(); + bool result = p.AddArticle(new Article("Jp", 0.4)) + Assert.True(result); + } + } + + public class UnitTest2 + { + [Theory] + [InlineData(true, "Jp", 0.5)] + [InlineData(false, "Jp", -0.5)] + public void Test_AddArticle(bool expectedResult, string name, float price) + { + PanierDAchat p = new PanierDAchat(); + bool result = p.AddArticle(new Article("Jp", 0.4)) + Assert.True(result); + } + } +} \ No newline at end of file