diff --git a/CourseApp.Tests/BirthdayTest.cs b/CourseApp.Tests/BirthdayTest.cs new file mode 100644 index 0000000..87fbc7c --- /dev/null +++ b/CourseApp.Tests/BirthdayTest.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace CourseApp.Tests +{ + public class BirthdayTest + { + [Fact] + public void TestBirthdayEqualsCurrentDate() + { + var ecString = "Years 0, month 0, days 0"; + Birthday birthday = new Birthday(DateTime.Now); + Assert.Equal(ecString, birthday.ToString()); + } + + [Fact] + public void TestBirthdayBeforeCurrentDate() + { + var ecString = "Years 18, month 219, days 6690"; + Birthday birthday = new Birthday(new DateTime(2001, 9, 12)); + Assert.Equal(ecString, birthday.ToString()); + } + + [Fact] + public void TestBirthdayAfterCurrentDate() + { + var ecString = "Years 0, month 0, days 0"; + Birthday birthday = new Birthday(new DateTime(3002, 9, 12)); + Assert.Equal(ecString, birthday.ToString()); + } + + [Fact] + public void TestFullYearsAndMonth() + { + var ecString = "Years 18, month 3, days 6"; + Birthday birthday = new Birthday(new DateTime(2001, 9, 12)); + Assert.Equal(ecString, birthday.CalculateFullCountYearsAndMonth()); + } + } +} diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index 8fb7e4a..d53285f 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -11,7 +11,6 @@ - @@ -19,12 +18,10 @@ - ../_stylecop/stylecop.ruleset true - diff --git a/CourseApp.Tests/PlatypusTest.cs b/CourseApp.Tests/PlatypusTest.cs index 77c4d8f..7a14ea9 100644 --- a/CourseApp.Tests/PlatypusTest.cs +++ b/CourseApp.Tests/PlatypusTest.cs @@ -6,50 +6,81 @@ namespace CourseApp.Tests public class PlatypusTest { [Fact] - public void TestEmptyConstructor() + public void TestCreateTable() { - var item = new Platypus(); - Assert.Equal(0, item.Age); - Assert.Equal("Untitled", item.Name); - Assert.True(item.IsMale); + var item = new Table(12, 13, "white"); + Assert.Equal(12, item.Weight); + Assert.Equal(13, item.Height); + Assert.Equal("white", item.Color); } [Fact] - public void TestView() + public void TestCreateTableWithoutColor() { - var item = new Platypus(); - var view = @" - _.-^~~^^^`~-,_,,~''''''```~,''``~'``~, - ______,' -o :. _ . ; ,'`, `. -( -\.._,.;;'._ ,( } _`_-_,, `, `, - ``~~~~~~' ((/'((((____/~~~~~~'(,(,___> `~' - "; - Assert.Equal(view, item.View()); + var item = new Table(12, 13); + Assert.Equal(12, item.Weight); + Assert.Equal(13, item.Height); + Assert.Equal("black", item.Color); } [Fact] - public void TestSetAge() + public void TestGetToString() { - var item = new Platypus(); - item.Age = 5; - Assert.Equal(5, item.Age); + var ecString = "Table: Weigh = 400, Height = 400, Color = red, Count legs = 4"; + var item = new Table(400, 400, "red"); + Assert.Equal(ecString, item.ToString()); } [Fact] - public void TestIncorrectSetAge() + public void TestCreateTableWithWrongWeigth() { - var item = new Platypus(); - item.Age = -5; - Assert.Equal(0, item.Age); + try + { + var item = new Table(0, 12, "red"); + } + catch (Exception exc) + { + Assert.Equal("Value does not fall within the expected range.", exc.Message); + } } [Fact] - public void TestCorrectIncorrectSetAge() + public void TestCreateTableWithWrongHeigth() { - var item = new Platypus(); - item.Age = 10; - item.Age = -5; - Assert.Equal(10, item.Age); + try + { + var item = new Table(12, 0, "red"); + } + catch (Exception exc) + { + Assert.Equal("Value does not fall within the expected range.", exc.Message); + } + } + + [Fact] + public void TestCreateTableWithWrongColorNull() + { + try + { + var item = new Table(12, 12, null); + } + catch (Exception exc) + { + Assert.Equal("Value does not fall within the expected range.", exc.Message); + } + } + + [Fact] + public void TestCreateTableWithWrongColorEmpty() + { + try + { + var item = new Table(12, 12, string.Empty); + } + catch (Exception exc) + { + Assert.Equal("Value does not fall within the expected range.", exc.Message); + } } } } diff --git a/CourseApp/Birthday.cs b/CourseApp/Birthday.cs new file mode 100644 index 0000000..f52d683 --- /dev/null +++ b/CourseApp/Birthday.cs @@ -0,0 +1,56 @@ +using System; + +namespace CourseApp +{ + public class Birthday + { + private DateTime birthday; + + public Birthday(DateTime birthday) + { + this.birthday = birthday; + } + + public int CalculateCountYear() + { + int years = DateTime.Now.Year - birthday.Year; + if (DateTime.Now.Month < birthday.Month) + { + years--; + } + years = years < 0 ? 0 : years; + return years; + } + + public int CalculateCountMonth() + { + if (birthday > DateTime.Now || (birthday.Year == DateTime.Now.Year && birthday.Month == DateTime.Now.Month && birthday.Day == DateTime.Now.Day)) + { + return 0; + } + int monthes = (DateTime.Now.Year - birthday.Year - 1) * 12; + monthes += 12 - birthday.Month - 1; + monthes += DateTime.Now.Month; + return monthes; + } + + public int CalculateCountDays() + { + int days = DateTime.Now.Subtract(birthday).Days; + return days > 0 ? days : 0; + } + + public string CalculateFullCountYearsAndMonth() + { + var currentYear = CalculateCountYear(); + var currentMonth = CalculateCountMonth() - CalculateCountYear() * 12; + var currentDays = DateTime.Now.Day; + return "Years " + currentYear + ", month " + currentMonth + ", days " + currentDays; + } + + public override string ToString() + { + return "Years " + CalculateCountYear() + ", month " + CalculateCountMonth() + ", days " + CalculateCountDays(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Chair.cs b/CourseApp/Chair.cs new file mode 100644 index 0000000..276b287 --- /dev/null +++ b/CourseApp/Chair.cs @@ -0,0 +1,23 @@ +using System; + +namespace CourseApp +{ + public class Chair : Furniture + { + public Chair(double weight, double height, string color = "white", int countLegs = 4): base(weight, height, color) + { + CountLegs = countLegs; + } + + public double CountLegs { get; private set; } + + public override void Build() { + Console.WriteLine("Build chair."); + } + + public override string ToString() + { + return "Chair: Weigh = " + Weight + ", Height = " + Height + ", Color = " + Color + ", Count legs = " + CountLegs; + } + } +} \ No newline at end of file diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index b244e47..8703eae 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -8,16 +8,13 @@ - - ../_stylecop/stylecop.ruleset true - diff --git a/CourseApp/Furniture.cs b/CourseApp/Furniture.cs new file mode 100644 index 0000000..3162433 --- /dev/null +++ b/CourseApp/Furniture.cs @@ -0,0 +1,26 @@ +using System; + +namespace CourseApp +{ + public abstract class Furniture + { + public Furniture(double weight, double height, string color) + { + if (weight == 0 || height == 0 || string.IsNullOrEmpty(color)) + { + throw new ArgumentException(); + } + Weight = weight; + Height = height; + Color = color; + } + + public double Weight { get; private set; } + + public double Height { get; private set; } + + public string Color { get; private set; } + + public abstract void Build(); + } +} \ No newline at end of file diff --git a/CourseApp/Platypus.cs b/CourseApp/Platypus.cs deleted file mode 100644 index eb3d463..0000000 --- a/CourseApp/Platypus.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; - -namespace CourseApp -{ - public class Platypus - { - private int age; - - public Platypus() - : this(0, "Untitled", true) - { - } - - public Platypus(int age, string name, bool isMale) - { - Name = name; - Age = age; - IsMale = isMale; - } - - public string Name { get; set; } - - public int Age - { - get - { - return this.age; - } - - set - { - if (value >= 0 && value < 20) - { - this.age = value; - } - else - { - Console.WriteLine("Age should be > 0 and < than 20"); - } - } - } - - public bool IsMale { get; set; } - - public bool IsPoisoned - { - get { return this.IsMale; } - } - - public string View() - { - return @" - _.-^~~^^^`~-,_,,~''''''```~,''``~'``~, - ______,' -o :. _ . ; ,'`, `. -( -\.._,.;;'._ ,( } _`_-_,, `, `, - ``~~~~~~' ((/'((((____/~~~~~~'(,(,___> `~' - "; - } - } -} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 637c621..6a69321 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; namespace CourseApp { @@ -58,14 +59,48 @@ public static void Main(string[] args) const double b = 0; var xB = new List { 0.15, 0.26, 0.37, 0.48, 0.53 }; var taskB = TaskB(a, b, xB); - for (var i = 0; i < taskB.Count; i++) + for (var i = 0; i < taskB.Count; i++) { Console.WriteLine($"x={xB[i]} y={taskB[i]}"); } - var item = new Platypus(); - Console.WriteLine(item.View()); + Console.WriteLine("Table info:"); + List furniture = new List() + { + new Table(500, 120), + new Table(400, 400, "red"), + new Chair(100, 100), + new Chair(120, 120, "yellow"), + new Chair(100, 100, "red", 4), + new Chair(120, 120, "yellow", 2), + new Table(400, 400, countLegs: 3), + }; + + for (int i = 0; i < furniture.Count; i++) + { + furniture[i].Build(); + Console.WriteLine(furniture[i].ToString()); + } + + try + { + furniture.Add(new Table(0, 12, null)); + } + catch (Exception exc) + { + Console.WriteLine(exc.Message); + } + + Console.WriteLine("Birthday info:"); + Birthday birthday = new Birthday(DateTime.Now); + Console.WriteLine("Birthday equals current date: {0}", birthday); + birthday = new Birthday(new DateTime(2001, 9, 12)); + Console.WriteLine("Birthday before current date: {0}", birthday); + birthday = new Birthday(new DateTime(3002, 9, 12)); + Console.WriteLine("Birthday after current date: {0}", birthday); + birthday = new Birthday(new DateTime(2001, 9, 12)); + Console.WriteLine("Full years and mounth: {0}", birthday.CalculateFullCountYearsAndMonth()); Console.ReadLine(); } } diff --git a/CourseApp/Table.cs b/CourseApp/Table.cs new file mode 100644 index 0000000..857b789 --- /dev/null +++ b/CourseApp/Table.cs @@ -0,0 +1,23 @@ +using System; + +namespace CourseApp +{ + public class Table : Furniture + { + public Table(double weight, double height, string color = "black", int countLegs = 4): base(weight, height, color) + { + CountLegs = countLegs; + } + + public double CountLegs { get; private set; } + + public override void Build() { + Console.WriteLine("Build table."); + } + + public override string ToString() + { + return "Table: Weigh = " + Weight + ", Height = " + Height + ", Color = " + Color + ", Count legs = " + CountLegs; + } + } +} \ No newline at end of file diff --git a/_stylecop/stylecop.json b/_stylecop/stylecop.json deleted file mode 100644 index 4a96e8f..0000000 --- a/_stylecop/stylecop.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", - "settings": { - "documentationRules": { - "documentExposedElements": false, - "documentInterfaces": false, - "companyName": "Test Company", - "copyrightText": "This source code is Copyright © {companyName} and MAY NOT be copied, reproduced,\npublished, distributed or transmitted to or stored in any manner without prior\nwritten consent from {companyName} (www.yourcompany.com).", - "xmlHeader":false - } - } -} \ No newline at end of file diff --git a/_stylecop/stylecop.ruleset b/_stylecop/stylecop.ruleset deleted file mode 100644 index 98806c8..0000000 --- a/_stylecop/stylecop.ruleset +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file