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

Evgenij Satyev #59

Open
wants to merge 28 commits into
base: Evgenij_Satyev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions CourseApp.Tests/AgeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Xunit;

namespace CourseApp.Tests
{
public class AgeTest
{
[Fact]
public void TestDate()
{
var res = new AgeCalc();
Assert.Equal("Вам 19 лет, 2 месяцев, 18 дней", res.CalcAge(22, 10, 2000, 8, 1, 2020));
}

[Fact]
public void BirthdayAboveToday()
{
var res = new AgeCalc();
try
{
res.CalcAge(27, 11, 2025, 8, 1, 2020);
Assert.True(false);
}
catch
{
Console.WriteLine("Birthday > Today");
Assert.True(true);
}
}

[Fact]
public void CurrentDayIsBirthdayTest()
{
var res = new AgeCalc();
var day = DateTime.Today.Day;
var month = DateTime.Today.Month;
var year = DateTime.Today.Year;
try
{
Assert.Equal(res.CalcAge(12, 12, 2019, 12, 12, 2019), $"Возраст:0 лет, 0 месяцев, 0 дней");
}
catch
{
Assert.True(true);
}
}
}
}
6 changes: 3 additions & 3 deletions CourseApp.Tests/PistolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void TestEmptyConstructor()
var item = new Pistol();
Assert.Equal(0, item.Kalibr);
Assert.Equal("No model", item.Model);
Assert.True(item.Fire);
Assert.True(item.Hit);
}

[Fact]
Expand All @@ -27,7 +27,7 @@ public void TestShoot()
{
var item = new Pistol("Glock", 10, true);
var act = item.Shoot(true);
Assert.Equal($"Pistol Glock and 10 made the shot!", act);
Assert.Equal($"Pistol Glock and 10 hit!", act);
}

[Fact]
Expand All @@ -38,7 +38,7 @@ public void TestNumShoot()
Assert.Equal($"Glock made of 15 shots", act);
}

[Fact]
[Fact]
public void TestCorrectIncorrectSetKalibr()
{
var item = new Pistol();
Expand Down
54 changes: 54 additions & 0 deletions CourseApp.Tests/WeaponTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Xunit;

namespace CourseApp.Tests
{
public class WeaponTest
{
[Fact]
public void TestEmptyConstructorForPistol()
{
var item = new Pistol();
Assert.Equal(0, item.Kalibr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тестов как то сильно маловато

Assert.Equal("No model", item.Model);
}

[Fact]
public void TestForToString()
{
var item = new Rifle();
Assert.Equal("Rifle-No model, 0, True", item.ToString());
}

[Fact]
public void TestShoot()
{
var item = new Rifle("Glock", 10, true);
var act = item.Shoot(true);
Assert.Equal($"Rifle Glock and 10 hit!", act);
}

[Fact]
public void TestCorrectIncorrectSetKalibr()
{
var item = new Pistol();
try
{
item.Kalibr = -5;
Assert.Equal(-5, item.Kalibr);
}
catch (System.Exception)
{
Assert.True(true);
}
}

[Fact]
public void TestNumShoot()
{
var item = new Pistol("Glock", 10, true);
var act = item.NumShoot(15);
Assert.Equal($"Glock made of 15 shots", act);
}
}
}
32 changes: 32 additions & 0 deletions CourseApp/AgeCalc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace CourseApp
{
public class AgeCalc
{
private DateTime now = DateTime.Today;

public string CalcAge(int day, int month, int year)
{
var today = DateTime.Today;
var ageCalc = new AgeCalc();
return ageCalc.CalcAge(day, month, year, today.Day, today.Month, today.Year);
}

public string CalcAge(int day, int month, int year, int heDay, int heMonth, int heYear)
{
var birthday = new DateTime(year, month, day);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Два метода дублируют код. Один должен вызывать другой

var today = new DateTime(heYear, heMonth, heDay);

if (now.Ticks < birthday.Ticks)
{
throw new Exception("You haven't been born yet");
}
else
{
var res = new DateTime(today.Ticks - birthday.Ticks);
return $"Вам {res.Year - 1} лет, {res.Month - 1} месяцев, {res.Day - 1} дней";
}
}
}
}
3 changes: 3 additions & 0 deletions CourseApp/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static void Main(string[] args)
Console.WriteLine($"y={item}");
}

var ageCalc = new AgeCalc();
Console.WriteLine(ageCalc.CalcAge(22, 10, 2000));

Console.ReadLine();
}
}
Expand Down
30 changes: 13 additions & 17 deletions CourseApp/Pistol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CourseApp
{
public class Pistol
public class Pistol : Weapon
{
private double kalibr;

Expand All @@ -11,16 +11,17 @@ public Pistol()
{
}

public Pistol(string model, double kalibr, bool fire)
public Pistol(string model, double kalibr, bool hit)
: base(model, kalibr, hit)
{
Model = model;
Kalibr = kalibr;
Fire = fire;
Hit = hit;
}

public string Model { get; set; }
public override string Model { get; set; }

public double Kalibr
public override double Kalibr
{
get
{
Expand All @@ -40,31 +41,26 @@ public double Kalibr
}
}

public bool Fire { get; set; }
public override bool Hit { get; set; }

public bool CanShoot
public override string Shoot(bool hit)
{
get { return this.Fire; }
}

public string Shoot(bool canShoot)
{
if (canShoot == true)
if (hit == true)
{
return $"Pistol {Model} and {Kalibr} made the shot!";
return $"Pistol {Model} and {Kalibr} hit!";
}
else
{
return $"Pistol {Model} and {Kalibr} not made the shot!";
return $"Pistol {Model} and {Kalibr} not hit!";
}
}

public override string ToString()
{
return $"Pistil-{Model}, {Kalibr}, {Fire}";
return $"Pistil-{Model}, {Kalibr}, {Hit}";
}

public string NumShoot(int shot)
public override string NumShoot(int shot)
{
return $"{Model} made of {shot} shots";
}
Expand Down
59 changes: 59 additions & 0 deletions CourseApp/Rifle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;

namespace CourseApp
{
public class Rifle : Weapon
{
private double kalibr;

public Rifle()
: this("No model", 0, true)
{
}

public Rifle(string model, double kalibr, bool hit)
: base(model, kalibr, hit)
{
Model = model;
Kalibr = kalibr;
Hit = hit;
}

public override double Kalibr
{
get
{
return this.kalibr;
}

set
{
if (value >= 0 && value < 20)
{
this.kalibr = value;
}
else
{
throw new Exception("Enter correct kalibr");
}
}
}

public override string ToString()
{
return $"Rifle-{Model}, {Kalibr}, {Hit}";
}

public override string Shoot(bool hit)
{
if (hit == true)
{
return $"Rifle {Model} and {Kalibr} hit!";
}
else
{
return $"Rifle {Model} and {Kalibr} not hit!";
}
}
}
}
32 changes: 32 additions & 0 deletions CourseApp/Weapon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace CourseApp
{
public abstract class Weapon
{
public Weapon(string model, double kalibr, bool hit)
{
Model = model;
Kalibr = kalibr;
Hit = hit;
}

public virtual string Model { get; set; }

public virtual double Kalibr { get; set; }

public virtual bool Hit { get; set; }

public abstract string Shoot(bool canShoot);

public new virtual string ToString()
{
return $"Weapon-{Model}, {Kalibr}, {Hit}";
}

public virtual string NumShoot(int shot)
{
return $"{Model} made of {shot} shots";
}
}
}