diff --git a/Pupils/Pupil.cs b/Pupils/Pupil.cs new file mode 100644 index 0000000..489dacf --- /dev/null +++ b/Pupils/Pupil.cs @@ -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; + + /// + /// Gets the age of the pupil. + /// + public int Age + { + get + { + var today = DateOnly.FromDateTime(DateTime.Today); + var age = today.Year - _dateOfBirth.Year; + if (today < _dateOfBirth.AddYears(age)) + { + age--; + } + return age; + } + } + + /// + /// Gets or sets the ZIP code of the pupil. + /// + /// + /// The ZIP code must be between 1000 and 9999 inclusive. + /// + public int? ZipCode + { + get + { + return _zipCode; + } + + set + { + if (value >= 1000 && value <= 9999) + { + _zipCode = value; + } + } + } + + /// + /// Gets the first name of the pupil. + /// + 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; + } + } + + /// + /// Gets the last name of the pupil. + /// + 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; + } + } + + /// + /// Gets the unique ID of the pupil. + /// + public int Id { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The first name of the pupil. + /// The last name of the pupil. + /// The date of birth of the pupil. + public Pupil(string firstName, string lastName, DateOnly dateOfBirth) + { + Id = ++_nextId; + Name = $"{firstName} {lastName}"; + _dateOfBirth = dateOfBirth; + } + + /// + /// Determines whether the pupil is of a specified age type. + /// + /// The age type to check. + /// true if the pupil is of the specified age type; otherwise, false. + 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; + } + + /// + /// Determines whether the current pupil is older than another pupil. + /// + /// The other pupil to compare with. + /// true if the current pupil is older than the other pupil; otherwise, false. + public bool IsOlderThan(Pupil other) + { + return Age > other.Age; + } + + /// + /// Determines whether the current pupil lives nearby another pupil. + /// + /// The other pupil to compare with. + /// true if the first two digits of the ZIP codes match; otherwise, false. + 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; + } +} \ No newline at end of file diff --git a/Pupils/PupilTests.cs b/Pupils/PupilTests.cs new file mode 100644 index 0000000..a0c74b1 --- /dev/null +++ b/Pupils/PupilTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Pupils/Pupils.csproj b/Pupils/Pupils.csproj index 4cb8184..2ff6a61 100644 --- a/Pupils/Pupils.csproj +++ b/Pupils/Pupils.csproj @@ -8,4 +8,8 @@ true + + + + \ No newline at end of file