diff --git a/CourseApp.Tests/AgeTest.cs b/CourseApp.Tests/AgeTest.cs new file mode 100644 index 0000000..eafcd06 --- /dev/null +++ b/CourseApp.Tests/AgeTest.cs @@ -0,0 +1,36 @@ +using System; +using Xunit; + +namespace CourseApp.Tests +{ + public class AgeTest + { + [Fact] + public void TestAge() + { + DateTime birthday = new DateTime(2000, 8, 21); + DateTime exp = new DateTime(DateTime.Now.Ticks - birthday.Ticks); + Assert.Equal($"Вам {exp.Year - 1} лет, {exp.Month - 1} месяцев и {exp.Day - 1} дня", AgeClass.Age(birthday)); + } + + [Fact] + public void TestBirthdayAboveToday() + { + try + { + AgeClass.Age(new DateTime(2056, 2, 12)); + } + catch (Exception) + { + Console.WriteLine("Birthday > Today"); + Assert.True(true); + } + } + + [Fact] + public void TestBirthdayToday() + { + Assert.Equal("Вам 0 лет, 0 месяцев и 0 дня", AgeClass.Age(DateTime.Now)); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/AutoTest.cs b/CourseApp.Tests/AutoTest.cs index 2955e6e..3f6f5ff 100644 --- a/CourseApp.Tests/AutoTest.cs +++ b/CourseApp.Tests/AutoTest.cs @@ -79,5 +79,36 @@ public void TestSetYear() item.Year = 1972; Assert.Equal(1972, item.Year); } + + [Fact] + public void TestSetColor() + { + var item = new Auto(); + item.Color = "white"; + Assert.Equal("white", item.Color); + } + + [Fact] + public void TestIncorrectSetColor() + { + var item = new Auto(); + + try + { + item.Color = "blue"; + } + catch (System.Exception) + { + Assert.True(true); + } + } + + [Fact] + public void TestOverride() + { + var item = new Auto(); + Assert.Equal("The black car Untitled(2000) is moving at a speed of 60 km/h.", item.ToString()); + Assert.Equal("This car is 20 years old.", item.Info()); + } } } \ No newline at end of file diff --git a/CourseApp.Tests/MotocycleTest.cs b/CourseApp.Tests/MotocycleTest.cs new file mode 100644 index 0000000..3d96787 --- /dev/null +++ b/CourseApp.Tests/MotocycleTest.cs @@ -0,0 +1,66 @@ +using System; +using Xunit; + +namespace CourseApp.Tests +{ + public class MotocycleTest + { + [Fact] + public void TestConstructor() + { + var item = new Motocycle("harley davidson", 2000, 60, 300); + Assert.Equal(2000, item.Year); + Assert.Equal(60, item.Speed); + Assert.Equal("harley davidson", item.Name); + Assert.Equal(300, item.Power); + } + + [Fact] + public void TestSetSpeed() + { + var item = new Motocycle(); + item.Speed = 30; + Assert.Equal(30, item.Speed); + } + + [Fact] + public void TestIncorrectSetSpeed() + { + var item = new Motocycle(); + try + { + item.Speed = -1; + } + catch (System.Exception) + { + Console.WriteLine("Speed should be more 0 and less than 300"); + Assert.True(true); + } + + Assert.Equal(40, item.Speed); + } + + [Fact] + public void TestIncorrectSetPower() + { + var item = new Motocycle(); + + try + { + item.Power = -10; + } + catch (System.Exception) + { + Assert.True(true); + } + } + + [Fact] + public void TestOverride() + { + var item = new Motocycle(); + Assert.Equal("The motorcycle has a capacity of 100 horsepower.", item.Info()); + Assert.Equal("The motocycle Untitled(1999) is moving at a speed of 40 km/h.", item.ToString()); + } + } +} \ No newline at end of file diff --git a/CourseApp/AgeClass.cs b/CourseApp/AgeClass.cs new file mode 100644 index 0000000..d67f5c4 --- /dev/null +++ b/CourseApp/AgeClass.cs @@ -0,0 +1,26 @@ +using System; + +namespace CourseApp +{ + public class AgeClass + { + public static string Age(DateTime birthday) + { + DateTime res = DateCompare(birthday, DateTime.Now); + return $"Вам {res.Year - 1} лет, {res.Month - 1} месяцев и {res.Day - 1} дня"; + } + + public static DateTime DateCompare(DateTime date1, DateTime date2) + { + if (date1.Ticks < date2.Ticks) + { + DateTime res = new DateTime(date2.Ticks - date1.Ticks); + return res; + } + else + { + throw new Exception("Birthday > Today"); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Auto.cs b/CourseApp/Auto.cs index 2fc5343..cb7ea8b 100644 --- a/CourseApp/Auto.cs +++ b/CourseApp/Auto.cs @@ -2,9 +2,9 @@ namespace CourseApp { - public class Auto + public class Auto : Transport { - private int speed; + private string color; public Auto() : this("Untitled") @@ -17,44 +17,45 @@ public Auto(string name) } public Auto(string name, int year) - : this(name, year, 60) + : this(name, year, 60, "black") { } - public Auto(string name, int year, int speed) + public Auto(string name, int year, int speed, string color) + : base(name, year, speed) { - this.speed = speed; - this.Name = name; - this.Year = year; + this.color = color; } - public int Speed + public string Color { get { - return this.speed; + return this.color; } set { - if (value >= 0 && value < 300) + if (value == "white" || value == "black" || value == "green") { - this.speed = value; + this.color = value; } else { - throw new System.Exception("Speed should be more 0 and less than 300"); + throw new System.Exception(); } } } - public string Name { get; set; } - - public int Year { get; set; } + public override string Info() + { + string s = $"This car is {2020 - Year} years old."; + return s; + } public override string ToString() { - string s = $"The car {Name}({Year}) is moving at a speed of {Speed} km/h."; + string s = $"The {Color} car {Name}({Year}) is moving at a speed of {Speed} km/h."; return s; } } diff --git a/CourseApp/Motocycle.cs b/CourseApp/Motocycle.cs new file mode 100644 index 0000000..e9fd054 --- /dev/null +++ b/CourseApp/Motocycle.cs @@ -0,0 +1,53 @@ +using System; + +namespace CourseApp +{ + public class Motocycle : Transport + { + private int power; + + public Motocycle() + : base("Untitled", 1999, 40) + { + this.power = 100; + } + + public Motocycle(string name, int year, int speed, int power) + : base(name, year, speed) + { + this.power = power; + } + + public int Power + { + get + { + return this.power; + } + + set + { + if (value > 0) + { + this.power = value; + } + else + { + throw new System.Exception("Power should be more 0"); + } + } + } + + public override string Info() + { + string s = $"The motorcycle has a capacity of {Power} horsepower."; + return s; + } + + public override string ToString() + { + string s = $"The motocycle {Name}({Year}) is moving at a speed of {Speed} km/h."; + return s; + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 5a6baf6..ea58adf 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -58,15 +58,30 @@ public static void Main(string[] args) Console.WriteLine($"x={xB[i]} y={taskB[i]}"); } - Auto[] cars = new Auto[3]; - cars[0] = new Auto("Mercedes Benz", 2005, 60); - cars[1] = new Auto("bugatti veyron", 2011, 150); - cars[2] = new Auto("lada vesta", 2005, 75); - for (int i = 0; i < 3; i++) + Console.WriteLine("=========="); + + Transport[] ar = new Transport[2]; + ar[0] = new Motocycle("harley davidson", 2005, 60, 300); + ar[1] = new Auto("BMW x5", 2010, 120, "white"); + foreach (var item in ar) { - Console.WriteLine(cars[i].ToString()); + Console.WriteLine(item); + Console.WriteLine(item.ToString()); + Console.WriteLine(item.Info()); + Console.WriteLine(); } + Console.WriteLine("=========="); + Console.WriteLine("Введите год своего рождения:"); + int years = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Введите месяц своего рождения:"); + int months = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Введите день своего рождения:"); + int days = Convert.ToInt32(Console.ReadLine()); + + Console.WriteLine(AgeClass.Age(new DateTime(years, months, days))); + Console.WriteLine("=========="); + Console.ReadLine(); } } diff --git a/CourseApp/Transport.cs b/CourseApp/Transport.cs new file mode 100644 index 0000000..94a2d19 --- /dev/null +++ b/CourseApp/Transport.cs @@ -0,0 +1,42 @@ +using System; + +namespace CourseApp +{ + public abstract class Transport + { + private int speed; + + public Transport(string name, int year, int speed) + { + this.speed = speed; + this.Name = name; + this.Year = year; + } + + public int Speed + { + get + { + return this.speed; + } + + set + { + if (value >= 0 && value < 300) + { + this.speed = value; + } + else + { + throw new System.Exception("Speed should be more 0 and less than 300"); + } + } + } + + public string Name { get; set; } + + public int Year { get; set; } + + public abstract string Info(); + } +} \ No newline at end of file