Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public abstract class Shape : IComparable
- {
- protected double s;
- public Shape(double s)
- {
- this.s = s;
- }
- public virtual double S()
- {
- return s;
- }
- public virtual int CompareTo(object o)
- {
- if (o == null) return 1;
- Shape otherShape = o as Shape;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- public class Square : Shape
- {
- double side;
- public Square(double side) : base(side)
- {
- this.side = side;
- }
- public override double S()
- {
- s = side * side;
- return s;
- }
- public override int CompareTo(object o)
- {
- if (o == null) return 1;
- Square otherShape = o as Square;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- public class CustomShape : Shape
- {
- double area;
- public CustomShape(double area) : base(area)
- {
- this.area = area;
- }
- public override double S()
- {
- return area;
- }
- public override int CompareTo(object o)
- {
- if (o == null) return 1;
- CustomShape otherShape = o as CustomShape;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- public class Triangle : Shape
- {
- double _base;
- double height;
- public Triangle(double _base, double height) : base(_base)
- {
- this._base = _base;
- this.height = height;
- }
- public override double S()
- {
- s = 0.5 * _base * height;
- return s;
- }
- public override int CompareTo(object o)
- {
- if (o == null) return 1;
- Triangle otherShape = o as Triangle;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- public class Circle : Shape
- {
- double r;
- public Circle(double r) : base(r)
- {
- this.r = r;
- }
- public override double S()
- {
- s = r * r * Math.PI;
- return s;
- }
- public override int CompareTo(object o)
- {
- if (o == null) return 1;
- Circle otherShape = o as Circle;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- public class Rectangle : Shape
- {
- double width;
- double height;
- public Rectangle(double width, double height) : base(width)
- {
- this.width = width;
- this.height = height;
- }
- public override double S()
- {
- s = height * width;
- return s;
- }
- public override int CompareTo(object o)
- {
- if (o == null) return 1;
- Rectangle otherShape = o as Rectangle;
- if (otherShape != null)
- return this.S().CompareTo(otherShape.S());
- else
- throw new Exception("Невозможно сравнить два объекта");
- }
- }
- //using NUnit.Framework;
- //using System;
- //using System.Linq;
- //using System.Collections.Generic;
- //[TestFixture]
- //public class ShapesTests
- //{
- // [Test]
- // public void ShapesAreSortableOnArea()
- // {
- // // Arrange
- // double width, height, triangleBase, side, radius, area;
- // Random random = new Random((int)DateTime.UtcNow.Ticks);
- // var expected = new List<Shape>();
- // area = 1.1234;
- // expected.Add(new CustomShape(area));
- // side = 1.1234;
- // expected.Add(new Square(side));
- // radius = 1.1234;
- // expected.Add(new Circle(radius));
- // triangleBase = 5;
- // height = 2;
- // expected.Add(new Triangle(triangleBase, height));
- // height = 3;
- // triangleBase = 4;
- // expected.Add(new Triangle(triangleBase, height));
- // width = 4;
- // expected.Add(new Rectangle(width, height));
- // area = 16.1;
- // expected.Add(new CustomShape(area));
- // var actual = expected.OrderBy(x => random.Next()).ToList();
- // // Act
- // actual.Sort();
- // // Assert
- // for (var i = 0; i < 5; i++)
- // Assert.AreEqual(expected[i], actual[i]);
- // }
- //}
Add Comment
Please, Sign In to add comment