axeefectushka

Untitled

Feb 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6.  
  7. public abstract class Shape : IComparable
  8. {
  9.     protected double s;
  10.     public Shape(double s)
  11.     {
  12.         this.s = s;
  13.     }
  14.     public virtual double S()
  15.     {
  16.         return s;
  17.     }
  18.     public virtual int CompareTo(object o)
  19.     {
  20.         if (o == null) return 1;
  21.  
  22.         Shape otherShape = o as Shape;
  23.         if (otherShape != null)
  24.             return this.S().CompareTo(otherShape.S());
  25.         else
  26.             throw new Exception("Невозможно сравнить два объекта");
  27.     }
  28.  
  29. }
  30. public class Square : Shape
  31. {
  32.     double side;
  33.     public Square(double side) : base(side)
  34.     {
  35.         this.side = side;
  36.     }
  37.     public override double S()
  38.     {
  39.         s = side * side;
  40.         return s;
  41.     }
  42.     public override int CompareTo(object o)
  43.     {
  44.         if (o == null) return 1;
  45.  
  46.         Square otherShape = o as Square;
  47.         if (otherShape != null)
  48.             return this.S().CompareTo(otherShape.S());
  49.         else
  50.             throw new Exception("Невозможно сравнить два объекта");
  51.     }
  52.  
  53. }
  54. public class CustomShape : Shape
  55. {
  56.     double area;
  57.     public CustomShape(double area) : base(area)
  58.     {
  59.         this.area = area;
  60.     }
  61.     public override double S()
  62.     {
  63.         return area;
  64.     }
  65.     public override int CompareTo(object o)
  66.     {
  67.         if (o == null) return 1;
  68.  
  69.         CustomShape otherShape = o as CustomShape;
  70.         if (otherShape != null)
  71.             return this.S().CompareTo(otherShape.S());
  72.         else
  73.             throw new Exception("Невозможно сравнить два объекта");
  74.     }
  75.  
  76. }
  77. public class Triangle : Shape
  78. {
  79.     double _base;
  80.     double height;
  81.     public Triangle(double _base, double height) : base(_base)
  82.     {
  83.         this._base = _base;
  84.         this.height = height;
  85.     }
  86.     public override double S()
  87.     {
  88.         s = 0.5 * _base * height;
  89.         return s;
  90.     }
  91.     public override int CompareTo(object o)
  92.     {
  93.         if (o == null) return 1;
  94.  
  95.         Triangle otherShape = o as Triangle;
  96.         if (otherShape != null)
  97.             return this.S().CompareTo(otherShape.S());
  98.         else
  99.             throw new Exception("Невозможно сравнить два объекта");
  100.     }
  101.  
  102. }
  103. public class Circle : Shape
  104. {
  105.     double r;
  106.     public Circle(double r) : base(r)
  107.     {
  108.         this.r = r;
  109.     }
  110.     public override double S()
  111.     {
  112.         s = r * r * Math.PI;
  113.         return s;
  114.     }
  115.     public override int CompareTo(object o)
  116.     {
  117.         if (o == null) return 1;
  118.  
  119.         Circle otherShape = o as Circle;
  120.         if (otherShape != null)
  121.             return this.S().CompareTo(otherShape.S());
  122.         else
  123.             throw new Exception("Невозможно сравнить два объекта");
  124.     }
  125.  
  126. }
  127. public class Rectangle : Shape
  128. {
  129.     double width;
  130.     double height;
  131.     public Rectangle(double width, double height) : base(width)
  132.     {
  133.         this.width = width;
  134.         this.height = height;
  135.     }
  136.     public override double S()
  137.     {
  138.         s = height * width;
  139.         return s;
  140.     }
  141.     public override int CompareTo(object o)
  142.     {
  143.         if (o == null) return 1;
  144.  
  145.         Rectangle otherShape = o as Rectangle;
  146.         if (otherShape != null)
  147.             return this.S().CompareTo(otherShape.S());
  148.         else
  149.             throw new Exception("Невозможно сравнить два объекта");
  150.     }
  151. }
  152. //using NUnit.Framework;
  153. //using System;
  154. //using System.Linq;
  155. //using System.Collections.Generic;
  156.  
  157. //[TestFixture]
  158. //public class ShapesTests
  159. //{
  160. //    [Test]
  161. //    public void ShapesAreSortableOnArea()
  162. //    {
  163. //        // Arrange
  164. //        double width, height, triangleBase, side, radius, area;
  165. //        Random random = new Random((int)DateTime.UtcNow.Ticks);
  166.  
  167. //        var expected = new List<Shape>();
  168.  
  169. //        area = 1.1234;
  170. //        expected.Add(new CustomShape(area));
  171.  
  172. //        side = 1.1234;
  173. //        expected.Add(new Square(side));
  174.  
  175. //        radius = 1.1234;
  176. //        expected.Add(new Circle(radius));
  177.  
  178. //        triangleBase = 5;
  179. //        height = 2;
  180. //        expected.Add(new Triangle(triangleBase, height));
  181.  
  182. //        height = 3;
  183. //        triangleBase = 4;
  184. //        expected.Add(new Triangle(triangleBase, height));
  185.  
  186. //        width = 4;
  187. //        expected.Add(new Rectangle(width, height));
  188.  
  189. //        area = 16.1;
  190. //        expected.Add(new CustomShape(area));
  191.  
  192. //        var actual = expected.OrderBy(x => random.Next()).ToList();
  193.  
  194. //        // Act
  195. //        actual.Sort();
  196.  
  197. //        // Assert
  198. //        for (var i = 0; i < 5; i++)
  199. //            Assert.AreEqual(expected[i], actual[i]);
  200. //    }
  201. //}
Add Comment
Please, Sign In to add comment