diff --git a/CourseApp.Tests/CalcAgeTest.cs b/CourseApp.Tests/CalcAgeTest.cs new file mode 100644 index 0000000..652304f --- /dev/null +++ b/CourseApp.Tests/CalcAgeTest.cs @@ -0,0 +1,51 @@ +using System; +using Xunit; + +namespace CourseApp.Tests +{ + public class CalcAgeTest + { + [Fact] + public void CorrectAgeInputTest() + { + Assert.Equal(CalcAge.CalculateAge(18, 12, 2000, 19, 12, 2019, true), $"Возраст:19 лет, 0 месяцев, 1 дней"); + } + + [Fact] + public void FutureDateInputTest() + { + try + { + CalcAge.CalculateAge(16, 6, 2021, true); + } + catch + { + Assert.True(true); + } + } + + [Fact] + public void CurrentDayIsBirthdayTest() + { + var day = DateTime.Today.Day; + var month = DateTime.Today.Month; + var year = DateTime.Today.Year; + try + { + Assert.Equal(CalcAge.CalculateAge(12, 12, 2019, 12, 12, 2019, true), $"Возраст:0 лет, 0 месяцев, 0 дней"); + } + catch + { + Assert.True(true); + } + } + + [Theory] + [InlineData(30, 12, 2000, 18)] + [InlineData(16, 12, 2000, 19)] + public void CurrectYearCountTest(int d, int m, int y, int exp) + { + Assert.Equal($"Возраст:{exp} лет", CalcAge.CalculateAge(d, m, y, 19, 12, 2019, false)); + } + } +} \ No newline at end of file diff --git a/CourseApp/CalcAge.cs b/CourseApp/CalcAge.cs new file mode 100644 index 0000000..d36928f --- /dev/null +++ b/CourseApp/CalcAge.cs @@ -0,0 +1,40 @@ +using System; + +namespace CourseApp +{ + public static class CalcAge + { + public static string CalculateAge(int day, int month, int year, bool fullAge) + { + var today = DateTime.Today; + return CalcAge.CalculateAge(day, month, year, today.Day, today.Month, today.Year, fullAge); + } + + public static string CalculateAge(int day, int month, int year, int currDay, int currMonth, int currYear, bool fullAge) + { + var birthday = new DateTime(year, month, day); + var today = new DateTime(currYear, currMonth, currDay); + + if (birthday.Ticks > today.Ticks) + { + throw new Exception("you cannot enter a date that did not occur"); + } + else if (birthday.Ticks == today.Ticks) + { + throw new Exception("he/she/you was born today"); + } + else + { + var age = new DateTime(today.Ticks - birthday.Ticks); + if (fullAge == true) + { + return $"Возраст:{age.Year - 1} лет, {age.Month - 1} месяцев, {age.Day - 1} дней"; + } + else + { + return $"Возраст:{age.Year - 1} лет"; + } + } + } + } +} diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cb9854c..dec4146 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -72,6 +72,8 @@ public static void Main(string[] args) Console.WriteLine($"y={item}"); } + Console.WriteLine(CalcAge.CalculateAge(28, 6, 2000, true)); + Console.ReadLine(); } }