public Figure Build() { if (_figure == null) { throw new Exception("Type de la figure indéfini."); } else if (_figure is Carre) { Carre fig = (Carre)_figure; if (_points.Count >= 1) { fig.Origine = _points[0]; } if (_length.Count >= 1) { fig.Cote = _length[0]; } } else if (_figure is Cercle) { Cercle fig = (Cercle)_figure; if (_points.Count >= 1) { fig.Centre = _points[0]; } if (_length.Count >= 1) { fig.Rayon = _length[0]; } } else if (_figure is Triangle) { if (_points.Count >= 3) { _figure = new Triangle(_points[0], new Segment(_points[1], _points[2])); } else { throw new Exception("Nombre de points insuffisant pour construire un triangle."); } } return(_figure); }
static void Main(string[] args) { Console.WriteLine("Mon premier programme C# !"); Dessin.Instance.DefinirCadre(-50.0, -50.0, 50.0, 50.0); Point p1 = new Point(); p1.X = 1.0; p1.Y = 2.0; Point p2 = new Point(2.0, 4.0); Point p3 = new Point3D(3.0, 6.0, 9.0); Point p4 = new Point3D(4.0, 6.0, 9.0); Console.WriteLine(p4.ToString()); Point p5 = (Point)p2.Clone(); //Point[] tab = { p1, p2, p3, p4 }; //foreach (Point p in tab) //{ // Console.WriteLine(p); //} Console.WriteLine(p1); p1.Deplacer(104.0, 4.0); Console.WriteLine(p1); Console.WriteLine(p2); Segment s1 = new Segment(p1, p3); Console.WriteLine(s1); Console.WriteLine(" --- Déplacement du segment --- "); s1.Deplacer(3.0, 5.0); Console.WriteLine(s1); Carre ca1 = new Carre(p1, 5.0); Cercle ce1 = new Cercle(p2, 5.0); Triangle tri1 = new Triangle(p2, s1); Triangle tri2 = new Triangle(new Point(2.0, 0.0), new Segment(new Point(0.0, 2.0), new Point(2.0, 2.0))); Figure[] tab = { ca1, ce1, tri1, tri2 }; List <Figure> liste = tab.ToList <Figure>(); Console.WriteLine(" --- tab --- "); foreach (Figure fig in tab) { Console.WriteLine(fig); } liste.Sort(); Console.WriteLine(" --- liste triée --- "); foreach (Figure fig in liste) { Console.WriteLine(fig); } Console.WriteLine(ca1); p1.Afficher(); ce1.Afficher(); IAffichable[] listeDeChosesAffichables = { p1, ce1 }; foreach (IAffichable chose in listeDeChosesAffichables) { chose.Afficher(); } Console.WriteLine(" --- Design Pattern Builder --- "); Figure fig3 = new FigureBuilder() .SetType(FigureType.CARRE) .AddPoint(p1) .SetLength(3.0) .Build(); Figure fig4 = new FigureBuilder() .SetType(FigureType.TRIANGLE) .AddPoint(p1) .AddPoint(p2) .AddPoint(p3) .Build(); FigureBuilder builder2 = new FigureBuilder(FigureType.CARRE); String str = new StringBuilder("test") .Append("A") .Append(123) .Append("B") .ToString(); Carre ca2 = new CarreBuilder() .SetOrigine(p1) .SetCote(6.0) .Build(); Carre ca3 = new CarreBuilder() .LoadCSV("carre.csv") .Build(); Console.WriteLine(fig3); Console.WriteLine(fig4); Console.WriteLine(str); Console.ReadLine(); }