/// <summary> /// Calculates the age. /// </summary> /// <param name="dateOfBirth">The date of birth.</param> /// <returns></returns> private AgeResult CalculateAge(DateTime dateOfBirth) { AgeResult result = new AgeResult(); // get current date. DateTime adtCurrentDate = DateTime.Now; // find the literal difference result.Days = adtCurrentDate.Day - dateOfBirth.Day; result.Months = adtCurrentDate.Month - dateOfBirth.Month; result.Years = adtCurrentDate.Year - dateOfBirth.Year; if (result.Days < 0) { result.Days += DateTime.DaysInMonth(adtCurrentDate.Year, adtCurrentDate.Month); result.Months--; } if (result.Months < 0) { result.Months += 12; result.Years--; } return(result); }
/// <summary> /// Validates the player date of birth. /// </summary> /// <param name="membershipId">The membership identifier.</param> /// <param name="playerId">The player identifier.</param> /// <param name="dateOfBirth">The date of birth.</param> /// <exception cref="System.InvalidOperationException"> /// Player Id {playerId} /// or /// Player Id {playerId} /// </exception> /// <exception cref="InvalidOperationException">Player Id {playerId} /// or /// Player Id {playerId}</exception> private void ValidatePlayerDateOfBirth(Guid membershipId, Guid playerId, DateTime dateOfBirth) { // Get the players age AgeResult ageResult = this.CalculateAge(dateOfBirth); if (ageResult.Years < 13) { throw new InvalidOperationException($"Player Id {playerId} is too young."); } if (ageResult.Years >= 90) { throw new InvalidOperationException($"Player Id {playerId} is too old."); } }