2014-10-17 125 views
0

我是C#和asp.net中的開始程序員。我正忙着創建一個Hotel應用程序。有人能夠預定例如8天的假期。但現在我需要添加一個計算價格的公式。我寫的方法是從數據庫中獲取每晚房間的價格。而這個人留下來的日子被輸入到視圖中並傳遞給控制器​​。所以我想要計算控制器內部的價格。但現在我遇到了一個問題,因爲入住酒店的價格在旺季比在淡季要高。所以價格每天都不一樣。但現在我並不真正如何比較日期,以便我能夠提供準確的總價格。在C中比較日期#

我已經看過堆棧溢出的一些線程,他們經常建議使用Timespan來比較日期。但是我想知道Timespan是我最好的解決方案嗎?我的項目的原因是價格應該流動,而不是固定的。例如,它不應該是像5月28日 - 7月10日是每晚120歐元,但更像是5月28日€109,5月29日€112,5月30日€113 - 7月9日127€,7月10日130.

如果我會成功地創造一個不同的價格每天然後最後的事情不應該那麼難,我希望。每個日期的價格應該相加,所以我會得到總價。

所以我的問題是:

  • 是比較日期,時間跨度最好的方法是什麼?
  • 有沒有簡單的方法來計算?我不喜歡固定的日期。
  • 有沒有什麼好的教程呢?
+1

只要有開始和結束日期爲您的賽季,並檢查您的預訂日期在這個範圍內,比如'如果(bookingDate> = seasonStartDate && bookingDate <= seasonEndDate)' – Habib 2014-10-17 17:09:16

+0

如何d你打算存儲價格嗎?我會建議在一個數據庫,然後你可以寫一個查詢,將根據開始和結束日期計算價格。 – juharr 2014-10-17 17:13:14

+2

我會逐一獲取每一天的價格,然後將它們相加。這比用時間盤和邊緣案例搞得簡單很多,而且由於只有幾天,性能影響不大。 – 2014-10-17 17:14:07

回答

1

我只是比較開始日期和結束日期之間的每個Date對象,看看它是否落入定義的範圍內以確定速率,然後將它們相加。

這對你來說可能是過量的,但是我會在課堂中封裝不同的「季節」和他們的費率,並且增加一種方法來確定日期是否屬於「季節」。這將簡化其他方法。

然後,我會創建一個方法,給定一個日期,將返回該日期的比率。

最後,我將通過調用客戶端的開始日期(含)和結束日期(獨佔)之間每天的GetRate()方法來計算總價格。

下面是我如何做的一個例子。首先,類舉行「季節」

public class Season 
{ 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    public int Rate { get; set; } 

    public bool ContainsDate(DateTime date) 
    { 
     // Assumption: Year is ignored - seasons are considered 
     //    to start and end on the same date each year 
     // 
     // Rules: (remember a season may start in Dec and end in Jan, 
     //   so you cant just check if the date is greater than 
     //   the start or less than the end!) 
     // 
     // 1. If the start and end month are the same, 
     // return true if the month is equal to start (or end) month 
     // AND the day is between start and end days. 
     // 2. If the date is in the same month as the start month, 
     // return true if the day is greater than or equal to start day. 
     // 3. If the date is in the same month as the end month, 
     // return true if the day is less than or equal to end day. 
     // 4. If the StartMonth is less than the EndMonth, 
     // return true if the month is between them. 
     // 5. Otherwise, return true if month is NOT between them. 

     if (StartDate.Month == EndDate.Month) 
      return date.Month == StartDate.Month && 
        date.Day >= StartDate.Day && 
        date.Day <= EndDate.Day; 

     if (date.Month == StartDate.Month) 
      return date.Day >= StartDate.Day; 

     if (date.Month == EndDate.Month) 
      return date.Day <= EndDate.Day; 

     if (StartDate.Month <= EndDate.Month) 
      return date.Month > StartDate.Month && date.Month < EndDate.Month; 

     return date.Month < EndDate.Month || date.Month > StartDate.Month; 
    } 
} 

接下來,將計算速度在特定日期的方法:

public static int GetRate(DateTime date) 
{ 
    // Normally these 'seasons' and rates would not be hard coded here 
    const int offSeasonRate = 125; 

    var winterSeason = new Season 
    { 
     StartDate = DateTime.Parse("November 15"), 
     EndDate = DateTime.Parse("January 12"), 
     Rate = 150 
    }; 

    var springSeason = new Season 
    { 
     StartDate = DateTime.Parse("May 20"), 
     EndDate = DateTime.Parse("June 15"), 
     Rate = 140 
    }; 

    var summerSeason = new Season 
    { 
     StartDate = DateTime.Parse("July 10"), 
     EndDate = DateTime.Parse("August 31"), 
     Rate = 170 
    }; 

    // Create a list of all the seasons 
    var seasons = new List<Season> {winterSeason, springSeason, summerSeason}; 

    // Loop through all the seasons and see if this date is in one of them 
    foreach (var season in seasons) 
    { 
     if (season.ContainsDate(date)) 
     { 
      // Note: depending on your implementation, Rate could be a multiplier 
      // in which case you would return (offSeasonRate * season.Rate); 
      return season.Rate; 
     } 
    } 

    // If we get this far, the date was not in a 'season' 
    return offSeasonRate; 
} 

最後,這裏是得到總價格的方法日期範圍:

var startDate = DateTime.Today; 
var endDate = startDate.AddDays(2); 
var price = 0; 

// Sum the rates for each day between 
// start date (inclusive) and end date (exclusive). 
for (var curDate = startDate; curDate < endDate; curDate = curDate.AddDays(1)) 
{ 
    price += GetRate(curDate); 
} 

Console.WriteLine("The total cost from {0} to {1} is: €{2}", 
    startDate, endDate, price);