169 lines
No EOL
4.4 KiB
C#
169 lines
No EOL
4.4 KiB
C#
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;
|
|
}
|
|
} |