using System.Text.RegularExpressions; namespace TINK.Repository.Exception { /// Handles booking request which fail due to too many bikes requested/ booked. public class BookingDeclinedException : InvalidResponseException { /// Holds error description if user/ password combination is not valid. public const string BOOKING_FAILURE_STATUS_MESSAGE_UPPERCASE = "(OK: BOOKING_REQUEST DECLINED\\. MAX COUNT OF )([0-9]+)( OCCUPIED BIKES HAS BEEN REACHED)"; /// Prevents invalid use of exception. private BookingDeclinedException() : base(typeof(BookingDeclinedException).Name) { } /// Prevents invalid use of exception. public BookingDeclinedException(int maxBikesCount) : base(typeof(BookingDeclinedException).Name) { MaxBikesCount = maxBikesCount; } public static bool IsBookingDeclined(string responseState, out BookingDeclinedException exception) { // Check if there are too many bikes requested/ booked. var match = Regex.Match( responseState.ToUpper(), BOOKING_FAILURE_STATUS_MESSAGE_UPPERCASE); if (match.Groups.Count != 4 || !int.TryParse(match.Groups[2].ToString(), out int maxBikesCount)) { exception = null; return false; } exception = new BookingDeclinedException(maxBikesCount); return true; } /// Holds the maximum count of bikes allowed to reserve/ book. public int MaxBikesCount { get; private set; } } }