Everything done, but cannot run unit tests
This commit is contained in:
parent
e281d7a66e
commit
737f06cf19
3 changed files with 234 additions and 0 deletions
169
Pupils/Pupil.cs
Normal file
169
Pupils/Pupil.cs
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
using System.Security.Principal;
|
||||
|
||||
namespace Pupils;
|
||||
|
||||
public class Pupil
|
||||
{
|
||||
private static int _nextId;
|
||||
private readonly DateOnly _dateOfBirth;
|
||||
private int? _zipCode;
|
||||
private readonly string Name;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the age of the pupil.
|
||||
/// </summary>
|
||||
public int Age
|
||||
{
|
||||
get
|
||||
{
|
||||
var today = DateOnly.FromDateTime(DateTime.Today);
|
||||
var age = today.Year - _dateOfBirth.Year;
|
||||
if (today < _dateOfBirth.AddYears(age))
|
||||
{
|
||||
age--;
|
||||
}
|
||||
return age;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ZIP code of the pupil.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The ZIP code must be between 1000 and 9999 inclusive.
|
||||
/// </remarks>
|
||||
public int? ZipCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return _zipCode;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (value >= 1000 && value <= 9999)
|
||||
{
|
||||
_zipCode = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first name of the pupil.
|
||||
/// </summary>
|
||||
public string FirstName
|
||||
{
|
||||
get
|
||||
{
|
||||
string firstName = "";
|
||||
for (int i = 0; i < Name.Length; i++)
|
||||
{
|
||||
if (!String.IsNullOrWhiteSpace(char.ToString(Name[i])))
|
||||
{
|
||||
firstName += Name[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
return firstName;
|
||||
}
|
||||
}
|
||||
return firstName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last name of the pupil.
|
||||
/// </summary>
|
||||
public string LastName
|
||||
{
|
||||
get
|
||||
{
|
||||
string lastName = "";
|
||||
for (int i = Name.Length - 1; i > 0; i--)
|
||||
{
|
||||
if (!String.IsNullOrWhiteSpace(char.ToString(Name[i])))
|
||||
{
|
||||
lastName = Name[i] + lastName;
|
||||
}
|
||||
else
|
||||
{
|
||||
return lastName;
|
||||
}
|
||||
}
|
||||
return lastName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique ID of the pupil.
|
||||
/// </summary>
|
||||
public int Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Pupil"/> class.
|
||||
/// </summary>
|
||||
/// <param name="firstName">The first name of the pupil.</param>
|
||||
/// <param name="lastName">The last name of the pupil.</param>
|
||||
/// <param name="dateOfBirth">The date of birth of the pupil.</param>
|
||||
public Pupil(string firstName, string lastName, DateOnly dateOfBirth)
|
||||
{
|
||||
Id = ++_nextId;
|
||||
Name = $"{firstName} {lastName}";
|
||||
_dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the pupil is of a specified age type.
|
||||
/// </summary>
|
||||
/// <param name="ageType">The age type to check.</param>
|
||||
/// <returns><c>true</c> if the pupil is of the specified age type; otherwise, <c>false</c>.</returns>
|
||||
public bool IsOfAge(AgeType ageType)
|
||||
{
|
||||
int requiredAge;
|
||||
|
||||
switch (ageType)
|
||||
{
|
||||
case AgeType.VotingAge:
|
||||
requiredAge = 16;
|
||||
break;
|
||||
case AgeType.FullAge:
|
||||
requiredAge = 18;
|
||||
break;
|
||||
case AgeType.RetirementAge:
|
||||
requiredAge = 65;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return Age >= requiredAge;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current pupil is older than another pupil.
|
||||
/// </summary>
|
||||
/// <param name="other">The other pupil to compare with.</param>
|
||||
/// <returns><c>true</c> if the current pupil is older than the other pupil; otherwise, <c>false</c>.</returns>
|
||||
public bool IsOlderThan(Pupil other)
|
||||
{
|
||||
return Age > other.Age;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current pupil lives nearby another pupil.
|
||||
/// </summary>
|
||||
/// <param name="other">The other pupil to compare with.</param>
|
||||
/// <returns><c>true</c> if the first two digits of the ZIP codes match; otherwise, <c>false</c>.</returns>
|
||||
public bool LivesNearby(Pupil other)
|
||||
{
|
||||
if (ZipCode == null || other.ZipCode == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string thisZipPrefix = ZipCode.Value.ToString().Substring(0, 2);
|
||||
string otherZipPrefix = other.ZipCode.Value.ToString().Substring(0, 2);
|
||||
|
||||
return thisZipPrefix == otherZipPrefix;
|
||||
}
|
||||
}
|
||||
61
Pupils/PupilTests.cs
Normal file
61
Pupils/PupilTests.cs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
using Xunit;
|
||||
|
||||
namespace Pupils.Tests
|
||||
{
|
||||
public class PupilTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsOlderThan_ReturnsTrue_WhenCurrentPupilIsOlder()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2000, 1, 1));
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2005, 1, 1));
|
||||
|
||||
Assert.True(pupil1.IsOlderThan(pupil2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsOlderThan_ReturnsFalse_WhenCurrentPupilIsYounger()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2005, 1, 1));
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2000, 1, 1));
|
||||
|
||||
Assert.False(pupil1.IsOlderThan(pupil2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LivesNearby_ReturnsTrue_WhenZipCodesMatch()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2000, 1, 1)) { ZipCode = 4060 };
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2005, 1, 1)) { ZipCode = 4030 };
|
||||
|
||||
Assert.True(pupil1.LivesNearby(pupil2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LivesNearby_ReturnsFalse_WhenZipCodesDoNotMatch()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2000, 1, 1)) { ZipCode = 4060 };
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2005, 1, 1)) { ZipCode = 1010 };
|
||||
|
||||
Assert.False(pupil1.LivesNearby(pupil2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LivesNearby_ReturnsFalse_WhenEitherZipCodeIsNull()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2000, 1, 1)) { ZipCode = null };
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2005, 1, 1)) { ZipCode = 4030 };
|
||||
|
||||
Assert.False(pupil1.LivesNearby(pupil2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Id_IsUniqueForEachPupil()
|
||||
{
|
||||
var pupil1 = new Pupil("John", "Doe", new DateOnly(2000, 1, 1));
|
||||
var pupil2 = new Pupil("Jane", "Smith", new DateOnly(2005, 1, 1));
|
||||
|
||||
Assert.NotEqual(pupil1.Id, pupil2.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,4 +8,8 @@
|
|||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue