Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aleksandr ermakov(Наследование и Вычисление возраста) #76

Open
wants to merge 18 commits into
base: Aleksandr_Ermakov
Choose a base branch
from
36 changes: 36 additions & 0 deletions CourseApp.Tests/AgeTest.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
31 changes: 31 additions & 0 deletions CourseApp.Tests/AutoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
66 changes: 66 additions & 0 deletions CourseApp.Tests/MotocycleTest.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
26 changes: 26 additions & 0 deletions CourseApp/AgeClass.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
33 changes: 17 additions & 16 deletions CourseApp/Auto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace CourseApp
{
public class Auto
public class Auto : Transport
{
private int speed;
private string color;

public Auto()
: this("Untitled")
Expand All @@ -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;
}
}
Expand Down
53 changes: 53 additions & 0 deletions CourseApp/Motocycle.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
27 changes: 21 additions & 6 deletions CourseApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
42 changes: 42 additions & 0 deletions CourseApp/Transport.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}