views:

439

answers:

8

The purpose of the code below is to determine if a particular date qualifies as a "weekend" i.e after 12:00 PM on Thursday, minimum 2 days and before Monday 12:00 PM

Is there a better way? If-Else turns ugly and the Strategy pattern is way too much work for this.

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
    {
        TimeSpan ts = dropoffDate.Subtract(pickupDate);

        if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                    if (pickupDate.Hour >= 12)
                    {
                        switch (dropoffDate.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                return true;
                            case DayOfWeek.Monday:
                                if (dropoffDate.Hour <= 12)
                                {
                                    return true;
                                }
                                return false;
                        }
                    }
                    break;
                case DayOfWeek.Friday:
                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    break;
                case DayOfWeek.Saturday:
                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    return false;
            }
        }
        return false;
    }
+3  A: 

I think you could extract a method here:

private bool ValidateDropoff(DateTime dropoffDate)
{
    switch (dropoffDate.DayOfWeek)
    {
        case DayOfWeek.Sunday:
           return true;
        case DayOfWeek.Monday:
           return dropoffDate.Hour <= 12;
        default:
           return false;
    }
}
bruno conde
You need a 'return false;' at the end; otherwise, not every path will return a value. Alternatively, just move the 'return false;' you have outside the switch.
Gorpik
@Gorpik, the default case takes care of this. Every path will in fact return a value.
bruno conde
Should not Saturday be a case in this code? Please let me know will this code work in case PickUpDate: Thursday 1300 Hr and Drop Date: Saturday 1400 Hr?
Beginner
+1  A: 
if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
{
    var hour_limit = new Func<Boolean>(() => {
     switch (dropoffDate.DayOfWeek)
     {
      case DayOfWeek.Sunday:
       return true;
      case DayOfWeek.Monday:
       return dropoffDate.Hour <= 12;
      default:
       return false;
     }

    });

    switch (pickupDate.DayOfWeek)
    {
        case DayOfWeek.Thursday:
            if (pickupDate.Hour >= 12)  return hour_limit();
      break;
        case DayOfWeek.Friday:
        case DayOfWeek.Saturday:
            return hour_limit();
        default: 
            break;
    }
}

return false;
ChaosPandion
A: 

Not much clearer, but here you go:

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate){
    TimeSpan ts = dropoffDate.Subtract(pickupDate);

    if (ts.TotalDays >= 2 && ts.TotalDays <= 4){
        switch (pickupDate.DayOfWeek){
            case DayOfWeek.Thursday:
                if (pickupDate.Hour >= 12){
                    switch (dropoffDate.DayOfWeek){
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            return dropoffDate.Hour <= 12;
                    }
                }
                break;
            case DayOfWeek.Friday:
                switch (dropoffDate.DayOfWeek){
                    case DayOfWeek.Sunday:
                        return true;
                    case DayOfWeek.Monday:
                        return dropoffDate.Hour <= 12;
                }
                break;
            case DayOfWeek.Saturday:
                switch (dropoffDate.DayOfWeek){
                    case DayOfWeek.Sunday:
                        return true;
                    case DayOfWeek.Monday:
                        return dropoffDate.Hour <= 12;
                }
                return false;
        }
    }
    return false;
}
Mick Walker
+4  A: 

You definitely should refactor the dropoffDate out - because the code is duplicated 3 times! The simplest cleanup: I would introduce a function to check the pickupDate and another to check the dropoffDate:

private bool IsPickupWeekend(DateTime pickupDate)
{
    switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                    return pickupDate.Hour >= 12;
                case DayOfWeek.Friday:                    
                case DayOfWeek.Saturday:
                    return true;
            }
        }
        return false;
}

private bool IsWeekendDropOff(DateTime dropoffDate)
{
    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    return false;

}

And now your main funtion is a 2 liner:

if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
{
    return IsPickupWeekend(pickupDate) && IsWeekendDropOff(dropoffDate);
}
Grzenio
Pretty sure this will check for 12 AM not 12 PM
James
@Grzenio: Should not IsWeekendDropOff function have Saturday as the day for Drop OFf? Considering PickUpDate is 1300 Hr on Thursday then Drop Date can be Saturday 1400 Hr.
Beginner
See http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx specifically "The hour component, expressed as a value between 0 and 23"
James
@James 12AM = 0, 12PM = 12. This code checks for 12PM
Stan R.
12 PM as mentioned in question is 12 noon i.e in the clock of 0-23 it is it is 12 so should not Saturday be considered a day for DropOff?http://en.wikipedia.org/wiki/12-hour_clock
Beginner
I have just copied the bits of code from the question - the author asked only to provide a "less ugly" version - so I assume the code in the question works "correctly".
Grzenio
@Stan my mistake I read it wrong.
James
A: 

My first crack:

if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                case DayOfWeek.Friday:
                case DayOfWeek.Saturday:
                    if (pickupDate.DayOfWeek == DayOfWeek.Thursday && pickupDate.Hour <= 12)
                        return false;

                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            return dropoffDate.Hour <= 12;
                    }
                    return false;

                default:
                    return false;
            }
        }
        return false;
Pat
A: 

in the switch try

retrun (dropoffDate.DayOfWeek == DayOfWeek.Sunday && dropoffDate.Hour <= 12 || dropoffDate.DayOfWeek == DayOfWeek.Sunday)
harryovers
A: 

I would do it something like this

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
{
    TimeSpan ts = dropoffDate.Subtract(pickupDate);

    if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
    {
        switch (pickupDate.DayOfWeek)
        {
            case DayOfWeek.Thursday:
                if (pickupDate.Hour >= 12)
                {
                    reurn DayOfWeek(dropOffDate.DayOfWeek);
                }
                break;
            case DayOfWeek.Friday, DayOfWeek.Saturday:
                {
                    return DayOfWeek(dropOffDate.DayOfWeek);
                }
        }
    }
    return false;
}

public bool DayOfWeek(DateTime dropOffDate)
    {
switch (dropoffDate.DayOfWeek)
 {
  case DayOfWeek.Sunday:
   {
    return true;
   }
  case DayOfWeek.Monday:
            {
             if (dropoffDate.Hour <= 12)
                    {
                        return true;
                    }
                return false;
            }
       return false;
   }
 }
Sean Barlow
A: 

Here is my stab at it:

  /// <summary>
    /// Gets the weekend days.
    /// </summary>
    /// <returns></returns>
    public List<DayOfWeek> GetWeekendDays()
    {
        List<DayOfWeek> days = new List<DayOfWeek>()
                                   {
                                       DayOfWeek.Thursday,
                                       DayOfWeek.Friday,
                                       DayOfWeek.Sunday
                                   };
        return days;
    }

    /// <summary>
    /// Validates the weekend.
    /// </summary>
    /// <param name="pickupDate">The pickup date.</param>
    /// <param name="dropoffDate">The dropoff date.</param>
    /// <returns></returns>
    public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
    {
        bool isValid = false;
        TimeSpan ts = dropoffDate.Subtract(pickupDate);

        if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            List<DayOfWeek> days = GetWeekendDays();

            foreach (DayOfWeek day in days)
            {
                if(pickupDate.DayOfWeek == day)
                {
                   isValid = ValidateDropOff(dropoffDate);
                    break;
                }
            }
        }

        return isValid;
    }

    /// <summary>
    /// Validates the drop off.
    /// </summary>
    /// <param name="dropoffDate">The dropoff date.</param>
    /// <returns></returns>
    private static bool ValidateDropOff(DateTime dropoffDate)
    {
        bool isValidDropOff = (dropoffDate.DayOfWeek == DayOfWeek.Sunday);

        if(dropoffDate.DayOfWeek == DayOfWeek.Monday)
        {
            if (dropoffDate.Hour <= 12)
            {
                isValidDropOff = true;
            }
        }

        return isValidDropOff;
    }
Chuck Conway