我只是比較開始日期和結束日期之間的每個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);
只要有開始和結束日期爲您的賽季,並檢查您的預訂日期在這個範圍內,比如'如果(bookingDate> = seasonStartDate && bookingDate <= seasonEndDate)' – Habib 2014-10-17 17:09:16
如何d你打算存儲價格嗎?我會建議在一個數據庫,然後你可以寫一個查詢,將根據開始和結束日期計算價格。 – juharr 2014-10-17 17:13:14
我會逐一獲取每一天的價格,然後將它們相加。這比用時間盤和邊緣案例搞得簡單很多,而且由於只有幾天,性能影響不大。 – 2014-10-17 17:14:07