2009-11-25 136 views
4

下面的代碼的目的是確定某一特定日期下午12:00週四後有資格作爲「週末」,即最少2天前週一下午12:00C#的switch語句重構

是有更好的方法嗎?如果 - 其他人變得醜陋,戰略模式對於這一點太過分了。

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; 
    } 
+0

除了重構代碼複製到自己的方法,下面列出的,我是一個使用功能更強大的解決方案的忠實粉絲。將你每天的邏輯分解成它自己的特定功能,然後建立一個有幾天作爲關鍵字和適當的方法作爲值的地圖。如上所述,將您的提取日期放入自己的方法中,然後使用lamdas執行放棄邏輯,我認爲代碼變得更加清晰。 – 2009-11-25 15:56:00

回答

6

你一定要重構dropoffDate出來 - 因爲代碼重複了3次!最簡單的清理:我想介紹一個函數來檢查pickupDate,另一個檢查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; 

} 

現在你的主要功能是一個2班輪:

if (ts.TotalDays >= 2 && ts.TotalDays <= 4) 
{ 
    return IsPickupWeekend(pickupDate) && IsWeekendDropOff(dropoffDate); 
} 
+0

很確定這將檢查12 AM而不是12 PM – James 2009-11-25 15:50:26

+0

@Grzenio:不應該IsWeekendDropOff函數有星期六爲Drop OFf的一天嗎?考慮到PickUpDate星期四是1300小時,那麼下落日期可以是星期六1400HR。 – 2009-11-25 15:54:46

+0

具體參見http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx「小時組件,表示爲0到23之間的值」 – James 2009-11-25 15:55:20

3

我想你可以在這裏提取方法:

private bool ValidateDropoff(DateTime dropoffDate) 
{ 
    switch (dropoffDate.DayOfWeek) 
    { 
     case DayOfWeek.Sunday: 
      return true; 
     case DayOfWeek.Monday: 
      return dropoffDate.Hour <= 12; 
     default: 
      return false; 
    } 
} 
+0

您需要'返回false';最後;否則,不是每個路徑都會返回一個值。或者,只需移動'return false;'你在交換機之外。 – Gorpik 2009-11-25 15:38:35

+1

@Gorpik,默認情況下,照顧這一點。實際上每條路徑都會返回一個值。 – 2009-11-25 15:40:44

+0

這個代碼不應該是星期六嗎?請讓我知道此代碼的工作情況PickUpDate:週四1300小時和下落日期:週六1400小時? – 2009-11-25 16:13:03

1
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; 
0

不多清晰,但在這裏你去:

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; 
} 
0

我第一次破裂:

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; 
0

在開關嘗試

retrun (dropoffDate.DayOfWeek == DayOfWeek.Sunday && dropoffDate.Hour <= 12 || dropoffDate.DayOfWeek == DayOfWeek.Sunday) 
0

我會做這樣的事情

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; 
    } 
} 
0

這裏是我的刺吧:

/// <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; 
    } 
0
private readonly TimeSpan Midday = new TimeSpan(12, 0, 0); 

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

     if (lengthOfTrip.TotalDays < 2 || lengthOfTrip.TotalDays > 4) 
      return false; 

     return IsPickupDateConsideredWeekend(pickupDate) && IsDropoffDateConsideredWeekend(dropoffDate); 
    } 

    private bool IsPickupDateConsideredWeekend(DateTime pickupdate) 
    { 
     if (pickupdate.DayOfWeek == DayOfWeek.Thursday && pickupdate.TimeOfDay > Midday) 
      return true; 
     return false; 
    } 

    private bool IsDropoffDateConsideredWeekend(DateTime dropoffDate) 
    { 
     if (dropoffDate.DayOfWeek == DayOfWeek.Monday && dropoffDate.TimeOfDay <= Midday) 
      return true; 
     return false; 
    }