diff --git a/CourseApp.Tests/InterfaceTests.cs b/CourseApp.Tests/InterfaceTests.cs new file mode 100644 index 00000000..3445f4f8 --- /dev/null +++ b/CourseApp.Tests/InterfaceTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using Xunit; + +namespace CourseApp.Tests +{ + public class InterfaceTests + { + [Fact] + public void Test1() + { + Assert.True(true); + } + + [Fact] + public void IComparable() + { + Animal[] mass = new Animal[3]; + mass[0] = new Cat("Name1", 6, true, true); + mass[1] = new Mouse("Name2", 1, false, true); + mass[2] = new Cat("Name3", 3, false, true); + Array.Sort(mass); + Assert.Equal("Name2", mass[0].Name); + Assert.Equal("Name3", mass[1].Name); + Assert.Equal("Name1", mass[2].Name); + } + } +} \ No newline at end of file diff --git a/CourseApp/Animal.cs b/CourseApp/Animal.cs index f859af6f..617d313c 100644 --- a/CourseApp/Animal.cs +++ b/CourseApp/Animal.cs @@ -2,7 +2,7 @@ namespace CourseApp { - public abstract class Animal + public abstract class Animal : IComparable { private int age; @@ -51,5 +51,10 @@ public override string ToString() } public abstract string Voice(); + + public int CompareTo(Animal an) + { + return this.Age.CompareTo(an.Age); + } } } \ No newline at end of file diff --git a/CourseApp/Cat.cs b/CourseApp/Cat.cs index 6daf2a93..0c034d93 100644 --- a/CourseApp/Cat.cs +++ b/CourseApp/Cat.cs @@ -4,7 +4,7 @@ namespace CourseApp { - public class Cat : Animal + public class Cat : Animal, IFacts { public Cat() : this("Неизвестно") @@ -58,5 +58,7 @@ public override string Voice() { return "Meow"; } + + void IFacts.Facts() => Console.WriteLine("Сердце кошки бьется около 140 ударов в минуту.\nМяуканьем кошки пытаются обратить внимание человека.\n Усы, необходимы кошке для перемещения в пространстве."); } -} \ No newline at end of file +} diff --git a/CourseApp/IFacts.cs b/CourseApp/IFacts.cs new file mode 100644 index 00000000..3354caf9 --- /dev/null +++ b/CourseApp/IFacts.cs @@ -0,0 +1,7 @@ +namespace CourseApp +{ + public interface IFacts + { + void Facts(); + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index afa38911..e5f9819e 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; namespace CourseApp @@ -7,8 +7,8 @@ public class Program { public static double MyFunction(double a, double x) { - var y = Math.Pow(Math.Log10(a + x), 2) / Math.Pow(a + x, 2); - return y; + var y = Math.Pow(Math.Log10(a + x), 2) / Math.Pow(a + x, 2); + return y; } public static List TaskA(double a, double xn, double xk, double dx) @@ -62,9 +62,10 @@ public static void Main(string[] args) Console.WriteLine(); - Animal[] masss = new Animal[2]; - masss[0] = new Mouse("Larisa", 2, false, true); - masss[1] = new Cat("Kyza", 7, true, true); + Animal[] masss = new Animal[3]; + masss[0] = new Cat("Kyza", 7, true, true); + masss[1] = new Mouse("Larisa", 2, false, true); + masss[2] = new Cat("Murzik", 4, true, false); foreach (var item in masss) { Console.WriteLine(item.ToString()); @@ -72,6 +73,17 @@ public static void Main(string[] args) Console.WriteLine(); } + Array.Sort(masss); + Console.WriteLine("Отсортированный по возрасту массив:"); + foreach (Animal an in masss) + { + Console.WriteLine($"{an.Name} - {an.Age}"); + } + + Console.WriteLine("================================"); + + IFacts barsik = new Cat("Barsik", 5, true, true); + barsik.Facts(); Console.ReadLine(); } }