コード例 #1
0
        /// <summary>
        /// Verifies that any values for slots in the intent are valid.
        /// </summary>
        /// <param name="reservation"></param>
        /// <returns></returns>
        private ValidationResult Validate(Reservation reservation)
        {
            if (!string.IsNullOrEmpty(reservation.Location) && !TypeValidators.IsValidCity(reservation.Location))
            {
                return(new ValidationResult(false, LOCATION_SLOT,
                                            $"We currently do not support {reservation.Location} as a valid destination.  Can you try a different city?"));
            }

            if (!string.IsNullOrEmpty(reservation.CheckInDate))
            {
                DateTime checkinDate = DateTime.MinValue;
                if (!DateTime.TryParse(reservation.CheckInDate, out checkinDate))
                {
                    return(new ValidationResult(false, CHECK_IN_DATE_SLOT,
                                                "I did not understand your check in date.  When would you like to check in?"));
                }
                if (checkinDate < DateTime.Today)
                {
                    return(new ValidationResult(false, CHECK_IN_DATE_SLOT,
                                                "Reservations must be scheduled at least one day in advance.  Can you try a different date?"));
                }
            }

            if (!string.IsNullOrEmpty(reservation.Nights))
            {
                int nights;
                if (!int.TryParse(reservation.Nights, out nights))
                {
                    return(new ValidationResult(false, NIGHTS_SLOT,
                                                "I did not understand the number of nights.  Can you enter the number of nights again again?"));
                }
                if (nights < 1 || nights > 30)
                {
                    return(new ValidationResult(false, NIGHTS_SLOT,
                                                "You can make a reservations for from one to thirty nights.  How many nights would you like to stay for?"));
                }
            }

            return(ValidationResult.VALID_RESULT);
        }
コード例 #2
0
        /// <summary>
        /// Verifies that any values for slots in the intent are valid.
        /// </summary>
        /// <param name="reservation"></param>
        /// <returns></returns>
        private ValidationResult Validate(Reservation reservation)
        {
            if (!string.IsNullOrEmpty(reservation.PickUpCity) && !TypeValidators.IsValidCity(reservation.PickUpCity))
            {
                return(new ValidationResult(false, PICK_UP_CITY_SLOT,
                                            $"We currently do not support {reservation.PickUpCity} as a valid destination.  Can you try a different city?"));
            }

            DateTime pickupDate = DateTime.MinValue;

            if (!string.IsNullOrEmpty(reservation.PickUpDate))
            {
                if (!DateTime.TryParse(reservation.PickUpDate, out pickupDate))
                {
                    return(new ValidationResult(false, PICK_UP_DATE_SLOT,
                                                "I did not understand your departure date.  When would you like to pick up your car rental?"));
                }
                if (pickupDate < DateTime.Today)
                {
                    return(new ValidationResult(false, PICK_UP_DATE_SLOT,
                                                "Your pick up date is in the past!  Can you try a different date?"));
                }
            }

            DateTime returnDate = DateTime.MinValue;

            if (!string.IsNullOrEmpty(reservation.ReturnDate))
            {
                if (!DateTime.TryParse(reservation.ReturnDate, out returnDate))
                {
                    return(new ValidationResult(false, RETURN_DATE_SLOT,
                                                "I did not understand your return date.  When would you like to return your car rental?"));
                }
            }

            if (pickupDate != DateTime.MinValue && returnDate != DateTime.MinValue)
            {
                if (returnDate <= pickupDate)
                {
                    return(new ValidationResult(false, RETURN_DATE_SLOT,
                                                "Your return date must be after your pick up date.  Can you try a different return date?"));
                }

                var ts = returnDate.Date - pickupDate.Date;
                if (ts.Days > 30)
                {
                    return(new ValidationResult(false, RETURN_DATE_SLOT,
                                                "You can reserve a car for up to thirty days.  Can you try a different return date?"));
                }
            }

            int age = 0;

            if (!string.IsNullOrEmpty(reservation.DriverAge))
            {
                if (!int.TryParse(reservation.DriverAge, out age))
                {
                    return(new ValidationResult(false, DRIVER_AGE_SLOT,
                                                "I did not understand the driver's age.  Can you enter the driver's age again?"));
                }
                if (age < 18)
                {
                    return(new ValidationResult(false, DRIVER_AGE_SLOT,
                                                "Your driver must be at least eighteen to rent a car.  Can you provide the age of a different driver?"));
                }
            }

            if (!string.IsNullOrEmpty(reservation.CarType) && !TypeValidators.IsValidCarType(reservation.CarType))
            {
                return(new ValidationResult(false, CAR_TYPE_SLOT,
                                            "I did not recognize that model.  What type of car would you like to rent?  " +
                                            "Popular cars are economy, midsize, or luxury"));
            }

            return(ValidationResult.VALID_RESULT);
        }